如何解决AR对象在RealityKit中无法正确锚定或调整大小
我有一个AR场景,其中有两个对象,一头棕色母牛,一头黑色母牛。它们都应该在场景中显示,并且相隔一定距离。我最初只有一头棕色的母牛,那头母牛太大了。我改变了一些我不记得的东西,现在我的场景是从奶牛的内部出来的,我无法离开奶牛的尸体。当我这样做时,它似乎在移动。我认为问题是因为[最小界限]为正数,但我不确定。我还为母牛设置了z轴。如何在产卵时使母牛稍微矮一点并在离我约5-7码的地方?
import UIKit
import RealityKit
import ARKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
showModel()
overlayCoachingView()
setupARView()
arView.addGestureRecognizer(UITapGestureRecognizer(target: self,action:
#selector(handleTap(recognizer:))))
}
func showModel(){
let anchorEntity = AnchorEntity(plane: .horizontal,minimumBounds:[0.7,0.7])
let entity = try! Entity.loadModel(named: "COW_ANIMATIONS")
entity.setParent(anchorEntity)
arView.scene.addAnchor(anchorEntity)
}
func overlayCoachingView () {
let coachingView = ARCoachingOverlayView(frame: CGRect(x: 0,y: 0,width:
arView.frame.width,height: arView.frame.height))
coachingView.session = arView.session
coachingView.activatesAutomatically = true
coachingView.goal = .horizontalPlane
view.addSubview(coachingView)
}
// Load the "Box" scene from the "Experience" Reality File
// let boxAnchor = try! Experience.loadBox()
// Add the box anchor to the scene
//arView.scene.anchors.append(boxAnchor)
func setupARView(){
arView.automaticallyConfigureSession = false
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal,.vertical]
configuration.environmentTexturing = .automatic
arView.session.run(configuration)
}
//object placement
@objc
func handleTap(recognizer: UITapGestureRecognizer){
let location = recognizer.location(in:arView)
let results = arView.raycast(from: location,allowing: .estimatedPlane,alignment: .horizontal)
if let firstResult = results.first {
let anchor = ARAnchor(name: "COW_ANIMATIONS",transform: firstResult.worldTransform)
arView.session.add(anchor: anchor)
} else {
print("Object placement failed - couldn't find surface.")
//cow animations
let robot = try! ModelEntity.load(named: "COW_ANIMATIONS")
let anchor = AnchorEntity()
anchor.children.append(robot)
arView.scene.anchors.append(anchor)
robot.playAnimation(robot.availableAnimations[0].repeat(duration: .infinity),transitionDuration: 0.5,startsPaused: false)
//start cow animation
let brownCow = try! ModelEntity.load(named: "COW_ANIMATIONS")
let blackCow = try! ModelEntity.load(named: "Cow")
brownCow.position.x = -1.0
blackCow.position.x = 1.0
brownCow.setParent(anchor)
blackCow.setParent(anchor)
arView.scene.anchors.append(anchor)
let cowAnimationResource = brownCow.availableAnimations[0]
let horseAnimationResource = blackCow.availableAnimations[0]
brownCow.playAnimation(cowAnimationResource.repeat(duration: .infinity),transitionDuration: 1.25,startsPaused: false)
blackCow.playAnimation(horseAnimationResource.repeat(duration: .infinity),transitionDuration: 0.75,startsPaused: false)
//end cow animations
func placeObject(named entityName: String,for anchor: ARAnchor) {
let entity = try! ModelEntity.loadModel(named: entityName)
entity.generateCollisionShapes(recursive: true)
arView.installGestures([.rotation,.translation],for: entity)
let anchorEntity = AnchorEntity(anchor: anchor)
anchorEntity.addChild(entity)
arView.scene.addAnchor(anchorEntity)
}
}
extension ViewController: ARSessionDelegate {
func session( session: ARSession,didAdd anchors: [ARAnchor]) {
for anchor in anchors {
if let anchorName = anchor.name,anchorName == "COW_ANIMATIONS" {
placeObject(named: anchorName,for: anchor)
} }
}
}
解决方法
第一步
在RealityKit中,如果将模型与其个人锚点绑定在一起(如果一个锚点仅持有一个模型),则有两种缩放方法:
cowEntity.scale = [0.7,0.7,0.7]
// or
cowAnchor.scale = SIMD3<Float>([1,1,1] * 0.7)
,您至少有两种方法可以沿任何轴(例如,沿Z轴)定位母牛模型:
cowEntity.position = SIMD3<Float>(0,-2)
// or
cowAnchor.position.z = -2.0
因此,如您所见,在转换cowAnchor时,其所有子代也将进行此转换。
第二步
您需要在3D创作应用程序中适当放置模型的pivot point。目前,RealityKit还没有像simdPivot实例属性在SceneKit中那样可以固定轴心位置的工具。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。