Debian Codegraph @

screensaver.shadertoy/resources/shaders/ball.frag.glsl

raw
1 // See: https://www.shadertoy.com/view/XsfGWN
2
3 // fur ball
4 // (c) simon green 2013
5 // @simesgreen
6 // v1.1
7
8 const float uvScale = 1.0;
9 const float colorUvScale = 0.1;
10 const float furDepth = 0.2;
11 const int furLayers = 64;
12 const float rayStep = furDepth*2.0 / float(furLayers);
13 const float furThreshold = 0.4;
14 const float shininess = 50.0;
15
16 bool intersectSphere(vec3 ro, vec3 rd, float r, out float t)
17 {
18 float b = dot(-ro, rd);
19 float det = b*b - dot(ro, ro) + r*r;
20 if (det < 0.0) return false;
21 det = sqrt(det);
22 t = b - det;
23 return t > 0.0;
24 }
25
26 vec3 rotateX(vec3 p, float a)
27 {
28 float sa = sin(a);
29 float ca = cos(a);
30 return vec3(p.x, ca*p.y - sa*p.z, sa*p.y + ca*p.z);
31 }
32
33 vec3 rotateY(vec3 p, float a)
34 {
35 float sa = sin(a);
36 float ca = cos(a);
37 return vec3(ca*p.x + sa*p.z, p.y, -sa*p.x + ca*p.z);
38 }
39
40 vec2 cartesianToSpherical(vec3 p)
41 {
42 float r = length(p);
43
44 float t = (r - (1.0 - furDepth)) / furDepth;
45 p = rotateX(p.zyx, -cos(iTime*1.5)*t*t*0.4).zyx; // curl
46
47 p /= r;
48 vec2 uv = vec2(atan(p.y, p.x), acos(p.z));
49
50 //uv.x += cos(iTime*1.5)*t*t*0.4; // curl
51 //uv.y += sin(iTime*1.7)*t*t*0.2;
52 uv.y -= t*t*0.1; // curl down
53 return uv;
54 }
55
56 // returns fur density at given position
57 float furDensity(vec3 pos, out vec2 uv)
58 {
59 uv = cartesianToSpherical(pos.xzy);
60 vec4 tex = textureLod(iChannel0, uv*uvScale, 0.0);
61
62 // thin out hair
63 float density = smoothstep(furThreshold, 1.0, tex.x);
64
65 float r = length(pos);
66 float t = (r - (1.0 - furDepth)) / furDepth;
67
68 // fade out along length
69 float len = tex.y;
70 density *= smoothstep(len, len-0.2, t);
71
72 return density;
73 }
74
75 // calculate normal from density
76 vec3 furNormal(vec3 pos, float density)
77 {
78 float eps = 0.01;
79 vec3 n;
80 vec2 uv;
81 n.x = furDensity( vec3(pos.x+eps, pos.y, pos.z), uv ) - density;
82 n.y = furDensity( vec3(pos.x, pos.y+eps, pos.z), uv ) - density;
83 n.z = furDensity( vec3(pos.x, pos.y, pos.z+eps), uv ) - density;
84 return normalize(n);
85 }
86
87 vec3 furShade(vec3 pos, vec2 uv, vec3 ro, float density)
88 {
89 // lighting
90 const vec3 L = vec3(0, 1, 0);
91 vec3 V = normalize(ro - pos);
92 vec3 H = normalize(V + L);
93
94 vec3 N = -furNormal(pos, density);
95 //float diff = max(0.0, dot(N, L));
96 float diff = max(0.0, dot(N, L)*0.5+0.5);
97 float spec = pow(max(0.0, dot(N, H)), shininess);
98
99 // base color
100 vec3 color = textureLod(iChannel1, uv*colorUvScale, 0.0).xyz;
101
102 // darken with depth
103 float r = length(pos);
104 float t = (r - (1.0 - furDepth)) / furDepth;
105 t = clamp(t, 0.0, 1.0);
106 float i = t*0.5+0.5;
107
108 return color*diff*i + vec3(spec*i);
109 }
110
111 vec4 scene(vec3 ro,vec3 rd)
112 {
113 vec3 p = vec3(0.0);
114 const float r = 1.0;
115 float t;
116 bool hit = intersectSphere(ro - p, rd, r, t);
117
118 vec4 c = vec4(0.0);
119 if (hit) {
120 vec3 pos = ro + rd*t;
121
122 // ray-march into volume
123 for(int i=0; i<furLayers; i++) {
124 vec4 sampleCol;
125 vec2 uv;
126 sampleCol.a = furDensity(pos, uv);
127 if (sampleCol.a > 0.0) {
128 sampleCol.rgb = furShade(pos, uv, ro, sampleCol.a);
129
130 // pre-multiply alpha
131 sampleCol.rgb *= sampleCol.a;
132 c = c + sampleCol*(1.0 - c.a);
133 if (c.a > 0.95) break;
134 }
135
136 pos += rd*rayStep;
137 }
138 }
139
140 return c;
141 }
142
143 void mainImage( out vec4 fragColor, in vec2 fragCoord )
144 {
145 vec2 uv = fragCoord.xy / iResolution.xy;
146 uv = uv*2.0-1.0;
147 uv.x *= iResolution.x / iResolution.y;
148
149 vec3 ro = vec3(0.0, 0.0, 2.5);
150 vec3 rd = normalize(vec3(uv, -2.0));
151
152 vec2 mouse = iMouse.xy / iResolution.xy;
153 float roty = 0.0;
154 float rotx = 0.0;
155 if (iMouse.z > 0.0) {
156 rotx = (mouse.y-0.5)*3.0;
157 roty = -(mouse.x-0.5)*6.0;
158 } else {
159 roty = sin(iTime*1.5);
160 }
161
162 ro = rotateX(ro, rotx);
163 ro = rotateY(ro, roty);
164 rd = rotateX(rd, rotx);
165 rd = rotateY(rd, roty);
166
167 fragColor = scene(ro, rd);
168 }