Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » NDC to World Coordinates - Raycast

NDC to World Coordinates - Raycast

View previous topic View next topic Go down  Message [Page 1 of 1]

1 NDC to World Coordinates - Raycast on Wed Nov 02, 2016 11:20 am

mircbone


Member [Lv. 0]
Member [Lv. 0]
Hi!
I am doing a Raycast algorithm in the fragment shader in Unity and for that I need the world coordinates of the camera and the coordinates of each pixel. I have the NDC coordinates of each pixel and I am struggling with transforming it back to world coordinates. So far I have multiplied with the custom-made inverse VP matrix, but I think is not working properly because I tried to debug it by mapping X and Y channels with red and green colors as output to see if the coordinates actually change along with the camera, but nothing seems to change.

The current code is :


Shader "Custom/Camera" {

SubShader{
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform sampler2D _DepthTex;

// float4x4 _CameraToWorld;
struct vertIn {
float4 pos : POSITION;



};
struct vertOut {
float4 pos : SV_POSITION;


};
vertOut vert(vertIn i)
{
vertOut o;

o.pos = mul(UNITY_MATRIX_MVP, i.pos);

return o;
}

float4 frag(vertOut i) : COLOR
{
float imgAsp = _ScreenParams.x / _ScreenParams.y;
float xcord = (((i.pos.x +0.5)/ _ScreenParams.x) * 2 -1)*imgAsp;
float ycord = 1-((i.pos.y + 0.5) / _ScreenParams.y) * 2 ;


float m00 = determinant(float3x3 (UNITY_MATRIX_VP[1][1], UNITY_MATRIX_VP[1][2], UNITY_MATRIX_VP[1][3],
UNITY_MATRIX_VP[2][1], UNITY_MATRIX_VP[2][2], UNITY_MATRIX_VP[2][3],
UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][2], UNITY_MATRIX_VP[3][3]));
float m01 = -determinant(float3x3 (UNITY_MATRIX_VP[1][0], UNITY_MATRIX_VP[1][2], UNITY_MATRIX_VP[1][3],
UNITY_MATRIX_VP[2][0], UNITY_MATRIX_VP[2][2], UNITY_MATRIX_VP[2][3],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][2], UNITY_MATRIX_VP[3][3]));
float m02 = determinant(float3x3 (UNITY_MATRIX_VP[1][0], UNITY_MATRIX_VP[1][1], UNITY_MATRIX_VP[1][3],
UNITY_MATRIX_VP[2][0], UNITY_MATRIX_VP[2][1], UNITY_MATRIX_VP[2][3],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][3]));
float m03 = -determinant(float3x3 (UNITY_MATRIX_VP[1][0], UNITY_MATRIX_VP[1][1], UNITY_MATRIX_VP[1][2],
UNITY_MATRIX_VP[2][0], UNITY_MATRIX_VP[2][1], UNITY_MATRIX_VP[2][2],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][2]));
float m10 = -determinant(float3x3 (UNITY_MATRIX_VP[0][1], UNITY_MATRIX_VP[0][2], UNITY_MATRIX_VP[0][3],
UNITY_MATRIX_VP[2][1], UNITY_MATRIX_VP[2][2], UNITY_MATRIX_VP[2][3],
UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][2], UNITY_MATRIX_VP[3][3]));
float m11 = determinant(float3x3 (UNITY_MATRIX_VP[0][0], UNITY_MATRIX_VP[0][2], UNITY_MATRIX_VP[0][3],
UNITY_MATRIX_VP[2][0], UNITY_MATRIX_VP[2][2], UNITY_MATRIX_VP[2][3],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][2], UNITY_MATRIX_VP[3][3]));
float m12 = -determinant(float3x3 (UNITY_MATRIX_VP[0][0], UNITY_MATRIX_VP[0][1], UNITY_MATRIX_VP[0][3],
UNITY_MATRIX_VP[2][0], UNITY_MATRIX_VP[2][1], UNITY_MATRIX_VP[2][3],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][3]));
float m13 = determinant(float3x3 (UNITY_MATRIX_VP[0][0], UNITY_MATRIX_VP[0][1], UNITY_MATRIX_VP[0][2],
UNITY_MATRIX_VP[2][0], UNITY_MATRIX_VP[2][1], UNITY_MATRIX_VP[2][2],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][2]));
float m20 = determinant(float3x3 (UNITY_MATRIX_VP[0][1], UNITY_MATRIX_VP[0][2], UNITY_MATRIX_VP[0][3],
UNITY_MATRIX_VP[1][1], UNITY_MATRIX_VP[1][2], UNITY_MATRIX_VP[1][3],
UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][2], UNITY_MATRIX_VP[3][3]));
float m21 = -determinant(float3x3 (UNITY_MATRIX_VP[0][0], UNITY_MATRIX_VP[0][2], UNITY_MATRIX_VP[0][3],
UNITY_MATRIX_VP[1][0], UNITY_MATRIX_VP[1][2], UNITY_MATRIX_VP[1][3],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][2], UNITY_MATRIX_VP[3][3]));
float m22 = determinant(float3x3 (UNITY_MATRIX_VP[0][0], UNITY_MATRIX_VP[0][1], UNITY_MATRIX_VP[0][3],
UNITY_MATRIX_VP[1][0], UNITY_MATRIX_VP[1][1], UNITY_MATRIX_VP[1][3],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][3]));
float m23 = -determinant(float3x3 (UNITY_MATRIX_VP[0][0], UNITY_MATRIX_VP[0][1], UNITY_MATRIX_VP[0][2],
UNITY_MATRIX_VP[1][0], UNITY_MATRIX_VP[1][1], UNITY_MATRIX_VP[1][2],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][2]));

