- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如何使 ARNode
指向 ARAnchor
?
我想使用 art.scnassets/ship.scn
显示在屏幕中央并指向我刚刚放置在场景中某处的对象。
/// ViewController Class
func placeObject() {
let screenCentre : CGPoint = CGPoint(x: self.sceneView.bounds.midX, y: self.sceneView.bounds.midY)
guard let hitTestResult = sceneView.hitTest(screenCentre, types: [.featurePoint]).first else { return }
// Place an anchor for a virtual character.
let anchor = ARAnchor(name: identifierString, transform: hitTestResult.worldTransform)
sceneView.session.add(anchor: anchor)
// add to item model
ItemModel.shared.anchors.append((identifierString, anchor)
}
func showDirection(of object: ARAnchor) { // object: saved anchor
if !Guide.shared.isExist {
let startPoint = SCNVector3(0, 0 , -1)
let targetPoint = SCNVector3(object.transform.columns.3.x, object.transform.columns.3.y, object.transform.columns.3.z)
let guideNode = Guide.shared.setPosition(from: startPoint, to: targetPoint)
// add the ship in the center of the view
sceneView.pointOfView?.addChildNode(guideNode)
}
}
/// Guide Class
func setPosition(from start: SCNVector3, to target: SCNVector3) -> SCNNode {
isExist = true
guideNode.position = start
targetPosition = target
// create target node from saved anchor
let desNode = SCNNode()
desNode.position = targetPosition
let lookAtConstraints = SCNLookAtConstraint(target: desNode)
guideNode.constraints = [lookAtConstraints]
return guideNode
}
// MARK: - ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
if let name = anchor.name, name.hasPrefix(identifierString) {
// Create 3D Text
let textNode: SCNNode = createNewBubbleParentNode(identifierString)
node.addChildNode(textNode)
}
}
我试过 SCNLookAtConstraint
但它没有按预期工作,有什么建议吗?
最佳答案
我不知道这对你的情况是否有帮助。 (如果不是我会删除我的答案)
所以我遇到了同样的问题。我在两个 SCNVector 对象之间画墙的地方。问题是墙壁是双面的,因此在某些情况下,相机可见的部分会翻转。
就像如果你从两个向量中绘制墙,然后添加另一个具有相同欧拉角的节点(平面节点),添加的节点(平面)是垂直翻转的。
我已经搜索了很多解决方案并尝试了很多东西,但这对我有用。
class func node(from:SCNVector3,
to:SCNVector3,height:Float,needToAlignToCamera:Bool) -> SCNNode {
let distance = MathHelper().getMeasurementBetween(vector1: from, and: to)
let wall = SCNPlane(width: CGFloat(distance),
height: CGFloat(height))
wall.firstMaterial = wallMaterial()
let node = SCNNode(geometry: wall)
// always render before the beachballs
node.renderingOrder = -10
// HERE IS MAGIC LINES
//============================================
let normalizedTO = to.normalized()
let normalizedFrom = from.normalized()
let angleBetweenTwoVectors = normalizedTO.cross(normalizedFrom)
var from = from
var to = to
if angleBetweenTwoVectors.y > 0 && needToAlignToCamera {
let temp = from
from = to
to = temp
}
//============================================
node.position = SCNVector3(from.x + (to.x - from.x) * 0.5,
from.y + height * 0.5,
from.z + (to.z - from.z) * 0.5)
// orientation of the wall is fairly simple. we only need to orient it around the y axis,
// and the angle is calculated with 2d math.. now this calculation does not factor in the position of the
// camera, and so if you move the cursor right relative to the starting position the
// wall will be oriented away from the camera (in this app the wall material is set as double sided so you will not notice)
// - obviously if you want to render something on the walls, this issue will need to be resolved.
node.eulerAngles = SCNVector3(0, -atan2(to.x - node.position.x, from.z - node.position.z) - Float.pi * 0.5, 0)
return node
}
扩展
func cross(_ vec: SCNVector3) -> SCNVector3 {
return SCNVector3(self.y * vec.z - self.z * vec.y, self.z * vec.x - self.x * vec.z, self.x * vec.y - self.y * vec.x)
}
func normalized() -> SCNVector3 {
if self.length() == 0 {
return self
}
return self / self.length()
}
关于ios - 如何使 SCNNode 面向 ARAnchor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53530244/
我正在尝试使用新的 ARKit 来替换我拥有的另一个类似的解决方案。真是太棒了!但我似乎无法弄清楚如何以编程方式移动 ARAnchor。我想慢慢地将 anchor 移动到用户的左侧。 在用户前方 2
我正在试验 SpriteKit 和 ARKit,想知道如何计算同一场景中两个 ARAnchor 之间的角度。 这意味着获取 anchor 在世界上的位置并计算它们定义的线与水平面(或另一个任意平面)的
我有一个应用程序使用 ARImageAnchor 来通过相机检测图像。我注意到,虽然场景节点在 3D 空间中的位置会实时更新,但节点的方向(xyz 旋转)可能需要几秒钟才能更新。任何附加的场景节点在更
在将 SCNNode 添加到 ARSCNView 的场景后,我试图获取 anchor 。我的理解是应该自动创建 anchor ,但我似乎无法检索它。 下面是我添加它的方法。该节点保存在一个名为 tes
我在使用 ARSKView 的 ARkit 项目中遇到了障碍。 我想在屏幕上使用点击并创建 ARAnchor 时实现,多个 SKNodes 将从同一个 anchor 弹出。由于我需要单独与每个节点交互
你好,我是使用 ARKit 进行开发的新手,所以这可能是一个基本问题,但我可以将 ARAnchor 附加到移动的物体上,比如打印出来的条形码吗? 我正在尝试开发一个应用程序,其中不同的对象应该显示在不
我目前正在开发 ARKit 2.0 应用程序,用户需要在映射空间周围找到虚拟对象,我使用了 AR Persistence 并且我意识到在初始 ARWorldMap 重新定位后,所有对象都会立即出现,即
When application launched first a vertical surface is detected on one wall than camera focus to the
我正在使用 XCode 9 上的 AR 入门应用,其中 anchor 是在点击场景中创建的: override func touchesBegan(_ touches: Set, with event
我目前正在用 RealityKit 做一些实验。 我一直在查看一些示例代码,我对 ARAnchor 和 AnchorEntity 之间的区别以及何时使用其中一个感到有点困惑. 到目前为止我知道: 两者
在 ARKit 中,我们可以通过 .showFeaturePoints 类型属性可视化在 ARSession 中检测到的 Feature Points' Cloud: self.sceneView.d
这几天我一直在努力解决这个问题。 给定一个基于 ARKit 的应用程序,我可以在其中跟踪用户的面部,我如何才能从它的 anchor 获得面部的绝对旋转? 我可以获得 ARAnchor 的变换,它是一个
如何使 ARNode 指向 ARAnchor? 我想使用 art.scnassets/ship.scn 显示在屏幕中央并指向我刚刚放置在场景中某处的对象。 /// ViewController Cla
问题: 如何创建一个与 ARObjectAnchor.referenceObject 具有相同高度和宽度的新 SKSpriteNode。 上下文: 我目前正在摆弄 ARKit 的新对象检测功能,并且有
我正在使用 WWDC 2018 中推出的 ARKit 2.0 测试 Apple 的多用户 AR 演示应用程序:Creating a multiuser AR experience . 文档说,在每个设
我正在尝试将 ARAnchor 投影到 2D 空间,但我面临方向问题... 在我将左上角、右上角、左下角、右下角位置投影到二维空间的函数下面: /// Returns the projection o
我在我的项目中使用 ARAnchor,我想从这个 anchor 获取位置 (3D)。 代码: sceneView.session.currentFrame?.anchors.first?.transf
我正在使用ARSessionDelegate在 ARView我在其中初始化 ARBodyTrackingConfiguration . session方法 didAdd: [ARAnchor]和 di
我有下一个代码: guard let feauturePoint = frame.hitTest(normalizedPoint, types: .featurePoint).first?.w
我需要将多个不同的 ARWorldMap 实例组合在一起。换句话说,我需要使用 ARWorldMap 的两个(或更多)实例的组合 anchor 创建一个实例。 我可以在整个应用程序中共享同一个实例,但
我是一名优秀的程序员,十分优秀!