| 1 |
/** |
| 2 |
* Source code is based on Riccardo Balbo's SSR shader: https://github.com/riccardobl/SimpleSSRShader/ |
| 3 |
*/ |
| 4 |
|
| 5 |
uniform float near; |
| 6 |
uniform float far; |
| 7 |
uniform float defaultSurfaceGlossiness; |
| 8 |
uniform mat4 castle_ViewProjectionMatrix; |
| 9 |
uniform mat4 castle_ViewProjectionMatrixInverse; |
| 10 |
uniform vec3 castle_CameraPosition; |
| 11 |
|
| 12 |
#define INITIAL_STEP_LENGTH 1.0 |
| 13 |
#define RAY_SAMPLES 16 |
| 14 |
#define NEARBY_SAMPLES 4 |
| 15 |
#define DEPTH_TEST_BIAS 0.0001 |
| 16 |
|
| 17 |
/** |
| 18 |
* Ray structure used for ray marching |
| 19 |
*/ |
| 20 |
struct Ray { |
| 21 |
// World position of the surface from where the ray is originated |
| 22 |
vec3 worldFrom; |
| 23 |
// Same as before but in screenspace |
| 24 |
vec3 screenFrom; |
| 25 |
// Glossiness of the surface from where the ray is originated. Default is 1 |
| 26 |
float surfaceGlossiness; |
| 27 |
// Its direction |
| 28 |
vec3 worldDir; |
| 29 |
// The size of one pixel |
| 30 |
vec2 pixelSize; |
| 31 |
}; |
| 32 |
|
| 33 |
/** |
| 34 |
* Returned when the ray hit or miss the scene |
| 35 |
*/ |
| 36 |
struct HitResult { |
| 37 |
// Last tested screen position (-1,-1 if missed) |
| 38 |
vec3 screenPos; |
| 39 |
// How strong the reflection is |
| 40 |
float reflStrength; |
| 41 |
}; |
| 42 |
|
| 43 |
const vec2 nearFade = vec2(0.01, 1.0); |
| 44 |
const vec2 farFade = vec2(20, 300); |
| 45 |
vec2 _SAMPLES[4]; |
| 46 |
|
| 47 |
float getLinearDepth(float depth) { |
| 48 |
return (2.0 * near) / (far + near - depth * (far - near)); |
| 49 |
} |
| 50 |
|
| 51 |
vec2 getPixelSize() { |
| 52 |
return vec2(1.0 / float(screen_width), 1.0 / float(screen_height)); |
| 53 |
} |
| 54 |
|
| 55 |
vec3 worldToScreen(in vec3 worldPos) { |
| 56 |
vec4 pos = castle_ViewProjectionMatrix * vec4(worldPos, 1.0); |
| 57 |
pos.xyz /= pos.w; |
| 58 |
pos.xyz = pos.xyz * 0.5 + 0.5; |
| 59 |
return pos.xyz; |
| 60 |
} |
| 61 |
|
| 62 |
vec3 screenToWorld(in vec3 screenPos){ |
| 63 |
vec4 pos = vec4(screenPos, 1.0) * 2.0 - 1.0; |
| 64 |
pos = castle_ViewProjectionMatrixInverse * pos; |
| 65 |
return pos.xyz /= pos.w; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Calculate normal from depth buffer |
| 70 |
*/ |
| 71 |
vec3 getNormal(in vec3 worldPos, in vec2 pos) { |
| 72 |
vec2 pixelSize = getPixelSize(); |
| 73 |
float depth2 = screenf_01_get_depth(pos + vec2(pixelSize.x, 0.0)); |
| 74 |
float depth3 = screenf_01_get_depth(pos + vec2(0.0, pixelSize.y)); |
| 75 |
vec3 worldPos2 = screenToWorld(vec3(pos + vec2(pixelSize.x, 0.0), depth2)); |
| 76 |
vec3 worldPos3 = screenToWorld(vec3(pos + vec2(0.0, pixelSize.y), depth3)); |
| 77 |
vec3 v1 = worldPos3 - worldPos; |
| 78 |
vec3 v2 = worldPos2 - worldPos; |
| 79 |
return normalize(cross(v1, v2)); |
| 80 |
} |
| 81 |
|
| 82 |
Ray createRay(in vec2 pos, in float depth) { |
| 83 |
Ray ray; |
| 84 |
ray.screenFrom = vec3(pos, depth); |
| 85 |
ray.worldFrom = screenToWorld(ray.screenFrom); |
| 86 |
ray.surfaceGlossiness = defaultSurfaceGlossiness; |
| 87 |
vec3 worldNormal = getNormal(ray.worldFrom, pos); |
| 88 |
// direction from camera to fragment (in world space) |
| 89 |
vec3 worldDir = normalize(ray.worldFrom - castle_CameraPosition); |
| 90 |
// reflection vector |
| 91 |
ray.worldDir = normalize(reflect(worldDir, normalize(worldNormal))); |
| 92 |
ray.pixelSize = getPixelSize(); |
| 93 |
return ray; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Actual ray marching happens here |
| 98 |
*/ |
| 99 |
HitResult performRayMarching(in Ray ray){ |
| 100 |
HitResult result; |
| 101 |
result.screenPos = vec3(-1.0); |
| 102 |
|
| 103 |
// Current position of the sample along the ray |
| 104 |
vec3 sampleWorldPos; |
| 105 |
// Same of before, but in screen space |
| 106 |
vec3 sampleScreenPos; |
| 107 |
// Position of the nearest surface at the sample position (in screen space) |
| 108 |
vec3 hitSurfaceScreenPos; |
| 109 |
// Length of the next step |
| 110 |
float stepLength = INITIAL_STEP_LENGTH; |
| 111 |
|
| 112 |
float linearSourceDepth = getLinearDepth(ray.screenFrom.z); |
| 113 |
|
| 114 |
for (int i = 0; i < RAY_SAMPLES; i++) { |
| 115 |
sampleWorldPos = ray.worldFrom + ray.worldDir * stepLength; |
| 116 |
sampleScreenPos = worldToScreen(sampleWorldPos); |
| 117 |
float depth = screenf_01_get_depth(sampleScreenPos.xy); |
| 118 |
|
| 119 |
hitSurfaceScreenPos = vec3(sampleScreenPos.xy, depth); |
| 120 |
vec3 hitSurfaceWorldPos = screenToWorld(hitSurfaceScreenPos); |
| 121 |
|
| 122 |
int j = 0; |
| 123 |
do { |
| 124 |
// We need to linearize the depth to have consistent tests for distant samples |
| 125 |
float linearHitSurfaceDepth = getLinearDepth(hitSurfaceScreenPos.z); |
| 126 |
float linearSampleDepth = getLinearDepth(sampleScreenPos.z); |
| 127 |
bool hit = |
| 128 |
linearHitSurfaceDepth > linearSourceDepth // check if the thing we want to reflect is behind the source of the ray |
| 129 |
&& abs(linearSampleDepth - linearHitSurfaceDepth) < DEPTH_TEST_BIAS; // check if the ray is (~almost) hitting the surface |
| 130 |
// if first hit (letting the cycle running helds to better performances than breaking it) |
| 131 |
if (hit && result.screenPos.x == -1.0) { |
| 132 |
result.screenPos = sampleScreenPos; |
| 133 |
// Fade distant reflections |
| 134 |
result.reflStrength = distance(hitSurfaceWorldPos, ray.worldFrom); |
| 135 |
result.reflStrength = smoothstep(nearFade.x, nearFade.y, result.reflStrength) |
| 136 |
* (1.0 - smoothstep(farFade.x, farFade.y, result.reflStrength)); |
| 137 |
} |
| 138 |
hitSurfaceScreenPos = vec3(sampleScreenPos.xy, screenf_01_get_depth(sampleScreenPos.xy + _SAMPLES[j].xy * ray.pixelSize)); |
| 139 |
j++; |
| 140 |
} while (j <= NEARBY_SAMPLES); |
| 141 |
// Compute next step length |
| 142 |
stepLength = length(ray.worldFrom - hitSurfaceWorldPos); |
| 143 |
} |
| 144 |
return result; |
| 145 |
} |
| 146 |
|
| 147 |
void main(void) |
| 148 |
{ |
| 149 |
vec2 pos = screenf_01_position; |
| 150 |
vec4 baseColor = screenf_01_get_color(pos); |
| 151 |
float depth = screenf_01_get_depth(pos); |
| 152 |
_SAMPLES[0] = vec2(1.0, 0.0); |
| 153 |
_SAMPLES[1] = vec2(-1.0, 0.0); |
| 154 |
_SAMPLES[2] = vec2(0.0, 1.0); |
| 155 |
_SAMPLES[3] = vec2(0.0, -1.0); |
| 156 |
|
| 157 |
if (depth != 1.0) { // ignore the sky |
| 158 |
// Build the ray |
| 159 |
Ray ray = createRay(pos, depth); |
| 160 |
|
| 161 |
// Perform ray marching |
| 162 |
HitResult result = performRayMarching(ray); |
| 163 |
|
| 164 |
// Used to fade reflections near screen edges to remove artifacts |
| 165 |
float d = distance(result.screenPos.xy, vec2(0.5)); |
| 166 |
d = pow(1.0 - clamp(d, 0.0, 0.5) * 2.0, 2.0); |
| 167 |
|
| 168 |
// Render reflections |
| 169 |
if (result.screenPos.x > -1.0) { |
| 170 |
vec3 color = screenf_01_get_color(result.screenPos.xy).rgb; |
| 171 |
float a = d * ray.surfaceGlossiness * result.reflStrength; |
| 172 |
color = ((1.0 - a) * baseColor.rgb) + (a * color); |
| 173 |
baseColor.rgb = color.rgb; |
| 174 |
} |
| 175 |
} |
| 176 |
gl_FragColor = baseColor; |
| 177 |
} |