How to use a color SCNGeometrySource for a point cloud geometry in SceneKit?

Hello!


I'm building a visualizer for captured mesh and point clouds. I'm trying to color-code the vertices of my point cloud but they all appear dark grey or white depending on how I configure lights in my scene. What is the simplest way to color my vertices?


//: A SceneKit based Playground

import PlaygroundSupport
import SceneKit

func makePointCloud() -> SCNGeometry {
    let vertices : [SCNVector3] = [SCNVector3(x: -0.5, y: 0.0, z: -0.5),
                                   SCNVector3(x: -0.5, y: 0.0, z: 0.5),
                                   SCNVector3(x: 0.5, y: 0.0, z: -0.5),
                                   SCNVector3(x: 0.5, y: 0.0, z: 0.5),
                                   SCNVector3(x: -0.5, y: 1.0, z: -0.5),
                                   SCNVector3(x: -0.5, y: 1.0, z: 0.5),
                                   SCNVector3(x: 0.5, y: 1.0, z: -0.5),
                                   SCNVector3(x: 0.5, y: 1.0, z: 0.5)]
    let vertexSource = SCNGeometrySource(vertices: vertices)
    
    let colors : [UInt8] = [255,0,0,255,
                            255,0,0,255,
                            255,0,0,255,
                            255,0,0,255,
                            255,0,0,255,
                            255,0,0,255,
                            255,0,0,255,
                            255,0,0,255]
    let colorData = Data(bytes: colors)
    let colorSource = SCNGeometrySource(data: colorData,
                                        semantic: .color,
                                        vectorCount: colors.count,
                                        usesFloatComponents: false,
                                        componentsPerVector: 4,
                                        bytesPerComponent: 1,
                                        dataOffset: 0,
                                        dataStride: 4)
    
    let indices : [UInt8] = Array(0..<8)
    let pointCloudElement = SCNGeometryElement(indices: indices,
                                               primitiveType: .point)
    pointCloudElement.pointSize = 0.01
    pointCloudElement.minimumPointScreenSpaceRadius = 2
    pointCloudElement.maximumPointScreenSpaceRadius = 10
    let geometry = SCNGeometry(sources: [vertexSource, colorSource],
                               elements: [pointCloudElement])
    
    let material = SCNMaterial()
    material.diffuse.contents = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    geometry.firstMaterial = material
    
    return geometry
}

let scene = SCNScene()
let geometry = makePointCloud()
let pointCloudNode = SCNNode()
pointCloudNode.geometry = geometry
scene.rootNode.addChildNode(pointCloudNode)

let sceneView = SCNView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
sceneView.scene = scene
sceneView.autoenablesDefaultLighting = true
sceneView.allowsCameraControl = true
sceneView.showsStatistics = true
sceneView.backgroundColor = #colorLiteral(red: 0.9487829359, green: 0.9487829359, blue: 0.9487829359, alpha: 1)


PlaygroundPage.current.liveView = sceneView
PlaygroundPage.current.needsIndefiniteExecution = true

Hello,


Setting your geometry's first material's lighting model to constant should fix your issue

How to use a color SCNGeometrySource for a point cloud geometry in SceneKit?
 
 
Q