MVP矩阵变换

  1. 1. MVP矩阵变换
    1. 1.1. 前置工作
    2. 1.2. 模型矩阵(M)
    3. 1.3. 视图矩阵(V)
    4. 1.4. 投影矩阵(P)
    5. 1.5. 绘制

MVP矩阵变换

本文不做具体的理论推导。

前置工作

  1. 矩阵类,实现后续要用到的矩阵运算。

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
class Matrix {
constructor(row, col) {
this.data = [];
this.row = row;
this.col = col;
this.init();
}
init() {
for (let i = 0; i < this.row; i++) {
this.data[i] = []
for (let j = 0; j < this.col; j++) {
this.data[i][j] = 0
}
}
}
set(row, col, value) {
if (row < 0 || row >= this.row || col < 0 || col >= this.col) {
return
}
this.data[row][col] = value;
}
get(row, col) {
if (row < 0 || row >= this.row || col < 0 || col >= this.col) {
return
}
return this.data[row][col];
}

add(m) {
if (this.row != m.row || this.col != m.col) {
return
}
for (let i = 0; i < this.row; i++) {
for (let j = 0; j < this.col; j++) {
this.data[i][j] += m.data[i][j]
}
}
}

mul(m) {
if (this.col != m.row) {
return;
}
let res = new Matrix(this.row, m.col);
for (let i = 0; i < this.row; i++) {
for (let j = 0; j < m.col; j++) {
let sum = 0
for (let k = 0; k < this.col; k++) {
sum += this.data[i][k] * m.data[k][j];
}
res.set(i, j, sum);
}
}
return res;
}

transpose() {
let res = new Matrix(this.col, this.row);
for (let i = 0; i < this.row; i++) {
for (let j = 0; j < this.col; j++) {
res.set(j, i, this.data[i][j]);
}
}
return res;
}
};
  • 变换

平移变换

缩放

旋转

  • 绕X轴旋转

  • 绕Y轴旋转

  • 绕Z轴旋转

  1. 变换类,实现平移、缩放、旋转。

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
class Transform {
constructor(position = [0, 0, 0], rotation = [0, 0, 0], scale = [1, 1, 1]) {
this._position = position;
this._rotation = rotation; // 这里采用欧拉角控制旋转
this._scale = scale;
}
get position() {
return this._position;
}
set position(value) {
this._position = value;
}
get rotation() {
return this._rotation;
}
set rotation(value) {
this._rotation = value;
}
get scale() {
return this._scale;
}
set scale(value) {
this._scale = value;
}

get transformMatrix() {
// 变换矩阵 = T * R * S 先缩放在旋转、再平移
/**
* 缩放矩阵
* sx 0 0 0
* 0 sy 0 0
* 0 0 sz 0
* 0 0 0 1
*/
let S = new Matrix(4, 4);
for (let i = 0; i < 3; i++) {
S.set(i, i, this._scale[i]);
}
S.set(3, 3, 1);
// 旋转矩阵
let R = this.eulerToMatrix(this._rotation);
/**
* 平移矩阵
* 1 0 0 tx
* 0 1 0 ty
* 0 0 1 tz
* 0 0 0 1
*/
let T = new Matrix(4, 4);
for (let i = 0; i < 3; i++) {
T.set(i, i, 1);
T.set(i, 3, this._position[i]);
}
T.set(3, 3, 1);
return T.mul(R).mul(S);
}

eulerToMatrix(eulerAngle) {
let rX = new Matrix(4, 4);
let rY = new Matrix(4, 4);
let rZ = new Matrix(4, 4);

/**
* 绕 x 轴旋转 a
* 1 0 0 0
* 0 cos(a) -sin(a) 0
* 0 sin(a) cos(a) 0
* 0 0 0 1
*/
rX.set(0, 0, 1);
rX.set(1, 1, Math.cos(eulerAngle[0] * Math.PI / 180));
rX.set(1, 2, -Math.sin(eulerAngle[0] * Math.PI / 180));
rX.set(2, 1, Math.sin(eulerAngle[0] * Math.PI / 180));
rX.set(2, 2, Math.cos(eulerAngle[0] * Math.PI / 180));
rX.set(3, 3, 1);

/**
* 绕 y 轴旋转 b
* cos(b) 0 sin(b) 0
* 0 1 0 0
* -sin(b) 0 cos(b) 0
* 0 0 0 1
*/
rY.set(0, 0, Math.cos(eulerAngle[1] * Math.PI / 180));
rY.set(0, 2, Math.sin(eulerAngle[1] * Math.PI / 180));
rY.set(1, 1, 1);
rY.set(2, 0, -Math.sin(eulerAngle[1] * Math.PI / 180));
rY.set(2, 2, Math.cos(eulerAngle[1] * Math.PI / 180));
rY.set(3, 3, 1);

/**
* 绕 z 轴旋转 c
* cos(c) -sin(c) 0 0
* sin(c) cos(c) 0 0
* 0 0 1 0
* 0 0 0 1
*/
rZ.set(0, 0, Math.cos(eulerAngle[2] * Math.PI / 180));
rZ.set(0, 1, -Math.sin(eulerAngle[2] * Math.PI / 180));
rZ.set(1, 0, Math.sin(eulerAngle[2] * Math.PI / 180));
rZ.set(1, 1, Math.cos(eulerAngle[2] * Math.PI / 180));
rZ.set(2, 2, 1);
rZ.set(3, 3, 1);

return rZ.mul(rY).mul(rX);
}
}

