Ejemplo n.º 1
0
        public void MaterialImportTest()
        {
            var material = MaterialFactory.CreateMaterial(0, new glTFMaterial {
            }, false).GetOrCreate(null);

            Assert.AreEqual("Standard", material.shader.name);
        }
Ejemplo n.º 2
0
        public void TextureTransformTest()
        {
            var tex0 = new Texture2D(128, 128)
            {
                wrapMode   = TextureWrapMode.Repeat,
                filterMode = FilterMode.Bilinear,
            };

            var textureManager = new TextureExportManager(new Texture[] { tex0 });
            var srcMaterial    = new Material(Shader.Find("Standard"));

            var offset = new Vector2(0.3f, 0.2f);
            var scale  = new Vector2(0.5f, 0.6f);

            srcMaterial.mainTexture       = tex0;
            srcMaterial.mainTextureOffset = offset;
            srcMaterial.mainTextureScale  = scale;

            var materialExporter = new MaterialExporter();
            var gltfMaterial     = materialExporter.ExportMaterial(srcMaterial, textureManager);

            gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions = gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions.Deserialize();

            var dstMaterial = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);

            Assert.AreEqual(dstMaterial.mainTextureOffset.x, offset.x, 0.3f);
            Assert.AreEqual(dstMaterial.mainTextureOffset.y, offset.y, 0.2f);
            Assert.AreEqual(dstMaterial.mainTextureScale.x, scale.x, 0.5f);
            Assert.AreEqual(dstMaterial.mainTextureScale.y, scale.y, 0.6f);
        }
Ejemplo n.º 3
0
        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 gltfMaterial = new glTFMaterial
                {
                    alphaMode            = "OPAQUE",
                    pbrMetallicRoughness = new glTFPbrMetallicRoughness
                    {
                        baseColorFactor = new float[] { 1, 0, 0, 1 },
                    },
                    extensions = extension,
                };

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

                var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }
        }
Ejemplo n.º 4
0
        public static async Task <Material> CreateAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture)
        {
            if (getTexture == null)
            {
                getTexture = (IAwaitCaller _awaitCaller, glTF _gltf, TextureImportParam _param) => Task.FromResult <Texture2D>(null);
            }

            if (i < 0 || i >= parser.GLTF.materials.Count)
            {
                return(MaterialFactory.CreateMaterial(i, null, ShaderName));
            }

            var src           = parser.GLTF.materials[i];
            var material      = MaterialFactory.CreateMaterial(i, src, ShaderName);
            var standardParam = default(TextureImportParam);

            if (src.pbrMetallicRoughness != null || src.occlusionTexture != null)
            {
                if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null)
                {
                    standardParam = StandardTexture(parser, src);
                }
                if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4)
                {
                    var color = src.pbrMetallicRoughness.baseColorFactor;
                    material.color = (new Color(color[0], color[1], color[2], color[3])).gamma;
                }

                if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1)
                {
                    material.mainTexture = await getTexture(awaitCaller, parser.GLTF, BaseColorTexture(parser, src));

                    // Texture Offset and Scale
                    MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex");
                }

                if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
                {
                    material.EnableKeyword("_METALLICGLOSSMAP");

                    var texture = await getTexture(awaitCaller, parser.GLTF, standardParam);

                    if (texture != null)
                    {
                        material.SetTexture(TextureImportParam.METALLIC_GLOSS_PROP, texture);
                    }

                    material.SetFloat("_Metallic", 1.0f);
                    // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212.
                    material.SetFloat("_GlossMapScale", 1.0f);

                    // Texture Offset and Scale
                    MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.metallicRoughnessTexture, "_MetallicGlossMap");
                }
                else
                {
                    material.SetFloat("_Metallic", src.pbrMetallicRoughness.metallicFactor);
                    material.SetFloat("_Glossiness", 1.0f - src.pbrMetallicRoughness.roughnessFactor);
                }
            }

            if (src.normalTexture != null && src.normalTexture.index != -1)
            {
                material.EnableKeyword("_NORMALMAP");
                var texture = await getTexture(awaitCaller, parser.GLTF, NormalTexture(parser, src));

                if (texture != null)
                {
                    material.SetTexture(TextureImportParam.NORMAL_PROP, texture);
                    material.SetFloat("_BumpScale", src.normalTexture.scale);
                }

                // Texture Offset and Scale
                MaterialFactory.SetTextureOffsetAndScale(material, src.normalTexture, "_BumpMap");
            }

            if (src.occlusionTexture != null && src.occlusionTexture.index != -1)
            {
                var texture = await getTexture(awaitCaller, parser.GLTF, standardParam);

                if (texture != null)
                {
                    material.SetTexture(TextureImportParam.OCCLUSION_PROP, texture);
                    material.SetFloat("_OcclusionStrength", src.occlusionTexture.strength);
                }

                // Texture Offset and Scale
                MaterialFactory.SetTextureOffsetAndScale(material, src.occlusionTexture, "_OcclusionMap");
            }

            if (src.emissiveFactor != null ||
                (src.emissiveTexture != null && src.emissiveTexture.index != -1))
            {
                material.EnableKeyword("_EMISSION");
                material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack;

                if (src.emissiveFactor != null && src.emissiveFactor.Length == 3)
                {
                    material.SetColor("_EmissionColor", new Color(src.emissiveFactor[0], src.emissiveFactor[1], src.emissiveFactor[2]));
                }

                if (src.emissiveTexture != null && src.emissiveTexture.index != -1)
                {
                    var texture = await getTexture(awaitCaller, parser.GLTF, TextureFactory.CreateSRGB(parser, src.emissiveTexture.index));

                    if (texture != null)
                    {
                        material.SetTexture("_EmissionMap", texture);
                    }

                    // Texture Offset and Scale
                    MaterialFactory.SetTextureOffsetAndScale(material, src.emissiveTexture, "_EmissionMap");
                }
            }

            BlendMode blendMode = BlendMode.Opaque;

            // https://forum.unity.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/#post-2229980
            switch (src.alphaMode)
            {
            case "BLEND":
                blendMode = BlendMode.Fade;
                material.SetOverrideTag("RenderType", "Transparent");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                material.SetInt("_ZWrite", 0);
                material.DisableKeyword("_ALPHATEST_ON");
                material.EnableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = 3000;
                break;

            case "MASK":
                blendMode = BlendMode.Cutout;
                material.SetOverrideTag("RenderType", "TransparentCutout");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt("_ZWrite", 1);
                material.SetFloat("_Cutoff", src.alphaCutoff);
                material.EnableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = 2450;

                break;

            default:     // OPAQUE
                blendMode = BlendMode.Opaque;
                material.SetOverrideTag("RenderType", "");
                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt("_ZWrite", 1);
                material.DisableKeyword("_ALPHATEST_ON");
                material.DisableKeyword("_ALPHABLEND_ON");
                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                material.renderQueue = -1;
                break;
            }

            material.SetFloat("_Mode", (float)blendMode);

            return(material);
        }
