Example #1
0
        public virtual GLTFMaterial ExportMaterial(Material m, TextureExportManager textureManager)
        {
            var material = CreateMaterial(m);

            // common params
            material.name = m.name;
            Export_Color(m, textureManager, material);
            Export_Metallic(m, textureManager, material);
            Export_Normal(m, textureManager, material);
            Export_Occlusion(m, textureManager, material);
            Export_Emission(m, textureManager, material);

            return(material);
        }
Example #2
0
        static void Export_Normal(Material m, TextureExportManager textureManager, GLTFMaterial material)
        {
            if (m.HasProperty("_BumpMap"))
            {
                var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), new NormalConverter());
                if (index != -1)
                {
                    material.normalTexture = new GLTFMaterialNormalTextureInfo()
                    {
                        index = index,
                    };
                }

                if (index != -1 && m.HasProperty("_BumpScale"))
                {
                    material.normalTexture.scale = m.GetFloat("_BumpScale");
                }
            }
        }
Example #3
0
        static void Export_Color(Material m, TextureExportManager textureManager, GLTFMaterial material)
        {
            if (m.HasProperty("_Color"))
            {
                material.pbrMetallicRoughness.baseColorFactor = m.color.ToArray();
            }

            if (m.HasProperty("_MainTex"))
            {
                var index = textureManager.CopyAndGetIndex(m.GetTexture("_MainTex"), RenderTextureReadWrite.sRGB);
                if (index != -1)
                {
                    material.pbrMetallicRoughness.baseColorTexture = new GLTFMaterialBaseColorTextureInfo()
                    {
                        index = index,
                    };
                }
            }
        }
Example #4
0
        static void Export_Occlusion(Material m, TextureExportManager textureManager, GLTFMaterial material)
        {
            if (m.HasProperty("_OcclusionMap"))
            {
                var index = textureManager.ConvertAndGetIndex(m.GetTexture("_OcclusionMap"), new OcclusionConverter());
                if (index != -1)
                {
                    material.occlusionTexture = new GLTFMaterialOcclusionTextureInfo()
                    {
                        index = index,
                    };
                }

                if (index != -1 && m.HasProperty("_OcclusionStrength"))
                {
                    material.occlusionTexture.strength = m.GetFloat("_OcclusionStrength");
                }
            }
        }
Example #5
0
        static void Export_Emission(Material m, TextureExportManager textureManager, GLTFMaterial material)
        {
            if (m.HasProperty("_EmissionColor"))
            {
                var color = m.GetColor("_EmissionColor");
                material.emissiveFactor = new float[] { color.r, color.g, color.b };
            }

            if (m.HasProperty("_EmissionMap"))
            {
                var index = textureManager.CopyAndGetIndex(m.GetTexture("_EmissionMap"), RenderTextureReadWrite.sRGB);
                if (index != -1)
                {
                    material.emissiveTexture = new GLTFMaterialEmissiveTextureInfo()
                    {
                        index = index,
                    };
                }
            }
        }
Example #6
0
        void FromGameObject(GLTFRoot gltf, GameObject go, bool useSparseAccessorForMorphTarget = false, int textureFormat = 0)
        {
            var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]);
            var bufferIndex = gltf.AddBuffer(bytesBuffer);

            GameObject tmpParent = null;

            if (go.transform.childCount == 0)
            {
                tmpParent = new GameObject("tmpParent");
                go.transform.SetParent(tmpParent.transform, true);
                go = tmpParent;
            }

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

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

                textureManager = new TextureExportManager(unityTextures);

                gltf.materials = materials.Select(x => _materialExporter.ExportMaterial(x, textureManager)).ToList();
                Debug.Log("image count = " + unityTextures.Count);
                for (int i = 0; i < unityTextures.Count; ++i)
                {
                    gltf.ExportTexture(bufferIndex, textureManager.GetExportTexture(i), unityTextures[i].TextureType, textureFormat);
                }
                #endregion


                #region Meshes
                var unityMeshes = nodes
                                  .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();
                Debug.Log("unityMesher...." + unityMeshes.Count);
                _meshExporter.Export(gltf, bufferIndex, unityMeshes, materials, useSparseAccessorForMorphTarget);
                meshes = unityMeshes.Select(x => x.mesh).ToList();
                #endregion

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

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

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

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

#if UNITY_EDITOR
                #region Animations

                var clips     = new List <AnimationClip>();
                var animator  = go.GetComponent <Animator>();
                var animation = go.GetComponent <Animation>();
                if (animator != null)
                {
                    clips = _animationExporter.GetAnimationClips(animator);
                }
                else if (animation != null)
                {
                    clips = _animationExporter.GetAnimationClips(animation);
                }

                if (clips.Any())
                {
                    Debug.Log("export clips.." + clips.Count);
                    foreach (AnimationClip clip in clips)
                    {
                        var animationWithCurve = _animationExporter.Export(clip, go.transform, nodes);

                        foreach (var kv in animationWithCurve.samplers)
                        {
                            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;
                            Debug.Log(sampler.interpolation + ">>" + string.Join(",", kv.Value.output));

                            // 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 1:
                                outputAccessor.type = "SCALAR";
                                //outputAccessor.count = ;
                                break;

                            case 3:
                                outputAccessor.type   = "VEC3";
                                outputAccessor.count /= 3;
                                break;

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

                            default:
                                throw new NotImplementedException();
                            }
                        }
                        animationWithCurve.animation.name = clip.name;
                        gltf.animations.Add(animationWithCurve.animation);
                    }
                }
                #endregion
#endif
            }
            finally
            {
                if (tmpParent != null)
                {
                    tmpParent.transform.GetChild(0).SetParent(null);
                    if (Application.isPlaying)
                    {
                        GameObject.Destroy(tmpParent);
                    }
                    else
                    {
                        GameObject.DestroyImmediate(tmpParent);
                    }
                }
            }
        }