コード例 #1
0
        public void MaterialTest()
        {
            var model = new glTF_VRM_Material
            {
                floatProperties = new Dictionary <string, float>
                {
                    { "float", 1.0f }
                },
                vectorProperties = new Dictionary <string, float[]>
                {
                    { "vector", new float[] { 0, 1, 2, 3 } }
                },
                textureProperties = new Dictionary <string, int>
                {
                    { "texture", 0 }
                },
                keywordMap = new Dictionary <string, bool>
                {
                    { "keyword", true }
                },
                tagMap = new Dictionary <string, string>
                {
                    { "tag", "map" }
                },
            };

            var json = model.ToJson();

            Assert.AreEqual(@"{""renderQueue"":-1,""floatProperties"":{""float"":1},""vectorProperties"":{""vector"":[0,1,2,3]},""textureProperties"":{""texture"":0},""keywordMap"":{""keyword"":true},""tagMap"":{""tag"":""map""}}", json);
            Debug.Log(json);
        }
コード例 #2
0
        public static bool TryGetTextureFromMaterialProperty(GltfData data, glTF_VRM_Material vrmMaterial, string textureKey, out (SubAssetKey, TextureDescriptor) texture)
        {
            // 任意の shader の import を許容する
            if (/*vrmMaterial.shader == MToon.Utils.ShaderName &&*/ vrmMaterial.textureProperties.TryGetValue(textureKey, out var textureIdx))
            {
                var(offset, scale) = (new Vector2(0, 0), new Vector2(1, 1));
                if (TryGetTextureOffsetAndScale(vrmMaterial, textureKey, out var os))
                {
                    offset = os.offset;
                    scale  = os.scale;
                }

                switch (textureKey)
                {
                case MToon.Utils.PropBumpMap:
                    texture = GltfTextureImporter.CreateNormal(data, textureIdx, offset, scale);
                    break;

                default:
                    texture = GltfTextureImporter.CreateSrgb(data, textureIdx, offset, scale);
                    break;
                }
                return(true);
            }

            texture = default;
            return(false);
        }
コード例 #3
0
        public static string ToJson(this glTF_VRM_Material self)
        {
            var f = new JsonFormatter();

            VRMSerializer.Serialize_vrm_materialProperties_ITEM(f, self);
            return(f.ToString());
        }
コード例 #4
0
        public static glTF_VRM_Material Deserialize_vrm_materialProperties_LIST(ListTreeNode <JsonValue> parsed)
        {
            var value = new glTF_VRM_Material();

            foreach (var kv in parsed.ObjectItems())
            {
                var key = kv.Key.GetString();

                if (key == "name")
                {
                    value.name = kv.Value.GetString();
                    continue;
                }

                if (key == "shader")
                {
                    value.shader = kv.Value.GetString();
                    continue;
                }

                if (key == "renderQueue")
                {
                    value.renderQueue = kv.Value.GetInt32();
                    continue;
                }

                if (key == "floatProperties")
                {
                    value.floatProperties = Deserialize_vrm_materialProperties__floatProperties(kv.Value);
                    continue;
                }

                if (key == "vectorProperties")
                {
                    value.vectorProperties = Deserialize_vrm_materialProperties__vectorProperties(kv.Value);
                    continue;
                }

                if (key == "textureProperties")
                {
                    value.textureProperties = Deserialize_vrm_materialProperties__textureProperties(kv.Value);
                    continue;
                }

                if (key == "keywordMap")
                {
                    value.keywordMap = Deserialize_vrm_materialProperties__keywordMap(kv.Value);
                    continue;
                }

                if (key == "tagMap")
                {
                    value.tagMap = Deserialize_vrm_materialProperties__tagMap(kv.Value);
                    continue;
                }
            }
            return(value);
        }
コード例 #5
0
        public static void Serialize_vrm_materialProperties_ITEM(JsonFormatter f, glTF_VRM_Material value)
        {
            f.BeginMap();


            if (!string.IsNullOrEmpty(value.name))
            {
                f.Key("name");
                f.Value(value.name);
            }

            if (!string.IsNullOrEmpty(value.shader))
            {
                f.Key("shader");
                f.Value(value.shader);
            }

            if (true)
            {
                f.Key("renderQueue");
                f.Value(value.renderQueue);
            }

            if (value.floatProperties != null && value.floatProperties.Count > 0)
            {
                f.Key("floatProperties");
                Serialize_vrm_materialProperties__floatProperties(f, value.floatProperties);
            }

            if (value.vectorProperties != null && value.vectorProperties.Count > 0)
            {
                f.Key("vectorProperties");
                Serialize_vrm_materialProperties__vectorProperties(f, value.vectorProperties);
            }

            if (value.textureProperties != null && value.textureProperties.Count > 0)
            {
                f.Key("textureProperties");
                Serialize_vrm_materialProperties__textureProperties(f, value.textureProperties);
            }

            if (value.keywordMap != null && value.keywordMap.Count > 0)
            {
                f.Key("keywordMap");
                Serialize_vrm_materialProperties__keywordMap(f, value.keywordMap);
            }

            if (value.tagMap != null && value.tagMap.Count > 0)
            {
                f.Key("tagMap");
                Serialize_vrm_materialProperties__tagMap(f, value.tagMap);
            }

            f.EndMap();
        }
