2

I need to be able to tap on the view, get the X, Y tap position, and then for a scenekit node to appear in that position, including depth. So if I tap at top of screen, it appears far away, and if lower, then closer. It needs to take into account camera view rotation, lateral position, and should appear exactly where touched on screen. Snapchat do this very well with their newest augmented reality objects, as seen here.

https://cnet3.cbsistatic.com/img/zjwcEw0O0BQ7smi3X1KrksTliu4=/fit-in/270x0/2017/04/17/0fae55ef-4d61-4b0f-80ec-de943ae1dc7b/snapchat-world-lenses.gif

I have heard about scenekits projected/unprojected points, and tried the answer to this How to use iOS (Swift) SceneKit SCNSceneRenderer unprojectPoint properly

let projectedOrigin = scnView.projectPoint(SCNVector3Zero)  

let vpWithZ = SCNVector3(x: tapX, y: tapY, z: projectedOrigin.z)
let worldPoint = scnView.unprojectPoint(vpWithZ)

However the node that moves to worldPoint shows appears much to close anywhere I tap (how do I scale it?), and even though it shows signs of working laterally, it is very inaccurate. I want it to just move along the xz plane like in snapchat. How exactly does the projection system work to implement it (it is not well explained elsewhere), and is there a better alternative?

Thank you.

Community
  • 1
  • 1
harry lakins
  • 803
  • 1
  • 13
  • 28
  • You need to cast a ray from the camera through the tap point, and see what it collides with first. – Ben May 10 '17 at 16:39
  • See this question for a starter: http://stackoverflow.com/questions/33474533/scenekit-hit-testing-between-camera-orientation-and-geometry – Ben May 10 '17 at 16:41
  • when you say what it collides with, do you mean where on what axis? I am not using hittest as I am not trying to retrieve a node, im trying to retrieve 3D point. – harry lakins May 10 '17 at 17:11
  • Where do you get depth from if you aren't using HitTest? You have only 2 coordinates and you want three. The depth has to come from somewhere. – Ben May 10 '17 at 17:20
  • Well how is hittest implemented without expecting a node ? Doesnt it just return an empty result? – harry lakins May 10 '17 at 17:31
  • It has to collide with an object in the scene, e.g. if you are selecting an object. If you are trying to create a new object, and work out what location, then you might collide it with the ground, for example. – Ben May 11 '17 at 08:09
  • So you are suggest I make an invisible ground along x axis, then when hit tests detects it, there I can find the exact position of the ground it hits? – harry lakins May 11 '17 at 08:11
  • Yes, Exactly... – Ben May 11 '17 at 08:12
  • https://developer.apple.com/reference/scenekit/scnhittestresult just noticed all the coordinates properties that get returned on a hittest. Im guessing its those ? (worldCoords, local, etc) – harry lakins May 11 '17 at 08:14

0 Answers0