Repository navigation
Expand file tree
/
Copy path006_sdf_lighting.frag
More file actions
137 lines (107 loc) · 3.54 KB
/
Copy path006_sdf_lighting.frag
File metadata and controls
137 lines (107 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
daily: 006
author: Will Stallwood
insta: https://www.instagram.com/willstall/
*/
#ifdef GL_ES
precision mediump float;
#endif
#define PI 3.14159265359
#define HALF_PI 1.57079632675
#define TWO_PI 6.283185307
uniform float u_float;
uniform vec2 u_vec2;
uniform vec3 u_vec3;
uniform vec4 u_vec4;
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
uniform sampler2D u_buffer0;
uniform sampler2D u_buffer1;
vec3 FindNormal(sampler2D tex, vec2 uv, float u)
{
//u is one uint size, ie 1.0/texture size
vec2 offsets[4];
offsets[0] = uv + vec2(-u, 0);
offsets[1] = uv + vec2(u, 0);
offsets[2] = uv + vec2(0, -u);
offsets[3] = uv + vec2(0, u);
float hts[4];
for(int i = 0; i < 4; i++)
{
hts[i] = texture2D(tex, offsets[i]).x;
}
vec2 _step = vec2(1.0, 0.0);
vec3 va = normalize( vec3(_step.xy, hts[1]-hts[0]) );
vec3 vb = normalize( vec3(_step.yx, hts[3]-hts[2]) );
return cross(va,vb).rbg; //you may not need to swizzle the normal
}
vec3 palette(float t,vec3 a,vec3 b,vec3 c,vec3 d )
{
return a + b * cos( TWO_PI * (c*t+d));
}
vec2 center(vec2 st)
{
float aspect = u_resolution.x/u_resolution.y;
st.x = st.x * aspect - aspect * 0.5 + 0.5;
return st;
}
vec3 colorize(float d,float t)
{
return palette(d+t,vec3(0.5),
vec3(0.5,0.5,0.5),
vec3(0.5,0.5,0.5),
vec3(0.0,.1,0.2));
}
void main()
{
vec2 st;
vec3 buffer,color;
#ifdef BUFFER_0
st = gl_FragCoord.xy / u_resolution.xy;
st = center(st);
buffer = texture2D(u_buffer0, st).rgb;
// buffer *= 0.9999;
vec2 position = vec2(0.5+0.1*sin(u_time),0.5+0.1*cos(u_time));
// float t = u_time * 1.0;
float d = length(st - position) - .3;
// d = length(st - vec2(0.5)) - .1;
d = abs(sin(d*20.0))-0.6;
// d = step(0.1,d);
d = smoothstep(0.0,0.3,d);
color = vec3(0.0);
color += d;
// color = mix(buffer,color,.03);
// color *= d;
gl_FragColor = vec4(color, 1.0);
#elif defined( BUFFER_1 )
st = gl_FragCoord.xy / u_resolution.xy;
// we generate the normal map here, but we can change it?
vec2 mSt = u_mouse / u_resolution;
float l = abs(length(mSt-vec2(0.5)));
vec2 offset = vec2(0.01 * dot(mSt,vec2(0.5)));
// mSt = abs(length(mSt-0.5));
vec3 normal = FindNormal(u_buffer0,st,0.02);//0.003+0.02*l); remove lighting distance for daily
// normal = step(0.01,normal);
color = normal;
gl_FragColor = vec4(color, 1.0);
#else
st = gl_FragCoord.xy / u_resolution.xy;
buffer = texture2D(u_buffer1, st).rgb;
// buffer.g = 0.0;
// vec3 buffer0 = texture2D(u_buffer0, st).rgb;
color = texture2D(u_buffer0, st).rgb;
vec2 mSt = u_mouse / u_resolution;
float mouse = step(0.01,length(center(st) - center(mSt)) - .001);
vec3 position = vec3(0.5+0.1*sin(u_time)*3.0,0.5+0.1*cos(u_time)*3.0,0.5);
vec3 mPosition = vec3(mSt.x,mSt.y,0.5);
vec3 light = vec3(.5*sin(-u_time),0.5*cos(-u_time),0.5);
light.rg = reflect(mSt,position.rg);
light = cross(mPosition,position);
float l = dot(buffer.rb,light.rg);
float d = min(length(color),length(st-mSt)-3.1);
color = length(color)*length(d) + (step(0.9,d) + vec3(length(d) * l));
color = colorize(length(color),u_time);
gl_FragColor = vec4(color, 1.0);
#endif
}