Hi everyone,
I have been generating a simple model using my own vertex data (4 vertices and 2 triangles, 6 indices to describe a sheet) and then I have introduced a light source based on material from chapter 5. However I am running into an issue which I think it may have to do with surface normals. As you can see in the following picture lighting does not look right at the joint line of the two triangles. Any tips would be appreciated particularly regarding the addNormal method of Model IO.
Thank you
Kaveh
Here is how I generate my model (basically it has 4 vertices and 6 indices):
let vertexData: [float3] = [
float3(-0.250, 0.750, 0.50),
float3(-0.250, -0.750, 0.50),
float3(0.250, -0.750, 0.50),
float3( 0.250, 0.750, 0.50)
]
let indices: [UInt16] = [
0,1,2,
2,3,0
]
let mdlMeshVertexBuffer = allocator.newBuffer(MemoryLayout<float3>.stride * vertexData.count, type: .vertex)
let vertexMap = mdlMeshVertexBuffer.map()
vertexMap.bytes.assumingMemoryBound(to: float3.self).assign(from: vertexData, count: vertexData.count)
let indexBuffer = allocator.newBuffer(MemoryLayout<UInt16>.stride * indices.count, type: .index)
let indexMap = indexBuffer.map()
indexMap.bytes.assumingMemoryBound(to: UInt16.self).assign(from: indices, count: indices.count)
let scatteringFunction = MDLScatteringFunction()
let material = MDLMaterial(name: "baseMaterial", scatteringFunction: scatteringFunction)
//material.setTextures(textures: [.baseColor:"gray_material.jpg"])
let submesh = MDLSubmesh(indexBuffer: indexBuffer,
indexCount: indices.count,
indexType: .uInt16,
geometryType: .triangles,
material: material)
let vertexDescriptor = MDLVertexDescriptor()
vertexDescriptor.attributes[0] = MDLVertexAttribute(name: MDLVertexAttributePosition,
format: .float3,
offset: 0,
bufferIndex: 0)
vertexDescriptor.layouts[0] = MDLVertexBufferLayout(stride: MemoryLayout<float3>.stride)
let mdlMesh = MDLMesh(vertexBuffer: mdlMeshVertexBuffer,
vertexCount: vertexData.count,
descriptor: vertexDescriptor,
submeshes: [submesh])
mdlMesh.addNormals(withAttributeNamed: MDLVertexAttributeNormal, creaseThreshold: 0.99)