Untitled
Loading...
Searching...
No Matches
coc.frag
Go to the documentation of this file.
1
2#version 430 core
3
4layout(std140, binding = 0) uniform CameraData {
5 vec4 cam_pos;
6 vec4 cam_vel;
8 mat4 unjittered_projection;
10 mat4 inv_projection;
11 mat4 unjittered_inv_projection;
12 mat4 inv_view;
13};
14
16
18
19uniform sampler2D gDepth;
20uniform sampler2D uScene;
21
22vec3 get_view_pos_from_depth(vec2 uv, float depth, mat4 invProjMatrix) {
23 vec4 ndc = vec4(uv * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
24
25 vec4 viewSpacePos = invProjMatrix * ndc;
26
27 return viewSpacePos.xyz / viewSpacePos.w;
28}
29
30float linearize(float z)
31{
32 float near = 0.1;
33 float far = 100.0;
34 return (2.0 * near) / (far + near - z * (far - near));
35}
36
37uniform float uFocalLength;
38uniform float uFStop;
39uniform float uSensorHeight;
40uniform float uMaxBlurRadius;
41
42struct MeshNode
43{
52 uint first;
53 uint count;
54 uint pad0;
55 uint pad1;
56};
57
58#define MAX_MESH_NODES 1024
59layout(std430, binding = 15) buffer MeshNodeData {
60
61 float focal_distance;
62 uint pad0;
63 uint pad1;
64 uint pad2;
66
67};
68
69void main() {
70 float rawDepth = texture(gDepth, vUV).r;
71
72 vec3 viewPos = get_view_pos_from_depth(vUV, rawDepth, unjittered_inv_projection);
73
74 float pixelDistance = -(viewPos.z);
75
76 float coeff = (uFocalLength * uFocalLength) / (uFStop * (focal_distance - uFocalLength));
77 float cocMeters = coeff * ((pixelDistance - focal_distance) / pixelDistance);
78
79 float cocUV = cocMeters / uSensorHeight;
80
81 float finalCoC = clamp(cocUV, -uMaxBlurRadius, uMaxBlurRadius);
82
83 fragColor = vec4(textureLod(uScene, vUV, 0).rgb, finalCoC);
84
85
86}
layout(local_size_x=16, local_size_y=16) in
void main()
uniform sampler2D uScene
Definition bloom.frag:3
out vec4 fragColor
Definition bloom.frag:6
uniform sampler2D gDepth
Definition coc.frag:19
uniform float uFocalLength
Definition coc.frag:37
vec3 get_view_pos_from_depth(vec2 uv, float depth, mat4 invProjMatrix)
Definition coc.frag:22
float linearize(float z)
Definition coc.frag:30
uniform float uSensorHeight
Definition coc.frag:39
uniform float uMaxBlurRadius
Definition coc.frag:40
uniform float uFStop
Definition coc.frag:38
#define MAX_MESH_NODES
Maximum MeshNodes in a scene (used for SSBO sizing).
in vec2 uv
uint pad1
Definition coc.frag:55
uint first
Definition coc.frag:52
vec4 min
Definition coc.frag:44
uint material
Definition coc.frag:51
vec4 max
Definition coc.frag:45
uint lm_offset
Definition coc.frag:50
uint pad0
Definition coc.frag:54
vec4 center
Definition coc.frag:46
uint instance_count
Definition coc.frag:48
bool static_mesh
If this mesh is static (DrawArrays) or dynamic (DrawElements), used to indicate Lightmap status.
size_t count
Count of vertices pushed into the MAIN_VBO.
vec4 extents
Definition coc.frag:47
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
in vec2 vUV
Definition lightmap.frag:13
float clamp(float a, float b, float c)
Definition mathf.h:379
uniform mat4 projection
Definition shadow.vert:15
uniform mat4 view
Definition shadow.vert:16
float w
Definition types.h:13
float z
Definition types.h:12
Definition types.h:21
Definition types.h:11
Definition types.h:12
Definition types.h:13