# how to use czm\_inverseViewProjection matrix?

**URL:** <https://community.cesium.com/t/how-to-use-czm-inverseviewprojection-matrix/7284>\
**Category:** CesiumJS\
**Created:** [August 10, 2018, 7:31am UTC](https://community.cesium.com/t/how-to-use-czm-inverseviewprojection-matrix/7284 "2018-08-10T07:31:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leerom](https://avatars.discourse-cdn.com/v4/letter/l/9fc348/32.png) [@leerom](https://community.cesium.com/u/leerom)\
**Post date:** [August 10, 2018, 7:31am UTC](https://community.cesium.com/t/how-to-use-czm-inverseviewprojection-matrix/7284/1 "2018-08-10T07:31:09Z")

</div>

In my post precessing shader code，I want to get the screen point to world coordinates. I found "AutomaticUniforms.js" have a "czm\_inverseViewProjection" variable，and document say:" It's An automatic GLSL uniform representing a 4x4 view-projection transformation matrix that transforms clip coordinates to world coordinates." So in the post pressing fragment Shader, I use code like this:

float depth = texture2D(depthTexture, v\_textureCoordinates).r;  
vec2 xy = vec2((depthTexture.x \* 2.0 - 1.0), ((1.0 - depthTexture.y) \* 2.0 - 1.0));  
vec4 posInWorld = czm\_inverseViewProjection \* vec4(xy, depth, 1.0);  
posInWorld = posInWorld / posInWorld.w;

But when I check the variable "posInWold", that seem not equal to the point world coordinates？  
what's problem？

---

<div class="post-metadata">

**Author:** ![Gabby\_Getz](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/gabby_getz/32/7838_2.png) [@Gabby\_Getz](https://community.cesium.com/u/Gabby_Getz)\
**Post date:** [August 14, 2018, 9:41pm UTC](https://community.cesium.com/t/how-to-use-czm-inverseviewprojection-matrix/7284/2 "2018-08-14T21:41:14Z")

</div>

You can get the position in eye coordinates by applying **czm\_inverseProjection** , then convert it to world coordinates with something like the following:

vec4 worldCoordinate4 = czm\_inverseView \* eyeCoordinate;

vec3 worldCoordinate = worldCoordinate4.xyz / worldCoordinate4.w;

``

Thanks!

Gabby

---

<div class="post-metadata">

**Author:** ![leerom](https://avatars.discourse-cdn.com/v4/letter/l/9fc348/32.png) [@leerom](https://community.cesium.com/u/leerom)\
**Post date:** [August 16, 2018, 2:21pm UTC](https://community.cesium.com/t/how-to-use-czm-inverseviewprojection-matrix/7284/3 "2018-08-16T14:21:01Z")

</div>

Thanks your help  
But in "Apps/Sandcastle/index.html?src=3D%20Tiles%20Feature%20Picking.html" demo. I write post processing shader code like this:

void main(void)  
{  
&nbsp;&nbsp;&nbsp;&nbsp;vec4 finalColor = texture2D(colorTexture, v\_textureCoordinates);

&nbsp;&nbsp;&nbsp;&nbsp;// get position clip coordinates  
&nbsp;&nbsp;&nbsp;&nbsp;vec2 uv = vec2(v\_textureCoordinates);  
&nbsp;&nbsp;&nbsp;&nbsp;float depth = czm\_readDepth(depthTexture, uv);  
&nbsp;&nbsp;&nbsp;&nbsp;vec2 xy = vec2((uv.x \* 2.0 - 1.0), ((1.0 - uv.y) \* 2.0 - 1.0));

&nbsp;&nbsp;&nbsp;&nbsp;// get position in eye coordinates  
&nbsp;&nbsp;&nbsp;&nbsp;vec4 eyeCoordinate = czm\_inverseProjection \* vec4(xy, depth, 1.0);  
&nbsp;&nbsp;&nbsp;&nbsp;eyeCoordinate = eyeCoordinate / eyeCoordinate.w;

&nbsp;&nbsp;&nbsp;&nbsp;// get position in world coordinates  
&nbsp;&nbsp;&nbsp;&nbsp;vec4 worldCoordinate4 = czm\_inverseView \* eyeCoordinate;  
&nbsp;&nbsp;&nbsp;&nbsp;vec3 worldCoordinate = worldCoordinate4.xyz / worldCoordinate4.w;  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;// use ellipsoid equation to test the position is on ellipsoid surface?  
&nbsp;&nbsp;&nbsp;&nbsp;vec3 radii = vec3(6378137.0, 6378137.0, 6356752.314245);  
&nbsp;&nbsp;&nbsp;&nbsp;vec3 inverseRadii = vec3(1.0 / radii.x, 1.0 / radii.y, 1.0 / radii.z);  
&nbsp;&nbsp;&nbsp;&nbsp;vec3 scaled = inverseRadii \* worldCoordinate;  
&nbsp;&nbsp;&nbsp;&nbsp;float one = dot(scaled, scaled);  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;if (one \>= 1.0)  
&nbsp;&nbsp;&nbsp;&nbsp;{// Isn't on ellipsoid surface, draw original color  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gl\_FragColor = finalColor;  
&nbsp;&nbsp;&nbsp;&nbsp;}else  
&nbsp;&nbsp;&nbsp;&nbsp;{// On ellipsoid surface, draw red color  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gl\_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n\  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

I want just show 3D build model and draw red to earth surface，but the picture isn't right. Do your have any suggest?

---

<div class="post-metadata">

**Author:** ![Gabby\_Getz](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/gabby_getz/32/7838_2.png) [@Gabby\_Getz](https://community.cesium.com/u/Gabby_Getz)\
**Post date:** [August 20, 2018, 9:18pm UTC](https://community.cesium.com/t/how-to-use-czm-inverseviewprojection-matrix/7284/4 "2018-08-20T21:18:59Z")

</div>

Are you using terrain? Terrain won’t match the shape of the WSG84 ellipsoid exactly.
