-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
CanvasMeshRenderer.ts
296 lines (242 loc) · 8.88 KB
/
CanvasMeshRenderer.ts
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { DRAW_MODES, extensions, ExtensionType, Texture } from '@pixi/core';
import { canvasUtils } from '@pixi/canvas-renderer';
import type { ExtensionMetadata } from '@pixi/core';
import type { CanvasRenderer } from '@pixi/canvas-renderer';
import type { Mesh } from '@pixi/mesh';
/**
* Renderer dedicated to meshes.
* @class
* @protected
* @memberof PIXI
*/
export class CanvasMeshRenderer
{
/** @ignore */
static extension: ExtensionMetadata = {
name: 'mesh',
type: ExtensionType.CanvasRendererPlugin,
};
/** A reference to the current renderer */
public renderer: CanvasRenderer;
/** @param renderer - A reference to the current renderer */
constructor(renderer: CanvasRenderer)
{
this.renderer = renderer;
}
/**
* Renders the Mesh
* @param mesh - the Mesh to render
*/
public render(mesh: Mesh): void
{
const renderer = this.renderer;
const transform = mesh.worldTransform;
renderer.canvasContext.activeContext.globalAlpha = mesh.worldAlpha;
renderer.canvasContext.setBlendMode(mesh.blendMode);
renderer.canvasContext.setContextTransform(transform, mesh.roundPixels);
if (mesh.drawMode !== DRAW_MODES.TRIANGLES)
{
this._renderTriangleMesh(mesh);
}
else
{
this._renderTriangles(mesh);
}
}
/**
* Draws the object in Triangle Mesh mode
* @private
* @param mesh - the Mesh to render
*/
private _renderTriangleMesh(mesh: Mesh): void
{
// draw triangles!!
const length = mesh.geometry.buffers[0].data.length;
for (let i = 0; i < length - 2; i++)
{
// draw some triangles!
const index = i * 2;
this._renderDrawTriangle(mesh, index, (index + 2), (index + 4));
}
}
/**
* Draws the object in triangle mode using canvas
* @private
* @param mesh - the current mesh
*/
private _renderTriangles(mesh: Mesh): void
{
// draw triangles!!
const indices = mesh.geometry.getIndex().data;
const length = indices.length;
for (let i = 0; i < length; i += 3)
{
// draw some triangles!
const index0 = indices[i] * 2;
const index1 = indices[i + 1] * 2;
const index2 = indices[i + 2] * 2;
this._renderDrawTriangle(mesh, index0, index1, index2);
}
}
/**
* Draws one of the triangles that from the Mesh
* @private
* @param mesh - the current mesh
* @param index0 - the index of the first vertex
* @param index1 - the index of the second vertex
* @param index2 - the index of the third vertex
*/
private _renderDrawTriangle(mesh: Mesh, index0: number, index1: number, index2: number): void
{
const context = this.renderer.canvasContext.activeContext;
const vertices = mesh.geometry.buffers[0].data;
const { uvs, texture } = mesh;
if (!texture.valid)
{
return;
}
const isTinted = mesh.tint !== 0xFFFFFF;
const base = texture.baseTexture;
const textureWidth = base.width;
const textureHeight = base.height;
// Invalidate texture if base texture was updated
// either because mesh.texture or mesh.shader.texture was changed
if (mesh._cachedTexture && mesh._cachedTexture.baseTexture !== base)
{
mesh._cachedTint = 0xffffff;
mesh._cachedTexture?.destroy();
mesh._cachedTexture = null;
mesh._tintedCanvas = null;
}
if (isTinted)
{
if (mesh._cachedTint !== mesh.tint)
{
mesh._cachedTint = mesh.tint;
mesh._cachedTexture = mesh._cachedTexture || new Texture(base);
mesh._tintedCanvas = canvasUtils.getTintedCanvas(
{ texture: mesh._cachedTexture },
mesh.tint
);
}
}
const textureSource = isTinted ? mesh._tintedCanvas : base.getDrawableSource();
const u0 = uvs[index0] * base.width;
const u1 = uvs[index1] * base.width;
const u2 = uvs[index2] * base.width;
const v0 = uvs[index0 + 1] * base.height;
const v1 = uvs[index1 + 1] * base.height;
const v2 = uvs[index2 + 1] * base.height;
let x0 = vertices[index0];
let x1 = vertices[index1];
let x2 = vertices[index2];
let y0 = vertices[index0 + 1];
let y1 = vertices[index1 + 1];
let y2 = vertices[index2 + 1];
const screenPadding = mesh.canvasPadding / this.renderer.canvasContext.activeResolution;
if (screenPadding > 0)
{
const { a, b, c, d } = mesh.worldTransform;
const centerX = (x0 + x1 + x2) / 3;
const centerY = (y0 + y1 + y2) / 3;
let normX = x0 - centerX;
let normY = y0 - centerY;
// Transform to screen space and calculate the distance
let screenX = (a * normX) + (c * normY);
let screenY = (b * normX) + (d * normY);
let screenDist = Math.sqrt((screenX * screenX) + (screenY * screenY));
// Factor by which to scale in order to add padding equal to screenPadding
let paddingFactor = 1 + (screenPadding / screenDist);
x0 = centerX + (normX * paddingFactor);
y0 = centerY + (normY * paddingFactor);
normX = x1 - centerX;
normY = y1 - centerY;
screenX = (a * normX) + (c * normY);
screenY = (b * normX) + (d * normY);
screenDist = Math.sqrt((screenX * screenX) + (screenY * screenY));
paddingFactor = 1 + (screenPadding / screenDist);
x1 = centerX + (normX * paddingFactor);
y1 = centerY + (normY * paddingFactor);
normX = x2 - centerX;
normY = y2 - centerY;
screenX = (a * normX) + (c * normY);
screenY = (b * normX) + (d * normY);
screenDist = Math.sqrt((screenX * screenX) + (screenY * screenY));
paddingFactor = 1 + (screenPadding / screenDist);
x2 = centerX + (normX * paddingFactor);
y2 = centerY + (normY * paddingFactor);
}
context.save();
context.beginPath();
context.moveTo(x0, y0);
context.lineTo(x1, y1);
context.lineTo(x2, y2);
context.closePath();
context.clip();
// Compute matrix transform
const delta = (u0 * v1) + (v0 * u2) + (u1 * v2) - (v1 * u2) - (v0 * u1) - (u0 * v2);
const deltaA = (x0 * v1) + (v0 * x2) + (x1 * v2) - (v1 * x2) - (v0 * x1) - (x0 * v2);
const deltaB = (u0 * x1) + (x0 * u2) + (u1 * x2) - (x1 * u2) - (x0 * u1) - (u0 * x2);
const deltaC = (u0 * v1 * x2) + (v0 * x1 * u2) + (x0 * u1 * v2) - (x0 * v1 * u2) - (v0 * u1 * x2) - (u0 * x1 * v2);
const deltaD = (y0 * v1) + (v0 * y2) + (y1 * v2) - (v1 * y2) - (v0 * y1) - (y0 * v2);
const deltaE = (u0 * y1) + (y0 * u2) + (u1 * y2) - (y1 * u2) - (y0 * u1) - (u0 * y2);
const deltaF = (u0 * v1 * y2) + (v0 * y1 * u2) + (y0 * u1 * v2) - (y0 * v1 * u2) - (v0 * u1 * y2) - (u0 * y1 * v2);
context.transform(
deltaA / delta,
deltaD / delta,
deltaB / delta,
deltaE / delta,
deltaC / delta,
deltaF / delta
);
context.drawImage(
textureSource,
0,
0,
textureWidth * base.resolution,
textureHeight * base.resolution,
0,
0,
textureWidth,
textureHeight
);
context.restore();
this.renderer.canvasContext.invalidateBlendMode();
}
/**
* Renders a flat Mesh
* @private
* @param mesh - The Mesh to render
*/
renderMeshFlat(mesh: Mesh): void
{
const context = this.renderer.canvasContext.activeContext;
const vertices = mesh.geometry.getBuffer('aVertexPosition').data;
const length = vertices.length / 2;
// this.count++;
context.beginPath();
for (let i = 1; i < length - 2; ++i)
{
// draw some triangles!
const index = i * 2;
const x0 = vertices[index];
const y0 = vertices[index + 1];
const x1 = vertices[index + 2];
const y1 = vertices[index + 3];
const x2 = vertices[index + 4];
const y2 = vertices[index + 5];
context.moveTo(x0, y0);
context.lineTo(x1, y1);
context.lineTo(x2, y2);
}
context.fillStyle = '#FF0000';
context.fill();
context.closePath();
}
/** destroy the renderer */
public destroy(): void
{
this.renderer = null;
}
}
extensions.add(CanvasMeshRenderer);