<p><span class="math-container">$$
\def\mvec#1{\begin{pmatrix}#1\end{pmatrix}}
\def\ivec#1{\left(\begin{smallmatrix}#1\end{smallmatrix}\right)}
\def\mmat#1{\begin{bmatrix}#1\end{bmatrix}}
\def\vec#1{\mathrm{\mathbf{#1}}}
\def\mat#1{\mathrm{\mathbf{#1}}}
$$</span>
If you have the screen-space x and y coordinates as well as the depth of your fragment, you can directly compute the coordinates of the corresponding point in world-space. To do so, first compute the clip-space coordinates of your fragment
<span class="math-container">\begin{equation}
 \vec p'' = \mvec{
  2\cdot\frac{x}{r_x} - 1 \\
  2\cdot\frac{y}{r_y} - 1 \\
  2 \cdot z - 1 \\
  1
 }
\end{equation}</span>
where <span class="math-container">$x$</span> and <span class="math-container">$y$</span> are the screen-space (pixel) coordinates of the fragment, <span class="math-container">$z$</span> is the fragment depth, and <span class="math-container">$r_x$</span> and <span class="math-container">$r_y$</span> are the width and height (resolution) of the viewport. The above computation is assuming that you're using OpenGL with the default depth range. If you're using Direct3D, use <span class="math-container">$z$</span> directly without applying a scaling and offset. If you're using a depth range different from the default, apply whatever is the the appropriate inverse transformation in your case to get from screen-space to clip-space <span class="math-container">$z$</span>.</p>

<p>Once you have the clip-space coordinates of your fragment, you can simply unproject the fragment by transforming with the inverse view-projection matrix:
<span class="math-container">\begin{equation}
 \vec p' = \mat M^{-1} \vec p''
\end{equation}</span>
where <span class="math-container">$\mat M$</span> would be your view-projection matrix. (Note: you will most likely want to pass the inverse view-projection matrix to the shader as a uniform as inverting the view-projection matrix in the shader for every fragment would most likely introduce quite some unnecessary overhead.)</p>

<p>The final step is then to homogenize to get the world-space coordinates of the point
<span class="math-container">\begin{equation}
 \vec p = \frac{1}{p'_w} \cdot \mvec{
  p'_x \\
  p'_y \\
  p'_z
 }.
\end{equation}</span></p>

<p>By the way: It would seem to me that rendering this "infinite plane" should be possible using just a single triangle with all corners at infinity in different directions instead of four triangles. Since the boundary of your polygon is pushed to infinity, it shouldn't matter how many edges it has&hellip;</p>

CC BY-SA 4.0
2019-01-06T04:23:57.240Z
This makes sense, but is there any way to do this without computing the world-space position in the fragment shader? It seems kind of brute-force, but I understand if that's the only realistic solution here. _Edit: I ask because I've been able to generate some symmetric patterns on the plane already without sending any additional data to the shaders._
softbodies
CC BY-SA 4.0
2019-01-06T04:26:29.020Z
I believe the reason there are four tris is because there are four "vanishing" points to contend with with regards to the infinite plane. If you only have one triangle, it would not be possible to cover the entire space?
softbodies
CC BY-SA 4.0
2019-01-03T21:30:47.527Z
2019-01-04T13:53:13.523Z
2019-01-04T13:53:13.523Z
Michael Kenzel
<p>I am rendering an infinite plane as described in the following answers:</p>

<ol>
<li><a href="https://stackoverflow.com/questions/12965161/rendering-infinitely-large-plane">https://stackoverflow.com/questions/12965161/rendering-infinitely-large-plane</a></li>
<li><a href="https://stackoverflow.com/questions/7380250/how-to-render-a-plane-of-seemingly-infinite-size">https://stackoverflow.com/questions/7380250/how-to-render-a-plane-of-seemingly-infinite-size</a></li>
</ol>

<p>Specifically, I am rendering an "infinite" XZ plane using the following five 4D vertices:
<span class="math-container">$$(0, 0, 0, 1)\quad(1, 0, 0, 0)\quad(0, 0, 1, 0)\quad(-1, 0, 0, 0)\quad(0, 0, -1, 0)$$</span></p>

<p>In my engine, this is displayed as a ground plane at origin.</p>

<p>What I would like to do is texture this plane. For now, it would be nice to just have a checkered grid. In my fragment shader, I am doing this via the varying <span class="math-container">$x$</span> and <span class="math-container">$z$</span> positions. This works fine for other "non-infinite" meshes, but for the infinite plane, it is incorrect. This is because the interpolated positions that are passed to the fragment shader don't really make sense in world space, as far as I can tell.</p>