コード例 #6
0
        public void MaterialTest()
        {
            var model = new glTF_VRM_Material
            {
                floatProperties = new Dictionary <string, float>
                {
                    { "float", 1.0f }
                },
                vectorProperties = new Dictionary <string, float[]>
                {
                    { "vector", new float[] { 0, 1, 2, 3 } }
                },
                textureProperties = new Dictionary <string, int>
                {
                    { "texture", 0 }
                },
                keywordMap = new Dictionary <string, bool>
                {
                    { "keyword", true }
                },
                tagMap = new Dictionary <string, string>
                {
                    { "tag", "map" }
                },
            };

            var json = model.ToJson();

            Assert.AreEqual(@"{""renderQueue"":-1,""floatProperties"":{""float"":1},""vectorProperties"":{""vector"":[0,1,2,3]},""textureProperties"":{""texture"":0},""keywordMap"":{""keyword"":true},""tagMap"":{""tag"":""map""}}", json);
            Debug.Log(json);

            var c = new JsonSchemaValidationContext("")
            {
                EnableDiagnosisForNotRequiredFields = true,
            };
            var json2 = JsonSchema.FromType <glTF_VRM_Material>().Serialize(model, c);

            // NOTE: New serializer outputs values which will not be used...
            Assert.AreEqual(json, json2);

            // deserialize
            var deserialized = default(glTF_VRM_Material);

            json.ParseAsJson().Deserialize(ref deserialized);
            Assert.AreEqual(1, deserialized.floatProperties.Count);
        }
