-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulb.mjs
More file actions
73 lines (60 loc) · 1.48 KB
/
Copy pathbulb.mjs
File metadata and controls
73 lines (60 loc) · 1.48 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
import {
distance,
dot,
magnitude,
normalize,
rotate3DX,
rotate3DY,
add,
sub,
abs,
mul,
} from "./shaderLib.mjs";
import { unionSDF, subtractSDF, rayMarch } from "./marchLib.mjs";
const cos = Math.cos,
sin = Math.sin,
acos = Math.acos,
atan = Math.atan,
pow = Math.pow,
log = Math.log,
sqrt = Math.sqrt;
let globalTime = 0;
/**
* Signed-distance-function for a Mandelbulb.
* @param point {[number, number, number]}
* @returns {number}
*/
const bulbSDF = (point) => {
let pointIter = [...point];
let m = dot(pointIter, pointIter);
let dz = 1.0;
for (let i = 0; i < 4; i++) {
dz = 8.0 * pow(m, 3.5) * dz + 1.0;
const [pointIter_x, pointIter_y, pointIter_z] = pointIter;
const radius = magnitude(pointIter);
const b = 8.0 * acos(pointIter_y / radius);
const angle = 8.0 * atan(pointIter_x, pointIter_z);
pointIter = add(
point,
mul([sin(b) * sin(angle), cos(b), sin(b) * cos(angle)], pow(radius, 8.0))
);
m = dot(pointIter, pointIter);
if (m > 256) break;
}
return (0.25 * log(m) * sqrt(m)) / dz;
};
const sceneSDF = (point) => {
const rotatedPoint = rotate3DY(
rotate3DX(point, Math.sin(globalTime)),
Math.cos(globalTime)
);
return bulbSDF(rotatedPoint);
};
export const fragment = (x, y, time) => {
globalTime = time / 4;
x -= 0.5;
y -= 0.5;
const source = [0, 0, -3 + 2 * Math.sin(globalTime)];
const direction = normalize([x, y, 1]);
return rayMarch(source, direction, sceneSDF);
};