Ejemplo n.º 5
0
        public void UnlitShaderImportTest()
        {
            {
                // OPAQUE/Color
                var gltfMaterial = new glTFMaterial
                {
                    alphaMode            = "OPAQUE",
                    pbrMetallicRoughness = new glTFPbrMetallicRoughness
                    {
                        baseColorFactor = new float[] { 1, 0, 0, 1 },
                    },
                    extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
                };
                var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }

            {
                // OPAQUE/Texture
                var gltfMaterial = new glTFMaterial
                {
                    alphaMode            = "OPAQUE",
                    pbrMetallicRoughness = new glTFPbrMetallicRoughness
                    {
                        baseColorTexture = new glTFMaterialBaseColorTextureInfo(),
                    },
                    extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
                };
                var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }

            {
                // OPAQUE/Color/Texture
                var gltfMaterial = new glTFMaterial
                {
                    alphaMode            = "OPAQUE",
                    pbrMetallicRoughness = new glTFPbrMetallicRoughness
                    {
                        baseColorFactor  = new float[] { 1, 0, 0, 1 },
                        baseColorTexture = new glTFMaterialBaseColorTextureInfo(),
                    },
                    extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
                };
                var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }

            {
                // BLEND/Color
                var gltfMaterial = new glTFMaterial
                {
                    alphaMode            = "BLEND",
                    pbrMetallicRoughness = new glTFPbrMetallicRoughness
                    {
                        baseColorFactor = new float[] { 1, 0, 0, 1 },
                    },
                    extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
                };
                var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }

            {
                // BLEND/Texture
                var gltfMaterial = new glTFMaterial
                {
                    alphaMode            = "BLEND",
                    pbrMetallicRoughness = new glTFPbrMetallicRoughness
                    {
                        baseColorTexture = new glTFMaterialBaseColorTextureInfo(),
                    },
                    extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
                };
                var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }

            {
                // BLEND/Color/Texture
                var gltfMaterial = new glTFMaterial
                {
                    alphaMode            = "BLEND",
                    pbrMetallicRoughness = new glTFPbrMetallicRoughness
                    {
                        baseColorFactor  = new float[] { 1, 0, 0, 1 },
                        baseColorTexture = new glTFMaterialBaseColorTextureInfo(),
                    },
                    extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
                };
                var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }

            {
                // MASK/Texture
                var gltfMaterial = new glTFMaterial
                {
                    alphaMode            = "MASK",
                    pbrMetallicRoughness = new glTFPbrMetallicRoughness
                    {
                        baseColorTexture = new glTFMaterialBaseColorTextureInfo(),
                    },
                    extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
                };
                var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }

            {
                // MASK/Color/Texture
                var gltfMaterial = new glTFMaterial
                {
                    alphaMode            = "MASK",
                    pbrMetallicRoughness = new glTFPbrMetallicRoughness
                    {
                        baseColorFactor  = new float[] { 1, 0, 0, 1 },
                        baseColorTexture = new glTFMaterialBaseColorTextureInfo(),
                    },
                    extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
                };
                var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }

            {
                // default
                var gltfMaterial = new glTFMaterial
                {
                    extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(),
                };
                var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreate(null);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }
        }