2

My SceneKit project uses an imported .OBJ file, which contains only one node with a huge image. Image comprises different objects or parts. I need to tap on a foot, or image specific part, and have it highlighted. This is code to import .obj File

    sceneView = SCNView(frame: self.view.frame)
    self.view.addSubview(sceneView)

    let scene = SCNScene(named: "OBJ.obj")!
     print("\(scene.rootNode.childNodes.first?.geometry?.materials.count)")
    sceneView.scene = scene

and here I am using tap gesture recognizer

  // add a tap gesture recognizer
    let doubleTapRecognizer = UITapGestureRecognizer(target: self, action : #selector(self.tapGesture(sender:)));
    doubleTapRecognizer.numberOfTapsRequired = 1;
    doubleTapRecognizer.numberOfTouchesRequired = 1;
    sceneView.addGestureRecognizer(doubleTapRecognizer);

func tapGesture(sender: UITapGestureRecognizer){
   let hitResult : SCNHitTestResult
    // check what nodes are tapped
    let p = sender.location(in: sceneView)
    let hitResults = sceneView.hitTest(p, options: nil)        
}

I have 3D Model with different parts like left foot right foot and floor needed to be tapped and show different color on them. The problem is that all of these are in single node.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • Are you sure it is just one node and that it does not have any children? It is common to have 1 image for multiple nodes. Check your node hierarchy again. Otherwise, as Hal Mueller says it is sensible to go back to a 3D modeling software and just split that one mesh into multiple meshes. – 3d-indiana-jones Dec 23 '16 at 18:31
  • yes am sure there is only one node . Node comprises of many Geometry elements which I got by using HitTest method. Finally I got each part of the 3d Model . Now I am facing problem that how can I apply Materials to diffuse geometry element colors? – Saleem Abbas Dec 26 '16 at 17:12
  • Check out the documentation for `SCNGeometryElement` https://developer.apple.com/reference/scenekit/scngeometryelement. Your geometry is made up of multiple elements which can correspond to multiple materials. – 3d-indiana-jones Dec 26 '16 at 17:26

1 Answers1

0

Your SCNHitTestResult will give you the index of the triangle that was tapped. But you'll still have to work out the logic of going from a single triangle to recognizing "that was the left arm".

I think your best bet is to go back to your 3D modeling software and break the object into smaller, logical components.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • I done first task to recognize the object by how can I shade or diffuse color of ElementIndex which returned by HITTEST Array? – Saleem Abbas Dec 22 '16 at 21:03
  • Effectively, you can't. Theoretically, of course, you can, but it's going to require you to have deep knowledge of how your modeling tool assigns materials to triangles, and how the element indices are assigned. I'm sure there's someone out there who knows how to do this, but your best bet is to use Blender or Maya or 3ds Max or Cheetah3D to break your .OBJ into smaller components. – Hal Mueller Dec 23 '16 at 04:05
  • sir actually I got an array by HitTest method , which comprises of many SCHHITTESTRESULT objects I took first one and used property geometryIndex which returned me all of the parts of 3D Model ranging from 1 to 36. Now the headache is how can I apply different color on that geometryIndex part? – Saleem Abbas Dec 26 '16 at 17:21
  • So that's your next question. How do I assign arbitrary materials to different triangles of a DAE based model knowing only the geometry index? – Hal Mueller Dec 27 '16 at 09:37