• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16export class XMat4 {
17  constructor() {
18    this.unit();
19  }
20
21  Get() {
22    return this.mat;
23  }
24  unit() {
25    this.mat = [
26      [1, 0, 0, 0],
27      [0, 1, 0, 0],
28      [0, 0, 1, 0],
29      [0, 0, 0, 1],
30    ];
31    return this;
32  }
33  copy(m) {
34    for (let i = 0; i < 4; i++) {
35      for (let j = 0; j < 4; j++) {
36        this.mat[i][j] = m.mat[i][j];
37      }
38    }
39  }
40  initRotateMatX(hd) {
41    this.unit();
42    let _cos = Math.cos(hd);
43    let _sin = Math.sin(hd);
44    this.mat[1][1] = _cos;
45    this.mat[1][2] = -_sin;
46    this.mat[2][1] = _sin;
47    this.mat[2][2] = _cos;
48  }
49  initRotateMatY(hd) {
50    this.unit();
51    let _cos = Math.cos(hd);
52    let _sin = Math.sin(hd);
53    this.mat[0][0] = _cos;
54    this.mat[0][2] = -_sin;
55    this.mat[2][0] = _sin;
56    this.mat[2][2] = _cos;
57  }
58
59  initRotateMatZ(hd) {
60    this.unit();
61    let _cos = Math.cos(hd);
62    let _sin = Math.sin(hd);
63    this.mat[0][0] = _cos;
64    this.mat[0][1] = -_sin;
65    this.mat[1][0] = _sin;
66    this.mat[1][1] = _cos;
67  }
68  initScaleMat(x, y, z) {
69    this.unit();
70    this.mat[0][0] = x;
71    this.mat[1][1] = y;
72    this.mat[2][2] = z;
73  }
74  move(x, y, z = 0) {
75    this.mat[3][0] += x;
76    this.mat[3][1] += y;
77    this.mat[3][2] += z;
78    return this;
79  }
80  rotate(x, y, z) {
81    if (x != 0) {
82      tmpmat.initRotateMatX((x * Math.PI) / 180);
83      this.mult(tmpmat);
84    }
85    if (y != 0) {
86      tmpmat.initRotateMatY((y * Math.PI) / 180);
87      this.mult(tmpmat);
88    }
89    if (z != 0) {
90      tmpmat.initRotateMatZ((z * Math.PI) / 180);
91      this.mult(tmpmat);
92    }
93    return this;
94  }
95
96  scale(x, y, z = 1) {
97    tmpmat.initScaleMat(x, y, z);
98    this.mult(tmpmat);
99    return this;
100  }
101
102  mult(m4) {
103    let tmp = [
104      [1, 0, 0, 0],
105      [0, 1, 0, 0],
106      [0, 0, 1, 0],
107      [0, 0, 0, 1],
108    ];
109    for (let i = 0; i < 4; i++) {
110      for (let j = 0; j < 4; j++) {
111        tmp[i][j] =
112          this.mat[i][0] * m4.mat[0][j] +
113          this.mat[i][1] * m4.mat[1][j] +
114          this.mat[i][2] * m4.mat[2][j] +
115          this.mat[i][3] * m4.mat[3][j];
116      }
117    }
118    this.mat = tmp;
119    return this;
120  }
121
122  MultRight(m4) {
123    this.mat = np.dot(m4.mat, this.mat);
124    return this;
125  }
126
127  PerspectiveMatrix(n, f, w = -1, h = -1) {
128    if (w == -1) w = Scr.logicw;
129    if (h == -1) h = Scr.logich;
130    let ret = w / (tan((30 * pi) / 180) * 2);
131    this.unit();
132    this.mat[0][0] = 2 / w;
133    this.mat[1][1] = 2 / h;
134
135    this.mat[2][2] = 1 / (f - n);
136    this.mat[2][3] = 1 / ret; //#2 / f
137    this.mat[3][2] = -n / (f - n);
138    this.mat[3][3] = 1;
139    return ret;
140  }
141
142  orthoMat(x, y, w, h) {
143    this.move(-w / 2 - x, -h / 2 - y, 0);
144    this.scale(2 / w, -2 / h, 0);
145  }
146
147  Make2DTransformMat(
148    mx = 0,
149    my = 0,
150    sw = 1,
151    sh = 1,
152    ra = 0,
153    ox = 0,
154    oy = 0,
155    realw = 0,
156    realh = 0
157  ) {
158    this.unit();
159    if (ox == -1) ox = 0;
160    if (ox == -2) ox = realw / 2;
161    if (ox == -3) ox = realw;
162    if (oy == -1) oy = 0;
163    if (oy == -2) oy = realh / 2;
164    if (oy == -3) oy = realh;
165    if (ox != 0 || oy != 0) this.move(-ox, -oy, 0);
166    if (sw != 1 || sh != 1) this.scale(sw, sh, 1);
167    if (ra != 0) this.rotate(0, 0, ra);
168    if (mx != 0 || my != 0) this.move(mx, my, 0);
169  }
170
171  Make2DTransformMat_(mx, my, sw, sh, ra, ox = 0, oy = 0) {
172    this.unit();
173    if (mx != 0 || my != 0) this.move(-mx, -my, 0);
174    if (ra != 0) this.rotate(0, 0, -ra);
175    if (sw != 1 || sh != 1) this.scale(1 / sw, 1 / sh, 1);
176    return this;
177  }
178}
179
180var tmpmat = new XMat4();
181