Search code examples
iosswiftscenekit

Color in SCNNodes


I am trying to set the color of a SCNNode to a custom RGBA Color however when I try to the box will end up white:

let box = SCNBox(width: 4, height: 1, length: 4, chamferRadius: 0)
    let boxNode = SCNNode(geometry: box)
    myScene.rootNode.addChildNode(boxNode)

    boxNode.castsShadow = true


    box.firstMaterial?.diffuse.contents  = UIColor(red: 30, green: 150, blue: 30, alpha: 1)

This makes the box white however doing something like this works:

box.firstMaterial?.diffuse.contents  = UIColor.greenColor()

How can I make the box have a custom RGBA color?

-Thanks


Solution

  • The values passed to the UIColor initializer need to be between 0 and 1. You should divide your rgb values by 255.

    box.firstMaterial?.diffuse.contents  = UIColor(red: 30.0 / 255.0, green: 150.0 / 255.0, blue: 30.0 / 255.0, alpha: 1)