Jump to content
  • Advertisement
Sign in to follow this  

Problem with reconstruced WorldPosition

This topic is 1043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Somthing is wrong..  but i don't know what.... 
-store WorldViewDistance to R32_F
-Fullscreen Quad (Positions x InvViewProjection)-WorldCamPos to get WorldRay (I'm Using Fullscreen Triangle)
-Normalize Ray
-Scale Ray by Distance from R32_F
-add WorldCamPos
Save Length

Out.vpos   = mul(pos,WorldView).xyz;

Out.ldepth = length(vpos.rgb);

FSTriangle VertexShader

struct VS_OUT
	float4 pos 	: SV_POSITION;
	float2 tex 	: TEXCOORD0;
  	float4 vray     : TEXCOORD1; 

VS_OUT VShader(uint ID : SV_VertexID)
	 VS_OUT Out;

 	float4 p = float4(0,0,0,1);
	float2 t = float2(0,0);
 	p.x = (float)(ID / 2) * 4 - 1;
 	p.y = (float)(ID % 2) * 4 - 1;

 	t.x = (float)(ID / 2) * 2;
	t.y = 1 - (float)(ID % 2) * 2;
 	float4 v = float4(p.x, p.y, 1, 1); //ndc (at the back of cube)
               v = mul(v,InvViewProjection);
               v.xyz -= CamPosition.xyz;
 	Out.pos = p;
	Out.tex = t;
	Out.vray= v;
	return Out;

GBuffer part

// Normalize the view ray, and apply the distance to reconstruct position 
float3 viewRay = normalize(vray); 
float  viewDistance = Source2; //F32 Texture 
float3 positionWS = CamPosition + viewRay * viewDistance; 

But if i write the position and the reconstructed position to ColorTargets, they are not the same



Share this post

Link to post
Share on other sites

What happens when you remove this bit: "v.xyz -= CamPosition.xyz;" at line 26?


edit: my bad - thought you were doing this in view space.

Edited by eppo

Share this post

Link to post
Share on other sites
ok.. i can't get it to work like that.. so my way:

- save viewpos.z to R32_Float Target
- Draw Fullscreenquad\Triangle and get ViewVec (multiply position * InvProjection in VertexShader)
- Scale ViewVec by saved viewpos.z

That's it !

so i'm moving light calculation to ViewSpace.. that's easy and cheap..

multiply ViewPos with InvViewMatrix to get WorldPosition

Share this post

Link to post
Share on other sites

Out.vpos = mul(pos,WorldView).xyz;
Out.ldepth = length(vpos.rgb);


I don't think this is correct. You're storing the length of the view space coordinates, but not the depth (or does WorldView also include the projection matrix?).


Instead, try taking the W component of your projected screen position and scale it by your far plane (hpos.w / farDistance). Then multiply that result with your camera ray (don't normalize the ray). The end result should just be lindepth*camRay+camPos.


At least, this is off the top of my head, been a while since I implemented it.

Share this post

Link to post
Share on other sites

I think you should divide v by w right after line 25 multiplication, like

v = mul(v,InvViewProjection);
v.xyz = (v.xyz / v.w) - CamPosition.xyz;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!