public static IndexBufferBinding ConvertIndexBufferBinding(SharpGLTF.Schema2.MeshPrimitive mesh, GraphicsDevice device) { var indices = mesh.GetTriangleIndices().Select(x => new int[] { x.A, x.C, x.B }).SelectMany(x => x).Select(x => (uint)x).ToArray(); var buf = Stride.Graphics.Buffer.Index.New(device, indices); return(new IndexBufferBinding(buf, true, indices.Length)); }
public static Mesh LoadMesh(SharpGLTF.Schema2.MeshPrimitive mesh, GraphicsDevice device) { var draw = new MeshDraw { PrimitiveType = ConvertPrimitiveType(mesh.DrawPrimitiveType), IndexBuffer = ConvertIndexBufferBinding(mesh, device), VertexBuffers = ConvertVertexBufferBinding(mesh, device), DrawCount = GetDrawCount(mesh) }; var result = new Mesh(draw, new ParameterCollection()) { Skinning = ConvertInverseBindMatrices(mesh.LogicalParent.LogicalParent), Name = mesh.LogicalParent.Name, MaterialIndex = mesh.LogicalParent.LogicalParent.LogicalMaterials.ToList().IndexOf(mesh.Material) }; //TODO : Add parameter collection only after checking if it has result.Parameters.Set(MaterialKeys.HasSkinningPosition, true); result.Parameters.Set(MaterialKeys.HasSkinningNormal, true); return(result); }
public static VertexBufferBinding[] ConvertVertexBufferBinding(SharpGLTF.Schema2.MeshPrimitive mesh, GraphicsDevice device) { var offset = 0; var vertElem = mesh.VertexAccessors .Select( x => { var y = ConvertVertexElement(x, offset); offset += y.Item2; return(y.Item1); }) .ToList(); var declaration = new VertexDeclaration( vertElem.ToArray() ); var size = mesh.VertexAccessors.First().Value.Count; var byteBuffer = Enumerable.Range(0, size) .Select( x => declaration.EnumerateWithOffsets() .Select(y => y.VertexElement.SemanticName .Replace("ORD", "ORD_" + y.VertexElement.SemanticIndex) .Replace("BLENDINDICES", "JOINTS_0") .Replace("BLENDWEIGHT", "WEIGHTS_0") ) .Select(y => mesh.GetVertexAccessor(y).TryGetVertexBytes(x).ToArray()) ) .SelectMany(x => x) .SelectMany(x => x) .ToArray(); var buffer = Stride.Graphics.Buffer.Vertex.New( device, new DataPointer( GCHandle.Alloc(byteBuffer, GCHandleType.Pinned).AddrOfPinnedObject(), byteBuffer.Length ) ); var binding = new VertexBufferBinding(buffer, declaration, size); return(new List <VertexBufferBinding>() { binding }.ToArray()); }
internal _MeshPrimitiveDecoder(SCHEMA2PRIMITIVE srcPrim) : base(srcPrim) { _Material = srcPrim.Material as TMaterial; }
private static int GetDrawCount(SharpGLTF.Schema2.MeshPrimitive mesh) { var tmp = mesh.GetTriangleIndices().Select(x => new int[] { x.A, x.C, x.B }).SelectMany(x => x).Select(x => (uint)x).ToArray(); return(mesh.GetTriangleIndices().Select(x => new int[] { x.A, x.C, x.B }).SelectMany(x => x).Select(x => (uint)x).ToArray().Length); }