简体   繁体   中英

Geometry Elements are not being detected when using tap gesture recognizer but it detects body-splitcontainers-split123

I have a scenekit file in which I have my body model. I am using it in my main scenekit file. I want to open different different scenes when I click on body parts like hand, foot, arm, head among other stuff. The body is a single node with multiple geometry elements. It can detect the material assigned to the geometry correctly when I tap but it does not show which geometry element it is, it shows a name like body-splitcontainer-split4. Therefore I need to fetch the geometry element. I am making a 3d view app.

I checked it by printing the node itself, its name and its properties individually. It does not also detect the geometry element. When I use the geometryindex property it returns me 0.

@objc func handleTap(_ gestureRecognize: UIGestureRecognizer) { // retrieve the SCNView

    let scnView = self.view as! SCNView

    // check what nodes are tapped

    let p = gestureRecognize.location(in: scnView)
    print(p)
    let hitResults = scnView.hitTest(p, options: nil)

    // check that we clicked on at least one object

    if hitResults.count > 0 {

    // retrieved the first clicked object

        let result = hitResults[0]

    // get the tapped node

        let index = result.node

    // Print the node name and geometry name

        let material = result.node.geometry?.firstMaterial
        print("Index is \(index.name)")
        print("Material is \(material?.name)")

    // highlight it

        SCNTransaction.begin()
        SCNTransaction.animationDuration = 0.5

    // on completion - unhighlight

        SCNTransaction.completionBlock = {
            SCNTransaction.begin()
            SCNTransaction.animationDuration = 0.5

            material!.emission.contents = UIColor.black

            SCNTransaction.commit()
        }

        material!.emission.contents = UIColor.yellow

        SCNTransaction.commit()
    }
}

I expected it to show me the actual geometry element name like: body-Element0 (for head) body-Element1 (for neck) but its showing me body-splitcontainer-split1 body-splitcontainer-split2 body-splitcontainer-split3 all for heads when I click on different parts of head. Although its showing the correct material name. For arm its body-splitcontainer-split22 etc

I got it to work. In the stimulator my material was being selected but it was not fetching my geometry elemets. But when I tested it in my actual device the code was making better sense, it was detecting my geomtery elements but returning the first material as coded. So I made a dictionary and used that dictionary to associate geometry elements with their material name as a workaround for geometry.firstmaterial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM