SceneKit unproject Z documentation explanation?

2020-08-01 05:07发布

问题:

I am going through some SceneKit concepts and one I am trying to solidify in my head is unprojectPoint.

I understand that the function will take a point in 2D and return a point in 3D (so one with the proper Z value).

When I read the documentation I read this:

  /*!
     @method unprojectPoint
     @abstract Unprojects a screenspace 2D point with depth info using the receiver's current point of view and viewport.
     @param point The screenspace position to be unprojected.
     @discussion A point whose z component is 0 (resp. 1) is unprojected on the near (resp. far) clip plane.
     */
    public func unprojectPoint(_ point: SCNVector3) -> SCNVector3

What I am not too clear on is the values 0 and 1 used when it talks about Z....

A point whose z component is 0 (resp. 1) is unprojected on the near (resp. far) clip plane.

As I was reading around online I then found this question:

How to use iOS (Swift) SceneKit SCNSceneRenderer unprojectPoint properly

When I deal with a SceneKit view, is Z = 0 always the near plane, and Z = 1 the far plane? If so, why? And, is Z = 0 and Z = 1 just normalized values?

So, can somebody help me understand why the value 0 and 1 are used for Z in this context? And ultimately help me understand the:

A point whose z component is 0 (resp. 1) is unprojected on the near (resp. far) clip plane.

statement?

回答1:

Perspective projection is the task of converting a point from the 3D space used for modeling your scene into the 2D pixel space of the view your scene is rendered in. It's something the GPU does thousands of times per frame during rendering.

But it's not entirely a 3D-to-2D conversion. It's important during rendering to sort out which objects are nearer to or farther from the camera (so they obscure each other properly), so perspective projection also outputs a normalized depth component, where lower values indicate a point nearer to the camera (and vice versa). (Values are normalized because at this point all that's needed is relative depth. And/or for reasons of traditional 3D graphics math history and GPU design.) This information gets used during rendering but effectively thrown away afterward — all you see is the 2D view.

"Unprojecting" a point is doing the same thing in reverse: given a point in 2D screen space, you want a point in 3D scene space. But a 2D point in screen space corresponds to a line in 3D space: each pixel in your view is looking along a ray into the 3D scene, and what you see in that pixel comes from the first 3D object that ray intersects.

Thus, to unproject a 2D point into 3D scene space, you need the 2D point itself to define a ray into the 3D scene, then a normalized depth value to decide how far along the ray you want the resulting 3D point to be. (Beware, normalized depth doesn't linearly correspond to distance because of perspective division.)

If you don't know the depth of the point your looking for, there are two things to consider...

  • Are you actually looking for the scene content (geometry) "behind" a specific pixel? If so, a hit test is more likely what you need.
  • Is the 3D point you want to get from unprojecting related to another point, enabling you to derive the Z value? There are a few other Q&As around here for that: How to use iOS (Swift) SceneKit SCNSceneRenderer unprojectPoint properly, How to convert 2D point to 3D using SceneKit's unprojectPoint without having a depth value?


标签: ios scenekit