-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.js
188 lines (168 loc) · 5.11 KB
/
main.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//////////////////////// WebAssembly Code ////////////////////////
let instance = [null, null]
let memoryStates = new WeakMap();
let mode = 1
let buffer = null
function startWebAssembly(index, wasmFilePath, drawPixel) {
function syscall(instance, n, args) {
switch (n) {
default:
// console.log("Syscall " + n + " NYI.");
break;
case /* brk */ 45: return 0;
case /* writev */ 146:
return instance.exports.writev_c(args[0], args[1], args[2]);
case /* mmap2 */ 192:
// debugger;
const memory = instance.exports.memory;
let memoryState = memoryStates.get(instance);
const requested = args[1];
if (!memoryState) {
memoryState = {
object: memory,
currentPosition: memory.buffer.byteLength,
};
memoryStates.set(instance, memoryState);
}
let cur = memoryState.currentPosition;
if (cur + requested > memory.buffer.byteLength) {
const need = Math.ceil((cur + requested - memory.buffer.byteLength) / 65536);
memory.grow(need);
}
memoryState.currentPosition += requested;
return cur;
}
}
let s = "";
fetch(wasmFilePath).then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, {
env: {
__syscall0: function __syscall0(n) { return syscall(instance[index], n, []); },
__syscall1: function __syscall1(n, a) { return syscall(instance[index], n, [a]); },
__syscall2: function __syscall2(n, a, b) { return syscall(instance[index], n, [a, b]); },
__syscall3: function __syscall3(n, a, b, c) { return syscall(instance[index], n, [a, b, c]); },
__syscall4: function __syscall4(n, a, b, c, d) { return syscall(instance[index], n, [a, b, c, d]); },
__syscall5: function __syscall5(n, a, b, c, d, e) { return syscall(instance[index], n, [a, b, c, d, e]); },
__syscall6: function __syscall6(n, a, b, c, d, e, f) { return syscall(instance[index], n, [a, b, c, d, e, f]); },
putc_js: function (c) {
c = String.fromCharCode(c);
if (c == "\n") {
console.log(s);
s = "";
} else {
s += c;
}
},
drawGuiLitePixel: function (x, y, color) {
drawPixel(x, y, color)
}
}
})
).then(results => {
instance[index] = results.instance
instance[index].exports.main(mode)
}).catch(console.error);
}
//////////////////////// Cube Code ////////////////////////
const cubeSize = 180;
const gain = 1;
const animationFrames = 200;
let graphic1, graphic2, graphic3;
function setup() {
createCanvas(500, 500, WEBGL);
angleMode(DEGREES);
graphic1 = createGraphics(240, 320);
graphic2 = createGraphics(240, 320);
graphic3 = createGraphics(240, 320);
graphic3.background(210, 0, 0)
graphic3.fill(255)
graphic3.textSize(64)
graphic3.text(' 👑\nGuiLite\n on 3D', 20, 100);
startWebAssembly(0, 'libs/particle.wasm', drawPixelOnGraphic1);
startWebAssembly(1, 'libs/wave.wasm', drawPixelOnGraphic2);
}
const faces = [
[0, 0, 0, '255, 0, 0'],
[0, 90, 0, '0, 255, 0'],
[0, 180, 0, '255, 0, 0'],
[0, -90, 0, '0, 255, 0'],
[90, 0, 0, '0, 0, 255'],
[270, 0, 0, '0, 0, 255'],
];
function draw() {
const progress = min(frameCount / animationFrames, 1);
background('gray');
noStroke();
rotateX(-30);
rotateY(frameCount);
if(progress == 1) {
updateGraphic(0);
updateGraphic(1);
}
faces.forEach((face, i) => {
textureFace(face, i, progress)
push();
rotateX(face[0] * progress);
rotateY(face[1] * progress);
rotateZ(face[2] * progress);
translate(0, 0, cubeSize / 2 * gain * progress);
plane(cubeSize);
pop();
});
}
function updateGraphic(index) {
if(!instance[index]){
return;
}
if (index == 0){
var pointer = instance[index].exports.updateHelloParticle()
var graphic = graphic1
} else{
var pointer = instance[index].exports.updateHelloWave()
var graphic = graphic2
}
if(mode != 0){
return;
}
if(buffer == null){
buffer = new Uint8Array(instance[index].exports.memory.buffer)
}
for(let y = 0; y < 320; y++) {
for(let x = 0; x < 240; x++) {
graphic.stroke('rgb(' + buffer[pointer + 1] + ', ' + buffer[pointer + 2] + ', ' + buffer[pointer + 3] + ')')
graphic.rect(x, y, 1, 1);
pointer = pointer + 4;
}
}
}
function textureFace(face, i, progress) {
if(progress < 1) {
return fill(`rgba(${face[3]}, 0.5)`);
}
if(i == 0 || i == 2) {
} else {
}
switch(i){
case 0:
case 2:
texture(graphic2)
break;
case 1:
case 3:
texture(graphic1)
break;
default:
texture(graphic3)
break;
}
}
function drawPixelOnGraphic1(x, y, color) {
graphic1.stroke('rgb(' + ((color & 0xff0000) >> 16) + ', ' + ((color & 0xff00) >> 8) + ', ' + (color & 0xff) + ')')
graphic1.rect(x, y, 1, 1);
}
function drawPixelOnGraphic2(x, y, color) {
graphic2.stroke('rgb(' + ((color & 0xff0000) >> 16) + ', ' + ((color & 0xff00) >> 8) + ', ' + (color & 0xff) + ')')
graphic2.rect(x, y, 1, 1);
}