ModelIO not working

I am trying to import 3D model files and render them Metalusing ModelIOand MetalKit(in OS X 10.11), but the behavior that I see from these frameworks (in particular ModelIO) is not as expected.

I can import files .objand convert them into tags MetalKitwithout any errors, but the grid (at least when rendering) seems to be just a big fan of triangles coming from one point. The model in the screenshot below should be a split version of the Suzanne monkey head: Incorrectly imported .obj file

MDLSubmesh . 0, , . , .obj .

3D ( ), , .obj, NSException MDLAsset init().

Xcode 7.2 OS X 10.11.

+4
1

, Metal, .

Melita, "" , . MDLVertexBufferLayout, :

, ( ) , .

  • meshs vertexBuffers MDLMeshBuffer meshs vertexDescriptor MDLVertexBufferLayout .

  • , . , , .

.layouts .attributes MDLVertexDescriptor , ( , ), .

.layouts .attributes , , , ... ?

Semi-durable implementation ... get it !?

class func setup(meshWithDevice device: MTLDevice) -> MTKMesh
{
    // Allocator
    let allocator = MTKMeshBufferAllocator(device: device)

    // Vertex Descriptor, tells the MDLAsset how to layout the buffers
    let vertexDescriptor = MDLVertexDescriptor()

    // Vertex Buffer Layout, tells how many buffers will be used, and the stride of its structs
    // (the init(stide: Int) crashes in the Beta)
    let vertexLayout = MDLVertexBufferLayout()
    vertexLayout.stride = MemoryLayout<Vertex>.size

    // Apply the Layouts
    vertexDescriptor.layouts = [vertexLayout]

    // Apply the attributes, in my case, position and normal (float4 x2)
    vertexDescriptor.attributes =
    [
        MDLVertexAttribute(name: MDLVertexAttributePosition, format: MDLVertexFormat.float4, offset: 0, bufferIndex: 0),
        MDLVertexAttribute(name: MDLVertexAttributeNormal, format: MDLVertexFormat.float4, offset: MemoryLayout<float4>.size, bufferIndex: 0)
    ]

    var error : NSError? = nil

    // Load the teapot
    let asset = MDLAsset(url: Bundle.main.url(forResource: "teapot", withExtension: "obj")!, vertexDescriptor: vertexDescriptor, bufferAllocator: allocator, preserveTopology: true, error: &error)

    if let error = error
    {
        print(error)
    }

    // Obtain the teapot Mesh
    let teapotModel = asset.object(at: 0) as! MDLMesh

    // Convert into MetalKit Mesh, insted of ModelIO
    let teapot = try! MTKMesh(mesh: teapotModel, device: device)

    return teapot
}

(Swift 3.0 XCode 8 Beta 6)

, .

:

Holy shi ---

Whelp, , :

//// Buffers
renderPass.setVertexBuffer(mesh.vertexBuffers[0].buffer, offset: 0, at: 0)
renderPass.setVertexBuffer(uniformBuffer, at: 1)

let submesh   = mesh.submeshes[0]
let indexSize = submesh.indexType == .uInt32 ? 4 : 2

//// Draw Indices
renderPass.drawIndexedPrimitives(submesh.primitiveType,
                                 indexCount:  submesh.indexBuffer.length / indexSize,
                                 indexType:   submesh.indexType,
                                 indexBuffer: submesh.indexBuffer.buffer,
                                 indexBufferOffset: 0)

let indexSize = submesh.indexType == .uInt32 ? 4 : 2, 32 : 16 , .length , , .

, Obj Metal, : , , .

+3

Source: https://habr.com/ru/post/1622491/


All Articles