有没有办法以编程方式更改在 Reality Composer 中创建的实体的 material?
Is there a way to programmatically change the material of an Entity that was created in Reality Composer?
我想在 Reality Composer 中创建实体后以编程方式更改实体的颜色。
由于 Reality Composer 不会创建 ModelEntity(它会创建通用实体),因此我似乎无权更改其颜色。当我对 ModelEntity 进行类型转换时,我现在可以访问 ModelComponent 材料。但是,当我尝试将其添加到场景中时,出现 Thread 1: signal SIGABART 错误。无法将类型 'RealityKit.Entity' (0x1fcebe6e8) 的值转换为 'RealityKit.ModelEntity' (0x1fceba970)。下面的示例代码。
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
// Load the "Box" scene from the "Experience" Reality File
let boxAnchor = try! Experience.loadBox()
// Typecast Steelbox as ModelEntity to change its color
let boxModelEntity = boxAnchor.steelBox as! ModelEntity
// Remove materials and create new material
boxModelEntity.model?.materials.removeAll()
let blueMaterial = SimpleMaterial(color: .blue, isMetallic: false)
boxModelEntity.model?.materials.append(blueMaterial)
// Add the box anchor to the scene
arView.scene.anchors.append(boxAnchor)
}
}
模型实体在 RealityKit 的层次结构中存储得更深,正如你所说,它是 Entity
,而不是 ModelEntity
。所以使用向下转换访问 mesh
和 materials
:
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let boxScene = try! Experience.loadBox()
print(boxScene)
let modelEntity = boxScene.steelBox?.children[0] as! ModelEntity
let material = SimpleMaterial(color: .green, isMetallic: false)
modelEntity.model?.materials = [material]
let anchor = AnchorEntity()
anchor.scale = [5,5,5]
modelEntity.setParent(anchor)
arView.scene.anchors.append(anchor)
}
}
我想在 Reality Composer 中创建实体后以编程方式更改实体的颜色。
由于 Reality Composer 不会创建 ModelEntity(它会创建通用实体),因此我似乎无权更改其颜色。当我对 ModelEntity 进行类型转换时,我现在可以访问 ModelComponent 材料。但是,当我尝试将其添加到场景中时,出现 Thread 1: signal SIGABART 错误。无法将类型 'RealityKit.Entity' (0x1fcebe6e8) 的值转换为 'RealityKit.ModelEntity' (0x1fceba970)。下面的示例代码。
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
// Load the "Box" scene from the "Experience" Reality File
let boxAnchor = try! Experience.loadBox()
// Typecast Steelbox as ModelEntity to change its color
let boxModelEntity = boxAnchor.steelBox as! ModelEntity
// Remove materials and create new material
boxModelEntity.model?.materials.removeAll()
let blueMaterial = SimpleMaterial(color: .blue, isMetallic: false)
boxModelEntity.model?.materials.append(blueMaterial)
// Add the box anchor to the scene
arView.scene.anchors.append(boxAnchor)
}
}
模型实体在 RealityKit 的层次结构中存储得更深,正如你所说,它是 Entity
,而不是 ModelEntity
。所以使用向下转换访问 mesh
和 materials
:
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let boxScene = try! Experience.loadBox()
print(boxScene)
let modelEntity = boxScene.steelBox?.children[0] as! ModelEntity
let material = SimpleMaterial(color: .green, isMetallic: false)
modelEntity.model?.materials = [material]
let anchor = AnchorEntity()
anchor.scale = [5,5,5]
modelEntity.setParent(anchor)
arView.scene.anchors.append(anchor)
}
}