private static int CreateNodeForMesh(Gltf gltf, int meshId, List <glTFLoader.Schema.Node> nodes, Transform transform = null) { var parentId = 0; if (transform != null) { var a = transform.XAxis; var b = transform.YAxis; var c = transform.ZAxis; var transNode = new Node(); transNode.Matrix = new[] { (float)a.X, (float)a.Y, (float)a.Z, 0.0f, (float)b.X, (float)b.Y, (float)b.Z, 0.0f, (float)c.X, (float)c.Y, (float)c.Z, 0.0f, (float)transform.Origin.X, (float)transform.Origin.Y, (float)transform.Origin.Z, 1.0f }; parentId = gltf.AddNode(nodes, transNode, 0); } // Add mesh node to gltf var node = new Node(); node.Mesh = meshId; var nodeId = gltf.AddNode(nodes, node, parentId); return(nodeId); }
internal static void AddLights(this Gltf gltf, List <DirectionalLight> lights, List <Node> nodes) { gltf.Extensions = new Dictionary <string, object>(); var lightCount = 0; var lightsArr = new List <object>(); foreach (var light in lights) { // Create the top level collection of lights. var gltfLight = new Dictionary <string, object>() { { "color", new[] { light.Color.Red, light.Color.Green, light.Color.Blue } }, { "type", "directional" }, { "intensity", light.Intensity }, { "name", light.Name != null ? light.Name : string.Empty } }; lightsArr.Add(gltfLight); // Create the light nodes var lightNode = new Node(); lightNode.Extensions = new Dictionary <string, object>() { { "KHR_lights_punctual", new Dictionary <string, object>() { { "light", lightCount } } } }; var ml = light.Transform.Matrix; lightNode.Matrix = new float[] { (float)ml.m11, (float)ml.m12, (float)ml.m13, 0f, (float)ml.m21, (float)ml.m22, (float)ml.m23, 0f, (float)ml.m31, (float)ml.m32, (float)ml.m33, 0f, (float)ml.tx, (float)ml.ty, (float)ml.tz, 1f }; gltf.AddNode(nodes, lightNode, 0); lightCount++; } gltf.Extensions.Add("KHR_lights_punctual", new Dictionary <string, object> { { "lights", lightsArr } }); }
internal static void AddInstanceMesh(this Gltf gltf, List <glTFLoader.Schema.Node> nodes, List <int> meshIds, Transform transform) { var a = transform.XAxis; var b = transform.YAxis; var c = transform.ZAxis; var matrix = new[] { (float)a.X, (float)a.Y, (float)a.Z, 0.0f, (float)b.X, (float)b.Y, (float)b.Z, 0.0f, (float)c.X, (float)c.Y, (float)c.Z, 0.0f, (float)transform.Origin.X, (float)transform.Origin.Y, (float)transform.Origin.Z, 1.0f }; foreach (var meshId in meshIds) { var node = new Node(); node.Matrix = matrix; node.Mesh = meshId; gltf.AddNode(nodes, node, 0); } }
internal static int AddLineLoop(this Gltf gltf, string name, List <byte> buffer, double[] vertices, ushort[] indices, double[] vMin, double[] vMax, ushort iMin, ushort iMax, int materialId, MeshPrimitive.ModeEnum mode, Transform transform = null) { var m = new glTFLoader.Schema.Mesh(); m.Name = name; var vBuff = gltf.AddBufferView(0, buffer.Count, vertices.Length * sizeof(float), null, null); var iBuff = gltf.AddBufferView(0, buffer.Count + vertices.Length * sizeof(float), indices.Length * sizeof(ushort), null, null); foreach (var v in vertices) { buffer.AddRange(BitConverter.GetBytes((float)v)); } foreach (var i in indices) { buffer.AddRange(BitConverter.GetBytes(i)); } while (buffer.Count % 4 != 0) { // Console.WriteLine("Padding..."); buffer.Add(0); } var vAccess = gltf.AddAccessor(vBuff, 0, Accessor.ComponentTypeEnum.FLOAT, vertices.Length / 3, new[] { (float)vMin[0], (float)vMin[1], (float)vMin[2] }, new[] { (float)vMax[0], (float)vMax[1], (float)vMax[2] }, Accessor.TypeEnum.VEC3); var iAccess = gltf.AddAccessor(iBuff, 0, Accessor.ComponentTypeEnum.UNSIGNED_SHORT, indices.Length, new[] { (float)iMin }, new[] { (float)iMax }, Accessor.TypeEnum.SCALAR); var prim = new MeshPrimitive(); prim.Indices = iAccess; prim.Material = materialId; prim.Mode = mode; prim.Attributes = new Dictionary <string, int> { { "POSITION", vAccess } }; m.Primitives = new[] { prim }; // Add mesh to gltf if (gltf.Meshes != null) { // TODO: Get rid of this resizing. var meshes = gltf.Meshes.ToList(); meshes.Add(m); gltf.Meshes = meshes.ToArray(); } else { gltf.Meshes = new[] { m }; } var parentId = 0; if (transform != null) { var a = transform.XAxis; var b = transform.YAxis; var c = transform.ZAxis; var transNode = new Node(); transNode.Matrix = new[] { (float)a.X, (float)a.Y, (float)a.Z, 0.0f, (float)b.X, (float)b.Y, (float)b.Z, 0.0f, (float)c.X, (float)c.Y, (float)c.Z, 0.0f, (float)transform.Origin.X, (float)transform.Origin.Y, (float)transform.Origin.Z, 1.0f }; parentId = gltf.AddNode(transNode, 0); } // Add mesh node to gltf var node = new Node(); node.Mesh = gltf.Meshes.Length - 1; gltf.AddNode(node, parentId); return(gltf.Meshes.Length - 1); }
internal static int AddTriangleMesh(this Gltf gltf, string name, List <byte> buffer, double[] vertices, double[] normals, ushort[] indices, float[] colors, double[] vMin, double[] vMax, double[] nMin, double[] nMax, ushort iMin, ushort iMax, int materialId, float[] cMin, float[] cMax, int?parent_index, Transform transform = null) { var m = new glTFLoader.Schema.Mesh(); m.Name = name; var vBuff = gltf.AddBufferView(0, buffer.Count, vertices.Length * sizeof(float), null, null); var nBuff = gltf.AddBufferView(0, buffer.Count + vertices.Length * sizeof(float), normals.Length * sizeof(float), null, null); var iBuff = gltf.AddBufferView(0, buffer.Count + vertices.Length * sizeof(float) + normals.Length * sizeof(float), indices.Length * sizeof(ushort), null, null); foreach (var v in vertices) { buffer.AddRange(BitConverter.GetBytes((float)v)); } foreach (var n in normals) { buffer.AddRange(BitConverter.GetBytes((float)n)); } foreach (var i in indices) { buffer.AddRange(BitConverter.GetBytes(i)); } while (buffer.Count % 4 != 0) { // Console.WriteLine("Padding..."); buffer.Add(0); } var vAccess = gltf.AddAccessor(vBuff, 0, Accessor.ComponentTypeEnum.FLOAT, vertices.Length / 3, new[] { (float)vMin[0], (float)vMin[1], (float)vMin[2] }, new[] { (float)vMax[0], (float)vMax[1], (float)vMax[2] }, Accessor.TypeEnum.VEC3); var nAccess = gltf.AddAccessor(nBuff, 0, Accessor.ComponentTypeEnum.FLOAT, normals.Length / 3, new[] { (float)nMin[0], (float)nMin[1], (float)nMin[2] }, new[] { (float)nMax[0], (float)nMax[1], (float)nMax[2] }, Accessor.TypeEnum.VEC3); var iAccess = gltf.AddAccessor(iBuff, 0, Accessor.ComponentTypeEnum.UNSIGNED_SHORT, indices.Length, new[] { (float)iMin }, new[] { (float)iMax }, Accessor.TypeEnum.SCALAR); var prim = new MeshPrimitive(); prim.Indices = iAccess; prim.Material = materialId; prim.Mode = MeshPrimitive.ModeEnum.TRIANGLES; prim.Attributes = new Dictionary <string, int> { { "NORMAL", nAccess }, { "POSITION", vAccess } }; // TODO: Add to the buffer above instead of inside this block. // There's a chance the padding operation will put padding before // the color information. if (colors.Length > 0) { var cBuff = gltf.AddBufferView(0, buffer.Count, colors.Length * sizeof(float), null, null); foreach (var c in colors) { buffer.AddRange(BitConverter.GetBytes((float)c)); } var cAccess = gltf.AddAccessor(cBuff, 0, Accessor.ComponentTypeEnum.FLOAT, colors.Length / 3, cMin, cMax, Accessor.TypeEnum.VEC3); prim.Attributes.Add("COLOR_0", cAccess); } m.Primitives = new[] { prim }; // Add mesh to gltf if (gltf.Meshes != null) { // TODO: Get rid of this resizing. var meshes = gltf.Meshes.ToList(); meshes.Add(m); gltf.Meshes = meshes.ToArray(); } else { gltf.Meshes = new[] { m }; } var parentId = 0; if (transform != null) { var a = transform.XAxis; var b = transform.YAxis; var c = transform.ZAxis; var transNode = new Node(); transNode.Matrix = new[] { (float)a.X, (float)a.Y, (float)a.Z, 0.0f, (float)b.X, (float)b.Y, (float)b.Z, 0.0f, (float)c.X, (float)c.Y, (float)c.Z, 0.0f, (float)transform.Origin.X, (float)transform.Origin.Y, (float)transform.Origin.Z, 1.0f }; parentId = gltf.AddNode(transNode, 0); } // Add mesh node to gltf var node = new Node(); node.Mesh = gltf.Meshes.Length - 1; gltf.AddNode(node, parentId); return(gltf.Meshes.Length - 1); }
internal static int AddLineLoop(this Gltf gltf, string name, List <byte> buffer, List <BufferView> bufferViews, List <Accessor> accessors, byte[] vertices, byte[] indices, double[] vMin, double[] vMax, ushort iMin, ushort iMax, int materialId, MeshPrimitive.ModeEnum mode, List <glTFLoader.Schema.Mesh> meshes, List <glTFLoader.Schema.Node> nodes, Transform transform = null) { var m = new glTFLoader.Schema.Mesh(); m.Name = name; var vBuff = AddBufferView(bufferViews, 0, buffer.Count, vertices.Length, null, null); var iBuff = AddBufferView(bufferViews, 0, buffer.Count + vertices.Length, indices.Length, null, null); buffer.AddRange(vertices); buffer.AddRange(indices); while (buffer.Count % 4 != 0) { // Console.WriteLine("Padding..."); buffer.Add(0); } var vAccess = AddAccessor(accessors, vBuff, 0, Accessor.ComponentTypeEnum.FLOAT, vertices.Length / sizeof(float) / 3, new[] { (float)vMin[0], (float)vMin[1], (float)vMin[2] }, new[] { (float)vMax[0], (float)vMax[1], (float)vMax[2] }, Accessor.TypeEnum.VEC3); var iAccess = AddAccessor(accessors, iBuff, 0, Accessor.ComponentTypeEnum.UNSIGNED_SHORT, indices.Length / sizeof(ushort), new[] { (float)iMin }, new[] { (float)iMax }, Accessor.TypeEnum.SCALAR); var prim = new MeshPrimitive(); prim.Indices = iAccess; prim.Material = materialId; prim.Mode = mode; prim.Attributes = new Dictionary <string, int> { { "POSITION", vAccess } }; m.Primitives = new[] { prim }; // Add mesh to gltf meshes.Add(m); var parentId = 0; if (transform != null) { var a = transform.XAxis; var b = transform.YAxis; var c = transform.ZAxis; var transNode = new Node(); transNode.Matrix = new[] { (float)a.X, (float)a.Y, (float)a.Z, 0.0f, (float)b.X, (float)b.Y, (float)b.Z, 0.0f, (float)c.X, (float)c.Y, (float)c.Z, 0.0f, (float)transform.Origin.X, (float)transform.Origin.Y, (float)transform.Origin.Z, 1.0f }; parentId = gltf.AddNode(nodes, transNode, 0); } // Add mesh node to gltf var node = new Node(); node.Mesh = meshes.Count - 1; gltf.AddNode(nodes, node, parentId); return(meshes.Count - 1); }
internal static int AddTriangleMesh(this Gltf gltf, string name, List <byte> buffer, double[] vertices, double[] normals, ushort[] indices, float[] colors, double[] vMin, double[] vMax, double[] nMin, double[] nMax, double[] cMin, double[] cMax, ushort iMin, ushort iMax, int materialId, int?parent_index, Transform transform = null) { var m = new glTFLoader.Schema.Mesh(); m.Name = name; var vBuff = gltf.AddBufferView(0, buffer.Count(), vertices.Length * sizeof(float), null, null); var nBuff = gltf.AddBufferView(0, buffer.Count() + vertices.Length * sizeof(float), normals.Length * sizeof(float), null, null); var iBuff = gltf.AddBufferView(0, buffer.Count() + vertices.Length * sizeof(float) + normals.Length * sizeof(float), indices.Length * sizeof(ushort), null, null); buffer.AddRange(vertices.SelectMany(v => BitConverter.GetBytes((float)v))); buffer.AddRange(normals.SelectMany(v => BitConverter.GetBytes((float)v))); buffer.AddRange(indices.SelectMany(v => BitConverter.GetBytes(v))); while (buffer.Count() % 4 != 0) { // Console.WriteLine("Padding..."); buffer.Add(0); } var vAccess = gltf.AddAccessor(vBuff, 0, Accessor.ComponentTypeEnum.FLOAT, vertices.Length / 3, new[] { (float)vMin[0], (float)vMin[1], (float)vMin[2] }, new[] { (float)vMax[0], (float)vMax[1], (float)vMax[2] }, Accessor.TypeEnum.VEC3); var nAccess = gltf.AddAccessor(nBuff, 0, Accessor.ComponentTypeEnum.FLOAT, normals.Length / 3, new[] { (float)nMin[0], (float)nMin[1], (float)nMin[2] }, new[] { (float)nMax[0], (float)nMax[1], (float)nMax[2] }, Accessor.TypeEnum.VEC3); var iAccess = gltf.AddAccessor(iBuff, 0, Accessor.ComponentTypeEnum.UNSIGNED_SHORT, indices.Length, new[] { (float)iMin }, new[] { (float)iMax }, Accessor.TypeEnum.SCALAR); var prim = new MeshPrimitive(); prim.Indices = iAccess; prim.Material = materialId; prim.Mode = MeshPrimitive.ModeEnum.TRIANGLES; prim.Attributes = new Dictionary <string, int> { { "NORMAL", nAccess }, { "POSITION", vAccess } }; m.Primitives = new[] { prim }; // Add mesh to gltf if (gltf.Meshes != null) { var meshes = gltf.Meshes.ToList(); meshes.Add(m); gltf.Meshes = meshes.ToArray(); } else { gltf.Meshes = new[] { m }; } var parentId = 0; if (transform != null) { var a = transform.XAxis; var b = transform.YAxis; var c = transform.ZAxis; var transNode = new Node(); transNode.Matrix = new[] { (float)a.X, (float)a.Y, (float)a.Z, 0.0f, (float)b.X, (float)b.Y, (float)b.Z, 0.0f, (float)c.X, (float)c.Y, (float)c.Z, 0.0f, (float)transform.Origin.X, (float)transform.Origin.Y, (float)transform.Origin.Z, 1.0f }; parentId = gltf.AddNode(transNode, 0); } // Add mesh node to gltf var node = new Node(); node.Mesh = gltf.Meshes.Length - 1; gltf.AddNode(node, parentId); return(gltf.Meshes.Length - 1); }