LordXido commited on
Commit
8bee28f
·
verified ·
1 Parent(s): cb68079

Update world.js

Browse files
Files changed (1) hide show
  1. world.js +40 -3
world.js CHANGED
@@ -1,4 +1,41 @@
1
- export function updateWorld(Psi, sigma){
2
- // fractal / field evolution placeholder
3
- // hook WebGL / Mandelbulb here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  }
 
1
+ let field;
2
+ let W = 256, H = 256;
3
+
4
+ export function initWorld() {
5
+ field = new Float32Array(W * H);
6
+ for (let i = 0; i < field.length; i++) {
7
+ field[i] = Math.random();
8
+ }
9
+ }
10
+
11
+ export function updateWorld(Psi, sigma) {
12
+ const next = new Float32Array(field.length);
13
+
14
+ for (let y = 1; y < H - 1; y++) {
15
+ for (let x = 1; x < W - 1; x++) {
16
+ const i = y * W + x;
17
+ const lap =
18
+ field[i - 1] +
19
+ field[i + 1] +
20
+ field[i - W] +
21
+ field[i + W] -
22
+ 4 * field[i];
23
+
24
+ next[i] = field[i] + sigma * lap + Psi * 0.01;
25
+ }
26
+ }
27
+ field = next;
28
+ }
29
+
30
+ export function renderWorld(ctx, canvas) {
31
+ const img = ctx.createImageData(W, H);
32
+ for (let i = 0; i < field.length; i++) {
33
+ const v = Math.max(0, Math.min(1, field[i]));
34
+ img.data[i * 4 + 0] = v * 255;
35
+ img.data[i * 4 + 1] = v * 255;
36
+ img.data[i * 4 + 2] = 255;
37
+ img.data[i * 4 + 3] = 255;
38
+ }
39
+ ctx.putImageData(img, 0, 0);
40
+ ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height);
41
  }