Exemple #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 data = new AmbiguousGltfFileParser(path).Parse();

                var matDesc = new GltfMaterialDescriptorGenerator().Get(data, 0);
                Assert.AreEqual("Standard", matDesc.ShaderName);
                Assert.AreEqual(5, matDesc.TextureSlots.Count);
                var(key, value) = matDesc.EnumerateSubAssetKeyValue().First();
                Assert.AreEqual(new SubAssetKey(typeof(Texture2D), "texture_0"), key);
            }
        }
Exemple #2
0
        public override void OnEnable()
        {
            base.OnEnable();

            m_importer = target as ZipArchivedGltfScriptedImporter;
            m_data     = new AutoGltfFileParser(m_importer.assetPath).Parse();

            var materialGenerator = new GltfMaterialDescriptorGenerator();
            var materialKeys      = m_data.GLTF.materials.Select((_, i) => materialGenerator.Get(m_data, i).SubAssetKey);
            var textureKeys       = new GltfTextureDescriptorGenerator(m_data).Get().GetEnumerable().Select(x => x.SubAssetKey);

            m_materialEditor  = new RemapEditorMaterial(materialKeys.Concat(textureKeys), GetEditorMap, SetEditorMap);
            m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_data.GLTF), GetEditorMap, SetEditorMap);
        }
Exemple #3
0
        public ImporterContext(
            GltfData data,
            IReadOnlyDictionary <SubAssetKey, UnityEngine.Object> externalObjectMap = null,
            ITextureDeserializer textureDeserializer = null)
        {
            Data = data;
            TextureDescriptorGenerator  = new GltfTextureDescriptorGenerator(Data);
            MaterialDescriptorGenerator = new GltfMaterialDescriptorGenerator();

            ExternalObjectMap   = externalObjectMap ?? new Dictionary <SubAssetKey, UnityEngine.Object>();
            textureDeserializer = textureDeserializer ?? new UnityTextureDeserializer();

            TextureFactory = new TextureFactory(textureDeserializer, ExternalObjectMap
                                                .Where(x => x.Value is Texture)
                                                .ToDictionary(x => x.Key, x => (Texture)x.Value),
                                                Data.MigrationFlags.IsRoughnessTextureValueSquared);
            MaterialFactory = new MaterialFactory(ExternalObjectMap
                                                  .Where(x => x.Value is Material)
                                                  .ToDictionary(x => x.Key, x => (Material)x.Value));
            AnimationClipFactory = new AnimationClipFactory(ExternalObjectMap
                                                            .Where(x => x.Value is AnimationClip)
                                                            .ToDictionary(x => x.Key, x => (AnimationClip)x.Value));
        }
Exemple #4
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 textureSlots = new Dictionary <string, TextureDescriptor>();
            var floatValues  = new Dictionary <string, float>();
            var colors       = new Dictionary <string, Color>();
            var vectors      = new Dictionary <string, Vector4>();
            var actions      = new List <Action <Material> >();
            var src          = data.GLTF.materials[i];

            var standardTexDesc = default(TextureDescriptor);

            if (src.pbrMetallicRoughness != null || src.occlusionTexture != null)
            {
                if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null)
                {
                    SubAssetKey key;
                    (key, standardTexDesc) = GltfPbrTextureImporter.StandardTexture(data, src);
                }

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

                if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1)
                {
                    var(key, textureParam) = GltfPbrTextureImporter.BaseColorTexture(data, src);
                    // from _MainTex !
                    textureSlots.Add("_BaseMap", textureParam);
                }

                if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
                {
                    actions.Add(material => material.EnableKeyword("_METALLICGLOSSMAP"));
                    textureSlots.Add("_MetallicGlossMap", standardTexDesc);
                    // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212.
                    floatValues.Add("_Metallic", 1.0f);
                    floatValues.Add("_GlossMapScale", 1.0f);
                    // default value is 0.5 !
                    floatValues.Add("_Smoothness", 1.0f);
                }
                else
                {
                    floatValues.Add("_Metallic", src.pbrMetallicRoughness.metallicFactor);
                    // from _Glossiness !
                    floatValues.Add("_Smoothness", 1.0f - src.pbrMetallicRoughness.roughnessFactor);
                }
            }

            if (src.normalTexture != null && src.normalTexture.index != -1)
            {
                actions.Add(material => material.EnableKeyword("_NORMALMAP"));
                var(key, textureParam) = GltfPbrTextureImporter.NormalTexture(data, src);
                textureSlots.Add("_BumpMap", textureParam);
                floatValues.Add("_BumpScale", src.normalTexture.scale);
            }

            if (src.occlusionTexture != null && src.occlusionTexture.index != -1)
            {
                textureSlots.Add("_OcclusionMap", standardTexDesc);
                floatValues.Add("_OcclusionStrength", src.occlusionTexture.strength);
            }

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

                if (src.emissiveFactor != null && src.emissiveFactor.Length == 3)
                {
                    var emissiveFactor = src.emissiveFactor.ToColor3(ColorSpace.Linear, ColorSpace.Linear);
                    if (UniGLTF.glTF_KHR_materials_emissive_strength.TryGet(src.extensions,
                                                                            out UniGLTF.glTF_KHR_materials_emissive_strength emissiveStrength))
                    {
                        emissiveFactor *= emissiveStrength.emissiveStrength;
                    }
                    else if (UniGLTF.Extensions.VRMC_materials_hdr_emissiveMultiplier.GltfDeserializer.TryGet(src.extensions,
                                                                                                              out UniGLTF.Extensions.VRMC_materials_hdr_emissiveMultiplier.VRMC_materials_hdr_emissiveMultiplier ex))
                    {
                        emissiveFactor *= ex.EmissiveMultiplier.Value;
                    }
                    colors.Add("_EmissionColor", emissiveFactor);
                }

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

            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);
            });

            matDesc = new MaterialDescriptor(
                GltfMaterialDescriptorGenerator.GetMaterialName(i, src),
                ShaderName,
                null,
                textureSlots,
                floatValues,
                colors,
                vectors,
                actions);
            return(true);
        }
Exemple #5
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);
        }
        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);
        }