gpt4 book ai didi

swift - RealityKit 在运行时更改 USDZ 漫反射 Material

转载 作者:行者123 更新时间:2023-12-04 16:40:09 25 4
gpt4 key购买 nike

我正在 Swift 中使用 RealityKit 加载 USDZ 模型。模型加载良好,具有纹理。

但是,我无法更改 Material 的漫反射属性, Material 总是返回 AnyMaterial .

我可以用 SimpleMaterial 完全覆盖 Material 或者我可以在运行应用程序之前更改 USDZ 漫反射颜色,但在运行时似乎无法做到这一点,有什么想法吗?

我只想能够改变一个属性

/*
Find an entity within the parent, and find the material, test to try and change it's material
**/
private func updateColor(entity: Entity) {

guard
let found = entity.findEntity(named: "MyItem"),
var modelComponent = found.components[ModelComponent] as? ModelComponent,
let existingMaterial = modelComponent.materials.first else {
return
}

// This is `AnyMaterial` - no diffuse property
// What can I use here to change the diffuse?
modelComponent.materials = [existingMaterial]

// Change to yellow for testing
//var material = SimpleMaterial()
//material.baseColor = .color(.yellow)
//modelComponent.materials = [material]

found.components.set(modelComponent)
}

最佳答案

在 RealityKit 1.0 中,您可以在运行时更改 Material ,但不能 animate它的参数。
这是一个代码:

class ViewController: UIViewController {

@IBOutlet var arView: ARView!

override func viewDidLoad() {
super.viewDidLoad()

let modelAnchor = try! Experience.loadBox()

modelAnchor.steelBox!.scale = [10,10,10]
let entity: Entity = modelAnchor.steelBox!.children[0]
arView.scene.anchors.append(modelAnchor)

DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {

var material = SimpleMaterial()
material.baseColor = try! .texture(.load(named: "texture.jpg"))

var component: ModelComponent = entity.components[ModelComponent]!.self
component.materials = [material]
entity.components.set(component)
}
}
}

在 iOS 15 的 RealityKit 2.0 中,您可以稍微不同地设置 Material 的颜色和纹理:
var material = SimpleMaterial()

material.color = .init(tint: .green, texture: nil)

关于swift - RealityKit 在运行时更改 USDZ 漫反射 Material ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62375385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com