-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cube.tsx
175 lines (159 loc) · 3.82 KB
/
Cube.tsx
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
import { useEffect, useRef } from 'react';
import { Tucunare, mat4, vec4, FragmentShader, VertexShader } from 'tucunare';
import { MAX_CANVAS_SIZE } from '../constants';
export const Cube = () => {
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
if (!canvasRef.current) {
return;
}
const canvas = canvasRef.current;
const context = canvas.getContext('2d');
if (!context) {
return;
}
const resizeCanvas = () => {
canvas.width =
window.innerWidth < MAX_CANVAS_SIZE
? window.innerWidth
: MAX_CANVAS_SIZE;
canvas.height =
window.innerHeight < MAX_CANVAS_SIZE
? window.innerHeight
: MAX_CANVAS_SIZE;
};
resizeCanvas();
const tc = new Tucunare(canvas);
tc.setClearColor(0, 0, 0, 1);
// uncomment if you want to draw both sides of triangles
// tc.backFaceCullingEnabled = false;
let projectionMatrix: mat4;
const camera = new vec4(0, 0, 5, 1);
const viewMatrix = mat4.translate(-camera.x, -camera.y, -camera.z);
let rotation = 0;
const windowResized = () => {
resizeCanvas();
tc.resize();
projectionMatrix = mat4.perspectiveAspectRatio(
canvas.width / canvas.height,
75,
0.1,
1000,
);
};
window.onresize = windowResized;
windowResized();
const halfSize = 1;
const left = -halfSize;
const right = halfSize;
const bottom = -halfSize;
const top = halfSize;
const near = halfSize;
const far = -halfSize;
const lbn = {
point: new vec4(left, bottom, near, 1),
color: new vec4(1, 0, 0, 1),
};
const rbn = {
point: new vec4(right, bottom, near, 1),
color: new vec4(0, 1, 0, 1),
};
const ltn = {
point: new vec4(left, top, near, 1),
color: new vec4(0, 0, 1, 1),
};
const rtn = {
point: new vec4(right, top, near, 1),
color: new vec4(1, 1, 1, 1),
};
const lbf = {
point: new vec4(left, bottom, far, 1),
color: new vec4(0, 0, 0, 1),
};
const rbf = {
point: new vec4(right, bottom, far, 1),
color: new vec4(1, 1, 0, 1),
};
const ltf = {
point: new vec4(left, top, far, 1),
color: new vec4(0, 1, 1, 1),
};
const rtf = {
point: new vec4(right, top, far, 1),
color: new vec4(1, 0, 1, 1),
};
const data = [
// near
lbn,
rbn,
rtn,
lbn,
rtn,
ltn,
// far
rbf,
lbf,
ltf,
rbf,
ltf,
rtf,
// left
lbf,
lbn,
ltn,
lbf,
ltn,
ltf,
// right
rbn,
rbf,
rtf,
rbn,
rtf,
rtn,
// bottom
lbf,
rbf,
rbn,
lbf,
rbn,
lbn,
// top
ltn,
rtn,
rtf,
ltn,
rtf,
ltf,
];
const points = data.map((item) => item.point);
const colors = data.map((item) => item.color);
const draw = () => {
tc.clear();
const modelMatrix = mat4
.rotateY(rotation)
.multiply(mat4.rotateX(rotation));
const mvp = projectionMatrix.multiply(viewMatrix.multiply(modelMatrix));
const vertShader: VertexShader = (input) => {
return {
position: mvp.multiplyVec4(input.point as vec4),
outputs: {
color: input.color,
},
};
};
const fragShader: FragmentShader = (input) => input.color as vec4;
tc.drawTriangles(
{ point: points, color: colors },
vertShader,
fragShader,
);
// flush the buffer to the canvas
tc.flush();
rotation = (rotation + 0.5) % 360;
};
const interval = setInterval(draw, 1000 / 60);
return () => clearInterval(interval);
}, []);
return <canvas ref={canvasRef}></canvas>;
};