-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
163 lines (142 loc) · 4.19 KB
/
script.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
import { ACamera } from "/Camera.js";
import { TG } from "/TG/TG.js";
// 初始化
const canvas = document.getElementById("canvas0");
canvas.width = 960;
canvas.height = 720;
var tg = new TG(canvas);
var camera = new ACamera(canvas);
// 加载图片
tg.goutouTexture = null;
var image = new Image();
image.onload = () => {
tg.goutouTexture = tg.image2texture(image);
}
image.src = '/src/goutou.png';
// 动画
var frame = 0;
function animate() {
requestAnimationFrame(animate);
frame++;
tg.clear();
var { position, target, mode, fov, near, far } = camera.getParams();
tg.setCamera(position, target, mode, fov, near, far);
var lightDir = [0.0, 0.0, -1.0];
var lightColor = [1.0, 1.0, 1.0];
tg.setLight(lightDir, lightColor);
// 绘制光线
tg.pushModelMatrix();
{
tg.translate(0.0, 0.0, 2.5);
tg.drawArrow([0, 0, 0], lightDir, lightColor)
tg.drawText("light", [lightDir[0] / 2, lightDir[1] / 2, lightDir[2] / 2], "#ffffff", 0.04, 1)
}
tg.popModelMatrix();
// 绘制坐标轴
tg.drawXYZ();
tg.pushModelMatrix();
{
var vertices = [
0.0, 0.0, 0.0, // 0
1.0, 0.0, 0.0, // 1
1.0, 1.0, 0.0, // 2
0.0, 1.0, 0.0, // 3
];
var colors = [
1.0, 0.0, 0.0, // 0
0.0, 1.0, 0.0, // 1
0.0, 0.0, 1.0, // 2
1.0, 1.0, 1.0, // 3
];
var indices = [
0, 1, 2,
0, 2, 3,
];
tg.translate(0.2, -1.2, 0);
tg.drawColorFaces(vertices, colors, indices);
}
tg.popModelMatrix();
tg.pushModelMatrix();
{
var vertices = [
0.0, 0.0, 0.0, // 0
1.0, 0.0, 0.0, // 1
1.0, 1.0, 0.0, // 2
0.0, 1.0, 0.0, // 3
];
var colors = [
1.0, 0.0, 0.0, // 0
0.0, 1.0, 0.0, // 1
0.0, 0.0, 1.0, // 2
1.0, 1.0, 1.0, // 3
];
var normals = [
0.0, 0.0, 1.0, // 0
0.0, 0.0, 1.0, // 1
0.0, 0.0, 1.0, // 2
0.0, 0.0, 1.0, // 3
]
var indices = [
0, 1, 2,
0, 2, 3,
];
tg.translate(-1.2, 0.2, 0);
tg.drawLightColorFaces(vertices, colors, normals, indices);
}
tg.popModelMatrix();
if (tg.goutouTexture) {
tg.pushModelMatrix();
{
var vertices = [
0.0, 0.0, 0.0, // 0 正方形左下角
1.0, 0.0, 0.0, // 1 正方形右下角
1.0, 1.0, 0.0, // 2 正方形右上角
0.0, 1.0, 0.0, // 3 正方形左上角
];
var texCoords = [
0.0, 1.0, // 0 图片左下角
1.0, 1.0, // 1 图片右下角
1.0, 0.0, // 2 图片右上角
0.0, 0.0, // 3 图片左上角
];
var indices = [
0, 1, 2,
0, 2, 3,
];
tg.translate(-1.2, -1.2, 0);
tg.drawTextureFaces(vertices, texCoords, tg.goutouTexture, indices);
}
tg.popModelMatrix();
tg.pushModelMatrix();
{
var vertices = [
0.0, 0.0, 0.0, // 0 正方形左下角
1.0, 0.0, 0.0, // 1 正方形右下角
1.0, 1.0, 0.0, // 2 正方形右上角
0.0, 1.0, 0.0, // 3 正方形左上角
];
var texCoords = [
0.0, 1.0, // 0 图片左下角
1.0, 1.0, // 1 图片右下角
1.0, 0.0, // 2 图片右上角
0.0, 0.0, // 3 图片左上角
];
var normals = [
0.0, 0.0, 1.0, // 0
0.0, 0.0, 1.0, // 1
0.0, 0.0, 1.0, // 2
0.0, 0.0, 1.0, // 3
]
var indices = [
0, 1, 2,
0, 2, 3,
];
tg.translate(0.2, 0.2, 0);
tg.drawLightTextureFaces(vertices, texCoords, normals, tg.goutouTexture, indices);
}
tg.popModelMatrix();
}
}
window.onload = function () {
animate();
};