コード例 #7
0
        public IEnumerable <TextureImportParam> EnumerateMaterial(GltfParser parser, glTF_VRM_Material vrmMaterial)
        {
            // MToon
            var offsetScaleMap = new Dictionary <string, float[]>();

            foreach (var kv in vrmMaterial.vectorProperties)
            {
                if (vrmMaterial.textureProperties.ContainsKey(kv.Key))
                {
                    // texture offset & scale
                    offsetScaleMap.Add(kv.Key, kv.Value);
                }
            }
            foreach (var kv in vrmMaterial.textureProperties)
            {
                var(offset, scale) = (Vector2.zero, Vector2.one);
                if (offsetScaleMap.TryGetValue(kv.Key, out float[] value))
コード例 #8
0
        public static bool TryCreateParam(GltfParser parser, int i, glTF_VRM_Material vrmMaterial, out MaterialImportParam param)
        {
            if (vrmMaterial.shader == VRM.glTF_VRM_Material.VRM_USE_GLTFSHADER)
            {
                // fallback to gltf
                param = default;
                return(false);
            }

            //
            // restore VRM material
            //
            // use material.name, because material name may renamed in GltfParser.
            var name = parser.GLTF.materials[i].name;

            param = new MaterialImportParam(name, vrmMaterial.shader);

            param.Actions.Add(material => material.renderQueue = vrmMaterial.renderQueue);

            foreach (var kv in vrmMaterial.floatProperties)
            {
                param.FloatValues.Add(kv.Key, kv.Value);
            }

            var offsetScaleMap = new Dictionary <string, float[]>();

            foreach (var kv in vrmMaterial.vectorProperties)
            {
                if (vrmMaterial.textureProperties.ContainsKey(kv.Key))
                {
                    // texture offset & scale
                    offsetScaleMap.Add(kv.Key, kv.Value);
                }
                else
                {
                    // vector4
                    var v = new Vector4(kv.Value[0], kv.Value[1], kv.Value[2], kv.Value[3]);
                    param.Vectors.Add(kv.Key, v);
                }
            }

            foreach (var kv in vrmMaterial.textureProperties)
            {
                var(offset, scale) = (Vector2.zero, Vector2.one);
                if (offsetScaleMap.TryGetValue(kv.Key, out float[] value))
コード例 #9
0
        public void MaterialTest()
        {
            var model = new glTF_VRM_Material();

            var json = model.ToJson();

            Assert.AreEqual(@"{""renderQueue"":-1,""floatProperties"":{},""vectorProperties"":{},""textureProperties"":{},""keywordMap"":{},""tagMap"":{}}", json);
            Debug.Log(json);

            var c = new JsonSchemaValidationContext("")
            {
                EnableDiagnosisForNotRequiredFields = true,
            };
            var json2 = JsonSchema.FromType <glTF_VRM_Material>().Serialize(model, c);

            // NOTE: New serializer outputs values which will not be used...
            Assert.AreEqual(json, json2);
        }
コード例 #10
0
        public static glTF_VRM_Material CreateFromMaterial(Material m, List <Texture> textures)
        {
            var material = new glTF_VRM_Material
            {
                name        = m.name,
                shader      = m.shader.name,
                renderQueue = m.renderQueue,
            };

            if (!PreShaderPropExporter.VRMExtensionShaders.Contains(m.shader.name))
            {
                material.shader = glTF_VRM_Material.VRM_USE_GLTFSHADER;
                return(material);
            }

            var prop = PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);

            if (prop == null)
            {
                Debug.LogWarningFormat("Fail to export shader: {0}", m.shader.name);
            }
            else
            {
                foreach (var keyword in m.shaderKeywords)
                {
                    material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword));
                }

                // get properties
                //material.SetProp(prop);
                foreach (var kv in prop.Properties)
                {
                    switch (kv.ShaderPropertyType)
                    {
                    case ShaderPropertyType.Color:
                    {
                        var value = m.GetColor(kv.Key).ToArray();
                        material.vectorProperties.Add(kv.Key, value);
                    }
                    break;

                    case ShaderPropertyType.Range:
                    case ShaderPropertyType.Float:
                    {
                        var value = m.GetFloat(kv.Key);
                        material.floatProperties.Add(kv.Key, value);
                    }
                    break;

                    case ShaderPropertyType.TexEnv:
                    {
                        var texture = m.GetTexture(kv.Key);
                        if (texture != null)
                        {
                            var value = textures.IndexOf(texture);
                            if (value == -1)
                            {
                                Debug.LogFormat("not found {0}", texture.name);
                            }
                            else
                            {
                                material.textureProperties.Add(kv.Key, value);
                            }
                        }

                        // offset & scaling
                        var offset  = m.GetTextureOffset(kv.Key);
                        var scaling = m.GetTextureScale(kv.Key);
                        material.vectorProperties.Add(kv.Key,
                                                      new float[] { offset.x, offset.y, scaling.x, scaling.y });
                    }
                    break;

                    case ShaderPropertyType.Vector:
                    {
                        var value = m.GetVector(kv.Key).ToArray();
                        material.vectorProperties.Add(kv.Key, value);
                    }
                    break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }

            foreach (var tag in TAGS)
            {
                var value = m.GetTag(tag, false);
                if (!String.IsNullOrEmpty(value))
                {
                    material.tagMap.Add(tag, value);
                }
            }

            return(material);
        }
コード例 #11
0
        public static async Task <Material> CreateAsync(IAwaitCaller awaitCaller, glTF gltf, int m_index, glTF_VRM_Material vrmMaterial, GetTextureAsyncFunc getTexture)
        {
            var item       = vrmMaterial;
            var shaderName = item.shader;
            var shader     = Shader.Find(shaderName);

            if (shader == null)
            {
                //
                // no shader
                //
                if (VRM_SHADER_NAMES.Contains(shaderName))
                {
                    Debug.LogErrorFormat("shader {0} not found. set Assets/VRM/Shaders/VRMShaders to Edit - project setting - Graphics - preloaded shaders", shaderName);
                }
                else
                {
                    // #if VRM_DEVELOP
                    //                     Debug.LogWarningFormat("unknown shader {0}.", shaderName);
                    // #endif
                }
                return(await MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, gltf, m_index, getTexture));
            }

            //
            // restore VRM material
            //
            var material = new Material(shader);

            // use material.name, because material name may renamed in GltfParser.
            material.name        = gltf.materials[m_index].name;
            material.renderQueue = item.renderQueue;

            foreach (var kv in item.floatProperties)
            {
                material.SetFloat(kv.Key, kv.Value);
            }
            foreach (var kv in item.vectorProperties)
            {
                if (item.textureProperties.ContainsKey(kv.Key))
                {
                    // texture offset & scale
                    material.SetTextureOffset(kv.Key, new Vector2(kv.Value[0], kv.Value[1]));
                    material.SetTextureScale(kv.Key, new Vector2(kv.Value[2], kv.Value[3]));
                }
                else
                {
                    // vector4
                    var v = new Vector4(kv.Value[0], kv.Value[1], kv.Value[2], kv.Value[3]);
                    material.SetVector(kv.Key, v);
                }
            }
            foreach (var kv in item.textureProperties)
            {
                var param   = GetTextureParam.Create(gltf, kv.Value, kv.Key, 1, 1);
                var texture = await getTexture(awaitCaller, gltf, param);

                if (texture != null)
                {
                    material.SetTexture(kv.Key, texture);
                }
            }
            foreach (var kv in item.keywordMap)
            {
                if (kv.Value)
                {
                    material.EnableKeyword(kv.Key);
                }
                else
                {
                    material.DisableKeyword(kv.Key);
                }
            }
            foreach (var kv in item.tagMap)
            {
                material.SetOverrideTag(kv.Key, kv.Value);
            }

            if (shaderName == MToon.Utils.ShaderName)
            {
                // TODO: Material拡張にMToonの項目が追加されたら旧バージョンのshaderPropから変換をかける
                // インポート時にUniVRMに含まれるMToonのバージョンに上書きする
                material.SetFloat(MToon.Utils.PropVersion, MToon.Utils.VersionNumber);
            }

            return(material);
        }
コード例 #12
0
        public static async Task <Material> CreateAsync(IAwaitCaller awaitCaller, GltfParser parser, int m_index, glTF_VRM_Material vrmMaterial, GetTextureAsyncFunc getTexture)
        {
            var item       = vrmMaterial;
            var shaderName = item.shader;
            var shader     = Shader.Find(shaderName);

            if (shader == null)
            {
                //
                // no shader
                //
                if (VRM_SHADER_NAMES.Contains(shaderName))
                {
                    Debug.LogErrorFormat("shader {0} not found. set Assets/VRM/Shaders/VRMShaders to Edit - project setting - Graphics - preloaded shaders", shaderName);
                }
                else
                {
                    // #if VRM_DEVELOP
                    //                     Debug.LogWarningFormat("unknown shader {0}.", shaderName);
                    // #endif
                }
                return(await MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, parser, m_index, getTexture));
            }

            //
            // restore VRM material
            //
            var material = new Material(shader);

            // use material.name, because material name may renamed in GltfParser.
            material.name        = parser.GLTF.materials[m_index].name;
            material.renderQueue = item.renderQueue;

            foreach (var kv in item.floatProperties)
            {
                material.SetFloat(kv.Key, kv.Value);
            }

            var offsetScaleMap = new Dictionary <string, float[]>();

            foreach (var kv in item.vectorProperties)
            {
                if (item.textureProperties.ContainsKey(kv.Key))
                {
                    // texture offset & scale
                    offsetScaleMap.Add(kv.Key, kv.Value);
                }
                else
                {
                    // vector4
                    var v = new Vector4(kv.Value[0], kv.Value[1], kv.Value[2], kv.Value[3]);
                    material.SetVector(kv.Key, v);
                }
            }

            foreach (var kv in item.textureProperties)
            {
                var(offset, scale) = (Vector2.zero, Vector2.one);
                if (offsetScaleMap.TryGetValue(kv.Key, out float[] value))
コード例 #13
0
ファイル: VRMMaterialExporter.cs プロジェクト: vrm-c/UniVRM
        public static glTF_VRM_Material CreateFromMaterial(Material m, ITextureExporter textureExporter)
        {
            var material = new glTF_VRM_Material
            {
                name        = m.name,
                shader      = m.shader.name,
                renderQueue = m.renderQueue,
            };

            if (m.shader.name != MToon.Utils.ShaderName)
            {
                material.shader = glTF_VRM_Material.VRM_USE_GLTFSHADER;
                return(material);
            }

            var prop = PreShaderPropExporter.GetPropsForMToon();

            if (prop == null)
            {
                throw new Exception("arienai");
            }
            else
            {
                foreach (var keyword in m.shaderKeywords)
                {
                    material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword));
                }

                // get properties
                //material.SetProp(prop);
                foreach (var kv in prop.Properties)
                {
                    switch (kv.ShaderPropertyType)
                    {
                    case ShaderPropertyType.Color:
                    {
                        // No color conversion. Because color property is serialized to raw float array.
                        var value = m.GetColor(kv.Key).ToFloat4(ColorSpace.Linear, ColorSpace.Linear);
                        material.vectorProperties.Add(kv.Key, value);
                    }
                    break;

                    case ShaderPropertyType.Range:
                    case ShaderPropertyType.Float:
                    {
                        var value = m.GetFloat(kv.Key);
                        material.floatProperties.Add(kv.Key, value);
                    }
                    break;

                    case ShaderPropertyType.TexEnv:
                    {
                        var texture = m.GetTexture(kv.Key);
                        if (texture != null)
                        {
                            var value       = -1;
                            var isNormalMap = kv.Key == "_BumpMap";
                            if (isNormalMap)
                            {
                                value = textureExporter.RegisterExportingAsNormal(texture);
                            }
                            else
                            {
                                var needsAlpha = kv.Key == "_MainTex";
                                value = textureExporter.RegisterExportingAsSRgb(texture, needsAlpha);
                            }
                            if (value == -1)
                            {
                                Debug.LogFormat("not found {0}", texture.name);
                            }
                            else
                            {
                                material.textureProperties.Add(kv.Key, value);
                            }
                        }

                        // offset & scaling
                        var offset  = m.GetTextureOffset(kv.Key);
                        var scaling = m.GetTextureScale(kv.Key);
                        material.vectorProperties.Add(kv.Key,
                                                      new float[] { offset.x, offset.y, scaling.x, scaling.y });
                    }
                    break;

                    case ShaderPropertyType.Vector:
                    {
                        var value = m.GetVector(kv.Key).ToArray();
                        material.vectorProperties.Add(kv.Key, value);
                    }
                    break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }

            foreach (var tag in TAGS)
            {
                var value = m.GetTag(tag, false);
                if (!String.IsNullOrEmpty(value))
                {
                    material.tagMap.Add(tag, value);
                }
            }

            return(material);
        }
コード例 #14
0
        public static glTF_VRM_Material CreateFromMaterial(Material m, List <Texture2D> textures)
        {
            var material = new glTF_VRM_Material
            {
                name        = m.name,
                shader      = m.shader.name,
                renderQueue = m.renderQueue,
            };

#if UNITY_EDITOR
            for (int i = 0; i < ShaderUtil.GetPropertyCount(m.shader); ++i)
            {
                var name     = ShaderUtil.GetPropertyName(m.shader, i);
                var propType = ShaderUtil.GetPropertyType(m.shader, i);
                switch (propType)
                {
                case ShaderUtil.ShaderPropertyType.Color:
                {
                    var value = m.GetColor(name).ToArray();
                    material.vectorProperties.Add(name, value);
                }
                break;

                case ShaderUtil.ShaderPropertyType.Range:
                case ShaderUtil.ShaderPropertyType.Float:
                {
                    var value = m.GetFloat(name);
                    material.floatProperties.Add(name, value);
                }
                break;

                case ShaderUtil.ShaderPropertyType.TexEnv:
                {
                    var texture = m.GetTexture(name) as Texture2D;
                    if (texture != null)
                    {
                        var value = textures.IndexOf(texture);
                        if (value == -1)
                        {
                            Debug.LogFormat("not found {0}", texture.name);
                        }
                        else
                        {
                            material.textureProperties.Add(name, value);
                        }
                    }

                    // offset & scaling
                    var offset  = m.GetTextureOffset(name);
                    var scaling = m.GetTextureScale(name);
                    material.vectorProperties.Add(name,
                                                  new float[] { offset.x, offset.y, scaling.x, scaling.y });
                }
                break;

                case ShaderUtil.ShaderPropertyType.Vector:
                {
                    var value = m.GetVector(name).ToArray();
                    material.vectorProperties.Add(name, value);
                }
                break;

                default:
                    throw new NotImplementedException();
                }
            }
#else
            Debug.LogWarning("cannot export material properties on runtime");
#endif

            foreach (var keyword in m.shaderKeywords)
            {
                material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword));
            }

            foreach (var tag in TAGS)
            {
                var value = m.GetTag(tag, false);
                if (!String.IsNullOrEmpty(value))
                {
                    material.tagMap.Add(tag, value);
                }
            }

            return(material);
        }
コード例 #15
0
 public static bool TryGetTextureOffsetAndScale(glTF_VRM_Material vrmMaterial, string unityTextureKey, out (Vector2 offset, Vector2 scale) os)
コード例 #16
0
 public MToonMaterialItem(int i, glTFMaterial src, bool hasVertexColor, glTF_VRM_Material vrmMaterial) : base(i, src)
 {
     m_hasVertexColor = hasVertexColor;
     m_vrmMaterial    = vrmMaterial;
 }