• Advertisement
Sign in to follow this  

Problem with reconstruced WorldPosition

This topic is 888 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

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

//Pixel
Out.ldepth = length(vpos.rgb);

 
 
 
FSTriangle VertexShader

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

//-----------------------------------------------------------------------------
//SHADER
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; 
	
Out.Position=positionWS

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

[sharedmedia=gallery:albums:1019]

 

Share this post


Link to post
Share on other sites
Advertisement

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


//Vertex
Out.vpos = mul(pos,WorldView).xyz;
 
//Pixel
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