~fireclawthefox/panda3dcodecollection/trunk

« back to all changes in this revision

Viewing changes to sfx/shaders/not included/depth-of-field/dof-frag.glsl

  • Committer: Fireclaw the Fox
  • Date: 2017-02-08 14:53:47 UTC
  • Revision ID: fireclawthefox@gmail.com-20170208145347-riqpstgmhgi9n16m
Fixed pyglet sample
removed not working shaders

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Vertex shader
2
 
#version 430
3
 
#pragma debug(on)
4
 
#pragma optimize(off)
5
 
/*
6
 
DoF with bokeh GLSL shader v2.4
7
 
original DoF with Bokeh GLSL shader 2.4 by Martins Upitis (martinsh) (devlog-martinsh.blogspot.com)
8
 
edited by Fireclaw the Fox
9
 
 
10
 
----------------------
11
 
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
12
 
So you are free to share, modify and adapt it for your needs, and even use it for commercial use.
13
 
I would also love to hear about a project you are using it.
14
 
 
15
 
Have fun,
16
 
Martins
17
 
----------------------
18
 
 
19
 
changelog:
20
 
Edited by Fireclaw the Fox to work with Panda3D
21
 
 
22
 
2.4:
23
 
- physically accurate DoF simulation calculated from "focalDepth" ,"focalLength", "f-stop" and "CoC" parameters.
24
 
- option for artist controlled DoF simulation calculated only from "focalDepth" and individual controls for near and far blur
25
 
- added "circe of confusion" (CoC) parameter in mm to accurately simulate DoF with different camera sensor or film sizes
26
 
- cleaned up the code
27
 
- some optimization
28
 
 
29
 
2.3:
30
 
- new and physically little more accurate DoF
31
 
- two extra input variables - focal length and aperture iris diameter
32
 
- added a debug visualization of focus point and focal range
33
 
 
34
 
2.1:
35
 
- added an option for pentagonal bokeh shape
36
 
- minor fixes
37
 
 
38
 
2.0:
39
 
- variable sample count to increase quality/performance
40
 
- option to blur depth buffer to reduce hard edges
41
 
- option to dither the samples with noise or pattern
42
 
- bokeh chromatic aberration/fringing
43
 
- bokeh bias to bring out bokeh edges
44
 
- image thresholding to bring out highlights when image is out of focus
45
 
 
46
 
*/
47
 
 
48
 
uniform sampler2D renderedTexture;
49
 
uniform sampler2D depthTexture;
50
 
uniform float renderedTextureWidth;
51
 
uniform float renderedTextureHeight;
52
 
 
53
 
#define PI  3.14159265
54
 
 
55
 
float width = renderedTextureWidth; //texture width
56
 
float height = renderedTextureHeight; //texture height
57
 
 
58
 
vec2 texel = vec2(1.0/width,1.0/height);
59
 
 
60
 
in vec2 texcoord;
61
 
 
62
 
//uniform variables from external script
63
 
 
64
 
uniform float focalDepth;  //focal distance value in meters, but you may use autofocus option below
65
 
uniform float focalLength; //focal length in mm
66
 
uniform float fstop; //f-stop value
67
 
uniform bool showFocus; //show debug focus point and focal range (red = focal point, green = focal range)
68
 
 
69
 
/*
70
 
make sure that these two values are the same for your camera, otherwise distances will be wrong.
71
 
*/
72
 
 
73
 
float znear = 1; //camera clipping start
74
 
float zfar = 1000; //camera clipping end
75
 
 
76
 
//------------------------------------------
77
 
//user variables
78
 
 
79
 
int samples = 3; //samples on the first ring
80
 
int rings = 3; //ring count
81
 
 
82
 
bool manualdof = false; //manual dof calculation
83
 
float ndofstart = 1.0; //near dof blur start
84
 
float ndofdist = 2.0; //near dof blur falloff distance
85
 
float fdofstart = 1.0; //far dof blur start
86
 
