Esempio n. 1
0
        public void GltfSampleModelsTest_DamagedHelmet()
        {
            var env = System.Environment.GetEnvironmentVariable("GLTF_SAMPLE_MODELS");

            if (string.IsNullOrEmpty(env))
            {
                return;
            }
            var root = new DirectoryInfo($"{env}/2.0");

            if (!root.Exists)
            {
                return;
            }

            {
                var path   = Path.Combine(root.FullName, "DamagedHelmet/glTF-Binary/DamagedHelmet.glb");
                var parser = new GltfParser();
                parser.ParsePath(path);

                var materialParam = new GltfMaterialImporter().GetMaterialParam(parser, 0);
                Assert.AreEqual("Standard", materialParam.ShaderName);
                Assert.AreEqual(5, materialParam.TextureSlots.Count);
                var(key, value) = materialParam.EnumerateSubAssetKeyValue().First();
                Assert.AreEqual(new SubAssetKey(typeof(Texture2D), "texture_0"), key);
            }
        }
Esempio n. 2
0
        public static IEnumerable <(SubAssetKey, TextureImportParam)> EnumerateTexturesForMaterial(GltfParser parser, int i)
        {
            var m = parser.GLTF.materials[i];

            int?metallicRoughnessTexture = default;

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

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

            // emission
            if (m.emissiveTexture != null)
            {
                var(offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(m.emissiveTexture);
                yield return(GltfTextureImporter.CreateSRGB(parser, m.emissiveTexture.index, offset, scale));
            }

            // normal
            if (m.normalTexture != null)
            {
                yield return(GltfPBRMaterial.NormalTexture(parser, 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(GltfPBRMaterial.StandardTexture(parser, m));
            }
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        public static (SubAssetKey, 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);
            var param = GltfTextureImporter.CreateStandard(parser,
                                                           src.pbrMetallicRoughness?.metallicRoughnessTexture?.index,
                                                           src.occlusionTexture?.index,
                                                           offset, scale,
                                                           metallicFactor,
                                                           roughnessFactor);
            var key = new SubAssetKey(typeof(Texture2D), param.UnityObjectName);

            return(key, param);
        }
Esempio n. 5
0
        public static bool TryCreateParam(GltfParser parser, int i, out MaterialImportParam param)
        {
            if (i < 0 || i >= parser.GLTF.materials.Count)
            {
                param = default;
                return(false);
            }

            var src = parser.GLTF.materials[i];

            param = new MaterialImportParam(GltfMaterialImporter.MaterialName(i, src), ShaderName);

            var standardParam = default(TextureImportParam);

            if (src.pbrMetallicRoughness != null || src.occlusionTexture != null)
            {
                if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null)
                {
                    SubAssetKey key;
                    (key, standardParam) = StandardTexture(parser, src);
                }

                if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4)
                {
                    param.Colors.Add("_Color",
                                     src.pbrMetallicRoughness.baseColorFactor.ToColor4(ColorSpace.Linear, ColorSpace.sRGB)
                                     );
                }

                if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1)
                {
                    var(key, textureParam) = BaseColorTexture(parser, src);
                    param.TextureSlots.Add("_MainTex", textureParam);
                }

                if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
                {
                    param.Actions.Add(material => material.EnableKeyword("_METALLICGLOSSMAP"));
                    param.TextureSlots.Add("_MetallicGlossMap", standardParam);
                    // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212.
                    param.FloatValues.Add("_Metallic", 1.0f);
                    param.FloatValues.Add("_GlossMapScale", 1.0f);
                }
                else
                {
                    param.FloatValues.Add("_Metallic", src.pbrMetallicRoughness.metallicFactor);
                    param.FloatValues.Add("_Glossiness", 1.0f - src.pbrMetallicRoughness.roughnessFactor);
                }
            }

            if (src.normalTexture != null && src.normalTexture.index != -1)
            {
                param.Actions.Add(material => material.EnableKeyword("_NORMALMAP"));
                var(key, textureParam) = NormalTexture(parser, src);
                param.TextureSlots.Add("_BumpMap", textureParam);
                param.FloatValues.Add("_BumpScale", src.normalTexture.scale);
            }

            if (src.occlusionTexture != null && src.occlusionTexture.index != -1)
            {
                param.TextureSlots.Add("_OcclusionMap", standardParam);
                param.FloatValues.Add("_OcclusionStrength", src.occlusionTexture.strength);
            }

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

                if (src.emissiveFactor != null && src.emissiveFactor.Length == 3)
                {
                    param.Colors.Add("_EmissionColor",
                                     src.emissiveFactor.ToColor3(ColorSpace.Linear, ColorSpace.Linear)
                                     );
                }

                if (src.emissiveTexture != null && src.emissiveTexture.index != -1)
                {
                    var(key, textureParam) = EmissiveTexture(parser, src);
                    param.TextureSlots.Add("_EmissionMap", textureParam);
                }
            }

            param.Actions.Add(material =>
            {
                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(true);
        }
Esempio n. 6
0
 public static (SubAssetKey, TextureImportParam Param) EmissiveTexture(GltfParser parser, glTFMaterial src)
 {
     var(offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(src.emissiveTexture);
     return(GltfTextureImporter.CreateSRGB(parser, src.emissiveTexture.index, offset, scale));
 }
Esempio n. 7
0
 public static (SubAssetKey, TextureImportParam Param) BaseColorTexture(GltfParser parser, glTFMaterial src)
 {
     var(offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
     return(GltfTextureImporter.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale));
 }
        public static bool TryCreateParam(GltfParser parser, int i, out MaterialImportParam param)
        {
            if (i < 0 || i >= parser.GLTF.materials.Count)
            {
                param = default;
                return(false);
            }

            var src = parser.GLTF.materials[i];

            if (!glTF_KHR_materials_unlit.IsEnable(src))
            {
                param = default;
                return(false);
            }

            param = new MaterialImportParam(GltfMaterialImporter.MaterialName(i, src), ShaderName);

            // texture
            if (src.pbrMetallicRoughness.baseColorTexture != null)
            {
                var(offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
                var textureParam = GltfTextureImporter.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale);
                param.TextureSlots.Add("_MainTex", textureParam);
            }

            // color
            if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4)
            {
                var color = src.pbrMetallicRoughness.baseColorFactor;
                param.Colors.Add("_Color", (new Color(color[0], color[1], color[2], color[3])).gamma);
            }

            //renderMode
            param.Actions.Add(material =>
            {
                if (src.alphaMode == "OPAQUE")
                {
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
                }
                else if (src.alphaMode == "BLEND")
                {
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent);
                }
                else if (src.alphaMode == "MASK")
                {
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout);
                    material.SetFloat("_Cutoff", src.alphaCutoff);
                }
                else
                {
                    // default OPAQUE
                    UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
                }

                // culling
                if (src.doubleSided)
                {
                    UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off);
                }
                else
                {
                    UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Back);
                }

                // VColor
                var hasVertexColor = parser.GLTF.MaterialHasVertexColor(i);
                if (hasVertexColor)
                {
                    UniUnlit.Utils.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply);
                }

                UniUnlit.Utils.ValidateProperties(material, true);
            });

            return(true);
        }
Esempio n. 9
0
 public static TextureImportParam NormalTexture(GltfParser parser, glTFMaterial src)
 {
     var(offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(src.normalTexture);
     return(GltfTextureImporter.CreateNormal(parser, src.normalTexture.index, offset, scale));
 }