模型矩阵(M)

由前置条件的变换可以得出M矩阵。

1
2
let modelTransform = new Transform([0, 0, 0], [0, 0, 0], [1, 1, 1]);
let model = modelTransform.transformMatrix;

视图矩阵(V)

就是由世界坐标系到观察坐标系的逆变换。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let viewTransform = new Transform([0, 0, 2], [0, 0, 0], [1, 1, 1]);
let viewInverse = viewTransform.transformMatrix;
// 求逆
let view = new Matrix(4, 4);
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
view.set(i, j, viewInverse.get(j, i));
}
}
view.set(3, 3, 1);
view = view.transpose();
let viewT = new Matrix(4, 4);
for (let i = 0; i < 4; i++) {
viewT.set(i, i, 1);
}
for (let i = 0; i < 3; i++) {
viewT.set(i, 3, -viewInverse.get(i, 3));
}
view = view.mul(viewT);

投影矩阵(P)

这里采用正交投影矩阵,即:

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
class Camera {
constructor(right, left, top, bottom, near, far) {
this.right = right;
this.left = left;
this.top = top;
this.bottom = bottom;
this.near = near;
this.far = far;
}
get projectionMatrix() {
let w = this.right - this.left; // width
let h = this.top - this.bottom; // height
let d = this.far - this.near; // depth

let dx = -1 * (this.right + this.left) / 2;
let dy = -1 * (this.top + this.bottom) / 2;
let dz = -1 * (this.far + this.near) / 2;

let P = new Matrix(4, 4);
P.set(0, 0, 2 / w);
P.set(1, 1, 2 / h);
P.set(2, 2, 2 / d);
P.set(3, 3, 1);

let Pt = new Matrix(4, 4);
for (let i = 0; i < 4; i++) {
Pt.set(i, i, 1);
}
Pt.set(0, 3, dx);
Pt.set(1, 3, dy);
Pt.set(2, 3, dz);

return P.mul(Pt);
}
}
let camera = new Camera(3, -3, 3, -3, 1, 3);
let projection = camera.projectionMatrix;

绘制

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
let faces_mvp = projection.mul(view).mul(model);    // 面的mvp矩阵
let axis_mvp = projection.mul(view).mul(model); // 轴的mvp矩阵
// 面的坐标
let faces = [
// buttom
{
points: [
[-1, -1, 1],
[1, -1, 1],
[1, -1, -1]
],
color: "#ff0000",
newPoints: []
},
{
points: [
[-1, -1, 1],
[-1, -1, -1],
[1, -1, -1]
],
color: "#ff0000",
newPoints: []
},
// top
{
points: [
[-1, 1, 1],
[1, 1, 1],
[1, 1, -1]
],
color: "#00ff00",
newPoints: []
},
{
points: [
[-1, 1, 1],
[-1, 1, -1],
[1, 1, -1]
],
color: "#00ff00",
newPoints: []
},
// back
{
points: [
[-1, -1, -1],
[1, -1, -1],
[-1, 1, -1]
],
color: "#0000ff",
newPoints: []
},
{
points: [
[-1, 1, -1],
[1, -1, -1],
[1, 1, -1]
],
color: "#0000ff",
newPoints: []
},
// front
{
points: [
[-1, -1, 1],
[1, -1, 1],
[1, 1, 1]
],
color: "#ff00ff",
newPoints: []
},
{
points: [
[-1, 1, 1],
[-1, -1, 1],
[1, 1, 1]
],
color: "#ff00ff",
newPoints: []
},
// right
{
points: [
[1, -1, -1],
[1, -1, 1],
[1, 1, 1]
],
color: "#ffff00",
newPoints: []
},
{
points: [
[1, 1, 1],
[1, 1, -1],
[1, -1, -1]
],
color: "#ffff00",
newPoints: []
},
// left
{
points: [
[-1, -1, -1],
[-1, -1, 1],
[-1, 1, 1]
],
color: "#00ffff",
newPoints: []
},
{
points: [
[-1, 1, 1],
[-1, 1, -1],
[-1, -1, -1]
],
color: "#00ffff",
newPoints: []
}
];
// x-y-z轴
let axis = [
{
points: [
[0, 0, 0],
[5, 0, 0]
],
text: "x",
color: "#ff0000",
newPoints: []
},
{
points: [
[0, 0, 0],
[0, 5, 0]
],
text: "y",
color: "#00ff00",
newPoints: []
},
{
points: [
[0, 0, 0],
[0, 0, 5]
],
text: "z",
color: "#0000ff",
newPoints: []
}
];

