- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在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
}
}
最佳答案
尝试以下逻辑为 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
}
}
或者,您可以实现对事件流的订阅。
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/
我从我的老师那里得到了一个代码,目前显示了一个 3D 地球和一个 2D 粒子系统。摄像机绕圈移动。粒子系统应该面向相机。 根据我的讲义,我必须将广告牌乘以相机 View 矩阵的逆矩阵。我很想尝试一下,
我需要使节点层次结构中的一个节点面向相机,或者换句话说,成为一个广告牌。对于每个节点,我将其世界矩阵和世界旋转存储为四元数。根据我对四元数的了解,我想要的操作是获取相机四元数和节点的旋转四元数之间的差
我希望能够创建一个 div 并为其指定高度、宽度和类。然后将 div 添加到 Cesium map 作为广告牌。 我可以创建带有图像和标签的广告牌并且还找到了 this有关如何使用 svg 的链接,但
每个人, 我正在尝试对基于Canvas的纹理进行动画处理,该纹理映射到平面上,如广告牌。我已经指出要包括 material.needsUpdate 和 texture.needsUpdate ,但是我
我创建了一个小应用程序来显示单个粒子: 当我围绕场景旋转时,光照发生变化,粒子几乎变得不可见: 是否可以在整个旋转过程中强制设置光照? 相关代码: // init glClear(GL_COLOR_B
我正在使用 LibGDX 博客中描述的 3d 粒子系统(Billboards ar 粒子):https://github.com/libgdx/libgdx/wiki/3D-Particle-Effe
我是一名优秀的程序员,十分优秀!