private BufferViewId AddBinaryDataToStreamHelper(GLBObject glbObject, Stream blobToAdd) { int previousCount = 0; if (glbObject.Root.BufferViews != null) { previousCount = glbObject.Root.BufferViews.Count; } uint previousGLBLength = glbObject.Header.FileLength; uint previousChunkLength = glbObject.BinaryChunkInfo.Length; uint bufferSize = GLBBuilder.CalculateAlignment((uint)blobToAdd.Length, 4); BufferViewId bufferViewId = GLBBuilder.AddBinaryData(glbObject, blobToAdd); uint headerModifier = previousChunkLength == 0 ? GLTFParser.CHUNK_HEADER_SIZE : 0; Assert.AreEqual(previousCount + 1, glbObject.Root.BufferViews.Count); Assert.AreEqual(previousCount, bufferViewId.Id); Assert.AreEqual(previousGLBLength + bufferSize + headerModifier, glbObject.Header.FileLength); Assert.AreEqual(previousChunkLength + bufferSize, glbObject.BinaryChunkInfo.Length); Assert.AreEqual(previousChunkLength + bufferSize, glbObject.Root.Buffers[0].ByteLength); Assert.AreEqual(glbObject.Header.FileLength, glbObject.Stream.Length); return(bufferViewId); }
public void RemoveMiddleDataFromStream() { string outPath = TestAssetPaths.GetOutPath(TestAssetPaths.GLB_BOX_OUT_PATH_TEMPLATE, 3, TestAssetPaths.GLB_EXTENSION); FileStream glbStream = File.OpenRead(TestAssetPaths.GLB_BOX_PATH); FileStream glbOutStream = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); GLBObject glbObject = GLBBuilder.ConstructFromStream(glbStream, glbOutStream); const uint numBuffersToAdd = 5; const uint bufferSize = 100; BufferViewId[] bufferViews = new BufferViewId[numBuffersToAdd]; for (int i = 0; i < numBuffersToAdd; ++i) { byte[] buffer = new byte[bufferSize]; bufferViews[i] = AddBinaryDataToStreamHelper(glbObject, new MemoryStream(buffer)); } uint previousFileLength = glbObject.Header.FileLength; uint previousBufferLength = glbObject.BinaryChunkInfo.Length; int previousBufferViewCount = glbObject.Root.BufferViews.Count; GLBBuilder.RemoveBinaryData(glbObject, bufferViews[2]); // remove from the middle Assert.AreEqual(previousFileLength, glbObject.Header.FileLength); Assert.AreEqual(previousBufferLength, glbObject.BinaryChunkInfo.Length); Assert.AreEqual(previousBufferViewCount - 1, glbObject.Root.BufferViews.Count); }
public void RemoveAllDataFromStream() { string outPath = TestAssetPaths.GetOutPath(TestAssetPaths.GLB_BOX_OUT_PATH_TEMPLATE, 3, TestAssetPaths.GLB_EXTENSION); FileStream glbStream = File.OpenRead(TestAssetPaths.GLB_BOX_PATH); FileStream glbOutStream = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); GLBObject glbObject = GLBBuilder.ConstructFromStream(glbStream, glbOutStream); BufferViewId id0 = new BufferViewId { Id = 0, Root = glbObject.Root }; int numBufferViews = glbObject.Root.BufferViews.Count; for (int i = 0; i < numBufferViews; ++i) { GLBBuilder.RemoveBinaryData(glbObject, id0); } Assert.AreEqual(0, glbObject.Root.Buffers.Count); Assert.AreEqual(0, glbObject.Root.BufferViews.Count); }
public void RemoveBinaryDataFromStream() { string outPath = TestAssetPaths.GetOutPath(TestAssetPaths.GLB_BOX_OUT_PATH_TEMPLATE, 2, TestAssetPaths.GLB_EXTENSION); FileStream glbStream = File.OpenRead(TestAssetPaths.GLB_BOX_PATH); FileStream glbOutStream = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); GLBObject glbObject = GLBBuilder.ConstructFromStream(glbStream, glbOutStream); const uint bufferSize = 100; byte[] buffer = new byte[bufferSize]; BufferViewId bufferViewId = AddBinaryDataToStreamHelper(glbObject, new MemoryStream(buffer)); uint length = (uint)bufferViewId.Value.ByteLength; uint previousFileLength = glbObject.Header.FileLength; uint previousBufferLength = glbObject.BinaryChunkInfo.Length; int previousBufferViewCount = glbObject.Root.BufferViews.Count; GLBBuilder.RemoveBinaryData(glbObject, bufferViewId); Assert.AreEqual(previousFileLength - length, glbObject.Header.FileLength); Assert.AreEqual(previousBufferLength - length, glbObject.BinaryChunkInfo.Length); Assert.AreEqual(previousBufferLength - length, (uint)glbObject.Root.Buffers[0].ByteLength); Assert.AreEqual(previousBufferViewCount - 1, glbObject.Root.BufferViews.Count); }
protected AccessorId ExportAccessor(Matrix4x4[] arr, BufferViewId bufferViewId = null) { var count = arr.Length; if (count == 0) { throw new Exception("Accessors can not have a count of 0."); } var accessor = new Accessor(); accessor.ComponentType = GLTFComponentType.Float; accessor.Count = count; accessor.Type = GLTFAccessorAttributeType.MAT4; var byteOffset = _bufferWriter.BaseStream.Position; foreach (var vec in arr) { _bufferWriter.Write(vec.m00); _bufferWriter.Write(vec.m10); _bufferWriter.Write(vec.m20); _bufferWriter.Write(vec.m30); _bufferWriter.Write(vec.m01); _bufferWriter.Write(vec.m11); _bufferWriter.Write(vec.m21); _bufferWriter.Write(vec.m31); _bufferWriter.Write(vec.m02); _bufferWriter.Write(vec.m12); _bufferWriter.Write(vec.m22); _bufferWriter.Write(vec.m32); _bufferWriter.Write(vec.m03); _bufferWriter.Write(vec.m13); _bufferWriter.Write(vec.m23); _bufferWriter.Write(vec.m33); } var byteLength = _bufferWriter.BaseStream.Position - byteOffset; if (bufferViewId != null) { accessor.BufferView = bufferViewId; accessor.ByteOffset = (int)byteOffset; } else { accessor.BufferView = ExportBufferView((int)byteOffset, (int)byteLength); } var id = new AccessorId { Id = _root.Accessors.Count, Root = _root }; _root.Accessors.Add(accessor); return(id); }
public static BufferViewId WriteBufferView(this GLTFRoot root, BufferId bufferId, int byteOffset, int byteLength) { var bufferView = new BufferView { Buffer = bufferId, ByteOffset = byteOffset, ByteLength = byteLength, }; var id = new BufferViewId { Id = root.BufferViews.Count, Root = root }; root.BufferViews.Add(bufferView); return(id); }
public static AccessorId WriteAccessor(this GLTFRoot root, BufferId bufferId, UnityEngine.Color[] arr, BufferViewId bufferViewId = null, BinaryWriter writer = null) { var count = arr.Length; if (count == 0) { MyLog.LogError("Accessors can not have a count of 0."); } var accessor = new Accessor(); accessor.ComponentType = GLTFComponentType.Float; accessor.Count = count; accessor.Type = GLTFAccessorAttributeType.VEC4; float minR = arr[0].r; float minG = arr[0].g; float minB = arr[0].b; float minA = arr[0].a; float maxR = arr[0].r; float maxG = arr[0].g; float maxB = arr[0].b; float maxA = arr[0].a; for (var i = 1; i < count; i++) { var cur = arr[i]; if (cur.r < minR) { minR = cur.r; } if (cur.g < minG) { minG = cur.g; } if (cur.b < minB) { minB = cur.b; } if (cur.a < minA) { minA = cur.a; } if (cur.r > maxR) { maxR = cur.r; } if (cur.g > maxG) { maxG = cur.g; } if (cur.b > maxB) { maxB = cur.b; } if (cur.a > maxA) { maxA = cur.a; } } accessor.Normalized = true; accessor.Min = new List <double> { minR, minG, minB, minA }; accessor.Max = new List <double> { maxR, maxG, maxB, maxA }; var byteOffset = writer.BaseStream.Position; foreach (var color in arr) { writer.Write(color.r); writer.Write(color.g); writer.Write(color.b); writer.Write(color.a); } var byteLength = writer.BaseStream.Position - byteOffset; if (bufferViewId != null) { accessor.BufferView = bufferViewId; accessor.ByteOffset = (int)byteOffset; } else { accessor.BufferView = root.WriteBufferView(bufferId, (int)byteOffset, (int)byteLength); } var id = new AccessorId { Id = root.Accessors.Count, Root = root }; root.Accessors.Add(accessor); return(id); }
public static AccessorId WriteAccessor(this GLTFRoot root, BufferId bufferId, Matrix4x4[] arr, BufferViewId bufferViewId = null, BinaryWriter writer = null) { var count = arr.Length; if (count == 0) { MyLog.LogError("Accessors can not have a count of 0."); } var accessor = new Accessor(); accessor.ComponentType = GLTFComponentType.Float; accessor.Count = count; accessor.Type = GLTFAccessorAttributeType.MAT4; var byteOffset = writer.BaseStream.Position; foreach (var vec in arr) { writer.Write(vec.m00); writer.Write(vec.m10); writer.Write(vec.m20); writer.Write(vec.m30); writer.Write(vec.m01); writer.Write(vec.m11); writer.Write(vec.m21); writer.Write(vec.m31); writer.Write(vec.m02); writer.Write(vec.m12); writer.Write(vec.m22); writer.Write(vec.m32); writer.Write(vec.m03); writer.Write(vec.m13); writer.Write(vec.m23); writer.Write(vec.m33); } var byteLength = writer.BaseStream.Position - byteOffset; if (bufferViewId != null) { accessor.BufferView = bufferViewId; accessor.ByteOffset = (int)byteOffset; } else { accessor.BufferView = root.WriteBufferView(bufferId, (int)byteOffset, (int)byteLength); } var id = new AccessorId { Id = root.Accessors.Count, Root = root }; root.Accessors.Add(accessor); return(id); }
public static AccessorId WriteAccessor(this GLTFRoot root, BufferId bufferId, Vector4[] arr, BufferViewId bufferViewId = null, bool normalized = false, BinaryWriter writer = null) { var count = arr.Length; if (count == 0) { MyLog.LogError("Accessors can not have a count of 0."); } var accessor = new Accessor(); accessor.ComponentType = GLTFComponentType.Float; accessor.Count = count; accessor.Type = GLTFAccessorAttributeType.VEC4; float minX = arr[0].x; float minY = arr[0].y; float minZ = arr[0].z; float minW = arr[0].w; float maxX = arr[0].x; float maxY = arr[0].y; float maxZ = arr[0].z; float maxW = arr[0].w; for (var i = 1; i < count; i++) { var cur = arr[i]; if (cur.x < minX) { minX = cur.x; } if (cur.y < minY) { minY = cur.y; } if (cur.z < minZ) { minZ = cur.z; } if (cur.w < minW) { minW = cur.w; } if (cur.x > maxX) { maxX = cur.x; } if (cur.y > maxY) { maxY = cur.y; } if (cur.z > maxZ) { maxZ = cur.z; } if (cur.w > maxW) { maxW = cur.w; } } accessor.Normalized = normalized; accessor.Min = new List <double> { minX, minY, minZ, minW }; accessor.Max = new List <double> { maxX, maxY, maxZ, maxW }; var byteOffset = writer.BaseStream.Position; foreach (var vec in arr) { writer.Write(vec.x); writer.Write(vec.y); writer.Write(vec.z); writer.Write(vec.w); } var byteLength = writer.BaseStream.Position - byteOffset; if (bufferViewId != null) { accessor.BufferView = bufferViewId; accessor.ByteOffset = (int)byteOffset; } else { accessor.BufferView = root.WriteBufferView(bufferId, (int)byteOffset, (int)byteLength); } var id = new AccessorId { Id = root.Accessors.Count, Root = root }; root.Accessors.Add(accessor); return(id); }
public static AccessorId WriteAccessor(this GLTFRoot root, BufferId bufferId, Vector3[] arr, BufferViewId bufferViewId = null, bool normalized = false, Transform rootNode = null, BinaryWriter writer = null) { var count = arr.Length; if (count == 0) { MyLog.LogError("Accessors can not have a count of 0."); } var accessor = new Accessor(); accessor.ComponentType = GLTFComponentType.Float; accessor.Count = count; accessor.Type = GLTFAccessorAttributeType.VEC3; /*float minX = arr[0].x; * float minY = arr[0].y; * float minZ = arr[0].z; * float maxX = arr[0].x; * float maxY = arr[0].y; * float maxZ = arr[0].z; * * for (var i = 1; i < count; i++) * { * var cur = arr[i]; * * if (cur.x < minX) * { * minX = cur.x; * } * if (cur.y < minY) * { * minY = cur.y; * } * if (cur.z < minZ) * { * minZ = cur.z; * } * if (cur.x > maxX) * { * maxX = cur.x; * } * if (cur.y > maxY) * { * maxY = cur.y; * } * if (cur.z > maxZ) * { * maxZ = cur.z; * } * }*/ accessor.Normalized = normalized; //accessor.Min = new List<double> { minX, minY, minZ }; //accessor.Max = new List<double> { maxX, maxY, maxZ }; var byteOffset = writer.BaseStream.Position; Matrix4x4 mA = rootNode != null ? rootNode.localToWorldMatrix : new Matrix4x4(); Matrix4x4 mB = rootNode != null ? rootNode.parent.worldToLocalMatrix : new Matrix4x4(); foreach (var vec in arr) { writer.Write(vec.x); writer.Write(vec.y); writer.Write(vec.z); } var byteLength = writer.BaseStream.Position - byteOffset; if (bufferViewId != null) { accessor.BufferView = bufferViewId; accessor.ByteOffset = (int)byteOffset; } else { accessor.BufferView = root.WriteBufferView(bufferId, (int)byteOffset, (int)byteLength); } var id = new AccessorId { Id = root.Accessors.Count, Root = root }; root.Accessors.Add(accessor); return(id); }
protected AccessorId ExportAccessor(UnityEngine.Color[] arr, BufferViewId bufferViewId = null) { var count = arr.Length; if (count == 0) { throw new Exception("Accessors can not have a count of 0."); } var accessor = new Accessor(); accessor.ComponentType = GLTFComponentType.Float; accessor.Count = count; accessor.Type = GLTFAccessorAttributeType.VEC4; float minR = arr[0].r; float minG = arr[0].g; float minB = arr[0].b; float minA = arr[0].a; float maxR = arr[0].r; float maxG = arr[0].g; float maxB = arr[0].b; float maxA = arr[0].a; for (var i = 1; i < count; i++) { var cur = arr[i]; if (cur.r < minR) { minR = cur.r; } if (cur.g < minG) { minG = cur.g; } if (cur.b < minB) { minB = cur.b; } if (cur.a < minA) { minA = cur.a; } if (cur.r > maxR) { maxR = cur.r; } if (cur.g > maxG) { maxG = cur.g; } if (cur.b > maxB) { maxB = cur.b; } if (cur.a > maxA) { maxA = cur.a; } } accessor.Normalized = true; accessor.Min = new List <double> { minR, minG, minB, minA }; accessor.Max = new List <double> { maxR, maxG, maxB, maxA }; var byteOffset = _bufferWriter.BaseStream.Position; foreach (var color in arr) { _bufferWriter.Write(color.r); _bufferWriter.Write(color.g); _bufferWriter.Write(color.b); _bufferWriter.Write(color.a); } var byteLength = _bufferWriter.BaseStream.Position - byteOffset; if (bufferViewId != null) { accessor.BufferView = bufferViewId; accessor.ByteOffset = (int)byteOffset; } else { accessor.BufferView = ExportBufferView((int)byteOffset, (int)byteLength); } var id = new AccessorId { Id = _root.Accessors.Count, Root = _root }; _root.Accessors.Add(accessor); return(id); }
protected AccessorId ExportAccessor(Vector4[] arr, BufferViewId bufferViewId = null, bool normalized = false) { var count = arr.Length; if (count == 0) { throw new Exception("Accessors can not have a count of 0."); } var accessor = new Accessor(); accessor.ComponentType = GLTFComponentType.Float; accessor.Count = count; accessor.Type = GLTFAccessorAttributeType.VEC4; float minX = arr[0].x; float minY = arr[0].y; float minZ = arr[0].z; float minW = arr[0].w; float maxX = arr[0].x; float maxY = arr[0].y; float maxZ = arr[0].z; float maxW = arr[0].w; for (var i = 1; i < count; i++) { var cur = arr[i]; if (cur.x < minX) { minX = cur.x; } if (cur.y < minY) { minY = cur.y; } if (cur.z < minZ) { minZ = cur.z; } if (cur.w < minW) { minW = cur.w; } if (cur.x > maxX) { maxX = cur.x; } if (cur.y > maxY) { maxY = cur.y; } if (cur.z > maxZ) { maxZ = cur.z; } if (cur.w > maxW) { maxW = cur.w; } } accessor.Normalized = normalized; accessor.Min = new List <double> { minX, minY, minZ, minW }; accessor.Max = new List <double> { maxX, maxY, maxZ, maxW }; var byteOffset = _bufferWriter.BaseStream.Position; foreach (var vec in arr) { _bufferWriter.Write(vec.x); _bufferWriter.Write(vec.y); _bufferWriter.Write(vec.z); _bufferWriter.Write(vec.w); } var byteLength = _bufferWriter.BaseStream.Position - byteOffset; if (bufferViewId != null) { accessor.BufferView = bufferViewId; accessor.ByteOffset = (int)byteOffset; } else { accessor.BufferView = ExportBufferView((int)byteOffset, (int)byteLength); } var id = new AccessorId { Id = _root.Accessors.Count, Root = _root }; _root.Accessors.Add(accessor); return(id); }
protected AccessorId ExportAccessor(Vector3[] arr, BufferViewId bufferViewId = null, bool normalized = false, Transform root = null) { var count = arr.Length; if (count == 0) { throw new Exception("Accessors can not have a count of 0."); } var accessor = new Accessor(); accessor.ComponentType = GLTFComponentType.Float; accessor.Count = count; accessor.Type = GLTFAccessorAttributeType.VEC3; /*float minX = arr[0].x; * float minY = arr[0].y; * float minZ = arr[0].z; * float maxX = arr[0].x; * float maxY = arr[0].y; * float maxZ = arr[0].z; * * for (var i = 1; i < count; i++) * { * var cur = arr[i]; * * if (cur.x < minX) * { * minX = cur.x; * } * if (cur.y < minY) * { * minY = cur.y; * } * if (cur.z < minZ) * { * minZ = cur.z; * } * if (cur.x > maxX) * { * maxX = cur.x; * } * if (cur.y > maxY) * { * maxY = cur.y; * } * if (cur.z > maxZ) * { * maxZ = cur.z; * } * }*/ accessor.Normalized = normalized; //accessor.Min = new List<double> { minX, minY, minZ }; //accessor.Max = new List<double> { maxX, maxY, maxZ }; var byteOffset = _bufferWriter.BaseStream.Position; Matrix4x4 mA = root != null ? root.localToWorldMatrix : new Matrix4x4(); Matrix4x4 mB = root != null ? root.parent.worldToLocalMatrix : new Matrix4x4(); foreach (var vec in arr) { if (root != null) { if (normalized) { var v = mB.MultiplyVector(mA.MultiplyVector(vec)); _bufferWriter.Write(v.x); _bufferWriter.Write(v.y); _bufferWriter.Write(v.z); } else { var v = mB.MultiplyPoint(mA.MultiplyPoint(vec)); _bufferWriter.Write(v.x); _bufferWriter.Write(v.y); _bufferWriter.Write(v.z); } } else { _bufferWriter.Write(vec.x); _bufferWriter.Write(vec.y); _bufferWriter.Write(vec.z); } } var byteLength = _bufferWriter.BaseStream.Position - byteOffset; if (bufferViewId != null) { accessor.BufferView = bufferViewId; accessor.ByteOffset = (int)byteOffset; } else { accessor.BufferView = ExportBufferView((int)byteOffset, (int)byteLength); } var id = new AccessorId { Id = _root.Accessors.Count, Root = _root }; _root.Accessors.Add(accessor); return(id); }