• 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
16import { XMat4 } from './XMat4.js';
17import { XShader } from './XShader.js';
18import { Scr } from '../XDefine.js';
19import { XTexture } from './XTexture.js';
20import { gl } from '../GLFrame.js';
21import { fAngle, iDistance } from '../XTools.js';
22const { NapiLog } = require('./../../hcs/NapiLog');
23const COLOR = 0xffffffff;
24
25export class X2DFast {
26  static gi() {
27    if (X2DFast.px2f == null) {
28      X2DFast.px2f = new X2DFast();
29    }
30    return X2DFast.px2f;
31  }
32
33  constructor() {
34    this.localBuffer = gl.createBuffer();
35    this.texSampleIdx = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
36
37    this.vertexArray = new ArrayBuffer(1024 * 1024 * 4 * 2);
38    this.vertexFloat32 = new Float32Array(this.vertexArray);
39    this.vertexUint32 = new Uint32Array(this.vertexArray);
40    this.whiteImg = XTexture.gi().loadTextureFromImage('CUSTOM_TEXTURE_1');
41    this.whiteCut = XTexture.gi().makeCut(this.whiteImg, 0, 0, 1, 1);
42    XShader.gi();
43
44    this.resetMat();
45  }
46  resetMat() {
47    X2DFast.transform2D.unit();
48    X2DFast.transform2D.orthoMat(0, 0, Scr.logicw, Scr.logich);
49    let tm = X2DFast.transform2D.mat;
50    this.t2dExt = [
51      tm[0][0],
52      tm[1][0],
53      tm[2][0],
54      tm[3][0],
55      tm[0][1],
56      tm[1][1],
57      tm[2][1],
58      tm[3][1],
59      tm[0][2],
60      tm[1][2],
61      tm[2][2],
62      tm[3][2],
63      tm[0][3],
64      tm[1][3],
65      tm[2][3],
66      tm[3][3],
67    ];
68  }
69
70  swapMode2D() {
71    gl.disable(gl.DEPTH_TEST);
72
73    gl.enable(gl.BLEND);
74    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
75  }
76
77  DrawCircle(ox, oy, rw, rh, c = 0xffffffff, lw = 1) {
78    let lx = -1;
79    let ly = -1;
80    let i = 0;
81    let gap = (Math.PI * 2) / 32;
82    while (i < Math.PI * 2 + 0.00001) {
83      let dx = Math.cos(i) * rw + ox;
84      let dy = Math.sin(i) * rh + oy;
85      if (lx != -1) {
86        this.drawLine(lx, ly, dx, dy, c, lw);
87      }
88      lx = dx;
89      ly = dy;
90      i += gap;
91    }
92  }
93
94  fillRect(x, y, w, h, c = 0xffffffff) {
95    this.drawCut(this.whiteCut, x, y, w, h, 0, 0, 0, c);
96  }
97
98  drawLine(x1, y1, x2, y2, c = 0xffffffff, linewidth = 1) {
99    this.drawCut(
100      this.whiteCut,
101      x1,
102      y1,
103      iDistance(x1 - x2, y1 - y2),
104      linewidth,
105      fAngle(x2 - x1, y2 - y1),
106      0,
107      0.5,
108      c
109    );
110  }
111
112  drawRect(x, y, w, h, c = 0xffffffff, lw = 1) {
113    this.drawLine(x - lw / 2, y, x + w + lw / 2, y, c, lw);
114    this.drawLine(x, y, x, y + h, c, lw);
115    this.drawLine(x + w, y + h, x + w, y, c, lw);
116    this.drawLine(x + w + lw / 2, y + h, x - lw / 2, y + h, c, lw);
117  }
118
119  static testTransform(x, y, sw, sh, ra, ox, oy, realw, realh) {
120    const LEFT = -1;
121    const MIDDLE = -2;
122    const RIGHT = -3;
123    const UP = -1;
124    const DOWN = -3;
125    X2DFast.tmpMat.unit();
126    if (ox == LEFT) {
127      ox = 0;
128    }
129    if (ox == MIDDLE) {
130      ox = Math.floor(realw / 2);
131    }
132    if (ox == RIGHT) {
133      ox = realw;
134    }
135    if (oy == UP) {
136      oy = 0;
137    }
138    if (oy == MIDDLE) {
139      oy = Math.floor(realh / 2);
140    }
141    if (oy == DOWN) {
142      oy = realh;
143    }
144    if (ox != 0 || oy != 0) {
145      X2DFast.tmpMat.move(-ox, -oy, 0);
146    }
147    if (sw != 1 || sh != 1) {
148      X2DFast.tmpMat.scale(sw, sh, 1);
149    }
150    if (ra != 0) {
151      X2DFast.tmpMat.rotate(0, 0, ra);
152    }
153    if (x != 0 || y != 0) {
154      X2DFast.tmpMat.move(x, y, 0);
155    }
156  }
157  clearBuffer() {
158    this.ridDict = {};
159    this.ridPoint = 0;
160    this.drawCount = 0;
161  }
162  swapC(c) {
163    let r = Math.floor((((c >> 16) & 0xff) * 63) / 255);
164    let g = Math.floor((((c >> 8) & 0xff) * 63) / 255);
165    let b = Math.floor(((c & 0xff) * 63) / 255);
166    let a = Math.floor((((c >> 24) & 0xff) * 63) / 255);
167    return ((a * 64 + r) * 64 + g) * 64 + b;
168  }
169  drawCut_(pcut, m00, m01, m10, m11, m22, m30, m31, c = COLOR) {
170    if (c == -1) {
171      c = COLOR;
172    }
173    c = this.swapC(c);
174    this.vertexFloat32.set([0.0, 0.0, 0.0, pcut.u0, pcut.v0, m00, m01, m10, m11, m22, m30, m31,
175      this.ridDict[pcut.rid], c,
176        pcut.w, 0.0, 0.0, pcut.u1, pcut.v1, m00, m01, m10, m11, m22, m30, m31, this.ridDict[pcut.rid], c,
177        pcut.w, pcut.h, 0.0, pcut.u2, pcut.v2, m00, m01, m10, m11, m22, m30, m31, this.ridDict[pcut.rid], c,
178        0.0, 0.0, 0.0, pcut.u0, pcut.v0, m00, m01, m10, m11, m22, m30, m31, this.ridDict[pcut.rid], c,
179        pcut.w, pcut.h, 0.0, pcut.u2, pcut.v2, m00, m01, m10, m11, m22, m30, m31, this.ridDict[pcut.rid], c,
180      0.0, pcut.h, 0.0, pcut.u3, pcut.v3, m00, m01, m10, m11, m22, m30, m31, this.ridDict[pcut.rid], c,],
181    this.drawCount * 14 * 6);
182    this.drawCount += 1;
183  }
184  drawCutEx(cid, tmat, c = COLOR) {
185    let pcut = XTexture.pinstance_.allCuts[cid];
186    if (!(pcut.rid in this.ridDict)) {
187      this.ridDict[pcut.rid] = this.ridPoint;
188      this.ridPoint += 1;
189    }
190    tmat = tmat.mat;
191    this.drawCut(pcut, tmat[0][0], tmat[0][1], tmat[1][0], tmat[1][1], tmat[2][2], tmat[3][0], tmat[3][1], c);
192  }
193  drawCut(cid, x = 0, y = 0, sw = 1, sh = 1, ra = 0, ox = 0, oy = 0, c = COLOR) {
194    let intX = parseInt(x);
195    let intY = parseInt(y);
196    let pcut = XTexture.gi().allCuts[cid];
197    if (pcut == null) {
198      NapiLog.logError('error occured getting object');
199      return;
200    }
201    if (!(pcut.rid in this.ridDict)) {
202      if (this.ridPoint >= 16) {
203        this.freshBuffer();
204        this.clearBuffer();
205      }
206      this.ridDict[pcut.rid] = this.ridPoint;
207      this.ridPoint += 1;
208    }
209    X2DFast.testTransform(intX, intY, sw, sh, ra, ox, oy, pcut.w, pcut.h);
210    let tmat = X2DFast.tmpMat.mat;
211    this.drawCut_(pcut, tmat[0][0], tmat[0][1], tmat[1][0], tmat[1][1], tmat[2][2], tmat[3][0], tmat[3][1], c);
212  }
213  drawText(s, size = 14, x = 0, y = 0, sw = 1, sh = 1, ra = 0, ox = 0, oy = 0, c = COLOR) {
214    if (s.length <= 0) {
215      NapiLog.logError('error occured s is null');
216      return 0;
217    }
218    let cid = XTexture.gi().getText(s, size);
219    if (cid >= 0) {
220      this.drawCut(cid, x, y, sw, sh, ra, ox, oy, c);
221    }
222    return XTexture.gi().allCuts[cid].w;
223  }
224  getTextWidth(s, size) {
225    if (s.length <= 0) {
226      return 0;
227    }
228    let cid = XTexture.gi().getText(s, size);
229    return XTexture.gi().allCuts[cid].w;
230  }
231  freshBuffer() {
232    XTexture.gi()._FreshText();
233    if (this.drawCount == 0) {
234      return;
235    }
236    let ps = XShader.gi().use(XShader.ID_SHADER_FAST);
237    for (let rid in this.ridDict) {
238      gl.activeTexture(gl.TEXTURE0 + this.ridDict[rid]);
239      gl.bindTexture(gl.TEXTURE_2D, XTexture.gi().ximages[rid].tex);
240
241      gl.uniform1i(ps.tex[this.ridDict[rid]], this.ridDict[rid]);
242    }
243
244    gl.uniformMatrix4fv(ps.uMat, false, this.t2dExt);
245
246    gl.bindBuffer(gl.ARRAY_BUFFER, this.localBuffer);
247    gl.bufferData(gl.ARRAY_BUFFER, this.vertexArray, gl.STATIC_DRAW);
248    gl.vertexAttribPointer(ps.position, 3, gl.FLOAT, false, 4 * 14, 0);
249    gl.enableVertexAttribArray(ps.position);
250    gl.vertexAttribPointer(ps.aTexCoord, 2, gl.FLOAT, false, 4 * 14, 4 * 3);
251    gl.enableVertexAttribArray(ps.aTexCoord);
252    gl.vertexAttribPointer(ps.ext1, 4, gl.FLOAT, false, 4 * 14, 4 * 5);
253    gl.enableVertexAttribArray(ps.ext1);
254    gl.vertexAttribPointer(ps.ext2, 4, gl.FLOAT, false, 4 * 14, 4 * 9);
255    gl.enableVertexAttribArray(ps.ext2);
256    gl.vertexAttribPointer(ps.inColor, 1, gl.FLOAT, false, 4 * 14, 4 * 13);
257    gl.enableVertexAttribArray(ps.inColor);
258
259    gl.drawArrays(gl.TRIANGLES, 0, 6 * this.drawCount);
260  }
261}
262X2DFast.tmpMat = new XMat4();
263X2DFast.transform2D = new XMat4();
264
265X2DFast.px2f = null;
266