-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72d59ac
commit 0dd0cac
Showing
11 changed files
with
176 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,78 @@ | ||
#define RAY_LENGTH 32.0 | ||
// References | ||
// https://github.com/MonkeyFirst/Urho3D_SSR_Shader/blob/master/bin/CoreData/Shaders/GLSL/SSR.glsl | ||
|
||
float3 SSR(const float3 baseColor, const float3 position, const float3 reflection, const float roughness) | ||
float2 binarySearch(float3 dir, float3 rayPosition) | ||
{ | ||
float4 projectedCoord; | ||
|
||
[unroll] | ||
for(int i = 16; i > 0; --i) | ||
{ | ||
projectedCoord = mul(float4(rayPosition.xyz, 1.0), uProjection); | ||
projectedCoord.xy = projectedCoord.xy / projectedCoord.w; | ||
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5); | ||
|
||
float4 testPosition = uTexPosition.Sample(uTexPositionSampler, projectedCoord.xy); | ||
float dDepth = rayPosition.z - testPosition.w; | ||
|
||
dir *= 0.5; | ||
if(dDepth > 0.0) | ||
rayPosition -= dir; | ||
else | ||
rayPosition += dir; | ||
} | ||
|
||
return projectedCoord.xy; | ||
} | ||
|
||
float3 SSR(const float3 baseColor, const float4 position, const float3 reflection, const float roughness) | ||
{ | ||
float4 projectedCoord; | ||
|
||
float3 beginPosition; | ||
float3 endPosition; | ||
// convert to view space | ||
float3 rayPosition = mul(float4(position.xyz, 1.0), uView).xyz; | ||
float3 viewReflection = normalize(mul(float4(reflection, 0.0), uView).xyz); | ||
|
||
float3 rayPosition = position; | ||
float4 viewPosition; | ||
|
||
float3 dir = reflection * RAY_LENGTH; | ||
// step 0.5m | ||
float3 dir = viewReflection * 0.5; | ||
|
||
float mipLevel = roughness * 5.0; | ||
|
||
// ray test | ||
float2 ssrUV; | ||
|
||
// RayMarch test | ||
[unroll] | ||
for (int i = 32; i > 0; --i) | ||
{ | ||
// begin ray | ||
beginPosition = rayPosition; | ||
rayPosition += dir; | ||
|
||
// end ray | ||
endPosition = rayPosition + dir; | ||
|
||
// mid ray | ||
rayPosition += dir * 0.5; | ||
|
||
// convert 3d position to 2d texture coord | ||
projectedCoord = mul(float4(rayPosition.xyz, 1.0), uViewProjection); | ||
// convert 3d position to 2d texture coord | ||
projectedCoord = mul(float4(rayPosition.xyz, 1.0), uProjection); | ||
projectedCoord.xy = projectedCoord.xy / projectedCoord.w; | ||
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5); | ||
|
||
float3 testPosition = uTexPosition.Sample(uTexPositionSampler, projectedCoord.xy).xyz; | ||
ssrUV = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5); | ||
float4 testPosition = uTexPosition.Sample(uTexPositionSampler, ssrUV); | ||
|
||
float3 d1 = testPosition - beginPosition; | ||
float lengthSQ1 = d1.x*d1.x + d1.y*d1.y + d1.z*d1.z; | ||
|
||
float3 d2 = testPosition - endPosition; | ||
float lengthSQ2 = d2.x*d2.x + d2.y*d2.y + d2.z*d2.z; | ||
|
||
// beginPosition is nearer | ||
if (lengthSQ1 < lengthSQ2) | ||
float depthDiff = rayPosition.z - testPosition.w; | ||
if(depthDiff >= 0.0) | ||
{ | ||
rayPosition = beginPosition; | ||
ssrUV = binarySearch(dir, rayPosition); | ||
break; | ||
} | ||
|
||
// binary search test | ||
dir *= 0.5; | ||
} | ||
|
||
// z clip when camera look down | ||
float z = mul(float4(reflection, 0.0), uView).z; | ||
z = clamp(z, 0.0, 1.0); | ||
|
||
// convert 3d position to 2d texture coord | ||
float3 color = uTexLastFrame.SampleLevel(uTexLastFrameSampler, projectedCoord.xy, 0).rgb; | ||
float3 color = uTexLastFrame.SampleLevel(uTexLastFrameSampler, ssrUV, mipLevel).rgb; | ||
|
||
// edge factor | ||
float2 dCoords = smoothstep(float2(0.0, 0.0), float2(0.5, 0.5), abs(float2(0.5, 0.5) - projectedCoord.xy)); | ||
float2 dCoords = smoothstep(float2(0.0, 0.0), float2(0.5, 0.5), abs(float2(0.5, 0.5) - ssrUV)); | ||
float screenEdgefactor = clamp(1.0 - (dCoords.x + dCoords.y), 0.0, 1.0); | ||
|
||
return lerp(baseColor * 0.8, color, screenEdgefactor * z); | ||
|
||
// test | ||
// return float3(screenEdgefactor * z, screenEdgefactor * z, screenEdgefactor * z); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.