public virtual void Export(ITextureSerializer textureSerializer) { Nodes = Copy.transform.Traverse() .Skip(1) // exclude root object for the symmetry with the importer .ToList(); var uniqueUnityMeshes = new MeshExportList(); uniqueUnityMeshes.GetInfo(Nodes, m_settings); #region Materials and Textures ReportProgress("Materials and Textures", 0.2f); Materials = uniqueUnityMeshes.GetUniqueMaterials().ToList(); _textureExporter = new TextureExporter(textureSerializer); var materialExporter = CreateMaterialExporter(); _gltf.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureExporter, m_settings)).ToList(); #endregion #region Meshes ReportProgress("Meshes", 0.4f); MeshBlendShapeIndexMap = new Dictionary <Mesh, Dictionary <int, int> >(); foreach (var unityMesh in uniqueUnityMeshes) { if (!unityMesh.CanExport) { continue; } var(gltfMesh, blendShapeIndexMap) = m_settings.DivideVertexBuffer ? MeshExporter_DividedVertexBuffer.Export(_data, unityMesh, Materials, m_settings.InverseAxis.Create(), m_settings) : MeshExporter_SharedVertexBuffer.Export(_data, unityMesh, Materials, m_settings.InverseAxis.Create(), m_settings) ; _gltf.meshes.Add(gltfMesh); Meshes.Add(unityMesh.Mesh); if (!MeshBlendShapeIndexMap.ContainsKey(unityMesh.Mesh)) { // 重複防止 MeshBlendShapeIndexMap.Add(unityMesh.Mesh, blendShapeIndexMap); } } #endregion #region Nodes and Skins ReportProgress("Nodes and Skins", 0.8f); var skins = uniqueUnityMeshes .SelectMany(x => x.Renderers) .Where(x => x.Item1 is SkinnedMeshRenderer && x.UniqueBones != null) .Select(x => x.Item1 as SkinnedMeshRenderer) .ToList() ; foreach (var node in Nodes) { var gltfNode = ExportNode(node, Nodes, uniqueUnityMeshes, skins); _gltf.nodes.Add(gltfNode); } _gltf.scenes = new List <gltfScene> { new gltfScene { nodes = Copy.transform.GetChildren().Select(x => Nodes.IndexOf(x)).ToArray(), } }; foreach (var x in uniqueUnityMeshes) { foreach (var(renderer, uniqueBones) in x.Renderers) { if (uniqueBones != null && renderer is SkinnedMeshRenderer smr) { var matrices = x.GetBindPoses().Select(m_settings.InverseAxis.Create().InvertMat4).ToArray(); var accessor = _data.ExtendBufferAndGetAccessorIndex(matrices, glBufferTarget.NONE); var skin = new glTFSkin { inverseBindMatrices = accessor, joints = uniqueBones.Select(y => Nodes.IndexOf(y)).ToArray(), skeleton = Nodes.IndexOf(smr.rootBone), }; var skinIndex = _gltf.skins.Count; _gltf.skins.Add(skin); foreach (var z in Nodes.Where(y => y.Has(renderer))) { var nodeIndex = Nodes.IndexOf(z); var node = _gltf.nodes[nodeIndex]; node.skin = skinIndex; } } } } #endregion if (m_animationExporter != null) { ReportProgress("Animations", 0.9f); m_animationExporter.Export(_data, Copy, Nodes); } ExportExtensions(textureSerializer); // Extension で Texture が増える場合があるので最後に呼ぶ var exported = _textureExporter.Export(); for (var exportedTextureIdx = 0; exportedTextureIdx < exported.Count; ++exportedTextureIdx) { var(unityTexture, colorSpace) = exported[exportedTextureIdx]; GltfTextureExporter.PushGltfTexture(_data, unityTexture, colorSpace, textureSerializer); } FixName(_gltf); }
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); } } } }