我使用以下代码创建了自定义SCNGeometry:
let data = NSData(bytes: position, length: MemoryLayout<float3>.size * self.objectMesh.pointCount)
let vertexSource = SCNGeometrySource(data: data as Data,
semantic: .vertex,
vectorCount: self.objectMesh.pointCount,
usesFloatComponents: true,
componentsPerVector: 3,
bytesPerComponent: MemoryLayout<Float32>.size,
dataOffset: 0,
dataStride: MemoryLayout<float3>.size)
let data2 = NSData(bytes: uvs, length: MemoryLayout<float2>.size * self.objectMesh.pointCount)
let tSource = SCNGeometrySource(data: data2 as Data,
semantic: .texcoord,
vectorCount: self.objectMesh.pointCount,
usesFloatComponents: true,
componentsPerVector: 2,
bytesPerComponent: MemoryLayout<Float32>.size,
dataOffset: 0,
dataStride: MemoryLayout<float2>.stride)
let indexData = NSData(bytes: faces, length: MemoryLayout<Int32>.size * self.objectMesh.faceCount)
let element = SCNGeometryElement(
data: indexData as Data,
primitiveType: .triangles,
primitiveCount: self.objectMesh.faceCount/3,
bytesPerIndex: MemoryLayout<Int32>.size)
let Geometry = SCNGeometry(sources: [vertexSource, tSource], elements: [element])
Geometry.firstMaterial?.diffuse.contents = self.MainTexture
Geometry.firstMaterial?.normal.contents = self.NormalTexture
现在的问题是:纹理显示但普通纹理贴图未应用于几何体。我不会使用每个顶点法线。当应用于例如SCNPlane几何体时,此纹理和法线纹理是有效的。但我现在不知道如何在我的自定义几何体中使用法线贴图纹理。
答案 0 :(得分:0)
SCNMaterial上的普通属性需要切线空间中的法线贴图,然后有效法线基于法线贴图和自定义几何体的顶点法线的组合。
模型i / o包含MDLMesh,它提供addNormals方法,可用于以编程方式创建法线(基于SCNGeometry创建MDLMesh,使用addNormals,并基于MDLMesh创建SCNGeometry并使用它取而代之的是新的SCNGeometry。但理想情况下,您可以使用自定义几何体包含法线。