1

I'm trying to render my fluid simulator with liquid effect,

here is my render result: enter image description here

but I want to get this result

enter image description here

here is my geometry and pixel shader

[maxvertexcount(4)] 
void mainGS(point GSPS_INPUT gInput[1],inout TriangleStream<GSPS_OUTPUT> TriStream) 
{ 


  float size = 0.065; 
  matrix mv = View; 
  float3 right  = normalize(float3(mv._11,mv._21,mv._31));  
  float3 up    = normalize(float3(mv._12,mv._22,mv._32)); 
  // 
  float3 posEye = mul( float4(gInput[0].Pos.xyz, 1.0),world).xyz; 
  // 
  float halfWidth  = size/length(posEye); 
  float halfHeight = size/length(posEye); 
  // 
  float4 v[4]; 
  v[0] = float4(gInput[0].Pos + halfWidth*right - halfHeight*up, 1.0f); 
  v[1] = float4(gInput[0].Pos + halfWidth*right + halfHeight*up, 1.0f); 
  v[2] = float4(gInput[0].Pos - halfWidth*right - halfHeight*up, 1.0f); 
  v[3] = float4(gInput[0].Pos - halfWidth*right + halfHeight*up, 1.0f); 
  // 
  // 
  GSPS_OUTPUT output; 
  [unroll] 
  for(int i=0; i<4; ++i) 
  { 

    // 
    output.Pos    = mul(v[i], View); 
    output.PosW    = (output.Pos.xyz); 

    output.Pos    = mul(output.Pos, Projection); 
    output.Tex    = gQuadTexC[i]; 
    TriStream.Append(output); 
  } 
  TriStream.RestartStrip(); 
} 

//pixel shader 

float4 PS(GSPS_OUTPUT input) : SV_TARGET 
{ 
  float3 N; 
  N.xy = input.Tex*float2(2.0f,-2.0f) + float2(-1.0f,1.0f); 
  float mag = dot(N.xy, N.xy); 
  if (mag > 1.0)  
  { 
    discard; 
  } 

  N.z = sqrt(1.0f-mag); 
  N = N * 0.5 + 0.5; 


  float4 pixelPos = float4(input.PosW + N*(1/32), 1.0); 
  float4 clipSpacePos = mul(pixelPos, Projection); 
  float depthval = (clipSpacePos.z / clipSpacePos.w);

I found here this, but it's not fully explanation Link

Community
  • 1
  • 1
Alatriste
  • 527
  • 2
  • 6
  • 21
  • I can't remember enough OpenGL to make a real answer to this, so comment it is. That looks quite close actually, what if you try enabling anti-aliasing? The real image just appears like yours, but with a fog effect too. – slugonamission Jun 09 '14 at 22:39
  • second image have not fog effect .. – Alatriste Jun 09 '14 at 22:44
  • It is unclear whether you are asking how to set the depth value in the pixel(fragment) shader or if you are asking if you are calculating the per pixel depth value correctly. You tagged this with both OpenGL and DirectX. Are you looking for a general solution or something specific to one of those? – user3256930 Jun 10 '14 at 00:25
  • 4
    You cannot, sprites are planar by nature. What you want is actually called an impostor. It will write depth values in such a way that even though the geometry all exists in a plane, the depth is representative of a sphere. You will still actually be rendering sprites, they will just behave like a surface with varying depth. Keep in mind, doing this will break a lot of hardware optimizations because the depth written in the pixel/fragment shader is no longer the same as what was generated during rasterization (so early depth tests are not possible). – Andon M. Coleman Jun 10 '14 at 00:51
  • what about that? http://stackoverflow.com/questions/12350624/how-do-i-calculate-pixel-shader-depth-to-render-a-circle-drawn-on-a-point-sprite – Alatriste Jun 10 '14 at 15:53
  • I used SV_Depth semantic with SV_target and I work's well but it's slow down perfomance... – Alatriste Jun 11 '14 at 15:14
  • Yes, that is what I was talking about in the last sentence of my comment. It destroys early Z testing. If you have DX11 hardware though, you can use conservative depth (basically guarantee that the depth will only be changed in one direction) to re-enable some hardware optimizations that are normally disabled when you write an arbitrary value to the depth buffer. – Andon M. Coleman Jun 12 '14 at 07:34
  • conservative depth? what you mean? – Alatriste Jun 15 '14 at 09:32

0 Answers0