Esempio n. 1
0
        public void TextureExportTest()
        {
            // Dummy texture
            var tex0 = new Texture2D(128, 128)
            {
                wrapMode   = TextureWrapMode.Clamp,
                filterMode = FilterMode.Trilinear,
            };
            var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset);

            var material = new Material(Shader.Find("Standard"));

            material.mainTexture = tex0;

            var materialExporter = new MaterialExporter();

            materialExporter.ExportMaterial(material, textureManager);

            var(convTex0, colorSpace) = textureManager.Exported[0];
            var sampler = TextureSamplerUtil.Export(convTex0);

            Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS);
            Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT);
            Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.minFilter);
            Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.magFilter);
        }
Esempio n. 2
0
        public void TextureTransformTest()
        {
            var tex0 = new Texture2D(128, 128)
            {
                wrapMode   = TextureWrapMode.Repeat,
                filterMode = FilterMode.Bilinear,
            };

            var textureExporter = new TextureExporter(new EditorTextureSerializer());
            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, textureExporter, new GltfExportSettings());

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

            Assert.IsTrue(glTF_KHR_texture_transform.TryGet(gltfMaterial.pbrMetallicRoughness.baseColorTexture, out glTF_KHR_texture_transform t));
            Assert.AreEqual(t.offset[0], offset.x, 0.3f);
            Assert.AreEqual(t.offset[1], offset.y, 0.2f);
            Assert.AreEqual(t.scale[0], scale.x, 0.5f);
            Assert.AreEqual(t.scale[1], scale.y, 0.6f);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);

            var shaderStore      = new ShaderStore(null);
            var materialImporter = new MaterialImporter(shaderStore, (int index) => { return(null); });
            var dstMaterial      = materialImporter.CreateMaterial(0, gltfMaterial, false);

            Assert.AreEqual(dstMaterial.mainTextureOffset.x, offset.x, 0.001f);
            Assert.AreEqual(dstMaterial.mainTextureOffset.y, offset.y, 0.001f);
            Assert.AreEqual(dstMaterial.mainTextureScale.x, scale.x, 0.001f);
            Assert.AreEqual(dstMaterial.mainTextureScale.y, scale.y, 0.001f);
        }
Esempio n. 5
0
        public void TextureExportTest()
        {
            // Dummy texture
            var tex0 = new Texture2D(128, 128)
            {
                wrapMode   = TextureWrapMode.Clamp,
                filterMode = FilterMode.Trilinear,
            };
            var textureExporter = new TextureExporter(new EditorTextureSerializer());

            var material = new Material(Shader.Find("Standard"));

            material.mainTexture = tex0;

            var materialExporter = new MaterialExporter();

            materialExporter.ExportMaterial(material, textureExporter, new GltfExportSettings());

            var exported = textureExporter.Export();

            var(convTex0, colorSpace) = exported[0];
            var sampler = TextureSamplerUtil.Export(convTex0);

            Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS);
            Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT);

            Assert.AreEqual(FilterMode.Trilinear, convTex0.filterMode);
            Assert.IsTrue(convTex0.mipmapCount > 1);
            // Tirilinear => LINEAR_MIPMAP_LINEAR
            Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.minFilter);
            Assert.AreEqual(glFilter.LINEAR, sampler.magFilter);
        }
Esempio n. 6
0
        public void MaterialExportTest()
        {
            var material = new Material(Shader.Find("Standard"));

            material.SetColor("_EmissionColor", new Color(0, 1, 2, 1));
            var materialExporter     = new MaterialExporter();
            var textureExportManager = new TextureExportManager(new Texture[] { });
            var gltfMaterial         = materialExporter.ExportMaterial(material, textureExportManager);

            Assert.AreEqual(gltfMaterial.emissiveFactor, new float[] { 0, 0.5f, 1 });
        }
Esempio n. 7
0
 public virtual string GetGltfMaterialTypeFromUnityShaderName(string shaderName)
 {
     if (shaderName == "Standard")
     {
         return("pbr");
     }
     if (MaterialExporter.IsUnlit(shaderName))
     {
         return("unlit");
     }
     return(null);
 }
