Exemple #1
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.CreateMaterialForTest(0, gltfMaterial);

            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);
        }
Exemple #2
0
        public void MaterialImportTest()
        {
            var material = MaterialFactory.CreateMaterialForTest(0, new glTFMaterial {
            });

            Assert.AreEqual("Standard", material.shader.name);
        }
Exemple #3
0
        public void MaterialImportTest()
        {
            var material = MaterialFactory.CreateMaterial(0, new glTFMaterial {
            }, false).GetOrCreate(null);

            Assert.AreEqual("Standard", material.shader.name);
        }
Exemple #4
0
        /// <summary>
        /// Root ヒエラルキーで使っているリソース
        /// </summary>
        /// <returns></returns>
        public virtual void TransferOwnership(TakeResponsibilityForDestroyObjectFunc take)
        {
            foreach (var mesh in Meshes.ToArray())
            {
                take(SubAssetKey.Create(mesh.Mesh), mesh.Mesh);
                Meshes.Remove(mesh);
            }

            AnimationClipFactory.TransferOwnership(take);
            TextureFactory.TransferOwnership(take);
            MaterialFactory.TransferOwnership(take);
        }
Exemple #5
0
        /// <summary>
        /// ImporterContextが所有する UnityEngine.Object を破棄する
        /// </summary>
        public virtual void Dispose()
        {
            foreach (var x in Meshes)
            {
                UnityObjectDestoyer.DestroyRuntimeOrEditor(x.Mesh);
            }
            Meshes.Clear();

            AnimationClipFactory?.Dispose();
            MaterialFactory?.Dispose();
            TextureFactory?.Dispose();
        }
Exemple #6
0
        public static IEnumerable <TextureImportParam> EnumerateTextures(GltfParser parser, glTFMaterial m)
        {
            int?metallicRoughnessTexture = default;

            if (m.pbrMetallicRoughness != null)
            {
                // base color
                if (m.pbrMetallicRoughness?.baseColorTexture != null)
                {
                    yield return(PBRMaterialItem.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) = MaterialFactory.GetTextureOffsetAndScale(m.emissiveTexture);
                yield return(TextureFactory.CreateSRGB(parser, m.emissiveTexture.index, offset, scale));
            }

            // normal
            if (m.normalTexture != null)
            {
                yield return(PBRMaterialItem.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(PBRMaterialItem.StandardTexture(parser, m));
            }
        }
Exemple #7
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) = MaterialFactory.GetTextureOffsetAndScale(src.pbrMetallicRoughness.metallicRoughnessTexture);
            return(TextureFactory.CreateStandard(parser,
                                                 src.pbrMetallicRoughness?.metallicRoughnessTexture?.index,
                                                 src.occlusionTexture?.index,
                                                 offset, scale,
                                                 metallicFactor,
                                                 roughnessFactor));
        }
Exemple #8
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.CreateMaterialForTest(0, gltfMaterial);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }
        }
        public async Task LoadMaterialsAsync(IAwaitCaller awaitCaller = null)
        {
            awaitCaller = awaitCaller ?? new ImmediateCaller();

            if (Data.GLTF.materials == null || Data.GLTF.materials.Count == 0)
            {
                // no material. work around.
                var param    = MaterialDescriptorGenerator.Get(Data, 0);
                var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync, awaitCaller);
            }
            else
            {
                for (int i = 0; i < Data.GLTF.materials.Count; ++i)
                {
                    var param    = MaterialDescriptorGenerator.Get(Data, i);
                    var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync, awaitCaller);
                }
            }
        }
Exemple #10
0
        public async Task LoadMaterialsAsync(IAwaitCaller awaitCaller = null)
        {
            awaitCaller = awaitCaller ?? new ImmediateCaller();

            if (Data.GLTF.materials == null || Data.GLTF.materials.Count == 0)
            {
                // no material. work around.
                // TODO: https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#default-material
                var param    = MaterialDescriptor.Default;
                var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync, awaitCaller);
            }
            else
            {
                for (int i = 0; i < Data.GLTF.materials.Count; ++i)
                {
                    var param    = MaterialDescriptorGenerator.Get(Data, i);
                    var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync, awaitCaller);
                }
            }
        }
Exemple #11
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 #12
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);
            }

            var material = new Material(Shader.Find(ShaderName));

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

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

            material.name = MaterialFactory.MaterialName(i, src);
            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)
                {
                    var param = BaseColorTexture(parser, src);
                    material.mainTexture = await getTexture(awaitCaller, parser.GLTF, param);

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

                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
                    var(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(src.pbrMetallicRoughness.metallicRoughnessTexture);
                    MaterialFactory.SetTextureOffsetAndScale(material, "_MetallicGlossMap", offset, scale);
                }
                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 param   = NormalTexture(parser, src);
                var texture = await getTexture(awaitCaller, parser.GLTF, param);

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

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

            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
                var(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(src.occlusionTexture);
                MaterialFactory.SetTextureOffsetAndScale(material, "_OcclusionMap", offset, scale);
            }

            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(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(src.emissiveTexture);
                    var param   = TextureFactory.CreateSRGB(parser, src.emissiveTexture.index, offset, scale);
                    var texture = await getTexture(awaitCaller, parser.GLTF, param);

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

                    // Texture Offset and Scale

                    MaterialFactory.SetTextureOffsetAndScale(material, "_EmissionMap", param.Offset, param.Scale);
                }
            }

            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);
        }
Exemple #13
0
 public static TextureImportParam NormalTexture(GltfParser parser, glTFMaterial src)
 {
     var(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(src.normalTexture);
     return(TextureFactory.CreateNormal(parser, src.normalTexture.index, offset, scale));
 }
Exemple #14
0
 public static TextureImportParam BaseColorTexture(GltfParser parser, glTFMaterial src)
 {
     var(offset, scale) = MaterialFactory.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
     return(TextureFactory.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale));
 }
Exemple #15
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.CreateMaterialForTest(0, gltfMaterial);
                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.CreateMaterialForTest(0, gltfMaterial);
                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.CreateMaterialForTest(0, gltfMaterial);
                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.CreateMaterialForTest(0, gltfMaterial);
                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.CreateMaterialForTest(0, gltfMaterial);
                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.CreateMaterialForTest(0, gltfMaterial);
                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.CreateMaterialForTest(0, gltfMaterial);
                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.CreateMaterialForTest(0, gltfMaterial);
                Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name);
            }

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