En the following code, I create and export a triangle to USDC using the MDLAsset export function. The exported USDC file is correct, but I don't see the material component. It seems like the material is not being exported. Did I miss something?
//new asset
MDLAsset *asset = [[MDLAsset alloc] initWithBufferAllocator:nil];
//------------------ vertex descriptor -------------------------------------------------------------------------
MDLVertexAttribute *atrPos = [[MDLVertexAttribute alloc] initWithName:@"position" format:MDLVertexFormatFloat3 offset:0 bufferIndex:0];
MDLVertexAttribute *atrNor = [[MDLVertexAttribute alloc] initWithName:@"normal" format:MDLVertexFormatFloat3 offset:12 bufferIndex:0];
MDLVertexAttribute *atrCol = [[MDLVertexAttribute alloc] initWithName:@"color" format:MDLVertexFormatFloat4 offset:24 bufferIndex:0];
//layout (12 + 12 + 16)
MDLVertexBufferLayout *lay = [[MDLVertexBufferLayout alloc] initWithStride:40];
//add descriptor and layaut
MDLVertexDescriptor *vdc = [[MDLVertexDescriptor alloc] init];
vdc.attributes = [NSMutableArray arrayWithObjects:atrPos, atrNor, atrCol, nil];
vdc.layouts = [NSMutableArray arrayWithObjects:lay, nil];
//-------------- material ---------------------------------------------------------------------------------------------------
MDLScatteringFunction *scatteringFunction = [MDLPhysicallyPlausibleScatteringFunction new];
MDLMaterial *material = [[MDLMaterial alloc] initWithName:@"matTest" scatteringFunction:scatteringFunction];
//--------------------- mesh ------------------------------------------------------------------------------------------------
//vertex (position, normal, and color RGBA)
static const float triangleVertexData[] =
{
0.0, 0.5, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
-0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0
};
//vertex buffer
int numVertices = 3;
int lenBufferForVertices = sizeof(triangleVertexData);
MTKMeshBuffer *mtkMeshBufferForVertices = (MTKMeshBuffer *)[[asset bufferAllocator] newBuffer:lenBufferForVertices type:MDLMeshBufferTypeVertex];
NSData *nsData_vertex = [NSData dataWithBytes:triangleVertexData length:lenBufferForVertices];
[mtkMeshBufferForVertices fillData:nsData_vertex offset:0];
//index
static uint16_t indices[] = {0, 1, 2};
//index buffer
int numIndices = 3;
int lenBufferForIndices = numIndices * sizeof(uint16_t);
MTKMeshBuffer *mtkMeshBufferForIndices = (MTKMeshBuffer *)[[asset bufferAllocator] newBuffer:lenBufferForIndices type:MDLMeshBufferTypeIndex];
NSData *nsData_indices = [NSData dataWithBytes:indices length:lenBufferForIndices];
[mtkMeshBufferForIndices fillData:nsData_indices offset:0];
//submesh
MDLSubmesh *submesh = [[MDLSubmesh alloc] initWithName:@"triangle"
indexBuffer:mtkMeshBufferForIndices
indexCount:numIndices
indexType:MDLIndexBitDepthUInt16
geometryType:MDLGeometryTypeTriangles
material:material];
//mesh
MDLMesh *mdlMesh = [[MDLMesh alloc] initWithVertexBuffer:mtkMeshBufferForVertices
vertexCount:numVertices
descriptor:vdc
submeshes:[NSArray arrayWithObjects:submesh, nil]];
//add mesh object to asset
[asset addObject:mdlMesh];
//export to usdc
BOOL res = [asset exportAssetToURL:[NSURL fileURLWithPath:filePath]];