let canvas = document.createElement("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let ctx = canvas.getContext("2d");
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(1, -1);
document.body.appendChild(canvas);
// 计算变化后的坐标
function transform() {
for (let i = 0; i < faces.length; i++) {
let newPoints = [];
for (let j = 0; j < faces[i].points.length; j++) {
let point = new Matrix(4, 1);
for (let k = 0; k < 3; k++) {
point.set(k, 0, faces[i].points[j][k]);
}
point.set(3, 0, 1);
point = faces_mvp.mul(point);
newPoints.push([point.get(0, 0) / point.get(3, 0), point.get(1, 0) / point.get(3, 0), point.get(2, 0) / point.get(3, 0)]);
}
faces[i].newPoints = newPoints;
}
// 深度应该利用插值来进行绘制,这里先利用一个简单的方法(求构成面的三个顶点的深度来进行排序)
faces.sort((a, b) => {
let sumZA = a.newPoints[0][2];
let sumZB = b.newPoints[0][2];
for (let i = 1; i < 3; i++) {
sumZA += a.newPoints[i][2];
sumZB += b.newPoints[i][2];
}
return sumZB - sumZA;
});
for (let i = 0; i < axis.length; i++) {
let newPoints = [];
for (let j = 0; j < axis[i].points.length; j++) {
let point = new Matrix(4, 1);
for (let k = 0; k < 3; k++) {
point.set(k, 0, axis[i].points[j][k]);
}
point.set(3, 0, 1);
point = axis_mvp.mul(point);
newPoints.push([point.get(0, 0) / point.get(3, 0), point.get(1, 0) / point.get(3, 0), point.get(2, 0) / point.get(3, 0)]);
}
axis[i].newPoints = newPoints;
}
}
// 绘制
function draw() {
ctx.clearRect(-canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height);
let scale = 500;
for (let i = 0; i < faces.length; i++) {
ctx.fillStyle = faces[i].color;
ctx.beginPath();
ctx.moveTo(faces[i].newPoints[0][0] * scale, faces[i].newPoints[0][1] * scale);
for (let j = 1; j < faces[i].newPoints.length; j++) {
ctx.lineTo(faces[i].newPoints[j][0] * scale, faces[i].newPoints[j][1] * scale);
}
ctx.closePath();
ctx.fill();
}
for (let i = 0; i < axis.length; i++) {
ctx.strokeStyle = axis[i].color;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(axis[i].newPoints[0][0] * scale, axis[i].newPoints[0][1] * scale);
ctx.lineTo(axis[i].newPoints[1][0] * scale, axis[i].newPoints[1][1] * scale);
ctx.closePath();
ctx.stroke();
ctx.fillStyle = axis[i].color;
ctx.font = "20px Arial";
ctx.fillText(axis[i].text, axis[i].newPoints[1][0] * scale, axis[i].newPoints[1][1] * scale);
}
}
transform();
draw();

// 事件监听,根据鼠标进行物体的旋转
let xa = 0;
let xb = 0;
let currentA = 0;
let currentB = 0;
let flag = false;
canvas.onmousedown = (e) => {
flag = true;
xa = e.clientX;
xb = e.clientY;
}
canvas.onmousemove = (e) => {
if (flag) {
let thetaA = (e.clientX - xa) / 100;
let thetaB = (e.clientY - xb) / 100;
let modelTransform = new Transform([0, 0, 0], [currentB + thetaB, currentA + thetaA, 0], [1, 1, 1]);
let model = modelTransform.transformMatrix;
faces_mvp = projection.mul(view).mul(model);
transform();
draw();
currentA += thetaA;
currentB += thetaB;
xa = currentA;
xb = currentB;
}
}
canvas.onmouseup = () => {
flag = false;
}

绘制结果