Inheritance: IJsonSerializable
 public void MaterialTestError()
 {
     var model = new glTFMaterial()
     {
         name           = "b",
         emissiveFactor = new float[] { 1.5f, 0.5f, 0.5f },
     };
 }
Esempio n. 2
0
 public static string GetMaterialName(int index, glTFMaterial src)
 {
     if (src != null && !string.IsNullOrEmpty(src.name))
     {
         return(src.name);
     }
     return($"material_{index:00}");
 }
 public void MaterialAlphaTest()
 {
     var model = new glTFMaterial()
     {
         name           = "a",
         emissiveFactor = new float[] { 0.5f, 0.5f, 0.5f },
         alphaMode      = "MASK",
     };
 }
        public void MaterialTest()
        {
            var model = new glTFMaterial()
            {
                name           = "a",
                emissiveFactor = new float[] { 0.5f, 0.5f, 0.5f },
            };

            var json = model.ToJson();

            Assert.AreEqual(@"{""name"":""a"",""pbrMetallicRoughness"":{""baseColorFactor"":[1,1,1,1],""metallicFactor"":1,""roughnessFactor"":1},""emissiveFactor"":[0.5,0.5,0.5],""doubleSided"":false}", json);
            Debug.Log(json);
        }
Esempio n. 5
0
        public static bool IsEnable(glTFMaterial m)
        {
            if (m.extensions is glTFExtensionImport imported)
            {
                foreach (var kv in imported.ObjectItems())
                {
                    if (kv.Key.GetUtf8String() == ExtensionNameUtf8)
                    {
                        return(kv.Value.Value.ValueType == ValueNodeType.Object);
                    }
                }
            }

            return(false);
        }
        public Shader GetShader(glTFMaterial material)
        {
            if (material == null)
            {
                return(Default);
            }

            if (material.extensions != null && material.extensions.KHR_materials_unlit != null)
            {
                return(UniUnlit);
            }

            // standard
            return(Default);
        }
        public Shader GetShader(glTFMaterial material)
        {
            if (material == null)
            {
                return(Default);
            }

            if (glTF_KHR_materials_unlit.IsEnable(material))
            {
                return(UniUnlit);
            }

            // standard
            return(Default);
        }
        static void Export_Color(Material m, List <Texture> textures, glTFMaterial material)
        {
            if (m.HasProperty("_Color"))
            {
                material.pbrMetallicRoughness.baseColorFactor = m.color.ToArray();
            }

            if (m.mainTexture != null)
            {
                material.pbrMetallicRoughness.baseColorTexture = new glTFTextureInfo
                {
                    index = textures.IndexOf(m.mainTexture),
                };
            }
        }
Esempio n. 9
0
        public static MaterialItemBase CreateMaterial(int i, glTFMaterial x, bool hasVertexColor)
        {
            if (x == null)
            {
                UnityEngine.Debug.LogWarning("glTFMaterial is empty");
                return(new PBRMaterialItem(i, x));
            }

            if (glTF_KHR_materials_unlit.IsEnable(x))
            {
                return(new UnlitMaterialItem(i, x, hasVertexColor));
            }

            return(new PBRMaterialItem(i, x));
        }
Esempio n. 10
0
        public static TextureImportParam StandardTexture(GltfParser parser, glTFMaterial src)
        {
            var metallicFactor  = 1.0f;
            var roughnessFactor = 1.0f;

            if (src.pbrMetallicRoughness != null)
            {
                metallicFactor  = src.pbrMetallicRoughness.metallicFactor;
                roughnessFactor = src.pbrMetallicRoughness.roughnessFactor;
            }
            return(TextureFactory.CreateStandard(parser,
                                                 src.pbrMetallicRoughness?.metallicRoughnessTexture?.index,
                                                 src.occlusionTexture?.index,
                                                 metallicFactor,
                                                 roughnessFactor));
        }
