-
Notifications
You must be signed in to change notification settings - Fork 0
/
raymarch_common.frag
156 lines (139 loc) · 5.25 KB
/
raymarch_common.frag
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#define TWOPI 6.2831853
float minimum(float x, float y) {return min(x,y);}
float minimum(float x, float y, float z) {return min(min(x,y), z);}
float minimum(float x, float y, float z, float w) {return min(min(x,y), min(z,w));}
float minimum(float x, float y, float z, float w, float s) {return min(min(x,y), minimum(z,w,s));}
float minimum(float x, float y, float z, float w, float s, float t) {return minimum(min(x,y), min(z,w), min(s,t));}
float minimum(float x, float y, float z, float w, float s, float t, float u) {
return minimum(minimum(x,y,z), min(w,s), min(t,u));
}
float minimum(float x, float y, float z, float w, float s, float t, float u, float v) {
return min(minimum(x,y,z,w), minimum(s,t,u,v));
}
float maximum(float x, float y) {return max(x,y);}
float maximum(float x, float y, float z) {return max(max(x,y), z);}
float maximum(float x, float y, float z, float w) {return max(max(x,y), max(z,w));}
float maximum(float x, float y, float z, float w, float s) {return max(max(x,y), maximum(z,w,s));}
float maximum(float x, float y, float z, float w, float s, float t) {return maximum(max(x,y), max(z,w), max(s,t));}
float maximum(float x, float y, float z, float w, float s, float t, float u) {
return maximum(maximum(x,y,z), max(w,s), max(t,u));
}
float maximum(float x, float y, float z, float w, float s, float t, float u, float v) {
return max(maximum(x,y,z,w), maximum(s,t,u,v));
}
mat2 rot2d(float t) {
return mat2(cos(t), sin(t), -sin(t), cos(t));
}
struct Light2 {
vec3 Colour;
vec2 Location;
};
struct Light3 {
vec3 Colour;
vec3 Location;
};
struct Light4 {
vec3 Colour;
vec4 Location;
};
#define EPSILON .00001
#define EPS vec2(EPSILON, 0.)
#define grad2(f,v) (vec2( f((v)+EPS.xy) - f((v)-EPS.xy),\
f((v)+EPS.yx) - f((v)-EPS.yx)\
)/(2.*EPSILON))
#define grad3(f,v) (vec3( f((v)+EPS.xyy) - f((v)-EPS.xyy),\
f((v)+EPS.yxy) - f((v)-EPS.yxy),\
f((v)+EPS.yyx) - f((v)-EPS.yyx)\
)/(2.*EPSILON))
#define grad4(f,v) (vec4( f((v)+EPS.xyyy) - f((v)-EPS.xyyy),\
f((v)+EPS.yxyy) - f((v)-EPS.yxyy),\
f((v)+EPS.yyxy) - f((v)-EPS.yyxy),\
f((v)+EPS.yyyx) - f((v)-EPS.yyyx)\
)/(2.*EPSILON))
vec2 complexPower(vec2 z, float n) {
float t = atan(z.y, z.x);
float r = length(z);
return pow(r,n)*vec2(cos(n*t), sin(n*t));
}
// Multiply up to five complex numbers.
vec2 complexProduct(vec2 p, vec2 q) {
return vec2(p.x*q.x-p.y*q.y, p.x*q.y+p.y*q.x);
}
vec2 complexProduct(vec2 p, vec2 q, vec2 r) {
return complexProduct(p,complexProduct(q,r));
}
vec2 complexProduct(vec2 p, vec2 q, vec2 r, vec2 s) {
return complexProduct(complexProduct(p,q),complexProduct(r,s));
}
vec2 complexProduct(vec2 p, vec2 q, vec2 r, vec2 s, vec2 t) {
return complexProduct(complexProduct(p,q),complexProduct(r,s), t);
}
// Multiply up to four quaternions.
vec4 quaternionProduct(vec4 p, vec4 q) {
return vec4(-dot(p.yzw,q.yzw), cross(p.yzw, q.yzw)) + p.x*q + q.x*p;
//return vec4(p.x*q.x - p.y*q.y - p.z*q.z - p.w*q.w,
// p.x*q.y + p.y*q.x + p.z*q.w - p.w*q.z,
// p.x*q.z + p.z*q.x + p.w*q.x - p.x*q.w,
// p.x*q.w + p.w*q.x + p.y*q.z - p.z*q.y
// );
}
vec4 quaternionProduct(vec4 p, vec4 q, vec4 r) {
return quaternionProduct(p,quaternionProduct(q,r));
}
vec4 quaternionProduct(vec4 p, vec4 q, vec4 r, vec4 s) {
return quaternionProduct(quaternionProduct(p,q),quaternionProduct(r,s));
}
float dCube(vec2 v, vec2 d) {
return .5*length(abs(v)-d + abs(abs(v)-d));
}
float dCube(vec3 v, vec3 d) {
return .5*length(abs(v)-d + abs(abs(v)-d));
}
float dCube(vec4 v, vec4 d) {
return .5*length(abs(v)-d + abs(abs(v)-d));
}
// Constant times L1 distance to orthoplex.
// Lower bound for Euclidean (L2) distance to orthoplex.
float dOrthoplex(vec2 v, float d) {
v = abs(v);
return .70710678*(v.x+v.y-d);
}
float dOrthoplex(vec3 v, float d) {
v = abs(v);
return .577350269*(v.x+v.y+v.z-d);
}
float dOrthoplex(vec4 v, float d) {
v = abs(v);
return .5*(v.x+v.y+v.z+v.w-d);
}
float dSphere(vec2 v, vec2 c, float r) {
return length(v-c)-r;
}
float dSphere(vec3 v, vec3 c, float r) {
return length(v-c)-r;
}
float dSphere(vec4 v, vec4 c, float r) {
return length(v-c)-r;
}
float dTorus(vec3 v, float R, float r) {
return length(vec2(length(v.xy)-R, v.z))-r;
}
float dTorus(vec4 v, float r1, float r2, float r) {
return length(vec2(length(v.xy)-r1, length(v.zw)-r2))-r;
}
float dSquareTorus(vec3 v, float R, float r) {
return dCube(vec2(dCube(v.xy,vec2(R)), v.z), vec2(r));
}
float dSquareTorus(vec4 v, float r1, float r2, float r) {
//return dCube(vec2(length(v.xy)-r1, dCube(v.zw,vec2(r2))), vec2(r));
//return length(vec2(length(v.xy)-r1, dCube(v.zw,vec2(r2)))) -r;
return length(vec2(length(v.xy)-r1, dOrthoplex(v.zw,r2))) -r;
//return length(vec2(dOrthoplex(v.xy,r1), dOrthoplex(v.zw,r2)))-r;
}
// Cartesian product of sphere and circle.
float dSphircle(vec4 v, float R, float r) {
return length(vec2(length(v.xyz)-R, v.w))-r;
}
float dSphircle2(vec4 v, float R, float r) {
return length(vec2(length(v.xyz), v.w-R))-r;
}