ARKIT - 如何粘贴两个物体

ARKIT - How to stick two objects

我有两个对象(两个立方体)。首先,我将第一个立方体添加到场景中。然后我想添加第二个,我希望它粘在第一个上,在第一个的一侧 - 我将 select 单击它的哪一侧(如下图所示)。是否可以只点击第一个立方体和第二个立方体的一个面自动出现在场景中并粘在第一个立方体上?我不知道该怎么做。

Photo

创建 SCNBoxGeometry 时:

The SCNBox class automatically creates SCNGeometryElement objects as needed to handle the number of materials.

因此,为了访问这些元素,您需要为 Box 的每个面创建一个 SCNMaterial。然后你可以执行一个SCNHitTest来检测检测到哪张脸:

When you perform a hit-test search, SceneKit looks for SCNGeometry objects along the ray you specify. For each intersection between the ray and and a geometry, SceneKit creates a hit-test result to provide information about both the SCNNode object containing the geometry and the location of the intersection on the geometry’s surface.

那么我们将如何处理这个问题?

假设您已经创建了名为:

的 SCNNodes
var cubeOne = SCNNode()
var cubeTwo = SCNNode()

它们都分配了 6 个不同的 SCNMaterials(每个面一个),如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    cubeOne.geometry = cubeGeometry()
    cubeOne.position = SCNVector3(0, -0.5, -1.5)
    cubeOne.name = "Cube One"

    cubeTwo.geometry = cubeGeometry2()
    cubeTwo.position = SCNVector3(30, 0, -1.5)
    cubeTwo.name = "Cube Two"

    self.augmentedRealityView.scene.rootNode.addChildNode(cubeOne)
    self.augmentedRealityView.scene.rootNode.addChildNode(cubeTwo)

}

/// Returns The 6 Faces Of An SCNBox
///
/// - Returns: SCNGeometry
func cubeGeometry() -> SCNGeometry{

    var colours: [UIColor] = [.red, .green, .cyan, .yellow, .purple, .blue]
    var faces = [SCNMaterial] ()
    let cubeGeometry = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)

    for faceIndex in 0..<5{

        let material = SCNMaterial()
        material.diffuse.contents = colours[faceIndex]
        faces.append(material)

    }

    cubeGeometry.materials = faces

    return cubeGeometry
}

现在你已经为面指定了 6 种材质,你将有六个几何元素对应于你的 SCNBox 的每一侧。

完成此操作后,我们可以快速创建一个与面孔顺序相对应的枚举:

enum BoxFaces: Int{

    case Front, Right, Back, Left, Top, Botton
}

现在,当我们执行 hitTest 时,我们可以记录命中的位置,例如:

/// Detects Which Cube Was Detected & Logs The Geometry Index
///
/// - Parameter gesture: UITapGestureRecognizer
@IBAction func cubeTapped(_ gesture: UITapGestureRecognizer){

    //1. Get The Current Touch Location
    let currentTouchLocation = gesture.location(in: self.augmentedRealityView)

    //2. Perform An SCNHitTest
    guard let hitTest = self.augmentedRealityView.hitTest(currentTouchLocation, options: nil).first else { return }

    //3. If The Node In Cube One Then Get The Index Of The Touched Material
    if let namedNode = hitTest.node.name{

        if namedNode == "Cube One"{

            //4. Get The Geometry Index
            if let faceIndex = BoxFaces(rawValue:  hitTest.geometryIndex){
                print("User Has Hit \(faceIndex)")

                //5. Position The Second Cube
                positionStickyNode(faceIndex)
            }

        }

    }
}

在第 5 部分中,您会注意到对函数 positionStickyNode 的调用,该函数将第二个立方体放置在第一个立方体的相应位置:

/// Position The Second Cube Based On The Face Tapped
///
/// - Parameter index: BoxFaces
func positionStickyNode(_ index: BoxFaces){

    let (min, max) = cubeTwo.boundingBox

    let cubeTwoWidth = max.x - min.x
    let cubeTwoHeight = max.y - min.y

    switch index {

    case .Front:
        cubeTwo.simdPosition = float3(cubeOne.position.x, cubeOne.position.y, cubeOne.position.z + cubeTwoWidth)
    case .Right:
        cubeTwo.simdPosition = float3(cubeOne.position.x + cubeTwoWidth, cubeOne.position.y, cubeOne.position.z)
    case .Back:
        cubeTwo.simdPosition = float3(cubeOne.position.x, cubeOne.position.y, cubeOne.position.z - cubeTwoWidth)
    case .Left:
        cubeTwo.simdPosition = float3(cubeOne.position.x - cubeTwoWidth, cubeOne.position.y, cubeOne.position.z)
    case .Top:
        cubeTwo.simdPosition = float3(cubeOne.position.x, cubeOne.position.y + cubeTwoHeight, cubeOne.position.z)
    case .Botton:
        cubeTwo.simdPosition = float3(cubeOne.position.x, cubeOne.position.y - cubeTwoHeight, cubeOne.position.z)
    }

这是一个非常粗略的例子,当你的立方体大小相同时它会起作用......但是你已经足够了,现在要弄清楚它如何适用于不同的大小等等。

希望对您有所帮助...