float fdofdist = 3.0; //far dof blur falloff distance
87
 
 
88
 
float CoC = 0.03;//circle of confusion size in mm (35mm film = 0.03mm)
89
 
 
90
 
bool vignetting = true; //use optical lens vignetting?
91
 
float vignout = 1.3; //vignetting outer border
92
 
float vignin = 0.0; //vignetting inner border
93
 
float vignfade = 22.0; //f-stops till vignete fades
94
 
 
95
 
bool autofocus = true; //use autofocus in shader? disable if you use external focalDepth value
96
 
vec2 focus = vec2(0.5,0.5); // autofocus point on screen (0.0,0.0 - left lower corner, 1.0,1.0 - upper right)
97
 
float maxblur = 1.0; //clamp value of max blur (0.0 = no blur,1.0 default)
98
 
 
99
 
float threshold = 0.5; //highlight threshold;
100
 
float gain = 2.0; //highlight gain;
101
 
 
102
 
float bias = 0.5; //bokeh edge bias
103
 
float fringe = 0.7; //bokeh chromatic aberration/fringing
104
 
 
105
 
bool noise = true; //use noise instead of pattern for sample dithering
106
 
float namount = 0.0001; //dither amount
107
 
 
108
 
bool depthblur = false; //blur the depth buffer?
109
 
float dbsize = 1.25; //depthblursize
110
 
 
111
 
/*
112
 
next part is experimental
113
 
not looking good with small sample and ring count
114
 
looks okay starting from samples = 4, rings = 4
115
 
*/
116
 
 
117
 
bool pentagon = false; //use pentagon as bokeh shape?
118
 
float feather = 0.4; //pentagon shape feather
119
 
 
120
 
out vec4 frameBufferColor;
121
 
//------------------------------------------
122
 
 
123
 
 
124
 
float penta(vec2 coords) //pentagonal shape
125
 
{
126
 
    float scale = float(rings) - 1.3;
127
 
    vec4  HS0 = vec4( 1.0,         0.0,         0.0,  1.0);
128
 
    vec4  HS1 = vec4( 0.309016994, 0.951056516, 0.0,  1.0);
129
 
    vec4  HS2 = vec4(-0.809016994, 0.587785252, 0.0,  1.0);
130
 
    vec4  HS3 = vec4(-0.809016994,-0.587785252, 0.0,  1.0);
131
 
    vec4  HS4 = vec4( 0.309016994,-0.951056516, 0.0,  1.0);
132
 
    vec4  HS5 = vec4( 0.0        ,0.0         , 1.0,  1.0);
133
 
 
134
 
    vec4  one = vec4( 1.0 );
135
 
 
136
 
    vec4 P = vec4((coords),vec2(scale, scale));
137
 
 
138
 
    vec4 dist = vec4(0.0);
139
 
    float inorout = -4.0;
140
 
 
141
 
    dist.x = dot( P, HS0 );
142
 
    dist.y = dot( P, HS1 );
143
 
    dist.z = dot( P, HS2 );
144
 
    dist.w = dot( P, HS3 );
145
 
 
146
 
    dist = smoothstep( -feather, feather, dist );
147
 
 
148
 
    inorout += dot( dist, one );
149
 
 
150
 
    dist.x = dot( P, HS4 );
151
 
    dist.y = HS5.w - abs( P.z );
152
 
 
153
 
    dist = smoothstep( -feather, feather, dist );
154
 
    inorout += dist.x;
155
 
 
156
 
    return clamp( inorout, 0.0, 1.0 );
157
 
}
158
 
 
159
 
float bdepth(vec2 coords) //blurring depth
160
 
