• Advertisement

Lotes

Member
  • Content count

    8
  • Joined

  • Last visited

Community Reputation

131 Neutral

About Lotes

  • Rank
    Newbie
  1. I finally fixed it :-). The view space position was not computed correctly. And the disk radius was wrong. Screenshot appended... //from: http://graphics.cs.williams.edu/papers/AlchemyHPG11/ #define PI 3.14159265 //Important: http://msdn.microsoft.com/en-us/library/bb509632%28v=vs.85%29.aspx cbuffer ConstBuffer : register(c0) { float4x4 inverseProjection; float cameraFOV; float cameraZNear; float cameraZFar; float width; float height; } static const float halfFOV = cameraFOV / 2; static const int samplesCount = 16; static const int spiralTurns = 10; static const float radius = 0.10; static const float beta = 0.2; static const float epsilon = 0.01; Texture2D normalDepthTexture: register(t0); SamplerState textureSampler { Filter = MIN_MAG_MIP_LINEAR; AddressU = Wrap; AddressV = Wrap; }; struct VS_INPUT { float3 position : POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD; }; struct VS_OUTPUT { float4 position : SV_POSITION; float2 uv : TEXCOORD; }; VS_OUTPUT VShader(VS_INPUT i) { VS_OUTPUT vo; vo.position = float4(i.position, 1); vo.uv = i.uv; return vo; } float4 readNormalDepth(float2 uv) { return normalDepthTexture.SampleLevel(textureSampler, uv, 0.0f); } float3 readNormal(float2 uv) { return (readNormalDepth(uv).xyz - 0.5) * 2; } float readDepth(float2 uv) { return readNormalDepth(uv).a; } float linearDepth(float d) { //http://www.humus.name/temp/Linearize%20depth.txt //returns a value between 0 (near) and 1 (far) return d / (cameraZFar - d * (cameraZFar - cameraZNear)); } float3 getViewPosition(float2 uv) { float depth = readDepth(uv); float4 screenCoordinate = 2 * (float4(uv.x, 1-uv.y, depth, 1) - 0.5); float4 viewCoordinate = mul(inverseProjection, screenCoordinate); return viewCoordinate.xyz / viewCoordinate.w; } float3 getPosition(float2 uv) { return getViewPosition(uv); } float2 tapLocation(int sampleNumber, float spinAngle) { float alpha = float(sampleNumber + 0.5) * (1.0 / samplesCount); float angle = alpha * (spiralTurns * 2 * PI) + spinAngle; float radius = alpha; return radius * float2(cos(angle), sin(angle)); } float4 PShader(VS_OUTPUT i) : SV_Target { float ao = 0; float3 currentPoint = getPosition(i.uv); float currentDepth = linearDepth(readDepth(i.uv)); float3 normal = readNormal(i.uv); float2 pixelPosition = i.uv * float2(width, height); float random = (3*int(pixelPosition.x)^int(pixelPosition.y)+int(pixelPosition.x)*int(pixelPosition.y))*10.0; float diskRadius = radius / (2 * (currentDepth * (cameraZFar - cameraZNear) + cameraZNear) * tan(halfFOV)); [unroll] for(int sampleIndex=0; sampleIndex < samplesCount; sampleIndex++) { float2 offset = tapLocation(sampleIndex, random); float2 samplePosition = i.uv + diskRadius * offset; float3 samplePoint = getPosition(samplePosition); float3 v = samplePoint - currentPoint; float vv = dot(v, v); float vn = dot(v, normal); if(currentDepth < 1) //do not AO for far plane ao += max(0, vn + currentDepth * 0.001) / (vv + epsilon); } ao *= 2.0 * PI * radius * beta / samplesCount; ao = min(1, max(0, 1 - ao)); return float4(ao, ao, ao, 1); }
  2. I now think the problem is, that on the even surfaces the dot product of v and the normal (for one sample) is not zero. The "getViewPosition" function must be wrong... This function should reconstruct the world space coordinate from texture coordinate (uv) and the depth buffer. My current version of "getWorldPosition" is something like that (it is not correct so far):   float3 getWorldPosition(float2 uv) {     float depth = readDepth(uv);     float4 projectionPosition = float4(uv, depth, 1);     float4 worldPosition = mul(inverseProjection, projectionPosition);         worldPosition /= worldPosition.w;         worldPosition = mul(inverseView, worldPosition);             return worldPosition.xyz; } What is the right way of reconstructing the world space coordinate?
  3. I want to implement the Alchemy SSAO algorithm (see http://graphics.cs.williams.edu/papers/AlchemyHPG11/). Some code is taken from here: http://www.gamedev.net/topic/648090-alchemy-ambient-occlusion/ But my shader is inverted on some shapes (see screenshot). I don't know where to search for the bug. Can someone help me? Here is my shader code... The input is a texture with normals in world space (rgb) and depth (a) and texture with noise. The noise texture is currently not read. Two user constants are defined for the texture width and texture height. #define PI 3.14159265 cbuffer ConstBuffer : register(c0) { float width; //texture width float height; //texture height } static const int samplesCount = 16; static const int spiralTurns = 1; static const float radius = 0.25; static const float epsilon = 0.01; static const float zNear = 0.1; static const float zFar = 500; static const float fov = 45.0 / 180.0 * PI; static const float2 focalLength = float2(1.0/tan(fov*0.5) * height / width, 1.0/tan(fov*0.5)); Texture2D normalDepthTexture: register(t0); Texture2D randomTexture: register(t1); SamplerState textureSampler { Filter = MIN_MAG_MIP_LINEAR; AddressU = Wrap; AddressV = Wrap; }; struct VS_INPUT { float3 position : POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD; }; struct VS_OUTPUT { float4 position : SV_POSITION; float2 uv : TEXCOORD; }; VS_OUTPUT VShader(VS_INPUT i) { VS_OUTPUT vo; vo.position = float4(i.position, 1); vo.uv = i.uv; return vo; } float4 readNormalDepth(float2 uv) { return normalDepthTexture.SampleLevel(textureSampler, uv, 0.0f); } float3 readNormal(float2 uv) { return normalize((readNormalDepth(uv).xyz - 0.5) * 2); } float readDepth(float2 uv) { return readNormalDepth(uv).a; } float linearDepth(float d) { d = d*2.0 - 1.0; float2 lin = float2((zNear-zFar)/(2.0*zNear*zFar), (zNear+zFar)/(2.0*zNear*zFar)); return -1.0/(lin.x*d+lin.y); } float3 getViewPosition(float2 uv) { float z = linearDepth(readDepth(uv)); float2 UVtoViewB = float2(1.0 / focalLength.x, 1.0 / focalLength.y); float2 UVtoViewA = 2 * UVtoViewB; return float3((UVtoViewA * uv - UVtoViewB) * z, z); } float2 tapLocation(int sampleNumber, float spinAngle) { float alpha = float(sampleNumber + 0.5) * (1.0 / samplesCount); float angle = alpha * (spiralTurns * 2 * PI) + spinAngle; float radius = alpha; return radius * float2(cos(angle), sin(angle)); } float4 PShader(VS_OUTPUT i) : SV_Target { float ao = 0; float3 currentPoint = getViewPosition(i.uv); float3 normal = readNormal(i.uv); float2 pixelPosition = float2(i.uv.x * width, i.uv.y * height); float random = (3*int(pixelPosition.x)^int(pixelPosition.y)+int(pixelPosition.x)*int(pixelPosition.y))*10.0; //randomTexture.SampleLevel(textureSampler, i.uv, 0.0f).x; float diskRadius = 0.5 * focalLength.y * radius / currentPoint.z; [unroll] for(int sampleIndex=0; sampleIndex < samplesCount; sampleIndex++) { float2 offset = tapLocation(sampleIndex, random); float2 samplePosition = i.uv + diskRadius * offset; float3 samplePoint = getViewPosition(samplePosition); float3 v = samplePoint - currentPoint; float vv = dot(v, v); float vn = dot(v, normal); ao += max(0, vn + currentPoint.z * 0.001) / (vv + epsilon); } ao *= 2.0 * PI * radius * 0.05 / samplesCount; ao *= 5; //makes ssao darker ao = min(1, max(0, 1 - ao)); return float4(ao, ao, ao, 1); }
  4. Render depth to texture

    It works with this code. But I find it very ugly to use a magic number like 25 as a factor in this case. How can I calculate this factor from my matrices? :-/ [CODE] VertexToPixel PositionsVS( float4 inPos : POSITION, float3 inNormal: NORMAL, float4 inColor: COLOR) { VertexToPixel Output = (VertexToPixel)0; float4x4 preViewProjection = mul (xView, xProjection); float4x4 preWorldViewProjection = mul (xWorld, preViewProjection); Output.Position = mul(inPos, preWorldViewProjection); Output.Color = 1; Output.Color.rgb = Output.Position.z; return Output; } PixelToFrame PositionsPS(VertexToPixel PSIn) { PixelToFrame Output = (PixelToFrame)0; Output.Color = PSIn.Color/25; return Output; } [/CODE] My cubes have a dimension of 0.9x0.9x0.9 at positions (0,0,-10), (0,0,-9) ... (0,0,10). My camera is at (0,10,-20) looking at (0,0,0).
  5. Render depth to texture

    they are drawn (black cubes on blue background). changing 200 to 200.0 did not help. i think in hlsl it does not matter.
  6. Render depth to texture

    Here is it... [CODE] VertexToPixel PositionsVS( float4 inPos : POSITION) { VertexToPixel Output = (VertexToPixel)0; float4x4 preWorldView= mul (xWorld, xView); Output.Position = mul(inPos, preWorldView); Output.Color = 1; Output.Color.rgb = Output.Position.z/200; Output.Position = mul(Output.Position,xProjection); return Output; } PixelToFrame PositionsPS(VertexToPixel PSIn) { PixelToFrame Output = (PixelToFrame)0; Output.Color = PSIn.Color; return Output; } [/CODE]
  7. Render depth to texture

    Thanks. My far clip plane has a distance of 200.0f. Is that the value for zFar? When I tried out your code my cubes became entirely black :-/.
  8. Hi, I work with XNA and I want to render the depth information of my scene into a texture. But all I get is my scene (some cubes) completly colored in white. So the depth information I determined is greater or equal 1.0 I guess. How is it done right? I googled a lot, but I can't find my mistake :'-(... Do I have to make any settings in XNA before rendering? Here are the HLSL shaders I use: [CODE] float4x4 xView; float4x4 xProjection; float4x4 xWorld; struct VertexToPixel { float4 Position : POSITION; float4 Color : COLOR0; }; struct PixelToFrame { float4 Color : COLOR0; }; VertexToPixel PositionsVS( float4 inPos : POSITION) { float4x4 preViewProjection = mul (xView, xProjection); float4x4 preWorldViewProjection = mul (xWorld, preViewProjection); VertexToPixel Output = (VertexToPixel)0; Output.Position = mul(inPos, preWorldViewProjection); Output.Color.a = 1; Output.Color.rgb = Output.Position.z/Output.Position.w; return Output; } PixelToFrame PositionsPS(VertexToPixel PSIn) { PixelToFrame Output = (PixelToFrame)0; Output.Color = PSIn.Color; return Output; } technique Positions { pass Pass0 { VertexShader = compile vs_3_0 PositionsVS(); PixelShader = compile ps_3_0 PositionsPS(); } } [/CODE]
  • Advertisement