Esempio n. 11
0
        public static GetTextureParam StandardTexture(glTF gltf, glTFMaterial src)
        {
            var metallicFactor  = 1.0f;
            var roughnessFactor = 1.0f;

            if (src.pbrMetallicRoughness != null)
            {
                metallicFactor  = src.pbrMetallicRoughness.metallicFactor;
                roughnessFactor = src.pbrMetallicRoughness.roughnessFactor;
            }
            return(GetTextureParam.CreateStandard(gltf,
                                                  src.pbrMetallicRoughness?.metallicRoughnessTexture?.index,
                                                  src.occlusionTexture?.index,
                                                  metallicFactor,
                                                  roughnessFactor));
        }
Esempio n. 12
0
        public bool MaterialHasVertexColor(glTFMaterial material)
        {
            if (material == null)
            {
                return(false);
            }

            var materialIndex = materials.IndexOf(material);

            if (materialIndex == -1)
            {
                return(false);
            }

            return(MaterialHasVertexColor(materialIndex));
        }
        static void Export_Metallic(Material m, TextureExportManager textureManager, glTFMaterial material)
        {
            int index = -1;

            if (m.HasProperty("_MetallicGlossMap"))
            {
                float smoothness = 0.0f;
                if (m.HasProperty("_GlossMapScale"))
                {
                    smoothness = m.GetFloat("_GlossMapScale");
                }

                // Bake smoothness values into a texture.
                var converter = new MetallicRoughnessConverter(smoothness);
                index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), converter);
                if (index != -1)
                {
                    material.pbrMetallicRoughness.metallicRoughnessTexture =
                        new glTFMaterialMetallicRoughnessTextureInfo()
                    {
                        index = index,
                    };

                    Export_MainTextureTransform(m, material.pbrMetallicRoughness.metallicRoughnessTexture);
                }
            }

            if (index != -1)
            {
                material.pbrMetallicRoughness.metallicFactor = 1.0f;
                // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212.
                material.pbrMetallicRoughness.roughnessFactor = 1.0f;
            }
            else
            {
                if (m.HasProperty("_Metallic"))
                {
                    material.pbrMetallicRoughness.metallicFactor = m.GetFloat("_Metallic");
                }

                if (m.HasProperty("_Glossiness"))
                {
                    material.pbrMetallicRoughness.roughnessFactor = 1.0f - m.GetFloat("_Glossiness");
                }
            }
        }
Esempio n. 14
0
        public void MaterialEmissiveStrengthTest()
        {
            // serialize
            var material = new glTFMaterial();

            glTF_KHR_materials_emissive_strength.Serialize(ref material.extensions, 5.0f);
            var json   = material.ToJson();
            var parsed = json.ParseAsJson();

            Assert.AreEqual(parsed["extensions"]["KHR_materials_emissive_strength"]["emissiveStrength"].GetSingle(), 5.0f);

            // deserialize
            var imported = GltfDeserializer.Deserialize_gltf_materials_ITEM(parsed);

            Assert.True(glTF_KHR_materials_emissive_strength.TryGet(imported.extensions, out glTF_KHR_materials_emissive_strength extension));
            Assert.AreEqual(extension.emissiveStrength, 5.0f);
        }
Esempio n. 15
0
        public void MaterialTest()
        {
            var q = "\"";

            {
                var data = new UniGLTF.glTFMaterial
                {
                    name = "Some",
                };

                var json = Serialize(data, UniGLTF.GltfSerializer.Serialize_gltf_materials_ITEM);
                Assert.AreEqual($"{{{q}name{q}:{q}Some{q},{q}pbrMetallicRoughness{q}:{{{q}baseColorFactor{q}:[1,1,1,1],{q}metallicFactor{q}:1,{q}roughnessFactor{q}:1}},{q}doubleSided{q}:false}}", json);
            }

            {
                var data = new UniGLTF.glTF();
                data.textures.Add(new UniGLTF.glTFTexture
                {
                });

                var json = Serialize(data, UniGLTF.GltfSerializer.Serialize);
                // Assert.Equal($"{{ {q}name{q}: {q}Some{q} }}", json);
            }

            {
                var data = new UniGLTF.glTFMaterial
                {
                    name = "Alicia_body",
                    pbrMetallicRoughness = new UniGLTF.glTFPbrMetallicRoughness
                    {
                        // BaseColorFactor = new[] { 1, 1, 1, 1 },
                        // BaseColorTexture= { },
                        metallicFactor  = 0,
                        roughnessFactor = 0.9f
                    },
                    alphaMode   = "OPAQUE",
                    alphaCutoff = 0.5f,
                    extensions  = new UniGLTF.glTFExtensionExport().Add(
                        UniGLTF.glTF_KHR_materials_unlit.ExtensionName,
                        new ArraySegment <byte>(UniGLTF.glTF_KHR_materials_unlit.Raw))
                };

                var json = Serialize(data, UniGLTF.GltfSerializer.Serialize_gltf_materials_ITEM);
                // Assert.Equal($"{{ {q}name{q}: {q}Some{q} }}", json);
            }
        }