Esempio n. 8
0
        public void MaterialExportTest()
        {
            var material = new Material(Shader.Find("Standard"));

            material.SetColor("_EmissionColor", new Color(0, 1, 2, 1));
            material.EnableKeyword("_EMISSION");
            var materialExporter = new MaterialExporter();
            var textureExporter  = new TextureExporter(new EditorTextureSerializer());
            var gltfMaterial     = materialExporter.ExportMaterial(material, textureExporter, new GltfExportSettings());

            Assert.AreEqual(gltfMaterial.emissiveFactor, new float[] { 0, 0.5f, 1 });
        }
 public static string DefaultGltfMaterialType(string shaderName)
 {
     if (shaderName == "Standard")
     {
         return("pbr");
     }
     if (MaterialExporter.IsUnlit(shaderName))
     {
         return("unlit");
     }
     return(null);
 }
Esempio n. 10
0
        public void ExportTest()
        {
            {
                var            material         = Resources.Load <Material>("Materials/pbr_opaque");
                var            exporter         = new MaterialExporter();
                var            textures         = GetTextures(material).ToList();
                List <Texture> exportedTextures = null;
                var            exported         = exporter.ExportMaterial(material, textures, out exportedTextures);
                Assert.True(exported.extensions == null || exported.extensions.KHR_materials_unlit == null);
                Assert.AreEqual("OPAQUE", exported.alphaMode);
            }

            {
                var            material         = Resources.Load <Material>("Materials/pbr_transparent");
                var            exporter         = new MaterialExporter();
                var            textures         = GetTextures(material).ToList();
                List <Texture> exportedTextures = null;
                var            exported         = exporter.ExportMaterial(material, textures, out exportedTextures);
                Assert.True(exported.extensions == null || exported.extensions.KHR_materials_unlit == null);
                Assert.AreEqual("BLEND", exported.alphaMode);
            }
            {
                var            material         = Resources.Load <Material>("Materials/pbr_cutout");
                var            exporter         = new MaterialExporter();
                var            textures         = GetTextures(material).ToList();
                List <Texture> exportedTextures = null;
                var            exported         = exporter.ExportMaterial(material, textures, out exportedTextures);
                Assert.True(exported.extensions == null || exported.extensions.KHR_materials_unlit == null);
                Assert.AreEqual("MASK", exported.alphaMode);
            }

            {
                var            material         = Resources.Load <Material>("Materials/unlit_color");
                var            exporter         = new MaterialExporter();
                var            textures         = GetTextures(material).ToList();
                List <Texture> exportedTextures = null;
                var            exported         = exporter.ExportMaterial(material, textures, out exportedTextures);
                Assert.NotNull(exported.extensions.KHR_materials_unlit);
                Assert.AreEqual("OPAQUE", exported.alphaMode);
            }
            {
                var            material         = Resources.Load <Material>("Materials/unlit_texture");
                var            exporter         = new MaterialExporter();
                var            textures         = GetTextures(material).ToList();
                List <Texture> exportedTextures = null;
                var            exported         = exporter.ExportMaterial(material, textures, out exportedTextures);
                Assert.NotNull(exported.extensions.KHR_materials_unlit);
                Assert.AreEqual("OPAQUE", exported.alphaMode);
            }
            {
                var            material         = Resources.Load <Material>("Materials/unlit_transparent");
                var            exporter         = new MaterialExporter();
                var            textures         = GetTextures(material).ToList();
                List <Texture> exportedTextures = null;
                var            exported         = exporter.ExportMaterial(material, textures, out exportedTextures);
                Assert.NotNull(exported.extensions.KHR_materials_unlit);
                Assert.AreEqual("BLEND", exported.alphaMode);
            }
            {
                var            material         = Resources.Load <Material>("Materials/unlit_cutout");
                var            exporter         = new MaterialExporter();
                var            textures         = GetTextures(material).ToList();
                List <Texture> exportedTextures = null;
                var            exported         = exporter.ExportMaterial(material, textures, out exportedTextures);
                Assert.NotNull(exported.extensions.KHR_materials_unlit);
                Assert.AreEqual("MASK", exported.alphaMode);
            }
        }
