3D河豚鱼—OpenGL着色器(Shader)和GLSL程序
效果图

程序代码
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
uniform vec3 pointers[1];
uniform vec2 resolution;
uniform float time;
vec3 pos_finn, pos_eyes;
vec3 dir_eye;
mat3 dir_mouth;
vec3 dir_light;
float heye, weye, beye;
float hmouth, cmouth;
float hfinns, htail;
float ttime;
float puff;
float tim_tail;
float ani_tail, ani_mouth;
vec3 col_water = vec3(.3, .7, 1.);
vec3 col_fish_1 = vec3(1., 0.4, 0.2);
vec3 col_fish_2 = vec3(1., 0.8, 0.5);
vec3 col_eyes = vec3(0.7, 0.75, 1.);
float t = 20.;
float maxdist = 5.;
float det = .001;
mat2 rot2D(float a) {
a = radians(a);
float s = sin(a);
float c = cos(a);
return mat2(c, s, -s, c);
}
mat3 lookat(vec3 fw, vec3 up) {
fw = normalize(fw);
vec3 rt = normalize(cross(fw, normalize(up)));
return mat3(rt, cross(rt, fw), fw);
}
float fmod(float p, float c) { return abs(c - mod(p, c * 2.)) / c; }
float smin(float a, float b, float k) {
float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
return mix(b, a, h) - k * h * (1.0 - h);
}
float smax(float a, float b, float k) {
float h = clamp(0.5 + 0.5 * (a - b) / k, 0.0, 1.0);
return mix(b, a, h) - k * h * (1.0 - h);
}
float sdTorus(vec3 p, vec2 t, vec3 s) {
p = p.yxz * s;
vec2 q = vec2(length(p.xz) - t.x, p.y);
return length(q) - t.y;
}
float thorns(vec3 p) {
p.xz*=rot2D(-25.);
float s1 = smoothstep(.0, .7, -p.x + p.z + .6);
float s2 = smoothstep(.15, .3, length(p.xy)) * smoothstep(.0, .3, length(p.yz));
float s3 = smoothstep(.0, .25, abs(p.y));
p.x = fmod(atan(p.x, p.y), .31459 / 2.);
p.y = fmod(atan(p.y, p.z), .31459 / 2.);
p.xz*=rot2D(25.);
return min(1., exp((-3. - puff*3.) * length(p.xy))) * s1 * s2 * s3;
}
float spiral(vec3 p, vec3 c) {
p.y = abs(p.y);
vec3 pos = p;
p = lookat(c, vec3(0., 1., 0.)) * p;
float a = length(p.xy) * 35.;
p.yx *= mat2(sin(a), cos(a), -cos(a), sin(a));
float s=pow(abs(p.x), 2.) * smoothstep