1

When I try to give the geometry a material with the diffuse.contents of red, I get a white object instead.

Here is the code I have used to create the node:

let geo = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.1)
let mat = SCNMaterial()
mat.diffuse.contents = Color.red
mat.diffuse.intensity = 1
geo.materials = [mat]
                
let node = SCNNode(geometry: geo)
SomeClass.scene!.rootNode.addChildNode(node)

And here is the code I have used to create the SceneView

         SceneView(scene: SomeClass.scene, 
             pointOfView: nil, 
                 options: [.allowsCameraControl, .autoenablesDefaultLighting], 
preferredFramesPerSecond: 60, 
        antialiasingMode: .multisampling2X, 
                delegate: sceneDelegate, 
               technique: nil)

SomeClass is just a basic class that holds the scene property. I have made this class for a reason I don't know.

I have tried to set the diffuse.contents to a CGColor instead of a "normal" Color and did some research on the internet, but couldn't find anybody with a similar issue.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Michaelll
  • 37
  • 6

1 Answers1

1

According to Apple documentation you should use UIColor or NSColor instance:

material.diffuse.contents = UIColor.red

...or CGColor instance:

material.diffuse.contents = UIColor.red.cgColor

Here's the code:

import SwiftUI
import SceneKit

struct ContentView: View {
    
    @State private var scene = SCNScene()
    let options: SceneView.Options = [.allowsCameraControl, 
                                      .autoenablesDefaultLighting]
    
    var body: some View {
        SceneView(scene: scene, options: [options]).ignoresSafeArea()
        self.loadModel()
    }
    
    func loadModel() -> EmptyView {
        scene.background.contents = UIColor.black
        
        let geometry = SCNBox(width: 1, height: 1, length: 1, 
                                                   chamferRadius: 0.1)
        let material = SCNMaterial()
        material.diffuse.contents = UIColor.red
        geometry.materials = [material]
                        
        let node = SCNNode(geometry: geometry)
        scene.rootNode.addChildNode(node)
        
        return EmptyView()
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220