SCNNodes 中的颜色
Color in SCNNodes
我正在尝试将 SCNNode 的颜色设置为自定义 RGBA 颜色,但是当我尝试设置时,方框最终会变成白色:
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)
这会使盒子变白,但是这样做是可行的:
box.firstMaterial?.diffuse.contents = UIColor.greenColor()
如何使盒子具有自定义 RGBA 颜色?
-谢谢
传递给 UIColor
初始化程序的值需要介于 0 和 1 之间。您应该将 rgb 值除以 255。
box.firstMaterial?.diffuse.contents = UIColor(red: 30.0 / 255.0, green: 150.0 / 255.0, blue: 30.0 / 255.0, alpha: 1)
为方便起见,您还可以添加一个 UIColor 扩展
extension UIColor {
convenience init(red: UInt, green: UInt, blue: UInt, alpha: UInt = 0xFF) {
self.init(
red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: CGFloat(alpha) / 255.0
)
}
}
然后您可以按如下方式使用它:
box.firstMaterial?.diffuse.contents = UIColor(red: 30, green: 150, blue: 30, alpha: 1.0)
或者,如果您希望使用十六进制值,请添加下一个 UIColor 扩展名
extension UIColor {
convenience init(argb: UInt) {
self.init(
red: CGFloat((argb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((argb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(argb & 0x0000FF) / 255.0,
alpha: CGFloat((argb & 0xFF000000) >> 24) / 255.0
)
}
}
并按如下方式使用它:
box.firstMaterial?.diffuse.contents = UIColor(argb: 0xFF1B98F5)
快乐编码
我正在尝试将 SCNNode 的颜色设置为自定义 RGBA 颜色,但是当我尝试设置时,方框最终会变成白色:
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)
这会使盒子变白,但是这样做是可行的:
box.firstMaterial?.diffuse.contents = UIColor.greenColor()
如何使盒子具有自定义 RGBA 颜色?
-谢谢
传递给 UIColor
初始化程序的值需要介于 0 和 1 之间。您应该将 rgb 值除以 255。
box.firstMaterial?.diffuse.contents = UIColor(red: 30.0 / 255.0, green: 150.0 / 255.0, blue: 30.0 / 255.0, alpha: 1)
为方便起见,您还可以添加一个 UIColor 扩展
extension UIColor {
convenience init(red: UInt, green: UInt, blue: UInt, alpha: UInt = 0xFF) {
self.init(
red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: CGFloat(alpha) / 255.0
)
}
}
然后您可以按如下方式使用它:
box.firstMaterial?.diffuse.contents = UIColor(red: 30, green: 150, blue: 30, alpha: 1.0)
或者,如果您希望使用十六进制值,请添加下一个 UIColor 扩展名
extension UIColor {
convenience init(argb: UInt) {
self.init(
red: CGFloat((argb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((argb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(argb & 0x0000FF) / 255.0,
alpha: CGFloat((argb & 0xFF000000) >> 24) / 255.0
)
}
}
并按如下方式使用它:
box.firstMaterial?.diffuse.contents = UIColor(argb: 0xFF1B98F5)
快乐编码