<p>How can I compute the world-space position coordinates in my fragment shader?
Is it possible to compute them from the vertex positions, since they represent points at infinity?</p>

<p>The next step would be UV coordinates, but as I understand that might not be possible. I am interested more in being able to easily procedurally texture the plane rather than apply an image to it.</p>

<rendering><shader><texture><geometry><interpolation>
Texturing an "infinite" plane
softbodies
# Texturing an "infinite" plane

(Asked by: username_0 on 2019-01-03)

I am rendering an infinite plane as described in the following answers:

1. <https://stackoverflow.com/questions/12965161/rendering-infinitely-large-plane>
2. <https://stackoverflow.com/questions/7380250/how-to-render-a-plane-of-seemingly-infinite-size>

Specifically, I am rendering an "infinite" XZ plane using the following five 4D vertices:
$$(0, 0, 0, 1)\quad(1, 0, 0, 0)\quad(0, 0, 1, 0)\quad(-1, 0, 0, 0)\quad(0, 0, -1, 0)$$

In my engine, this is displayed as a ground plane at origin.

What I would like to do is texture this plane. For now, it would be nice to just have a checkered grid. In my fragment shader, I am doing this via the varying $x$ and $z$ positions. This works fine for other "non-infinite" meshes, but for the infinite plane, it is incorrect. This is because the interpolated positions that are passed to the fragment shader don't really make sense in world space, as far as I can tell.

How can I compute the world-space position coordinates in my fragment shader?
Is it possible to compute them from the vertex positions, since they represent points at infinity?

The next step would be UV coordinates, but as I understand that might not be possible. I am interested more in being able to easily procedurally texture the plane rather than apply an image to it.

**Tags:** rendering, shader, texture, geometry, interpolation


---

## 1 answers

### Answer by username_1 on 2019-01-03. Score: 2

$$
\def\mvec#1{\begin{pmatrix}#1\end{pmatrix}}
\def\ivec#1{\left(\begin{smallmatrix}#1\end{smallmatrix}\right)}
\def\mmat#1{\begin{bmatrix}#1\end{bmatrix}}
\def\vec#1{\mathrm{\mathbf{#1}}}
\def\mat#1{\mathrm{\mathbf{#1}}}
$$
If you have the screen-space x and y coordinates as well as the depth of your fragment, you can directly compute the coordinates of the corresponding point in world-space. To do so, first compute the clip-space coordinates of your fragment
\begin{equation}
\vec p'' = \mvec{
2\cdot\frac{x}{r\_x} - 1 \\
2\cdot\frac{y}{r\_y} - 1 \\
2 \cdot z - 1 \\
1
}
\end{equation}
where $x$ and $y$ are the screen-space (pixel) coordinates of the fragment, $z$ is the fragment depth, and $r\_x$ and $r\_y$ are the width and height (resolution) of the viewport. The above computation is assuming that you're using OpenGL with the default depth range. If you're using Direct3D, use $z$ directly without applying a scaling and offset. If you're using a depth range different from the default, apply whatever is the the appropriate inverse transformation in your case to get from screen-space to clip-space $z$.

Once you have the clip-space coordinates of your fragment, you can simply unproject the fragment by transforming with the inverse view-projection matrix:
\begin{equation}
\vec p' = \mat M^{-1} \vec p''
\end{equation}
where $\mat M$ would be your view-projection matrix. (Note: you will most likely want to pass the inverse view-projection matrix to the shader as a uniform as inverting the view-projection matrix in the shader for every fragment would most likely introduce quite some unnecessary overhead.)

The final step is then to homogenize to get the world-space coordinates of the point
\begin{equation}
\vec p = \frac{1}{p'\_w} \cdot \mvec{
p'\_x \\
p'\_y \\
p'\_z
}.
\end{equation}

By the way: It would seem to me that rendering this "infinite plane" should be possible using just a single triangle with all corners at infinity in different directions instead of four triangles. Since the boundary of your polygon is pushed to infinity, it shouldn't matter how many edges it has…

> **Comments:**
> **username_0**: This makes sense, but is there any way to do this without computing the world-space position in the fragment shader? It seems kind of brute-force, but I understand if that's the only realistic solution here. _Edit: I ask because I've been able to generate some symmetric patterns on the plane already without sending any additional data to the shaders._  
> **username_0**: I believe the reason there are four tris is because there are four "vanishing" points to contend with with regards to the infinite plane. If you only have one triangle, it would not be possible to cover the entire space?  

---

