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));
            }
        }
示例#2
0
        public static (SubAssetKey, TextureDescriptor) StandardTexture(GltfData data, 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) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.metallicRoughnessTexture);
            return(GltfTextureImporter.CreateStandard(data,
                                                      src.pbrMetallicRoughness?.metallicRoughnessTexture?.index,
                                                      src.occlusionTexture?.index,
                                                      offset, scale,
                                                      metallicFactor,
                                                      roughnessFactor));
        }
示例#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));
        }
示例#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);
        }
示例#5
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));
 }
示例#6
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));
 }
示例#7
0
 public static (SubAssetKey, TextureDescriptor) EmissiveTexture(GltfData data, glTFMaterial src)
 {
     var(offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.emissiveTexture);
     return(GltfTextureImporter.CreateSrgb(data, src.emissiveTexture.index, offset, scale));
 }
示例#8
0
 public static (SubAssetKey, TextureDescriptor) BaseColorTexture(GltfData data, glTFMaterial src)
 {
     var(offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
     return(GltfTextureImporter.CreateSrgb(data, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale));
 }
示例#9
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)
                {
                    var color = src.pbrMetallicRoughness.baseColorFactor;
                    param.Colors.Add("_Color", (new Color(color[0], color[1], color[2], color[3])).gamma);
                }

                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", new Color(src.emissiveFactor[0], src.emissiveFactor[1], src.emissiveFactor[2]));
                }

                if (src.emissiveTexture != null && src.emissiveTexture.index != -1)
                {
                    var(offset, scale)     = GltfMaterialImporter.GetTextureOffsetAndScale(src.emissiveTexture);
                    var(key, textureParam) = GltfTextureImporter.CreateSRGB(parser, src.emissiveTexture.index, offset, scale);
                    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);
        }
        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);
        }
示例#11
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));
 }
示例#12
0
        public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc)
        {
            if (i < 0 || i >= data.GLTF.materials.Count)
            {
                matDesc = default;
                return(false);
            }

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

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

            matDesc = new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, src), ShaderName);

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

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

            //renderMode
            matDesc.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 = data.GLTF.MaterialHasVertexColor(i);
                if (hasVertexColor)
                {
                    UniUnlit.Utils.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply);
                }

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

            return(true);
        }
示例#13
0
        public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc)
        {
            if (i < 0 || i >= data.GLTF.materials.Count)
            {
                matDesc = default;
                return(false);
            }

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

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

            var textureSlots = new Dictionary <string, TextureDescriptor>();
            var colors       =
                src.pbrMetallicRoughness.baseColorFactor != null &&
                src.pbrMetallicRoughness.baseColorFactor.Length == 4
                    ? new Dictionary <string, Color>
            {
                {
                    "_Color",
                    src.pbrMetallicRoughness.baseColorFactor.ToColor4(ColorSpace.Linear, ColorSpace.sRGB)
                }
            }
                    : new Dictionary <string, Color>();

            // texture
            if (src.pbrMetallicRoughness.baseColorTexture != null)
            {
                var(offset, scale) =
                    GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
                var(key, textureParam) = GltfTextureImporter.CreateSrgb(data,
                                                                        src.pbrMetallicRoughness.baseColorTexture.index, offset, scale);
                textureSlots.Add("_MainTex", textureParam);
            }

            matDesc = new MaterialDescriptor(
                GltfMaterialDescriptorGenerator.GetMaterialName(i, src),
                UniUnlitUtil.ShaderName,
                null,
                textureSlots,
                new Dictionary <string, float>(),
                colors,
                new Dictionary <string, Vector4>(),
                new Action <Material>[]
            {
                //renderMode
                material =>
                {
                    switch (src.alphaMode)
                    {
                    case "OPAQUE":
                        UniUnlitUtil.SetRenderMode(material, UniUnlitRenderMode.Opaque);
                        break;

                    case "BLEND":
                        UniUnlitUtil.SetRenderMode(material, UniUnlitRenderMode.Transparent);
                        break;

                    case "MASK":
                        UniUnlitUtil.SetRenderMode(material, UniUnlitRenderMode.Cutout);
                        material.SetFloat(Cutoff, src.alphaCutoff);
                        break;

                    default:
                        // default OPAQUE
                        UniUnlitUtil.SetRenderMode(material, UniUnlitRenderMode.Opaque);
                        break;
                    }

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

                    // VColor
                    var hasVertexColor = data.MaterialHasVertexColor(i);
                    if (hasVertexColor)
                    {
                        UniUnlitUtil.SetVColBlendMode(material, UniUnlitVertexColorBlendOp.Multiply);
                    }

                    UniUnlitUtil.ValidateProperties(material, true);
                }
            }
                );

            return(true);
        }