SceneKit - 将 material 替换为节点数组

SceneKit - Replace material to an array of nodes

目标:将默认 material 添加到 Scenekit 场景中的所有子节点。

我做了什么:

// get child noses from scene
let allNodes = scene.rootNode.childNodes

//create a defaul material
let defaultMaterial = SCNMaterial()  
defaultMaterial.diffuse.contents = NSColor.red
         
//add defaul material to all child nodes
allChildNodes.replaceMaterial(at: 0, with: defaultMaterial)

问题:

看起来“replaceMaterial”不适用于节点数组。

问题:

如何将 material 添加到节点数组?

试试这个代码:

import SceneKit

class GameViewController: NSViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let sceneView = self.view as! SCNView
        let scene = SCNScene(named: "art.scnassets/scene.scn")!
        sceneView.scene = scene
        
        let material = SCNMaterial()
        material.diffuse.contents = NSColor.systemYellow
        
        let allNodes = scene.rootNode.childNodes[0].childNodes
                
        for i in 0...(allNodes.count-1) {
            allNodes[i].geometry?.firstMaterial = material
        }
    }
}