Esempio n. 16
0
        public void MaterialAlphaTest()
        {
            var model = new glTFMaterial()
            {
                name           = "a",
                emissiveFactor = new float[] { 0.5f, 0.5f, 0.5f },
                alphaMode      = "MASK",
            };

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

            Assert.AreEqual(@"{""name"":""a"",""emissiveFactor"":[0.5,0.5,0.5],""alphaMode"":""MASK"",""alphaCutoff"":0.5,""doubleSided"":false}", json);
        }
Esempio n. 17
0
        public static IEnumerable <GetTextureParam> EnumerateTextures(glTF gltf, glTFMaterial m)
        {
            int?metallicRoughnessTexture = default;

            if (m.pbrMetallicRoughness != null)
            {
                // base color
                if (m.pbrMetallicRoughness?.baseColorTexture != null)
                {
                    yield return(PBRMaterialItem.BaseColorTexture(gltf, m));
                }

                // metallic roughness
                if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null && m.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
                {
                    metallicRoughnessTexture = m.pbrMetallicRoughness?.metallicRoughnessTexture?.index;
                }
            }

            // emission
            if (m.emissiveTexture != null)
            {
                yield return(GetTextureParam.CreateSRGB(gltf, m.emissiveTexture.index));
            }

            // normal
            if (m.normalTexture != null)
            {
                yield return(PBRMaterialItem.NormalTexture(gltf, m));
            }

            // occlusion
            int?occlusionTexture = default;

            if (m.occlusionTexture != null && m.occlusionTexture.index != -1)
            {
                occlusionTexture = m.occlusionTexture.index;
            }

            // metallicSmooth and occlusion
            if (metallicRoughnessTexture.HasValue || occlusionTexture.HasValue)
            {
                yield return(PBRMaterialItem.StandardTexture(gltf, m));
            }
        }
Esempio n. 18
0
        public void MaterialTestError()
        {
            var model = new glTFMaterial()
            {
                name           = "b",
                emissiveFactor = new float[] { 1.5f, 0.5f, 0.5f },
            };

            var c = new JsonSchemaValidationContext("")
            {
                EnableDiagnosisForNotRequiredFields = true,
            };
            var ex = Assert.Throws <JsonSchemaValidationException>(
                () => JsonSchema.FromType <glTFMaterial>().Serialize(model, c)
                );

            Assert.AreEqual("[emissiveFactor.String] maximum: ! 1.5<=1", ex.Message);
        }
