场景套件中的彩色线?

时间:2015-02-07 20:59:06

标签: ios scenekit

我使用以下代码在两个节点之间绘制一条线:

class func lineBetweenNodeA(nodeA: SCNNode, nodeB: SCNNode) -> SCNNode {
    let positions: [Float32] = [nodeA.position.x, nodeA.position.y, nodeA.position.z, nodeB.position.x, nodeB.position.y, nodeB.position.z]
    let positionData = NSData(bytes: positions, length: sizeof(Float32)*positions.count)
    let indices: [Int32] = [0, 1]
    let indexData = NSData(bytes: indices, length: sizeof(Int32) * indices.count)
    let source = SCNGeometrySource(data: positionData, semantic: SCNGeometrySourceSemanticVertex, vectorCount: indices.count, floatComponents: true, componentsPerVector: 3, bytesPerComponent: sizeof(Float32), dataOffset: 0, dataStride: sizeof(Float32) * 3)
    let element = SCNGeometryElement(data: indexData, primitiveType: SCNGeometryPrimitiveType.Line, primitiveCount: indices.count, bytesPerIndex: sizeof(Int32))
    let line = SCNGeometry(sources: [source], elements: [element])
    line.firstMaterial?.lightingModelName = SCNLightingModelConstant
    line.firstMaterial?.emission.contents = UIColor.orangeColor()
    return SCNNode(geometry: line)
}

我希望能够在调用此函数时传递颜色,以便相应地更改颜色...

如何指定绘制线条的颜色?

我编写了适合我的代码。我使用发射属性而不是漫反射,我使用恒定光...

1 个答案:

答案 0 :(得分:6)

素材的lightingModelName默认为SCNLightingModelBlinn。使用此照明模型,漫反射材质属性使用如下:

color = ... + diffuse * max(0, dot(N, L)) + ...

但由于几何体没有法线,diffuse总是乘以0.

您可能希望使用SCNLightingModelConstant灯光模型或使用emission材质属性而不是diffuse