Esempio n. 11
0
        public void ExportTest()
        {
            {
                var material      = Resources.Load <Material>("Materials/pbr_opaque");
                var exporter      = new MaterialExporter();
                var textures      = GetTextures(material).ToList();
                var exportManager = new TextureExportManager(textures);
                var exported      = exporter.ExportMaterial(material, exportManager);
                Assert.True(exported.extensions == null || exported.extensions.KHR_materials_unlit == null);
                Assert.AreEqual("OPAQUE", exported.alphaMode);
            }

            {
                var material      = Resources.Load <Material>("Materials/pbr_transparent");
                var exporter      = new MaterialExporter();
                var textures      = GetTextures(material).ToList();
                var exportManager = new TextureExportManager(textures);
                var exported      = exporter.ExportMaterial(material, exportManager);
                Assert.True(exported.extensions == null || exported.extensions.KHR_materials_unlit == null);
                Assert.AreEqual("BLEND", exported.alphaMode);
            }
            {
                var material      = Resources.Load <Material>("Materials/pbr_cutout");
                var exporter      = new MaterialExporter();
                var textures      = GetTextures(material).ToList();
                var exportManager = new TextureExportManager(textures);
                var exported      = exporter.ExportMaterial(material, exportManager);
                Assert.True(exported.extensions == null || exported.extensions.KHR_materials_unlit == null);
                Assert.AreEqual("MASK", exported.alphaMode);
            }

            {
                var material      = Resources.Load <Material>("Materials/unlit_color");
                var exporter      = new MaterialExporter();
                var textures      = GetTextures(material).ToList();
                var exportManager = new TextureExportManager(textures);
                var exported      = exporter.ExportMaterial(material, exportManager);
                Assert.NotNull(exported.extensions.KHR_materials_unlit);
                Assert.AreEqual("OPAQUE", exported.alphaMode);
            }
            {
                var material      = Resources.Load <Material>("Materials/unlit_texture");
                var exporter      = new MaterialExporter();
                var textures      = GetTextures(material).ToList();
                var exportManager = new TextureExportManager(textures);
                var exported      = exporter.ExportMaterial(material, exportManager);
                Assert.NotNull(exported.extensions.KHR_materials_unlit);
                Assert.AreEqual("OPAQUE", exported.alphaMode);
            }
            {
                var material      = Resources.Load <Material>("Materials/unlit_transparent");
                var exporter      = new MaterialExporter();
                var textures      = GetTextures(material).ToList();
                var exportManager = new TextureExportManager(textures);
                var exported      = exporter.ExportMaterial(material, exportManager);
                Assert.NotNull(exported.extensions.KHR_materials_unlit);
                Assert.AreEqual("BLEND", exported.alphaMode);
            }
            {
                var material      = Resources.Load <Material>("Materials/unlit_cutout");
                var exporter      = new MaterialExporter();
                var textures      = GetTextures(material).ToList();
                var exportManager = new TextureExportManager(textures);
                var exported      = exporter.ExportMaterial(material, exportManager);
                Assert.NotNull(exported.extensions.KHR_materials_unlit);
                Assert.AreEqual("MASK", exported.alphaMode);
            }

            {
                // multimaterial
                var materials = new Material[] {
                    Resources.Load <Material>("Materials/unlit_transparent"),
                    Resources.Load <Material>("Materials/unlit_cutout")
                };
                var exporter      = new MaterialExporter();
                var textures      = materials.SelectMany(x => GetTextures(x)).ToList();
                var exportManager = new TextureExportManager(textures);
                {
                    var exported = exporter.ExportMaterial(materials[0], exportManager);
                    Assert.NotNull(exported.extensions.KHR_materials_unlit);
                    Assert.AreEqual("BLEND", exported.alphaMode);
                }
                {
                    var exported = exporter.ExportMaterial(materials[1], exportManager);
                    Assert.NotNull(exported.extensions.KHR_materials_unlit);
                    Assert.AreEqual("MASK", exported.alphaMode);
                }
            }
        }
