The obj file loaded with Model I/O. I receive vertex buffer and index buffer from mesh and submesh. I draw it with index buffer. In GPU buffer unpacked and load all triangles.
When I load asset I am creating vertex descriptor, to say what should be loaded from 3D asset. When I pass it to shader I am using [[ stage_in ]]
vertex parameter.
But somehow I need to change the structure of vertex that is created by loading model asset in parameter of vertex-shader function to pass more data with animation offset for each vertex. For example I need to pass data that in an 3D asset, to apply offset to all vertices.
vertex VertexOut vertex_main(VertexIn vertexIn [[ stage_in ]], constant Uniforms &uniforms [[ buffer(1) ]], uint vertexID [[ vertex_id ]]) {
float4 worldPosition = uniforms.modelMatrix *float4(vertexIn.position, 1);
VertexOut vertexOut;
vertexOut.position = uniforms.viewProjectionMatrix * worldPosition;
vertexOut.worldPosition = worldPosition.xyz;
vertexOut.worldNormal = uniforms.normalMatrix * vertexIn.normal;
vertexOut.texCoords = vertexIn.texCoords;
return vertexOut;
}
That how looks VertexIn
struct VertexIn {
float3 position [[attribute(0)]];
float3 normal [[attribute(1)]];
float2 texCoords [[attribute(2)]];
};
Drawing of 3D asset
for mesh in meshes {
for i in 0..<mesh.vertexBuffers.count {
let vertexBuffer = mesh.vertexBuffers[i]
commandEncoder.setVertexBuffer(vertexBuffer.buffer, offset: vertexBuffer.offset, index: i)
}
for submesh in mesh.submeshes {
let indexBuffer = submesh.indexBuffer
commandEncoder.drawIndexedPrimitives(type: submesh.primitiveType,
indexCount: submesh.indexCount,
indexType: submesh.indexType,
indexBuffer: indexBuffer.buffer,
indexBufferOffset: indexBuffer.offset)
}
}
Vertex descriptor for loading 3D Model asset
let vertexDescriptor = MDLVertexDescriptor()
vertexDescriptor.attributes[0] = MDLVertexAttribute(name: MDLVertexAttributePosition, format: .float3, offset: 0, bufferIndex: 0)
vertexDescriptor.attributes[1] = MDLVertexAttribute(name: MDLVertexAttributeNormal, format: .float3, offset: MemoryLayout<Float>.size * 3, bufferIndex: 0)
vertexDescriptor.attributes[2] = MDLVertexAttribute(name: MDLVertexAttributeTextureCoordinate, format: .float2, offset: MemoryLayout<Float>.size * 6, bufferIndex: 0)
vertexDescriptor.layouts[0] = MDLVertexBufferLayout(stride: MemoryLayout<Float>.size * 8)
Thank you for help. P.S. I was trying to change after loading of 3d asset, but it fails, because I can't change the vertex buffer created with Model I/O.