Skip to content

Commit c75f79e

Browse files
committed
Improve the cuboid normals and vertex calculation
1 parent 5250d32 commit c75f79e

File tree

3 files changed

+133
-116
lines changed

3 files changed

+133
-116
lines changed

src/engine.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,16 @@ export class Engine<T> {
375375
image
376376
);
377377

378-
if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
379-
gl.generateMipmap(gl.TEXTURE_2D);
380-
}
378+
gl.texParameteri(
379+
gl.TEXTURE_2D,
380+
gl.TEXTURE_WRAP_S,
381+
gl.CLAMP_TO_EDGE
382+
);
383+
gl.texParameteri(
384+
gl.TEXTURE_2D,
385+
gl.TEXTURE_WRAP_T,
386+
gl.CLAMP_TO_EDGE
387+
);
381388

382389
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
383390
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
@@ -401,6 +408,8 @@ export class Engine<T> {
401408
obj.texture._computed = {
402409
image,
403410
webglTexture,
411+
width: image.width,
412+
height: image.height,
404413
square:
405414
isPowerOf2(image.width) && isPowerOf2(image.height),
406415
};
@@ -489,12 +498,6 @@ export class Engine<T> {
489498
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
490499
}
491500

492-
const REPEAT_MAP: Record<repeat_mode, number> = {
493-
repeat: gl.REPEAT,
494-
clamp_to_edge: gl.CLAMP_TO_EDGE,
495-
mirrored_repeat: gl.MIRRORED_REPEAT,
496-
};
497-
498501
// Calculate the camera matrixes
499502
const { camera } = this.activeScene;
500503

src/geometries.ts

Lines changed: 119 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -12,118 +12,130 @@ export function rect2D(w: number, h: number) {
1212
}
1313

1414
export function tex2D(w: number, h: number) {
15-
return Flatten([
16-
[0, 0],
17-
[w, h],
18-
[0, h],
19-
[0, 0],
20-
[w, 0],
21-
[w, h],
22-
]);
15+
return [0, 0, w, 0, w, h, 0, 0, w, h, 0, h];
2316
}
2417

25-
export function cuboid(w: number, h: number, d: number) {
26-
return Flatten([
27-
// Front
28-
Vec3(0, 0, 0),
29-
Vec3(0, 0, d),
30-
Vec3(0, h, d),
31-
Vec3(0, h, 0),
32-
Vec3(0, 0, 0),
33-
Vec3(0, h, d),
34-
35-
// Left
36-
Vec3(w, 0, 0),
37-
Vec3(0, 0, 0),
38-
Vec3(w, h, 0),
39-
Vec3(w, h, 0),
40-
Vec3(0, 0, 0),
41-
Vec3(0, h, 0),
42-
43-
// Back
44-
Vec3(w, 0, 0),
45-
Vec3(w, h, 0),
46-
Vec3(w, h, d),
47-
Vec3(w, 0, d),
48-
Vec3(w, 0, 0),
49-
Vec3(w, h, d),
50-
51-
// Right
52-
Vec3(w, h, d),
53-
Vec3(0, 0, d),
54-
Vec3(w, 0, d),
55-
Vec3(w, h, d),
56-
Vec3(0, h, d),
57-
Vec3(0, 0, d),
58-
59-
// Top
60-
Vec3(w, h, d),
61-
Vec3(0, h, 0),
62-
Vec3(0, h, d),
63-
Vec3(w, h, d),
64-
Vec3(w, h, 0),
65-
Vec3(0, h, 0),
66-
67-
// Bottom
68-
Vec3(0, 0, 0),
69-
Vec3(w, 0, d),
70-
Vec3(0, 0, d),
71-
Vec3(w, 0, d),
72-
Vec3(0, 0, 0),
73-
Vec3(w, 0, 0),
74-
]);
18+
export function cuboid(w: number, d: number, h: number) {
19+
return [
20+
w,
21+
d,
22+
0,
23+
0,
24+
d,
25+
0,
26+
0,
27+
d,
28+
h,
29+
w,
30+
d,
31+
0,
32+
0,
33+
d,
34+
h,
35+
w,
36+
d,
37+
h,
38+
w,
39+
0,
40+
h,
41+
w,
42+
d,
43+
h,
44+
0,
45+
d,
46+
h,
47+
w,
48+
0,
49+
h,
50+
0,
51+
d,
52+
h,
53+
0,
54+
0,
55+
h,
56+
0,
57+
0,
58+
h,
59+
0,
60+
d,
61+
h,
62+
0,
63+
d,
64+
0,
65+
0,
66+
0,
67+
h,
68+
0,
69+
d,
70+
0,
71+
0,
72+
0,
73+
0,
74+
0,
75+
0,
76+
0,
77+
w,
78+
0,
79+
0,
80+
w,
81+
0,
82+
h,
83+
0,
84+
0,
85+
0,
86+
w,
87+
0,
88+
h,
89+
0,
90+
0,
91+
h,
92+
w,
93+
0,
94+
0,
95+
w,
96+
d,
97+
0,
98+
w,
99+
d,
100+
h,
101+
w,
102+
0,
103+
0,
104+
w,
105+
d,
106+
h,
107+
w,
108+
0,
109+
h,
110+
0,
111+
0,
112+
0,
113+
0,
114+
d,
115+
0,
116+
w,
117+
d,
118+
0,
119+
0,
120+
0,
121+
0,
122+
w,
123+
d,
124+
0,
125+
w,
126+
0,
127+
0,
128+
];
75129
}
76130

77131
export function cuboidNormals() {
78-
return Flatten([
79-
// Front
80-
Vec3(0, 0, 1),
81-
Vec3(0, 0, 1),
82-
Vec3(0, 0, 1),
83-
Vec3(0, 0, 1),
84-
Vec3(0, 0, 1),
85-
Vec3(0, 0, 1),
86-
87-
// Left
88-
Vec3(-1, 0, 0),
89-
Vec3(-1, 0, 0),
90-
Vec3(-1, 0, 0),
91-
Vec3(-1, 0, 0),
92-
Vec3(-1, 0, 0),
93-
Vec3(-1, 0, 0),
94-
95-
// Back
96-
Vec3(0, 0, -1),
97-
Vec3(0, 0, -1),
98-
Vec3(0, 0, -1),
99-
Vec3(0, 0, -1),
100-
Vec3(0, 0, -1),
101-
Vec3(0, 0, -1),
102-
103-
// Right
104-
Vec3(1, 0, 0),
105-
Vec3(1, 0, 0),
106-
Vec3(1, 0, 0),
107-
Vec3(1, 0, 0),
108-
Vec3(1, 0, 0),
109-
Vec3(1, 0, 0),
110-
111-
// Top
112-
Vec3(0, 1, 0),
113-
Vec3(0, 1, 0),
114-
Vec3(0, 1, 0),
115-
Vec3(0, 1, 0),
116-
Vec3(0, 1, 0),
117-
Vec3(0, 1, 0),
118-
119-
// Bottom
120-
Vec3(0, -1, 0),
121-
Vec3(0, -1, 0),
122-
Vec3(0, -1, 0),
123-
Vec3(0, -1, 0),
124-
Vec3(0, -1, 0),
125-
Vec3(0, -1, 0),
126-
]);
132+
return [
133+
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
134+
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
135+
0, -1, 0, 0, -1, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
136+
0, -1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
137+
-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1,
138+
];
127139
}
128140

129141
export function cylinder(sides, length, dia) {

src/models.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ export type texture = {
115115
webglTexture: WebGLTexture;
116116
image: HTMLImageElement;
117117
square: boolean;
118+
width: number;
119+
height: number;
118120
};
119121
};
120122

0 commit comments

Comments
 (0)