【问题标题】:Color line in scene kit?场景套件中的色线?
【发布时间】:2015-04-07 20:47:43
【问题描述】:

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

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

我希望能够在调用此函数时传入一种颜色,以便它相应地改变颜色...

如何指定画线的颜色?

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

【问题讨论】:

  • 您还没有指定颜色吗?我在您发布的代码中看到了一些关于 orangeColor 的信息。
  • 它看起来不起作用。这条线总是黑色的

标签: ios scenekit


【解决方案1】:

材质的lightingModelName 默认为SCNLightingModelBlinn。在这个光照模型中,漫反射材质属性的使用如下:

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

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

您可能想要使用SCNLightingModelConstant 照明模型或使用emission 材质属性而不是diffuse

【讨论】:

  • 我尝试使用发射属性而不是漫反射,但它仍然是黑色的。
  • 好的,所以我认为使用光照常数是可行的,虽然很难看到线条的颜色,因为它真的很细......我这样做了:line.firstMaterial?.lightingModelName = SCNLightingModelConstant
猜你喜欢
  • 2012-08-14
  • 1970-01-01
  • 2015-08-14
  • 2019-11-08
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
相关资源
最近更新 更多