gpt4 book ai didi

swift - Reality Composer - ar hittest 不起作用?

转载 作者:行者123 更新时间:2023-11-30 10:25:28 24 4
gpt4 key购买 nike

好吧,我正在通过 https://developer.apple.com/videos/play/wwdc2019/609/ 提供的 RealityComposer 游戏进行筛选。我正在尝试找出如何让实体移动到用户点击的位置。

我可以像这样在 .rc 场景中引用我的对象:

struct ARViewContainer: UIViewRepresentable {

let arView = ARView(frame: .zero)

func makeUIView(context: Context) -> ARView {

// arView = ARView(frame: .zero)

// Load the "Box" scene from the "Experience" Reality File
let boxAnchor = try! Experience.loadBox()

//*Notifications
setupNotifications(anchor: boxAnchor)

//*Access vars
if boxAnchor.box1 != nil {
//print(box1.position.x)
/// Set local position
boxAnchor.box1!.position = [1.0, 0.0, 0.5]
/// Set world position
//boxAnchor.box1.setPosition([0.5, 0.2, 1.5], relativeTo: nil)
}

我知道这涉及某种形式的 arhitest,但是通过以下内容我收到错误:

UIView has no member last?

func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

//1. Get The Current Touch Location
guard let currentTouchLocation = touches.first?.location(in: self.arView),

//2. Get The Tapped Node From An SCNHitTest
let hitTestResultNode = self.arView.hitTest(currentTouchLocation, with: nil).last?.node else { return } //error here

//3. Loop Through The ChildNodes
for node in hitTestResultNode.childNodes{

//4. If The Node Has A Name Then Print It Out
if let validName = node.name{
print("Node\(validName) Is A Child Node Of \(hitTestResultNode)")
}

}

}

我很不知道自己是否正确地处理了这件事。我引用Detect touch on SCNNode in ARKit但这不涉及 RealityComposer。

我该怎么做?

最佳答案

Entity通过触摸移动的最简单方法是使用Apple提供的内置手势;您可以在这里阅读更多信息:Apple Documentation

要启用您选择的手势(在本例中为翻译),请首先确保在 RealityComposer 中将 partcipates 值设置为 true在您希望与之交互的每个Entity上。

enter image description here

然后将一个 Has Collision 组件添加到Entity,这很简单:

A component that gives an entity the ability to collide with other entities that also have collision components.

使用此功能,您可以安装内置手势来为您完成繁重的工作。

假设我们在 Xcode 中有默认的 RealityKit 示例设置,并且我们已经为该框选择了参与项,那么就这么简单,用户可以使用它来平移它触摸位置:

class ViewController: UIViewController {

@IBOutlet var arView: ARView!

override func viewDidLoad() {

super.viewDidLoad()

//1. Load The Box Anchor
let boxAnchor = try! Experience.loadBox()

//2. Check The SteelBox Has A Collision Component & Add The Desired Gesture
if let hasCollision = boxAnchor.steelBox as? HasCollision {
arView.installGestures(.translation, for: hasCollision)
}

//Add The Box To The Scene Hierachy
arView.scene.anchors.append(boxAnchor)
}

}

或者,如果您想做一些繁重的工作(谁不想!),那么您可以通过创建一个全局变量来执行类似的操作,该变量将引用您选择的实体(在本例中称为 Steel Box) :

//Variable To Store Our Currently Selected Entity
var currentEntity: Entity?

然后使用 touchesBegantouchesMoved 你可以这样做:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

/* Get The Current Touch Location In The ARView
Perform A HitTest For The Nearest Entity
Checks That The Tapped Entity Is The Steel Box
Set It As The Current Entity
*/
guard let touchLocation = touches.first?.location(in: arView),
let tappedEntity = arView.hitTest(touchLocation, query: .nearest, mask: .default).first?.entity,
tappedEntity.name == "Steel Box" else {
return
}

currentEntity = tappedEntity

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

/* Get The Current Touch Location In The ARView
Perform A HitTest For An Existing Plane
Move The Current Entity To The New Transfortm
Set It As The Current Entity
*/
guard let touchLocation = touches.first?.location(in: arView),
let currentEntity = currentEntity else {
return
}

if let transform = arView.hitTest(touchLocation, types: .existingPlaneUsingExtent).first?.worldTransform {
currentEntity.move(to: transform, relativeTo: nil, duration: 0.1)

}
}

希望它能帮助您指明正确的方向。

关于swift - Reality Composer - ar hittest 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60082742/

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