gpt4 book ai didi

swift - 如何在 RealityKit 中实现广告牌效果(LookAt 相机)?

转载 作者:行者123 更新时间:2023-12-05 06:20:18 27 4
gpt4 key购买 nike

我想在RealityKit中实现billboard效果(飞机一直看着摄像头),我使用了Entity.Look()方法,但是结果很奇怪,我什至做不到看到飞机,我用的脚本如下,请问有什么问题吗?

struct ARViewContainer: UIViewRepresentable {

func makeUIView(context: Context) -> ARView {

let arView = ARView(frame: .zero)
let config = ARWorldTrackingConfiguration()
config.planeDetection = .horizontal
arView.session.run(config, options:[ ])
arView.session.delegate = arView
arView.createPlane()
return arView
}

func updateUIView(_ uiView: ARView, context: Context) { }
}

var planeMesh = MeshResource.generatePlane(width: 0.15, height: 0.15)
var planeMaterial = SimpleMaterial(color:.white,isMetallic: false)
var planeEntity = ModelEntity(mesh:planeMesh,materials:[planeMaterial])
var arCameraPostion : SIMD3<Float>!
var isPlaced = false

extension ARView : ARSessionDelegate{
func createPlane(){
let planeAnchor = AnchorEntity(plane:.horizontal)
planeAnchor.addChild(planeEntity)
self.scene.addAnchor(planeAnchor)
//planeAnchor.transform.rotation = simd_quatf(angle: .pi, axis: [0,1,0])

}

public func session(_ session: ARSession, didUpdate frame: ARFrame){
guard let arCamera = session.currentFrame?.camera else { return }
if isPlaced {
arCameraPostion = SIMD3(arCamera.transform.columns.3.x,0,arCamera.transform.columns.3.z)
planeEntity.look(at: arCameraPostion, from: planeEntity.position, upVector: [0, 1, 0],relativeTo: nil)
}
}

public func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
isPlaced = true
}
}

最佳答案

session(_:didUpdate:) 方法

尝试以下逻辑为 RealityKit 相机实现“广告牌”行为。您可以使用此代码作为起点。它根据相机位置生成模型围绕其局部 Y 轴的旋转。

import RealityKit
import ARKit

class ViewController: UIViewController {

@IBOutlet var arView: ARView!
var model = Entity()

override func viewDidLoad() {
super.viewDidLoad()

arView.session.delegate = self

let config = ARWorldTrackingConfiguration()
arView.session.run(config)

self.model = try! ModelEntity.load(named: "drummer")
let anchor = AnchorEntity(world: [0, 0, 0])
anchor.addChild(self.model)
arView.scene.anchors.append(anchor)
}
}

模型的枢轴点必须位于模型的中心(而不是距模型一定距离)。

extension ViewController: ARSessionDelegate {

func session(_ session: ARSession, didUpdate frame: ARFrame) {

let camTransform: float4x4 = arView.cameraTransform.matrix

let alongXZPlane: simd_float4 = camTransform.columns.3

let yaw: Float = atan2(alongXZPlane.x - model.position.x,
alongXZPlane.z - model.position.z)
print(yaw)

// Identity matrix 4x4
var positionAndScale = float4x4()

// position
positionAndScale.columns.3.z = -0.25

// scale
positionAndScale.columns.0.x = 0.01
positionAndScale.columns.1.y = 0.01
positionAndScale.columns.2.z = 0.01

// orientation matrix
let orientation = Transform(pitch: 0, yaw: yaw, roll: 0).matrix

// matrices multiplication
let transform = simd_mul(positionAndScale, orientation)

self.model.transform.matrix = transform
}
}


订阅(to:on:_:)方法

或者,您可以实现对事件流的订阅。

import RealityKit
import Combine

class ViewController: UIViewController {

@IBOutlet var arView: ARView!
var model = Entity()
var subs: [AnyCancellable] = []

override func viewDidLoad() {
super.viewDidLoad()
self.model = try! ModelEntity.load(named: "drummer")
let anchor = AnchorEntity(world: [0, 0, 0])
anchor.addChild(self.model)
arView.scene.anchors.append(anchor)

arView.scene.subscribe(to: SceneEvents.Update.self) { _ in
let camTransform: float4x4 = self.arView.cameraTransform.matrix
let alongXZPlane: simd_float4 = camTransform.columns.3
let yaw: Float = atan2(alongXZPlane.x - self.model.position.x,
alongXZPlane.z - self.model.position.z)

var positionAndScale = float4x4()
positionAndScale.columns.3.z = -0.25
positionAndScale.columns.0.x = 0.01
positionAndScale.columns.1.y = 0.01
positionAndScale.columns.2.z = 0.01
let orientation = Transform(pitch: 0, yaw: yaw, roll: 0).matrix
let transform = simd_mul(positionAndScale, orientation)
self.model.transform.matrix = transform
}.store(in: &subs)
}
}

关于swift - 如何在 RealityKit 中实现广告牌效果(LookAt 相机)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60577468/

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