Esempio n. 19
0
        public static TextureImportParam StandardTexture(GltfParser parser, glTFMaterial src)
        {
            var metallicFactor  = 1.0f;
            var roughnessFactor = 1.0f;

            if (src.pbrMetallicRoughness != null)
            {
                metallicFactor  = src.pbrMetallicRoughness.metallicFactor;
                roughnessFactor = src.pbrMetallicRoughness.roughnessFactor;
            }
            var(offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.metallicRoughnessTexture);
            return(GltfTextureImporter.CreateStandard(parser,
                                                      src.pbrMetallicRoughness?.metallicRoughnessTexture?.index,
                                                      src.occlusionTexture?.index,
                                                      offset, scale,
                                                      metallicFactor,
                                                      roughnessFactor));
        }
        public void glTF_KHR_materials_unlit_Test()
        {
            {
                var value = "{}".ParseAsJson();
                Assert.AreEqual(value.Value.ValueType, ValueNodeType.Object);
                Assert.AreEqual(0, value.GetObjectCount());
                var list = value.ObjectItems().ToArray();
                Assert.AreEqual(0, list.Length);
            }

            {
                var value = "{\"unlit\":{}}".ParseAsJson();
                Assert.AreEqual(value.Value.ValueType, ValueNodeType.Object);
                Assert.AreEqual(1, value.GetObjectCount());
                var list = value.ObjectItems().ToArray();
                Assert.AreEqual(1, list.Length);
                Assert.AreEqual("unlit", list[0].Key.GetString());
                Assert.AreEqual(0, list[0].Value.GetObjectCount());
            }

            {
                var extension = glTF_KHR_materials_unlit.Serialize().Deserialize();
                var list      = extension.ObjectItems().ToArray();
                Assert.AreEqual(1, list.Length);
                Assert.AreEqual(glTF_KHR_materials_unlit.ExtensionNameUtf8, list[0].Key.GetUtf8String());
                Assert.AreEqual(0, list[0].Value.GetObjectCount());

                var material = new glTFMaterial
                {
                    alphaMode            = "OPAQUE",
                    pbrMetallicRoughness = new glTFPbrMetallicRoughness
                    {
                        baseColorFactor = new float[] { 1, 0, 0, 1 },
                    },
                    extensions = extension,
                };

                Assert.IsTrue(glTF_KHR_materials_unlit.IsEnable(material));

                var shaderStore = new ShaderStore(null);
                var shader      = shaderStore.GetShader(material);
                Assert.AreEqual("UniGLTF/UniUnlit", shader.name);
            }
        }
Esempio n. 21
0
        static void Export_Color(Material m, TextureExportManager textureManager, glTFMaterial material)
        {
            if (m.HasProperty("_Color"))
            {
                material.pbrMetallicRoughness.baseColorFactor = m.color.ToArray();
            }

            if (m.HasProperty("_MainTex"))
            {
                var index = textureManager.CopyAndGetIndex(m.GetTexture("_MainTex"), RenderTextureReadWrite.sRGB);
                if (index != -1)
                {
                    material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo()
                    {
                        index = index,
                    };
                }
            }
        }
Esempio n. 22
0
        static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material)
        {
            if (m.HasProperty("_BumpMap"))
            {
                var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), new NormalConverter());
                if (index != -1)
                {
                    material.normalTexture = new glTFMaterialNormalTextureInfo()
                    {
                        index = index,
                    };
                }

                if (index != -1 && m.HasProperty("_BumpScale"))
                {
                    material.normalTexture.scale = m.GetFloat("_BumpScale");
                }
            }
        }
        static void Export_Emission(Material m, TextureExportManager textureManager, glTFMaterial material)
        {
            if (m.HasProperty("_EmissionColor"))
            {
                var color = m.GetColor("_EmissionColor");
                material.emissiveFactor = new float[] { color.r, color.g, color.b };
            }

            if (m.HasProperty("_EmissionMap"))
            {
                var index = textureManager.CopyAndGetIndex(m.GetTexture("_EmissionMap"), RenderTextureReadWrite.sRGB);
                if (index != -1)
                {
                    material.emissiveTexture = new glTFMaterialEmissiveTextureInfo()
                    {
                        index = index,
                    };
                }
            }
        }
Esempio n. 24
0
 /// <summary>
 /// for unittest
 /// </summary>
 public static glTF CreateMaterialForTest(glTFMaterial material)
 {
     return(new glTF
     {
         materials = new System.Collections.Generic.List <glTFMaterial> {
             material
         },
         textures = new List <glTFTexture> {
             new glTFTexture {
                 name = "texture_0"
             }
         },
         images = new List <glTFImage> {
             new glTFImage {
                 name = "image_0",
                 mimeType = "image/png",
             }
         },
     });
 }
Esempio n. 25
0
        public void MaterialTest()
        {
            var model = new glTFMaterial()
            {
                name           = "a",
                emissiveFactor = new float[] { 0.5f, 0.5f, 0.5f },
            };

            var json = model.ToJson();

            Assert.AreEqual(@"{""name"":""a"",""emissiveFactor"":[0.5,0.5,0.5],""doubleSided"":false}", json);
            Debug.Log(json);

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

            Assert.AreEqual(@"{""name"":""a"",""emissiveFactor"":[0.5,0.5,0.5],""doubleSided"":false}", json2);
        }
