【问题标题】:How to restrict position of a node to the surface of a custom plane in Scenekit?如何将节点的位置限制在 Scenekit 中自定义平面的表面?
【发布时间】:2021-01-18 10:43:33
【问题描述】:

我是 Swift 和 SceneKit 的新手,我正在尝试实现一个解决方案,用户可以将 3D 对象添加到场景中,并将其放置在平面表面的边缘。

我尝试使用平面节点的边界框值来假设其边缘和对象的潜在位置,但由于平面是自定义几何体,因此对象通常最终会超出平面的表面。

最重要的是,如果平面的特定边缘是对角线,我还需要旋转对象,使其始终平行于平面的边缘。

这是我的代码的 sn-p 和 image illustrating the problem and the desired outcome

//creating plane out of custom geometry values
let surfaceNode = SCNNode(geometry: surfaceGeometry)

//defining plane’s bounding box & center point
let minimum = SIMD3<Float>(surfaceNode.boundingBox.min)
let maximum = SIMD3<Float>(surfaceNode.boundingBox.max)
let translation = (maximum - minimum) * 0.5

//fixing pivot point
surfaceNode.pivot = SCNMatrix4MakeTranslation(surfaceNode.boundingBox.min.x + translation.x, 0, surfaceNode.boundingBox.min.z + translation.z)

//adding custom plane to the scene
surfaceNode.position = SCNVector3(0, 0 ,0)
sceneView.scene.rootNode.addChildNode(surfaceNode)

//creating object
let object = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)
let objectNode = SCNNode(geometry: object)
objectNode.pivot = SCNMatrix4MakeTranslation(0, -0.1, 0)

//positioning object on top of the plane’s surface
objectNode.position = SCNVector3(surfaceNode.position.x, surfaceNode.position.y, surfaceNode.position.z - translation.z)

//adding object to scene
sceneView.scene.rootNode.addChildNode(objectNode)

【问题讨论】:

  • 你试过把objectNode添加到surfaceNode吗?子节点将使用它的父节点的旋转和位置作为参考

标签: ios swift scenekit arkit


【解决方案1】:

我希望看到一个很酷的基于数学的解决方案,因为这将是一个巧妙的技巧,但因为我没有......

  1. 您没有说这必须是多么动态,并且 2) 我猜您有某种拖放的想法,所以为了避免一些数学问题,这将是我的方法。

我要做的是获取您的平面表面并计算一些对齐位置,其中包括放置对象所需的旋转。我推荐这个,因为有些角落不适合,而且你不能允许重叠。因此,请获取您的放置位置并找到最近的“可接受”插槽,然后“捕捉”最近的位置。

如果您想提供很大的灵活性,例如一次移动一个基本上 1 个像素,那么我会创建一个可接受的位置范围数组以及每个位置所需的旋转。如果有人拖到该范围之外,则要么不允许,要么将其移动到最近的可接受“点”。

设置多个几何形状有点痛苦,但根据您要完成的任务,它会是准确的,尽管有点“硬编码”。

【讨论】:

    【解决方案2】:

    另一个更简单的可能性是,如果你给你的移动盒子一个实体,你在飞机的所有边缘放置不可见的“墙”和实体,并使用碰撞蒙版让场景包为你完成工作。

    【讨论】:

      猜你喜欢
      • 2019-10-25
      • 2023-03-04
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 2017-04-21
      • 2017-12-08
      相关资源
      最近更新 更多