当我尝试为几何体赋予漫反射内容为红色的材质时,我得到的是一个白色物体。
这是我用来创建节点的代码:
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)
这是我用来创建SceneView
的代码 SceneView(scene: SomeClass.scene,
pointOfView: nil,
options: [.allowsCameraControl, .autoenablesDefaultLighting],
preferredFramesPerSecond: 60,
antialiasingMode: .multisampling2X,
delegate: sceneDelegate,
technique: nil)
SomeClass
只是一个包含场景属性的基本类。我上这门课是出于一个我不知道的原因。
我试图将
diffuse.contents
设置为 CGColor 而不是“正常”颜色,并在互联网上做了一些研究,但找不到有类似问题的人。
According to Apple documentation 你应该使用 UIColor 的(NSColor 的)实例:
material.diffuse.contents = UIColor.red
代码如下:
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()
}
}