自定义SCNGeometry不显示漫反射内容作为纹理

时间:2018-02-11 03:42:18

标签: ios scenekit scnnode scnmaterial scngeometry

我正在创建一个自定义SCNGeometry。首先,我将它发展为平面。它显示,我可以为其'漫反射内容应用颜色,但如果我尝试应用任何内容,如UIImage或CALayer,它将显示为白色。

代码:

let positions = [
    SCNVector3(-1, 0, 0),
    SCNVector3(1, 0, 0),
    SCNVector3(-1, 0, -1),
    SCNVector3(1, 0, -1)
]

let indices: [UInt8] = [
    0, 1, 2,
    1, 3, 2
]

let vertexSource = SCNGeometrySource(vertices: positions)

let indexData = Data(bytes: indices, count: indices.count)

let element = SCNGeometryElement(
    data: indexData,
    primitiveType: SCNGeometryPrimitiveType.triangles,
    primitiveCount: indices.count / 3,
    bytesPerIndex: 1)

let geometry = SCNGeometry(
    sources: [vertexSource],
    elements: [element])

geometry.firstMaterial?.diffuse.contents = UIImage(named: "homer")

1 个答案:

答案 0 :(得分:2)

为了能够纹理几何体,您还需要提供UV坐标(纹理坐标)源。

let textureCoordinates = [
    CGPoint(x: 0, y: 0),
    CGPoint(x: 1, y: 0),
    CGPoint(x: 0, y: 1),
    CGPoint(x: 1, y: 1)
]

let uvSource = SCNGeometrySource(textureCoordinates: textureCoordinates)

let element = ...

let geometry = SCNGeometry(
    sources: [vertexSource, uvSource],
    elements: [element])