Skip to main content
added 32 characters in body
Source Link
Ray
  • 633
  • 1
  • 7
  • 21

The documentation actually has an article for this which I found while writing the question. Basically, vertices and indices are stored in MeshDraw instances, which are then assigned to the Mesh.Draw property.

A short example demonstrates this quickly by using automatically generated mesh data:

MeshDraw meshDraw = GeometricPrimitive.Sphere.New(GraphicsDevice).ToMeshDraw();
Mesh mesh = new Mesh { Draw = meshDraw }; 

The other more elaborate example uses custom vertex data, which is exactly what I wanted:

// Create vertex buffer.
VertexPositionTexture[] vertices = new VertexPositionTexture[3];
vertices[0].Position = new Vector3(0, 0, 1);
vertices[1].Position = new Vector3(0, 1, 0);
vertices[2].Position = new Vector3(0, 1, 1);
Buffer<VertexPositionTexture> vertexBuffer = Stride.Graphics.Buffer.Vertex.New(GraphicsDevice, vertices);

// Create index buffer.
int[] indices = { 0, 2, 1 };
Buffer<int> indexBuffer = Stride.Graphics.Buffer.Index.New(GraphicsDevice, indices);

// Create the mesh with a MeshDraw instance.
Mesh mesh = new Mesh
{
    Draw = new MeshDraw
    {
        PrimitiveType = PrimitiveType.TriangleList,
        DrawCount = indices.Length,
        IndexBuffer = new IndexBufferBinding(indexBuffer, true, indices.Length),
        VertexBuffers = new[]
        {
            new VertexBufferBinding(vertexBuffer, VertexPositionTexture.Layout, vertexBuffer.ElementCount)
        },
    }
};

And then you'd just add these Mesh instances to Models which you put into ModelComponents:

// Create the model with the mesh.
Model model = new Model();
model.Meshes.Add(mesh);
Entity.Add(new ModelComponent(model));

The documentation actually has an article for this which I found while writing the question. Basically, vertices and indices are stored in MeshDraw instances, which are then assigned to the Mesh.Draw property.

A short example demonstrates this quickly by using automatically generated mesh data:

MeshDraw meshDraw = GeometricPrimitive.Sphere.New(GraphicsDevice).ToMeshDraw();
Mesh mesh = new Mesh { Draw = meshDraw }; 

The other more elaborate example uses custom vertex data, which is exactly what I wanted:

// Create vertex buffer.
VertexPositionTexture[] vertices = new VertexPositionTexture[3];
vertices[0].Position = new Vector3(0, 0, 1);
vertices[1].Position = new Vector3(0, 1, 0);
vertices[2].Position = new Vector3(0, 1, 1);
Buffer<VertexPositionTexture> vertexBuffer = Buffer.Vertex.New(GraphicsDevice, vertices);

// Create index buffer.
int[] indices = { 0, 2, 1 };
Buffer<int> indexBuffer = Buffer.Index.New(GraphicsDevice, indices);

// Create the mesh with a MeshDraw instance.
Mesh mesh = new Mesh
{
    Draw = new MeshDraw
    {
        PrimitiveType = PrimitiveType.TriangleList,
        DrawCount = indices.Length,
        IndexBuffer = new IndexBufferBinding(indexBuffer, true, indices.Length),
        VertexBuffers = new[]
        {
            new VertexBufferBinding(vertexBuffer, VertexPositionTexture.Layout, vertexBuffer.ElementCount)
        },
    }
};

And then you'd just add these Mesh instances to Models which you put into ModelComponents:

// Create the model with the mesh.
Model model = new Model();
model.Meshes.Add(mesh);
Entity.Add(new ModelComponent(model));

The documentation actually has an article for this which I found while writing the question. Basically, vertices and indices are stored in MeshDraw instances, which are then assigned to the Mesh.Draw property.

A short example demonstrates this quickly by using automatically generated mesh data:

MeshDraw meshDraw = GeometricPrimitive.Sphere.New(GraphicsDevice).ToMeshDraw();
Mesh mesh = new Mesh { Draw = meshDraw }; 

The other more elaborate example uses custom vertex data, which is exactly what I wanted:

// Create vertex buffer.
VertexPositionTexture[] vertices = new VertexPositionTexture[3];
vertices[0].Position = new Vector3(0, 0, 1);
vertices[1].Position = new Vector3(0, 1, 0);
vertices[2].Position = new Vector3(0, 1, 1);
Buffer<VertexPositionTexture> vertexBuffer = Stride.Graphics.Buffer.Vertex.New(GraphicsDevice, vertices);

// Create index buffer.
int[] indices = { 0, 2, 1 };
Buffer<int> indexBuffer = Stride.Graphics.Buffer.Index.New(GraphicsDevice, indices);

// Create the mesh with a MeshDraw instance.
Mesh mesh = new Mesh
{
    Draw = new MeshDraw
    {
        PrimitiveType = PrimitiveType.TriangleList,
        DrawCount = indices.Length,
        IndexBuffer = new IndexBufferBinding(indexBuffer, true, indices.Length),
        VertexBuffers = new[]
        {
            new VertexBufferBinding(vertexBuffer, VertexPositionTexture.Layout, vertexBuffer.ElementCount)
        },
    }
};

And then you'd just add these Mesh instances to Models which you put into ModelComponents:

// Create the model with the mesh.
Model model = new Model();
model.Meshes.Add(mesh);
Entity.Add(new ModelComponent(model));
Source Link
Ray
  • 633
  • 1
  • 7
  • 21

The documentation actually has an article for this which I found while writing the question. Basically, vertices and indices are stored in MeshDraw instances, which are then assigned to the Mesh.Draw property.

A short example demonstrates this quickly by using automatically generated mesh data:

MeshDraw meshDraw = GeometricPrimitive.Sphere.New(GraphicsDevice).ToMeshDraw();
Mesh mesh = new Mesh { Draw = meshDraw }; 

The other more elaborate example uses custom vertex data, which is exactly what I wanted:

// Create vertex buffer.
VertexPositionTexture[] vertices = new VertexPositionTexture[3];
vertices[0].Position = new Vector3(0, 0, 1);
vertices[1].Position = new Vector3(0, 1, 0);
vertices[2].Position = new Vector3(0, 1, 1);
Buffer<VertexPositionTexture> vertexBuffer = Buffer.Vertex.New(GraphicsDevice, vertices);

// Create index buffer.
int[] indices = { 0, 2, 1 };
Buffer<int> indexBuffer = Buffer.Index.New(GraphicsDevice, indices);

// Create the mesh with a MeshDraw instance.
Mesh mesh = new Mesh
{
    Draw = new MeshDraw
    {
        PrimitiveType = PrimitiveType.TriangleList,
        DrawCount = indices.Length,
        IndexBuffer = new IndexBufferBinding(indexBuffer, true, indices.Length),
        VertexBuffers = new[]
        {
            new VertexBufferBinding(vertexBuffer, VertexPositionTexture.Layout, vertexBuffer.ElementCount)
        },
    }
};

And then you'd just add these Mesh instances to Models which you put into ModelComponents:

// Create the model with the mesh.
Model model = new Model();
model.Meshes.Add(mesh);
Entity.Add(new ModelComponent(model));