Esempio n. 26
0
        static void Export_Color(Material m, ITextureExporter textureManager, glTFMaterial material)
        {
            if (m.HasProperty("_Color"))
            {
                material.pbrMetallicRoughness.baseColorFactor = m.GetColor("_Color").ToFloat4(ColorSpace.sRGB, ColorSpace.Linear);
            }

            if (m.HasProperty("_MainTex"))
            {
                var index = textureManager.ExportAsSRgb(m.GetTexture("_MainTex"));
                if (index != -1)
                {
                    material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo()
                    {
                        index = index,
                    };

                    Export_MainTextureTransform(m, material.pbrMetallicRoughness.baseColorTexture);
                }
            }
        }
Esempio n. 27
0
        static void Export_Normal(Material m, ITextureExporter textureExporter, glTFMaterial material)
        {
            if (m.HasProperty("_BumpMap"))
            {
                var index = textureExporter.ExportAsNormal(m.GetTexture("_BumpMap"));
                if (index != -1)
                {
                    material.normalTexture = new glTFMaterialNormalTextureInfo()
                    {
                        index = index,
                    };

                    Export_MainTextureTransform(m, material.normalTexture);
                }

                if (index != -1 && m.HasProperty("_BumpScale"))
                {
                    material.normalTexture.scale = m.GetFloat("_BumpScale");
                }
            }
        }
        static void Export_Color(Material m, TextureExporter textureManager, glTFMaterial material)
        {
            if (m.HasProperty("_Color"))
            {
                material.pbrMetallicRoughness.baseColorFactor = m.color.linear.ToArray();
            }

            if (m.HasProperty("_MainTex"))
            {
                var index = textureManager.ExportSRGB(m.GetTexture("_MainTex"));
                if (index != -1)
                {
                    material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo()
                    {
                        index = index,
                    };

                    Export_MainTextureTransform(m, material.pbrMetallicRoughness.baseColorTexture);
                }
            }
        }
Esempio n. 29
0
        static void Export_Emission(Material m, ITextureExporter textureExporter, glTFMaterial material, bool useEmissiveMultiplier)
        {
            if (m.IsKeywordEnabled("_EMISSION") == false)
            {
                return;
            }

            if (m.HasProperty("_EmissionColor"))
            {
                var color = m.GetColor("_EmissionColor");
                if (color.maxColorComponent > 1)
                {
                    var maxColorComponent = color.maxColorComponent;
                    color /= maxColorComponent;
                    if (useEmissiveMultiplier)
                    {
                        UniGLTF.Extensions.VRMC_materials_hdr_emissiveMultiplier.GltfSerializer.SerializeTo(ref material.extensions,
                                                                                                            new Extensions.VRMC_materials_hdr_emissiveMultiplier.VRMC_materials_hdr_emissiveMultiplier
                        {
                            EmissiveMultiplier = maxColorComponent,
                        });
                    }
                }
                material.emissiveFactor = color.ToFloat3(ColorSpace.Linear, ColorSpace.Linear);
            }

            if (m.HasProperty(EMISSION_TEX_PROP))
            {
                var index = textureExporter.RegisterExportingAsSRgb(m.GetTexture(EMISSION_TEX_PROP), needsAlpha: false);
                if (index != -1)
                {
                    material.emissiveTexture = new glTFMaterialEmissiveTextureInfo()
                    {
                        index = index,
                    };

                    Export_MainTextureTransform(m, material.emissiveTexture);
                }
            }
        }
Esempio n. 30
0
        public static glTFMaterial ExportMaterial(Material m, List <Texture> textures)
        {
            var material = new glTFMaterial
            {
                name = m.name,
                pbrMetallicRoughness = new glTFPbrMetallicRoughness(),
            };

            if (m.HasProperty("_Color"))
            {
                material.pbrMetallicRoughness.baseColorFactor = m.color.ToArray();
            }

            if (m.mainTexture != null)
            {
                material.pbrMetallicRoughness.baseColorTexture = new glTFTextureInfo
                {
                    index = textures.IndexOf(m.mainTexture),
                };
            }

            return(material);
        }