重建与相机的近平面射线相交。

huangapple go评论64阅读模式
英文:

Reconstruct near plane ray intersection with camera

问题

我正在尝试重构相机光线渲染当前像素与近裁剪平面相交的点。

我需要在正在渲染的对象的局部坐标中获得相交点的坐标。

这是我的当前实现:

nearClipLS /= nearClipLS.w;```

[![问题的示意图][1]][1]

  [1]: https://i.stack.imgur.com/qfMus.png

<details>
<summary>英文:</summary>

I am trying to reconstruct the point where the ray of the camera rendering the current pixel intersects the near plane.

I need the coordinates of the intersection point in the local coordinates of the object being rendered. 

This is my current implementation:

`float4 nearClipLS = mul(inv_modelViewProjectionMatrix , float4((i.vertex.x / i.vertex.w), (i.vertex.y / i.vertex.w),-1., 1.));
nearClipLS /= nearClipLS.w;`

[![Drawing of problem][1]][1]


  [1]: https://i.stack.imgur.com/qfMus.png

</details>


# 答案1
**得分**: 1

以下是翻译好的代码部分:

```plaintext
有一个更高效的方法来完成这个任务,但以下方法理论上应该有效。

找到从相机到像素的偏移向量:

    float3 cam2pos = v.worldPos - _WorldSpaceCameraPos;

获取相机的前向向量:

    float3 camFwd = UNITY_MATRIX_IT_MV[2].xyz;

计算这两者的点积,以确定点在相机前进轴方向上的投影距离:

    float projDist = dot(cam2pos, camFwd);

然后,你应该能够使用这些数据将点重新投影到近剪裁平面:

    float nearClipZ = _ProjectionParams.y;
    float3 nearPos = _WorldSpaceCameraPos + (cam2pos * (nearClipZ / projDist));

这个解决方案没有处理边缘情况(比如当它与相机在同一位置或在相机后面时可能会引发问题),所以你可能需要在让它工作后检查这些情况。
英文:

There's got to be a more efficient way to do it, but the following should, in theory, work.

Find the offset vector from the camera to the pixel:

float3 cam2pos = v.worldPos - _WorldSpaceCameraPos;

Get the camera's forward vector:

float3 camFwd = UNITY_MATRIX_IT_MV[2].xyz;

Get the dot product of the two to determine how far the point projects in the direction of the camera's forward axis:

float projDist = dot(cam2pos, camFwd);

Then, you should be able to use that data to re-project the point onto the near clip plane:

float nearClipZ = _ProjectionParams.y;
float3 nearPos = _WorldSpaceCameraPos+ (cam2pos * (nearClipZ  / projDist));

This solution doesn't address edge cases (like when it's even with or behind the camera, which could cause problems), so you may want to check those once you get it working.

huangapple
  • 本文由 发表于 2023年2月18日 01:35:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定