{
161
 
    float d = 0.0;
162
 
    float kernel[9];
163
 
    vec2 offset[9];
164
 
 
165
 
    vec2 wh = vec2(texel.x, texel.y) * dbsize;
166
 
 
167
 
    offset[0] = vec2(-wh.x,-wh.y);
168
 
    offset[1] = vec2( 0.0, -wh.y);
169
 
    offset[2] = vec2( wh.x -wh.y);
170
 
 
171
 
    offset[3] = vec2(-wh.x,  0.0);
172
 
    offset[4] = vec2( 0.0,   0.0);
173
 
    offset[5] = vec2( wh.x,  0.0);
174
 
 
175
 
    offset[6] = vec2(-wh.x, wh.y);
176
 
    offset[7] = vec2( 0.0,  wh.y);
177
 
    offset[8] = vec2( wh.x, wh.y);
178
 
 
179
 
    kernel[0] = 1.0/16.0;   kernel[1] = 2.0/16.0;   kernel[2] = 1.0/16.0;
180
 
    kernel[3] = 2.0/16.0;   kernel[4] = 4.0/16.0;   kernel[5] = 2.0/16.0;
181
 
    kernel[6] = 1.0/16.0;   kernel[7] = 2.0/16.0;   kernel[8] = 1.0/16.0;
182
 
 
183
 
 
184
 
    for( int i=0; i<9; i++ )
185
 
    {
186
 
                float tmp = texture(depthTexture, coords + offset[i]).r;
187
 
        d += tmp * kernel[i];
188
 
    }
189
 
 
190
 
    return d;
191
 
}
192
 
 
193
 
 
194
 
vec3 color(vec2 coords,float blur) //processing the sample
195
 
{
196
 
    vec3 col = vec3(0.0);
197
 
 
198
 
        col.r = texture(renderedTexture,coords + vec2(0.0,1.0)*texel*fringe*blur).r;
199
 
        col.g = texture(renderedTexture,coords + vec2(-0.866,-0.5)*texel*fringe*blur).g;
200
 
        col.b = texture(renderedTexture,coords + vec2(0.866,-0.5)*texel*fringe*blur).b;
201
 
 
202
 
    vec3 lumcoeff = vec3(0.299,0.587,0.114);
203
 
    float lum = dot(col.rgb, lumcoeff);
204
 
    float thresh = max((lum-threshold)*gain, 0.0);
205
 
    return col+mix(vec3(0.0),col,thresh*blur);
206
 
}
207
 
 
208
 
vec2 rand(vec2 coord) //generating noise/pattern texture for dithering
209
 
{
210
 
    float noiseX = ((fract(1.0-coord.s*(width/2.0))*0.25)+(fract(coord.t*(height/2.0))*0.75))*2.0-1.0;
211
 
    float noiseY = ((fract(1.0-coord.s*(width/2.0))*0.75)+(fract(coord.t*(height/2.0))*0.25))*2.0-1.0;
212
 
 
213
 
    if (noise)
214
 
    {
215
 
        noiseX = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233))) * 43758.5453),0.0,1.0)*2.0-1.0;
216
 
        noiseY = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233)*2.0)) * 43758.5453),0.0,1.0)*2.0-1.0;
217
 
    }
218
 
    return vec2(noiseX,noiseY);
219
 
}
220
 
 
221
 
vec3 debugFocus(vec3 col, float blur, float depth)
222
 
{
223
 
    float edge = 0.002*depth; //distance based edge smoothing
224
 
    float m = clamp(smoothstep(0.0,edge,blur),0.0,1.0);
225
 
    float e = clamp(smoothstep(1.0-edge,1.0,blur),0.0,1.0);
226
 
 
227
 
    col = mix(col,vec3(1.0,0.5,0.0),(1.0-m)*0.6);
228
 
    col = mix(col,vec3(0.0,0.5,1.0),((1.0-e)-(1.0-m))*0.2);
229
 
 
230
 
    return col;
231
 
}
232
 
 
233
 
float linearize(float depth)
234
 
{
235
 
    return -zfar * znear / (depth * (zfar - znear) - zfar);
236
 
}
237
 
 
238
 
float vignette()
239
 
