RealityKit – How to add motion to a loaded ModelEntity from USDZ file?(RealityKit-如何从USDZ文件向加载的ModelEntity添加运动?)
本文介绍了RealityKit-如何从USDZ文件向加载的ModelEntity添加运动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已成功将USDZ文件加载到我的场景中。现在我想添加ModelEntity运动。我已添加了PhysicsMotionComponent,但它不起作用。加载模型后,它像往常一样是静态的。没有动静。如何在RealityKit中给出实体运动?
当我看到组件时,我看到组件已添加。但实体是不动的。我做错了什么?
我的代码:
import SwiftUI
import RealityKit
import ARKit
import Combine
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arVIew = ARView(frame: .zero)
context.coordinator.arVIew = arVIew
arVIew.session.delegate = context.coordinator
let config = ARWorldTrackingConfiguration()
arVIew.session.run(config, options: [.resetTracking,.removeExistingAnchors])
let anchorEntity = AnchorEntity()
loadASingleModel(name:"toy_biplane") { entity in
if let entity = entity {
let kinematics: PhysicsBodyComponent = .init(massProperties: .default,
material: nil,
mode: .kinematic)
let motion: PhysicsMotionComponent = .init(linearVelocity: [0.1 ,0, 0],
angularVelocity: [3, 3, 3])
entity.components.set(kinematics)
entity.components.set(motion)
anchorEntity.addChild(entity)
//anchorEntity.transform.matrix.columns.3.z = -1.0
arVIew.scene.anchors.append(anchorEntity)
print("Model Added: ",entity.name)
}else{
print("No entity avaible")
}
}
return arVIew
}
// MARK: - AsychLoading working
func loadASingleModel(name:String,completion:@escaping (_ model:ModelEntity?)->Void){
var cancellable:AnyCancellable?
cancellable = Entity.loadModelAsync(named:name)
.sink(receiveCompletion: { handler in
if case let .failure(error) = handler {
print("Unable to load a model due to error (error)")
}
cancellable?.cancel()
completion(nil)
}, receiveValue: { [self] (model: ModelEntity?) in
if let model = model {
cancellable?.cancel()
print("Congrats! Model is successfully loaded!")
completion(model)
}
})
}
func updateUIView(_ uiView: ARView, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(arViewContainer: self)
}
class Coordinator:NSObject{
var parent:ARViewContainer
var arVIew:ARView?
init(arViewContainer:ARViewContainer){
parent = arViewContainer
}
}
}
extension ARViewContainer.Coordinator:ARSessionDelegate{
func session(_ session: ARSession, didUpdate frame: ARFrame) {
}
}
推荐答案
为每个参与者(实体)添加.generateCollisionShapes(recursive:)
实例方法,不仅可以创建碰撞的形状,还允许您模拟物理。
import SwiftUI
import RealityKit
struct ARViewContainer: UIViewRepresentable {
let boxx = ModelEntity(mesh: .generateBox(size: 0.5))
let ball = ModelEntity(mesh: .generateSphere(radius: 0.25))
let anchor = AnchorEntity()
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
// BALL
ball.physicsBody = .init()
ball.physicsMotion = .init()
ball.physicsMotion?.linearVelocity = [10,0,0]
ball.position.x = -3
ball.generateCollisionShapes(recursive: true)
anchor.addChild(ball)
// BOX
boxx.physicsBody = .init()
boxx.physicsMotion = .init()
boxx.physicsMotion?.linearVelocity = [0,2,0]
boxx.generateCollisionShapes(recursive: true)
anchor.addChild(boxx)
// Anchor
anchor.position.z = -3
arView.scene.addAnchor(anchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) { }
}
...
struct ContentView : View {
var body: some View {
return ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
有关更多详细信息,请阅读THIS帖子。
这篇关于RealityKit-如何从USDZ文件向加载的ModelEntity添加运动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:RealityKit-如何从USDZ文件向加载的ModelEntity添加运动?
基础教程推荐
猜你喜欢
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01