summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/VRM/VRMShaders/ShaderProperty/Runtime/ShaderProps.cs
blob: 36d9a8258a6eac90b481457c16e0d713604ff6ca (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
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
#endif


namespace UniGLTF.ShaderPropExporter
{
    public enum ShaderPropertyType
    {
        TexEnv,
        Color,
        Range,
        Float,
        Vector,
    }

    public struct ShaderProperty
    {
        public string Key;
        public ShaderPropertyType ShaderPropertyType;

        public ShaderProperty(string key, ShaderPropertyType propType)
        {
            Key = key;
            ShaderPropertyType = propType;
        }
    }

    public class ShaderProps
    {
        public ShaderProperty[] Properties;

#if UNITY_EDITOR
        static ShaderPropertyType ConvType(ShaderUtil.ShaderPropertyType src)
        {
            switch (src)
            {
                case ShaderUtil.ShaderPropertyType.TexEnv: return ShaderPropertyType.TexEnv;
                case ShaderUtil.ShaderPropertyType.Color: return ShaderPropertyType.Color;
                case ShaderUtil.ShaderPropertyType.Float: return ShaderPropertyType.Float;
                case ShaderUtil.ShaderPropertyType.Range: return ShaderPropertyType.Range;
                case ShaderUtil.ShaderPropertyType.Vector: return ShaderPropertyType.Vector;
                default: throw new NotImplementedException();
            }
        }

        public static ShaderProps FromShader(Shader shader)
        {
            var properties = new List<ShaderProperty>();
            for (int i = 0; i < ShaderUtil.GetPropertyCount(shader); ++i)
            {
                var name = ShaderUtil.GetPropertyName(shader, i);
                var propType = ShaderUtil.GetPropertyType(shader, i);
                properties.Add(new ShaderProperty(name, ConvType(propType)));
            }

            return new ShaderProps
            {
                Properties = properties.ToArray(),
            };
        }
#endif
    }
}