{
240
 
    float dist = distance(texcoord.xy, vec2(0.5,0.5));
241
 
    dist = smoothstep(vignout+(fstop/vignfade), vignin+(fstop/vignfade), dist);
242
 
    return clamp(dist,0.0,1.0);
243
 
}
244
 
 
245
 
void main()
246
 
{
247
 
    //scene depth calculation
248
 
 
249
 
        float depth = linearize(texture(depthTexture,texcoord.xy).x);
250
 
 
251
 
    if (depthblur)
252
 
    {
253
 
        depth = linearize(bdepth(texcoord.xy));
254
 
    }
255
 
 
256
 
    //focal plane calculation
257
 
 
258
 
    float fDepth = focalDepth;
259
 
 
260
 
    if (autofocus)
261
 
    {
262
 
        fDepth = linearize(texture(depthTexture,focus).x);
263
 
    }
264
 
 
265
 
    //dof blur factor calculation
266
 
 
267
 
    float blur = 0.0;
268
 
 
269
 
    if (manualdof)
270
 
    {
271
 
        float a = depth-fDepth; //focal plane
272
 
        float b = (a-fdofstart)/fdofdist; //far DoF
273
 
        float c = (-a-ndofstart)/ndofdist; //near Dof
274
 
        blur = (a>0.0)?b:c;
275
 
    }
276
 
 
277
 
    else
278
 
    {
279
 
        float f = focalLength; //focal length in mm
280
 
        float d = fDepth*1000.0; //focal plane in mm
281
 
        float o = depth*1000.0; //depth in mm
282
 
 
283
 
        float a = (o*f)/(o-f);
284
 
        float b = (d*f)/(d-f);
285
 
        float c = (d-f)/(d*fstop*CoC);
286
 
 
287
 
        blur = abs(a-b)*c;
288
 
    }
289
 
 
290
 
    blur = clamp(blur,0.0,1.0);
291
 
 
292
 
    // calculation of pattern for ditering
293
 
 
294
 
    vec2 noise = rand(texcoord.xy)*namount*blur;
295
 
 
296
 
    // getting blur x and y step factor
297
 
 
298
 
    float w = (1.0/width)*blur*maxblur+noise.x;
299
 
    float h = (1.0/height)*blur*maxblur+noise.y;
300
 
 
301
 
    // calculation of final color
302
 
 
303
 
    vec3 col = vec3(0.0);
304
 
 
305
 
    if(blur < 0.05) //some optimization thingy
306
 
    {
307
 
        col = texture(renderedTexture, texcoord.xy).rgb;
308
 
    }
309
 
 
310
 
    else
311
 
    {
312
 
        col = texture(renderedTexture, texcoord.xy).rgb;
313
 
        float s = 1.0;
314
 
        int ringsamples;
315
 
 
316
 
        for (int i = 1; i <= rings; i += 1)
317
 
        {
318
 
            ringsamples = i * samples;
319
 
 
320
 
            for (int j = 0 ; j < ringsamples ; j += 1)
321
 
            {
322
 
                float step = PI*2.0 / float(ringsamples);
323
 
                float pw = (cos(float(j)*step)*float(i));
324
 
                float ph = (sin(float(j)*step)*float(i));
325
 
                float p = 1.0;
326
 
                if (pentagon)
327
 
                {
328
 
                    p = penta(vec2(pw,ph));
329
 
                }
330
 
                col += color(texcoord.xy + vec2(pw*w,ph*h),blur)*mix(1.0,(float(i))/(float(rings)),bias)*p;
331
 
                s += 1.0*mix(1.0,(float(i))/(float(rings)),bias)*p;
332
 
            }
333
 
        }
334
 
        col /= s; //divide by sample count
335
 
    }
336
 
 
337
 
    if (showFocus)
338
 
    {
339
 
        col = debugFocus(col, blur, depth);
340
 
        }
341
 
 
342
 
    if (vignetting)
343
 
    {
344
 
        col *= vignette();
345
 
    }
346
 
 
347
 
    frameBufferColor.rgb = col;
348
 
    frameBufferColor.a = 1.0;
349
 
}