如何在SceneKit中使用包括Alpha值的颜色正确渲染点?

时间:2019-05-23 16:53:39

标签: swift macos scenekit metal

我正在尝试在SceneKit中渲染点的集合,这些点的颜色的一部分都具有小于1的alpha值。尽管这似乎可行,但这取决于您在看该点的角度。

我当前的方法是创建一个具有相应颜色和索引的顶点数组,将其包装在SCNGeometrySource / SCNGeometryElement中,并根据这些内容创建自定义SCNGeometry。索引的SCNGeometryElement设置为呈现为点。下面的一些示例代码说明了我的设置方法。

var vertices = [SCNVector3]()
var colors = [float4]()
var indices = [UInt32]()

for x in 0...10 {
    for y in 0...10 {
        for z in 0...10 {
            vertices.append(SCNVector3(x - 5, y - 5, z - 5))
            colors.append(float4(0.7, 0.7, 0.7, 0.1))
            indices.append(UInt32(vertices.count - 1))
        }
    }
}

let vertexSource = SCNGeometrySource(vertices: vertices)

let colorsData = Data(bytes: &colors, count: colors.count * MemoryLayout<float4>.stride)
let colorSource = SCNGeometrySource(data: colorsData, semantic: .color, vectorCount: colors.count, usesFloatComponents: true, componentsPerVector: 4, bytesPerComponent: MemoryLayout<Float>.stride, dataOffset: 0, dataStride: MemoryLayout<float4>.stride)

let pointsElement = SCNGeometryElement(indices: indices, primitiveType: .point)
pointsElement.pointSize = 0.2
pointsElement.minimumPointScreenSpaceRadius = 1
pointsElement.maximumPointScreenSpaceRadius = 50

let geometry = SCNGeometry(sources: [vertexSource, colorSource], elements: [pointsElement])
let node = SCNNode(geometry: geometry)

scene.rootNode.addChildNode(node)

当我的相机节点位于以下位置时,一切看起来都很好:

cameraNode.eulerAngles = SCNVector3(0, 0.5 * CGFloat.pi, 0)
cameraNode.position = SCNVector3(x: 10, y: 0, z: 0)

Expected rendering

但是,如果我将相机顺时针移动到立方体的下一个面,则alpha将不再渲染:

cameraNode.eulerAngles = SCNVector3(0, -0.5 * CGFloat.pi, 0)
cameraNode.position = SCNVector3(x: -10, y: 0, z: 0)

Undesired rendering

将相机放置在一半位置可将两者结合在一起:

cameraNode.eulerAngles = SCNVector3(0, 0, 0)
cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)

Semi-expected rendering

0 个答案:

没有答案