void BufferTest(int init, params int[] size) { var initBytes = init == 0 ? null : new byte[init]; var storage = new ArrayByteBuffer(initBytes); var buffer = new GLTFBuffer(storage); var values = new List <byte>(); int offset = 0; foreach (var x in size) { var nums = Enumerable.Range(offset, x).Select(y => (Byte)y).ToArray(); values.AddRange(nums); var bytes = new ArraySegment <Byte>(nums); offset += x; buffer.Append(bytes, GLTFBufferTarget.NONE); } Assert.AreEqual(values.Count, buffer.byteLength); Assert.True(Enumerable.SequenceEqual(values, buffer.GetBytes().ToArray())); }
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); } } } }