summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/MeshBaker/scripts/TextureBlenders/TextureBlenderMaterialPropertyCacheHelper.cs
blob: ebaf3cbab172dd8608276b804eb60b07a74b752b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

namespace DigitalOpus.MB.Core
{
    public class TextureBlenderMaterialPropertyCacheHelper
    {
        private struct MaterialPropertyPair
        {
            public Material material;
            public string property;

            public MaterialPropertyPair(Material m, string prop)
            {
                material = m;
                property = prop;
            }

            public override bool Equals(object obj)
            {
                if (!(obj is MaterialPropertyPair)) return false;
                MaterialPropertyPair b = (MaterialPropertyPair)obj;
                if (!material.Equals(b.material)) return false;
                if (property != b.property) return false;
                return true;
            }

            public override int GetHashCode()
            {
                return base.GetHashCode();
            }
        }

        private Dictionary<MaterialPropertyPair, object> nonTexturePropertyValuesForSourceMaterials = new Dictionary<MaterialPropertyPair, object>();

        private bool AllNonTexturePropertyValuesAreEqual(string prop)
        {
            bool foundFirst = false;
            object firstVal = null;
            foreach (MaterialPropertyPair k in nonTexturePropertyValuesForSourceMaterials.Keys)
            {
                if (k.property.Equals(prop))
                {
                    if (!foundFirst)
                    {
                        firstVal = nonTexturePropertyValuesForSourceMaterials[k];
                        foundFirst = true;
                    }
                    else
                    {
                        if (!firstVal.Equals(nonTexturePropertyValuesForSourceMaterials[k]))
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        public void CacheMaterialProperty(Material m, string property, object value)
        {
            nonTexturePropertyValuesForSourceMaterials[new MaterialPropertyPair(m, property)] = value;
        }

        public object GetValueIfAllSourceAreTheSameOrDefault(string property, object defaultValue)
        {
            if (AllNonTexturePropertyValuesAreEqual(property))
            {
                foreach (MaterialPropertyPair k in nonTexturePropertyValuesForSourceMaterials.Keys)
                {
                    if (k.property.Equals(property))
                    {
                        return nonTexturePropertyValuesForSourceMaterials[k];
                    }
                }
            }

            return defaultValue;
        }
    }
}