Debian Codegraph @

src/scene/glsl/generated-pascal/ssr.glsl.inc

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