float m30 = -determinant(float3x3 (UNITY_MATRIX_VP[0][1], UNITY_MATRIX_VP[0][2], UNITY_MATRIX_VP[0][3],
UNITY_MATRIX_VP[1][1], UNITY_MATRIX_VP[1][2], UNITY_MATRIX_VP[1][3],
UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][2], UNITY_MATRIX_VP[3][3]));
float m31 = determinant(float3x3 (UNITY_MATRIX_VP[0][0], UNITY_MATRIX_VP[0][2], UNITY_MATRIX_VP[0][3],
UNITY_MATRIX_VP[1][0], UNITY_MATRIX_VP[1][2], UNITY_MATRIX_VP[1][3],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][2], UNITY_MATRIX_VP[3][3]));
float m32 = -determinant(float3x3 (UNITY_MATRIX_VP[0][0], UNITY_MATRIX_VP[0][1], UNITY_MATRIX_VP[0][3],
UNITY_MATRIX_VP[1][0], UNITY_MATRIX_VP[1][1], UNITY_MATRIX_VP[1][3],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][3]));
float m33 = determinant(float3x3 (UNITY_MATRIX_VP[0][0], UNITY_MATRIX_VP[0][1], UNITY_MATRIX_VP[0][2],
UNITY_MATRIX_VP[1][0], UNITY_MATRIX_VP[1][1], UNITY_MATRIX_VP[1][2],
UNITY_MATRIX_VP[3][0], UNITY_MATRIX_VP[3][1], UNITY_MATRIX_VP[3][2]));

float4x4 cov = float4x4(m00,m01,m02,m03,m10,m11,m12,m13,m20,m21,m22,m23,m30,m31,m32,m33);



float4x4 invVP = float4x4 (0.4,0.2,-0.8,0.0,-0.4,0.9,0.0,0.0,0.8,0.3,0.4,0.0,5.4,3.0,-0.1,1.0);
float4 worldCord = mul(transpose(cov)/determinant(UNITY_MATRIX_VP), float4(xcord,ycord,-1.0,1.0));
return float4(worldCord.x, worldCord.y, 0.0, 1.0);



}
ENDCG
}
}
}

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum