-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2.html
95 lines (84 loc) · 3.14 KB
/
v2.html
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
<html>
<body>
<canvas id='canvas' width='800' height='600'
style='border: 1px solid black;'></canvas>
<script>
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
// Define the world by an array of faces. Each face consists of an array of
// vertices (3D points) in the format [x, y, z].
let face_vertices = [
// This is a cube.
[[-2, 0, 1], [-2, 0, 2], [-1, 0, 2], [-1, 0, 1]],
[[-2, 1, 1], [-2, 1, 2], [-1, 1, 2], [-1, 1, 1]],
[[-2, 0, 1], [-2, 0, 2], [-2, 1, 2], [-2, 1, 1]],
[[-1, 0, 1], [-1, 0, 2], [-1, 1, 2], [-1, 1, 1]],
[[-2, 0, 1], [-1, 0, 1], [-1, 1, 1], [-2, 1, 1]],
[[-2, 0, 2], [-1, 0, 2], [-1, 1, 2], [-2, 1, 2]],
// This is another cube.
[[1, 0, 1], [1, 0, 2], [2, 0, 2], [2, 0, 1]],
[[1, 1, 1], [1, 1, 2], [2, 1, 2], [2, 1, 1]],
[[1, 0, 1], [1, 0, 2], [1, 1, 2], [1, 1, 1]],
[[2, 0, 1], [2, 0, 2], [2, 1, 2], [2, 1, 1]],
[[1, 0, 1], [2, 0, 1], [2, 1, 1], [1, 1, 1]],
[[1, 0, 2], [2, 0, 2], [2, 1, 2], [1, 1, 2]],
// This is a square pyramid.
[[-0.5, 0, 4], [-0.5, 0, 5], [0.5, 0, 5], [0.5, 0, 4]],
[[-0.5, 0, 4], [-0.5, 0, 5], [0.0, 1, 4.5]],
[[-0.5, 0, 5], [0.5, 0, 5], [0.0, 1, 4.5]],
[[0.5, 0, 5], [0.5, 0, 4], [0.0, 1, 4.5]],
[[0.5, 0, 4], [-0.5, 0, 4], [0.0, 1, 4.5]],
];
// Define an RGB color for each of the faces in face_vertices.
let face_colors = [
[230, 25, 75],
[60, 180, 75],
[255, 225, 25],
[67, 99, 216],
[245, 130, 49],
[145, 30, 180],
[70, 240, 240],
[240, 50, 230],
[188, 246, 12],
[250, 190, 190],
[0, 128, 128],
[230, 190, 255],
[154, 99, 36],
[0, 0, 0],
[128, 0, 0],
[0, 0, 117],
[128, 128, 128],
];
// Only vertices with x-coordinates in the range -x_clip <--> x_clip will be
// rendered.
let x_clip = 3;
// Draw each face using a simple orthographic projection.
for (let i = 0; i < face_vertices.length; i++) {
let vertices = face_vertices[i];
let color = face_colors[i];
// Convert color (e.g. [255, 0, 0]) to an rgb string (e.g. 'rgb(255,0,0)').
ctx.fillStyle = 'rgb(' + color[0] + ',' + color[1] + ',' + color[2] + ')';
ctx.beginPath();
// Iterate over each vertex in the face.
for (let j = 0; j < vertices.length; j++) {
let vertex = vertices[j];
// Project the 3D vertex to 2D using "orthographic projection". This
// means we ignore the z-coordinate and just plot the x- and
// y-coordinates, scaled up by some amount. The amount we scale by is
// determined by how large the viewing frustum we want to be. This is
// determined by the x_clip parameter.
// Note that we are offsetting from the center coordinate of the screen
// which is (canvas.width / 2, canvas.height / 2).
let scale = (canvas.width / 2) / x_clip;
let x = canvas.width / 2 + vertex[0] * scale;
let y = canvas.height / 2 - vertex[1] * scale;
// If it is the first vertex of the face, we must "move to it",
// otherwise draw a line from the last vertex.
if (j == 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.fill();
}
</script>
</body>
</html>