Esempio n. 12
0
        public static Exported FromGameObject(glTF gltf, GameObject go, bool useSparseAccessorForMorphTarget = false)
        {
            var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]);
            var bufferIndex = gltf.AddBuffer(bytesBuffer);

            if (go.transform.childCount == 0)
            {
                throw new UniGLTFException("empty root GameObject required");
            }

            var unityNodes = go.transform.Traverse()
                             .Skip(1) // exclude root object for the symmetry with the importer
                             .ToList();

            #region Materials and Textures
            var unityMaterials = unityNodes.SelectMany(x => x.GetSharedMaterials()).Where(x => x != null).Distinct().ToList();
            var unityTextures  = unityMaterials.SelectMany(x => TextureIO.GetTextures(x)).Where(x => x.Texture != null).Distinct().ToList();

            for (int i = 0; i < unityTextures.Count; ++i)
            {
                var texture = unityTextures[i];
                TextureIO.ExportTexture(gltf, bufferIndex, texture.Texture, texture.IsNormalMap);
            }

            var textures         = unityTextures.Select(y => y.Texture).ToList();
            var materialExporter = new MaterialExporter();
            gltf.materials = unityMaterials.Select(x => materialExporter.ExportMaterial(x, textures)).ToList();
            #endregion

            #region Meshes
            var unityMeshes = unityNodes
                              .Select(x => new MeshWithRenderer
            {
                Mesh       = x.GetSharedMesh(),
                Rendererer = x.GetComponent <Renderer>(),
            })
                              .Where(x =>
            {
                if (x.Mesh == null)
                {
                    return(false);
                }
                if (x.Rendererer.sharedMaterials == null ||
                    x.Rendererer.sharedMaterials.Length == 0)
                {
                    return(false);
                }

                return(true);
            })
                              .ToList();
            ExportMeshes(gltf, bufferIndex, unityMeshes, unityMaterials, useSparseAccessorForMorphTarget);
            #endregion

            #region Skins
            var unitySkins = unityNodes
                             .Select(x => x.GetComponent <SkinnedMeshRenderer>()).Where(x => x != null)
                             .ToList();
            gltf.nodes  = unityNodes.Select(x => ExportNode(x, unityNodes, unityMeshes.Select(y => y.Mesh).ToList(), unitySkins)).ToList();
            gltf.scenes = new List <gltfScene>
            {
                new gltfScene
                {
                    nodes = go.transform.GetChildren().Select(x => unityNodes.IndexOf(x)).ToArray(),
                }
            };

            foreach (var x in unitySkins)
            {
                var matrices = x.sharedMesh.bindposes.Select(y => y.ReverseZ()).ToArray();
                var accessor = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, matrices, glBufferTarget.NONE);

                var skin = new glTFSkin
                {
                    inverseBindMatrices = accessor,
                    joints   = x.bones.Select(y => unityNodes.IndexOf(y)).ToArray(),
                    skeleton = unityNodes.IndexOf(x.rootBone),
                };
                var skinIndex = gltf.skins.Count;
                gltf.skins.Add(skin);

                foreach (var z in unityNodes.Where(y => y.Has(x)))
                {
                    var nodeIndex = unityNodes.IndexOf(z);
                    var node      = gltf.nodes[nodeIndex];
                    node.skin = skinIndex;
                }
            }
            #endregion

#if UNITY_EDITOR
            #region Animations
            var animation = go.GetComponent <Animation>();
            if (animation != null)
            {
                foreach (AnimationState state in animation)
                {
                    var animationWithCurve = ExportAnimation(state.clip, go.transform, unityNodes);

                    foreach (var kv in animationWithCurve.SamplerMap)
                    {
                        var sampler = animationWithCurve.Animation.samplers[kv.Key];

                        var inputAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, kv.Value.Input);
                        sampler.input = inputAccessorIndex;

                        var outputAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, kv.Value.Output);
                        sampler.output = outputAccessorIndex;

                        // modify accessors
                        var outputAccessor = gltf.accessors[outputAccessorIndex];
                        var channel        = animationWithCurve.Animation.channels.First(x => x.sampler == kv.Key);
                        switch (glTFAnimationTarget.GetElementCount(channel.target.path))
                        {
                        case 3:
                            outputAccessor.type   = "VEC3";
                            outputAccessor.count /= 3;
                            break;

                        case 4:
                            outputAccessor.type   = "VEC4";
                            outputAccessor.count /= 4;
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                    }

                    gltf.animations.Add(animationWithCurve.Animation);
                }
            }
            #endregion
#endif

            return(new Exported
            {
                Meshes = unityMeshes,
                Nodes = unityNodes.Select(x => x.transform).ToList(),
                Materials = unityMaterials,
                Textures = textures,
            });
        }