swift - How to correctly render points in SceneKit with colours including alpha values?
问题描述
I am trying to render a collection of points in SceneKit all of which have an alpha value less than 1 as part of their colour. While this seems to work, it depends on the angle at which you are looking at the point.
My current method has been to create an array of vertices with corresponding colours and indices, wrap these up inside SCNGeometrySource/SCNGeometryElement and create a custom SCNGeometry from theses. The SCNGeometryElement for the indices is set to render as points. Some sample code below of how I'm setting this up.
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)
When my camera node is located as follow, everything looks good:
cameraNode.eulerAngles = SCNVector3(0, 0.5 * CGFloat.pi, 0)
cameraNode.position = SCNVector3(x: 10, y: 0, z: 0)
However if I move my camera to the next face of the cube clockwise the alpha no longer renders:
cameraNode.eulerAngles = SCNVector3(0, -0.5 * CGFloat.pi, 0)
cameraNode.position = SCNVector3(x: -10, y: 0, z: 0)
Placing the camera half way gives a combination of the two:
cameraNode.eulerAngles = SCNVector3(0, 0, 0)
cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
解决方案
推荐阅读
- java - 使用java中的子例程按升序对数组进行排序
- r - R:ggsurvplot 彩色图例标签(文本)
- angular - Angular 9.1.4 版导入错误,错误 TS2314:
- reactjs - 也是函数的参数 - VSCode 中的语义语法高亮
- javascript - Javascript 函数转到定义仅在 VSCode 上显示“任何”
- javascript - 为什么 Intl 和 Reflect 的默认字符串表示为 `[object Object]`?
- android - 当 SimpleCursorAdapter 将值从表添加到 listView 时检查列值
- mysql - 使用另一个表中的值创建表
- android - 使用偏好时应用程序不断靠近
- cytoscape - Cytoscape(桌面应用程序):选择所有通过边缘连接的具有给定属性的相邻节点