我的目标:给定一些 indexi
和 some UIColor
c
,使该索引处的脸变成该颜色。
let vertices = // all of my vertices in the format of SCNVector3
let vertexSource = SCNGeometrySource(data: verticesData,
semantic: .vertex,
vectorCount: vertices.count,
usesFloatComponents: true,
componentsPerVector: 3,
bytesPerComponent: MemoryLayout<Float>.size,
dataOffset: 0,
dataStride: MemoryLayout<SCNVector3>.size)
var colors: [SCNVector3] = vertices.map { SCNVector3(1, 1, 1) } // Make it all white color at first
colors[10] = SCNVector3(1, 0, 0) // Pick one at random and make it red
let colorSource = SCNGeometrySource(data: NSData(bytes: colors, length: MemoryLayout<SCNVector3>.size * colors.count) as Data,
semantic: .color,
vectorCount: colors.count,
usesFloatComponents: true,
componentsPerVector: 3,
bytesPerComponent: MemoryLayout<Float>.size,
dataOffset: 0,
dataStride: MemoryLayout<SCNVector3>.size)
let elements = SCNGeometryElement(data: NSData(bytes: indices, length: MemoryLayout<Int32>.size * indices.count) as Data,
primitiveType: .polygon,
primitiveCount: indices.count / 4,
bytesPerIndex: MemoryLayout<Int32>.size)
let g = SCNGeometry(sources: [vertexSource, colorSource], elements: [element])
我的 3D 对象渲染正确。正如预期的那样,它全是白色。然而,那张红脸并没有出现。我看到一丝红色,但看起来 SceneKit 正在尝试在顶点周围着色,而不是那些顶点形成的面。如何强制它只为这些顶点创建的面/多边形着色?