我使用 USDZ 檔案使用 SceneKit 創建了一個視圖,如何直接在 SwiftUI 代碼中將特定材質的顏色從灰色更改為其他顏色?
var scene = SCNScene(named: "2020_BMW_M5.usdz")
var body: some View {
VStack {
SceneView(scene: scene,
options: [.autoenablesDefaultLighting, .allowsCameraControl])
}
}
uj5u.com熱心網友回復:
使用下標方法 ( .childNodes[n].childNodes[n].childNodes[n]
) 以獲取所需層次結構中的模型,然后像往常一樣更改其顏色:
import SwiftUI
import SceneKit
struct ContentView : View {
var body: some View {
return SceneKitViewContainer().ignoresSafeArea()
}
}
struct SceneKitViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> SCNView {
let scnView = SCNView(frame: .zero)
scnView.allowsCameraControl = true
scnView.autoenablesDefaultLighting = true
scnView.scene = SCNScene(named: "Model.usdz")
let model = scnView.scene?.rootNode.childNode(withName: "SomeModel",
recursively: true)
model?.childNodes[1].geometry?.firstMaterial?.diffuse.contents =
UIColor.red
print(model?.childNodes[1])
return scnView
}
func updateUIView(_ uiView: SCNView, context: Context) { }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486953.html