我试图弄清楚如何以编程方式为 ARKit(XCode 9 / Swift 4)场景中的某个对象(SCNNode)分配材质。我正在尝试以编程方式执行此操作,因为我希望使用太多变体(或用户生成的图像)来渲染相同形状的对象,以便能够通过场景中的菜单分配来执行此操作。该对象只是一个立方体 - 现在,我只是试图让一侧显示从 Assets 文件夹中提取的材料。
这是我尝试引用以前的 Stack 帖子的当前代码,但对象只是保持白色。
let material = SCNMaterial()
material.diffuse.contents = UIImage(named: "texture.jpg")
let nodeObject = self.lastUsedObject?.childNode(withName: "box", recursively: true)
// I believed this lets me grab the last thing I rendered in an ARKit scene - please
// correct me if I'm wrong. My object is also labeled "box".
nodeObject?.geometry?.materials
nodeObject?.geometry?.materials[0] = material // I wanted to grab the first face of the box
Run Code Online (Sandbox Code Playgroud)
非常感谢您!我一直在摆弄这个一段时间,但我似乎无法掌握 Swift 中 3D 对象/场景的编程方法。