如何在RealityKit中使用Raycast方法? [英] How to use Raycast methods in RealityKit?
问题描述
在RealityKit
框架中有三种检测交叉点的方法,但是我不知道如何在我的项目中使用它.
There are three ways about Detecting Intersections in RealityKit
framework, but I don't know how to use it in my project.
1.
func raycast(origin: SIMD3<Float>,
direction: SIMD3<Float>,
length: Float,
query: CollisionCastQueryType,
mask: CollisionGroup,
relativeTo: Entity?) -> [CollisionCastHit]
2.
func raycast(from: SIMD3<Float>,
to: SIMD3<Float>,
query: CollisionCastQueryType,
mask: CollisionGroup,
relativeTo: Entity?) -> [CollisionCastHit]
3.
func convexCast(convexShape: ShapeResource,
fromPosition: SIMD3<Float>,
fromOrientation: simd_quatf,
toPosition: SIMD3<Float>,
toOrientation: simd_quatf,
query: CollisionCastQueryType,
mask: CollisionGroup,
relativeTo: Entity?) -> [CollisionCastHit]
推荐答案
简单的射线铸造
如果您想了解如何使用Ray-Casting方法将Reality Composer中制作的模型放置到RealityKit场景(具有检测到的水平面)中,请使用以下代码:
If you want to find out how to position a model made in Reality Composer into a RealityKit scene (that has a detected horizontal plane) using Ray-Casting method, use the following code:
import RealityKit
import ARKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
let scene = try! Experience.loadScene()
@IBAction func onTap(_ sender: UITapGestureRecognizer) {
scene.steelBox!.name = "Parcel"
let tapLocation: CGPoint = sender.location(in: arView)
let estimatedPlane: ARRaycastQuery.Target = .estimatedPlane
let alignment: ARRaycastQuery.TargetAlignment = .horizontal
let result: [ARRaycastResult] = arView.raycast(from: tapLocation,
allowing: estimatedPlane,
alignment: alignment)
guard let rayCast: ARRaycastResult = result.first
else { return }
let anchor = AnchorEntity(world: rayCast.worldTransform)
anchor.addChild(scene)
arView.scene.anchors.append(anchor)
print(rayCast)
}
}
请注意课程ARRaycastQuery
.此类来自ARKit,而非RealityKit .
Pay attention to a class ARRaycastQuery
. This class comes from ARKit, not from RealityKit.
像
raycast(from:to:query:mask:relativeTo:)
这样的凸射线投射方法是沿直线滑动凸形形状并在与场景中任何碰撞形状的第一个相交处停止的操作. Sceneraycast()
方法对场景中具有collision shapes
的所有实体执行命中测试. 没有碰撞形状的实体将被忽略.
A Convex-Ray-Casting methods like
raycast(from:to:query:mask:relativeTo:)
is the op of swiping a convex shapes along a straight line and stopping at the very first intersection with any of the collision shape in the scene. Sceneraycast()
method performs a hit-tests against all entities withcollision shapes
in the scene. Entities without a collision shape are ignored.
您可以使用以下代码从开始位置到结束进行凸射线投射:
You can use the following code to perform a convex-ray-cast from start position to end:
import RealityKit
let startPosition: SIMD3<Float> = [0, 0, 0]
let endPosition: SIMD3<Float> = [5, 5, 5]
let query: CollisionCastQueryType = .all
let mask: CollisionGroup = .all
let raycasts: [CollisionCastHit] = arView.scene.raycast(from: startPosition,
to: endPosition,
query: query,
mask: mask,
relativeTo: nil)
guard let rayCast: CollisionCastHit = raycasts.first
else { return }
print(rayCast.distance) /* The distance from the ray origin to the hit */
print(rayCast.entity.name) /* The entity's name that was hit */
CollisionCastHit
结构是碰撞投射的命中结果,它存在于RealityKit的场景中.
A CollisionCastHit
structure is a hit result of a collision cast and it lives in RealityKit's scene.
PS
当您使用
raycast(from:to:query:mask:relativeTo:)
方法测量相机到实体的距离时,无论ARCamera的方向是什么,都与其 position 在世界坐标系中.
When you use
raycast(from:to:query:mask:relativeTo:)
method for measuring a distance from camera to entity it doesn't matter what an orientation of ARCamera is, it only matters what its position is in world coordinates.
这篇关于如何在RealityKit中使用Raycast方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!