Untitled
Loading...
Searching...
No Matches
depth_pre_pass.frag
Go to the documentation of this file.
1
2#extension GL_ARB_bindless_texture : require
3layout(location = 0)out vec2 gVel;
4layout(location = 1)out vec4 gNormal;
5layout(location = 2)out float gPomDepth;
6layout(location = 3)out vec4 gAlbedo;
7layout(location = 4)out vec4 gExtra;
8layout(location = 5)out vec2 gUV;
9
10layout(std140, binding = 0) uniform CameraData {
11 vec4 cam_pos;
12 vec4 cam_vel;
14 mat4 unjittered_projection;
15 mat4 view;
16 mat4 inv_projection;
17 mat4 unjittered_inv_projection;
18 mat4 inv_view;
19};
20struct Material
21{
22
23 vec4 ambient;
24 vec4 diffuse;
25 vec4 specular;
26 vec4 emissive;
28 uint pad0;
29 uint pad1;
31 uint pad2;
32 uint pad3;
33
34 sampler2D albedo_tex;
35 sampler2D normal_tex;
36 sampler2D arm_tex;
37 sampler2D emissive_tex;
38 // int albedo_tex;
39 // uint pad0;
40 // int normal_tex;
41 // uint pad1;
42 // int arm_tex;
43 // uint pad2;
44 // int emissive_tex;
45 // uint pad3;
46
47 float metallic;
48 float roughness;
49 float ao_strength;
50 float normal_scale;
51 uint alpha_mode;
52 float alpha_cutoff;
53 uint double_sided;
55
56};
57
58
59layout(std430, binding = 2) buffer MaterialData {
60
61 Material materials[];
62
63};
64
65
66
69flat in uint Mat;
71
76const float height_scale = 0.02f;
77uniform sampler2D uAlbedo;
78uniform sampler2D uARM;
79uniform sampler2D uNormal;
80// uniform vec2 uJitterOffset;
81
82vec2 ParallaxMapping(sampler2D normal_map, vec2 texCoords, vec3 viewDir, out float final_depth)
83{
84
85 vec2 dx = dFdx(texCoords);
86 vec2 dy = dFdy(texCoords);
87 const float minLayers = 2.0;
88 const float maxLayers = 16.0;
89
90 float tx_width = float(textureSize(normal_map, 0).x);
91
92 vec2 dx2 = max(dx * dx * tx_width * tx_width, dy * dy * tx_width * tx_width);
93 float mip_level = 0.5 * log2(max(dx2.x, dx2.y));
94
95 float fade = clamp(1.0 - (mip_level / 3.0), 0.0, 1.0);
96
97
98 float current_max_layers = mix(minLayers, maxLayers, fade);
99
100
101 float numLayers = mix(current_max_layers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), viewDir)));
102
103 float layerDepth = 1.0 / numLayers;
104 float currentLayerDepth = 0.0;
105
106
107
108 float uv_scale = length(fwidth(texCoords));
109 // vec2 P = viewDir.xy / max(viewDir.z, 0.001) * height_scale;
110 vec2 P = viewDir.xy *height_scale;
111 vec2 deltaTexCoords = vec2(P.x, -P.y) / numLayers;
112
113 vec2 currentTexCoords = texCoords;
114 float freq = 10;
115 float currentDepthMapValue = textureGrad(normal_map, currentTexCoords, dx, dy).a;
116
117 int safety = 20;
118 int cur = 0;
119 while(currentLayerDepth < currentDepthMapValue && safety > cur)
120 {
121 currentTexCoords -= deltaTexCoords;
122
123
124 currentDepthMapValue = textureGrad(normal_map, currentTexCoords, dx, dy).a;
125 currentLayerDepth += layerDepth;
126 cur++;
127 }
128
129 vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
130 float afterDepth = currentLayerDepth - currentDepthMapValue;
131 float current_depth = textureGrad(normal_map, currentTexCoords, dx, dy).a;
132 float prev_depth = textureGrad(normal_map, prevTexCoords, dx, dy).a;
133 float beforeDepth = (prev_depth) - currentLayerDepth + layerDepth;
134
135 float weight = afterDepth / (afterDepth + beforeDepth);
136 vec2 finalTexCoords = mix(currentTexCoords, prevTexCoords, weight);
137 final_depth = mix(current_depth, prev_depth, weight);
138
139 return finalTexCoords;
140}
141// in vec3 vWorldPos;
142void main()
143{
144
145 Material mat = materials[Mat];
146 vec3 viewDir = normalize(ViewDirTanSpace);
147 vec3 viewDirViewSpace = normalize(ViewPos.xyz);
148 float final_depth = 0.0;
149 vec2 uv = UV;
150 // #ifdef POM
151 // uv = ParallaxMapping(mat.normal_tex, UV, viewDir, final_depth);
152 // #endif
153
154 vec3 displaced_view_pos = ViewPos.xyz + (viewDirViewSpace * (final_depth * height_scale / ViewDirTanSpace.z));
155
156 vec4 clip_space_pos = projection * vec4(displaced_view_pos, 1.0);
157
158 float ndc_depth = clip_space_pos.z / clip_space_pos.w;
159
160 float ndc_screen_depth = ndc_depth * 0.5 + 0.5;
161
162 gPomDepth = ndc_screen_depth;
163
164 gUV = uv;
165 // gl_FragDepth= ndc_screen_depth;
166
167
168 // vec2 uv = UV;
169 gAlbedo = texture(mat.albedo_tex, uv) * mat.diffuse;
170 // gAlbedo = texture(uAlbedo, uv) * mat.diffuse;
171 vec3 map_normal = texture(mat.normal_tex, uv).rgb * 2.0 - 1.0;
172 // vec3 map_normal = texture(uNormal, uv).rgb * 2.0 - 1.0;
173 vec3 N = normalize(TBN * map_normal);
174 // vec3 N = Normal;
175
176 // vec3 ARM = texture(uARM, uv).rgb;
177 vec3 ARM = texture(mat.arm_tex, uv).rgb;
178 gNormal = vec4(N, 1.0);
179 gExtra = vec4(ARM.r, ARM.g * mat.roughness, ARM.b * mat.metallic, mat.emissive_strength);
180 // gExtra = vec4(ARM.r * mat.ao_strength, ARM.g * mat.roughness,0, mat.emissive_strength);
181 // gExtra = vec4(ARM.r * mat.ao_strength, 0.0, 0.7, mat.emissive_strength);
182 // gExtra = vec3(ARM.r, , mat.emissive_strength);
183
184 vec2 current_ndc = ClipPos.xy / ClipPos.w;
185 vec2 previous_ndc = PrevClipPos.xy / PrevClipPos.w;
186
187 vec2 current_uv = current_ndc * 0.5 + 0.5;
188 vec2 previous_uv = previous_ndc * 0.5 + 0.5;
189
190 gVel = (current_uv - previous_uv);
191 // gVel = (current_uv - previous_uv);
192 // gVel = vec2(1,1);
193}
layout(local_size_x=16, local_size_y=16) in
void main()
in vec3 Normal
in vec4 ClipPos
in vec3 ViewDirTanSpace
in vec2 UV
uniform sampler2D uAlbedo
flat in uint Mat
in vec3 ViewPos
uniform sampler2D uARM
const float height_scale
uniform sampler2D uNormal
vec2 ParallaxMapping(sampler2D normal_map, vec2 texCoords, vec3 viewDir, out float final_depth)
in mat3 TBN
in vec4 PrevClipPos
uniform sampler2D gAlbedo
Definition dynamic.frag:219
uniform sampler2D gNormal
Definition dynamic.frag:220
uniform sampler2D gUV
Definition dynamic.frag:230
uniform sampler2D gExtra
Definition dynamic.frag:221
The GPU side SSBO structure.
in vec2 uv
sampler2D albedo_tex
float normal_scale
Unused - Used to determine how much of the texture normal to use.
sampler2D emissive_tex
float emissive_strength
sampler2D arm_tex
sampler2D normal_tex
float alpha_cutoff
Used to discard transparent Materials.
float absorption_coefficients[6]
How much audio is absorbed when hitting this Material.
float scattering_coefficients[6]
How much audio specularly reflects when hitting this Material.
The Materials parsed in from scene files that determine rendering pipeline setup, uploads,...
float clamp(float a, float b, float c)
Definition mathf.h:379
uniform sampler2D gVel
uniform mat4 projection
Definition shadow.vert:15
uniform mat4 view
Definition shadow.vert:16
float x
Definition types.h:11
float w
Definition types.h:13
float z
Definition types.h:13
float y
Definition types.h:11
float z
Definition types.h:12
Definition types.h:20
Definition types.h:21
Definition types.h:11
Definition types.h:12
Definition types.h:13