zumzum
zumzum

Reputation: 20248

Color line in scene kit?

I am using the following code to draw a line between two nodes:

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)
}

I want to be able to pass in a color when I call this function so that it changes color accordingly...

How can I specify the color for the line drawn?

I edited the code to what works for me. I used the emission property instead of diffuse and I used constant light...

Upvotes: 4

Views: 1338

Answers (1)

mnuages
mnuages

Reputation: 13462

a material's lightingModelName defaults to SCNLightingModelBlinn. With this lighting model the diffuse material property is used as follows:

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

but since your geometry does not have normals, diffuse is always multiplied by 0.

You might want to use the SCNLightingModelConstant lighting model or use the emission material property instead of diffuse.

Upvotes: 6

Related Questions