@awayjs/scene 0.13.226 → 0.13.232

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/bundle/awayjs-scene.umd.js +865 -387
  2. package/bundle/awayjs-scene.umd.js.map +1 -1
  3. package/bundle/awayjs-scene.umd.min.js.map +1 -1
  4. package/bundle/awayjs-scene.umd.min.js.map.map +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/lib/Settings.d.ts +3 -0
  7. package/dist/lib/Settings.d.ts.map +1 -1
  8. package/dist/lib/Settings.js +14 -0
  9. package/dist/lib/display/TextField.d.ts +36 -5
  10. package/dist/lib/display/TextField.d.ts.map +1 -1
  11. package/dist/lib/display/TextField.js +227 -138
  12. package/dist/lib/image/SceneImage2D.d.ts.map +1 -1
  13. package/dist/lib/image/SceneImage2D.js +33 -8
  14. package/dist/lib/text/FNTGenerator.d.ts +5 -4
  15. package/dist/lib/text/FNTGenerator.d.ts.map +1 -1
  16. package/dist/lib/text/FNTGenerator.js +15 -11
  17. package/dist/lib/text/FNTGeneratorCanvas.d.ts +23 -0
  18. package/dist/lib/text/FNTGeneratorCanvas.d.ts.map +1 -0
  19. package/dist/lib/text/FNTGeneratorCanvas.js +205 -0
  20. package/dist/lib/text/FNTGeneratrorBase.d.ts +8 -0
  21. package/dist/lib/text/FNTGeneratrorBase.d.ts.map +1 -0
  22. package/dist/lib/text/FNTGeneratrorBase.js +7 -0
  23. package/dist/lib/text/TesselatedFontChar.d.ts +6 -4
  24. package/dist/lib/text/TesselatedFontChar.d.ts.map +1 -1
  25. package/dist/lib/text/TesselatedFontChar.js +8 -3
  26. package/dist/lib/text/TesselatedFontTable.d.ts +19 -3
  27. package/dist/lib/text/TesselatedFontTable.d.ts.map +1 -1
  28. package/dist/lib/text/TesselatedFontTable.js +339 -225
  29. package/dist/lib/text/TextShape.d.ts +6 -3
  30. package/dist/lib/text/TextShape.d.ts.map +1 -1
  31. package/dist/lib/text/TextShape.js +27 -2
  32. package/index.ts +1 -1
  33. package/lib/Settings.ts +20 -1
  34. package/lib/display/TextField.ts +317 -145
  35. package/lib/image/SceneImage2D.ts +81 -9
  36. package/lib/text/FNTGenerator.ts +5 -7
  37. package/lib/text/FNTGeneratorCanvas.ts +288 -0
  38. package/lib/text/FNTGeneratrorBase.ts +11 -0
  39. package/lib/text/TesselatedFontChar.ts +8 -12
  40. package/lib/text/TesselatedFontTable.ts +438 -255
  41. package/lib/text/TextShape.ts +30 -3
  42. package/package.json +1 -1
@@ -288,6 +288,7 @@ export class SceneImage2D extends BitmapImage2D {
288
288
  //setup the projection
289
289
  SceneImage2D._renderer.disableClear = true;
290
290
  SceneImage2D._renderer.view.backgroundAlpha = 0;
291
+ SceneImage2D._renderer.view.backgroundColor = 0x0;
291
292
  SceneImage2D._renderer.view.projection = projection;
292
293
  SceneImage2D._renderer.view.projection.transform = new Transform();
293
294
  SceneImage2D._renderer.view.projection.transform.scaleTo(1, -1, 1);
@@ -316,6 +317,7 @@ export class SceneImage2D extends BitmapImage2D {
316
317
  //setup the projection
317
318
  SceneImage2D._billboardRenderer.disableClear = true;
318
319
  SceneImage2D._billboardRenderer.view.backgroundAlpha = 0;
320
+ SceneImage2D._billboardRenderer.view.backgroundColor = 0x0;
319
321
  SceneImage2D._billboardRenderer.view.projection = projection;
320
322
  SceneImage2D._billboardRenderer.view.projection.transform = new Transform();
321
323
  SceneImage2D._billboardRenderer.view.projection.transform.moveTo(0, 0, -1000);
@@ -505,13 +507,60 @@ export class SceneImage2D extends BitmapImage2D {
505
507
  this._initalFillColor = null;
506
508
  }
507
509
 
508
- this._stage.copyPixels(source, this, sourceRect, destPoint, alphaBitmapData, alphaPoint, mergeAlpha);
510
+ let compositeSource: Image2D = source;
511
+
512
+ // merge alpha with source
513
+ if (alphaBitmapData) {
514
+ compositeSource = this._stage.filterManager.popTemp(source.width, source.height);
515
+
516
+ //this._stage.filterManager.copyPixels(source, compositeSource, source.rect, source.rect.topLeft);
517
+ this._stage.filterManager.copyPixels(
518
+ alphaBitmapData,
519
+ compositeSource,
520
+ new Rectangle(
521
+ alphaPoint.x,
522
+ alphaPoint.y,
523
+ sourceRect.width,
524
+ sourceRect.height
525
+ ),
526
+ new Point(0,0)
527
+ );
528
+
529
+ this._stage.filterManager.copyPixels(
530
+ source,
531
+ compositeSource,
532
+ source.rect,
533
+ new Point(0,0),
534
+ true,
535
+ 'alpha_back'
536
+ );
537
+ }
538
+
539
+ this._stage.filterManager.copyPixels(
540
+ compositeSource,
541
+ this,
542
+ sourceRect,
543
+ destPoint,
544
+ mergeAlpha
545
+ );
546
+
547
+ if (alphaBitmapData) {
548
+ this._stage.filterManager.pushTemp(compositeSource);
549
+ }
550
+
509
551
  this._imageDataDirty = true;
510
552
  }
511
553
 
512
554
  public threshold(
513
- source: BitmapImage2D, sourceRect: Rectangle, destPoint: Point,
514
- operation: string, threshold: number, color: number, mask: number, copySource: boolean): void {
555
+ source: BitmapImage2D,
556
+ sourceRect: Rectangle,
557
+ destPoint: Point,
558
+ operation: string,
559
+ threshold: number,
560
+ color: number,
561
+ mask: number,
562
+ copySource: boolean
563
+ ): void {
515
564
 
516
565
  this._lastUsedFill = null;
517
566
  this.dropAllReferences();
@@ -746,20 +795,28 @@ export class SceneImage2D extends BitmapImage2D {
746
795
  this._initalFillColor = null;
747
796
  }
748
797
 
798
+ const stage = this._stage;
799
+ const mappedBlend = SceneImage2D._mapSupportedBlendMode(blendMode);
800
+ const supportNativeBlend = !blendMode || mappedBlend !== BlendMode.LAYER || blendMode == BlendMode.LAYER;
801
+ const useTmp = (!supportNativeBlend || this === source);
802
+ const target = useTmp ?
803
+ stage.filterManager.popTemp(this.width, this.height, false)
804
+ : this;
805
+
749
806
  const renderer = SceneImage2D._billboardRenderer;
750
807
  const root = SceneImage2D._billboardRoot;
751
808
  const billboard = SceneImage2D._billboard;
752
809
 
753
810
  billboard.sampler.smooth = smoothing;
754
811
 
755
- renderer.disableClear = true;
756
- renderer.view.target = this;
757
- renderer.view.projection.scale = 1000 / this.rect.height;
812
+ renderer.disableClear = !useTmp;
813
+ renderer.view.target = target;
814
+ renderer.view.projection.scale = 1000 / target.height;
758
815
 
759
816
  billboard.material.style.image = source;
760
817
 
761
818
  // not all blend modes can be used for rendering
762
- billboard.material.blendMode = SceneImage2D._mapSupportedBlendMode(blendMode);
819
+ billboard.material.blendMode = !useTmp ? SceneImage2D._mapSupportedBlendMode(blendMode) : BlendMode.LAYER;
763
820
 
764
821
  (<MaterialBase> billboard.material).useColorTransform = !!colorTransform;
765
822
 
@@ -773,20 +830,35 @@ export class SceneImage2D extends BitmapImage2D {
773
830
  m._rawData[4] = matrix.c;
774
831
  m._rawData[5] = -matrix.d;
775
832
  m._rawData[12] = matrix.tx;
776
- m._rawData[13] = this.rect.height - matrix.ty;
833
+ m._rawData[13] = target.height - matrix.ty;
777
834
 
778
835
  root.transform.invalidateComponents();
779
836
  } else {
780
837
 
781
838
  root.transform.rotateTo(0,0,0);
782
839
  root.transform.scaleTo(1, -1, 1);
783
- root.transform.moveTo(0, this.rect.height,0);
840
+ root.transform.moveTo(0, target.height,0);
784
841
  }
785
842
 
786
843
  root.transform.colorTransform = colorTransform;
787
844
 
788
845
  //render
789
846
  renderer.render();
847
+
848
+ //
849
+ if (useTmp) {
850
+ stage.filterManager.copyPixels(
851
+ target,
852
+ this,
853
+ this.rect,
854
+ TMP_POINT,
855
+ true,
856
+ blendMode
857
+ );
858
+
859
+ stage.filterManager.pushTemp(target);
860
+ }
861
+
790
862
  }
791
863
 
792
864
  private _drawAsDisplay(
@@ -7,18 +7,17 @@ import { Stage, BitmapImage2D } from '@awayjs/stage';
7
7
  import { DefaultRenderer, RenderGroup } from '@awayjs/renderer';
8
8
 
9
9
  import { DisplayObjectContainer } from '../display/DisplayObjectContainer';
10
-
11
10
  import { BasicPartition, NodePool, View } from '@awayjs/view';
12
11
  import { Sprite } from '../display/Sprite';
13
12
  import { Shape } from '@awayjs/graphics';
13
+ import { FNTGeneratorBase } from './FNTGeneratrorBase';
14
14
 
15
- export class FNTGenerator {
15
+ export class FNTGenerator extends FNTGeneratorBase {
16
16
  private _root: DisplayObjectContainer;
17
17
  private _renderer: DefaultRenderer;
18
- private _stage: Stage;
19
18
 
20
- constructor(stage) {
21
- this._stage = stage;
19
+ constructor(stage: Stage) {
20
+ super (stage);
22
21
 
23
22
  //create the projection
24
23
  const projection = new PerspectiveProjection();
@@ -39,10 +38,9 @@ export class FNTGenerator {
39
38
  this._renderer.view.projection.transform.lookAt(new Vector3D());
40
39
 
41
40
  this._renderer.renderableSorter = null;//new RenderableSort2D();
42
-
43
41
  }
44
42
 
45
- public generate(font: Font, maxSize: number, fontSize: number, padding: number): any {
43
+ public generate(font: Font, maxSize: number, fontSize: number, padding: number): BitmapImage2D[] {
46
44
  let outputBitmap: BitmapImage2D;
47
45
  const outputBitmaps: BitmapImage2D[] = [];
48
46
  let mipSize: number;
@@ -0,0 +1,288 @@
1
+ import { FNTGeneratorBase } from './FNTGeneratrorBase';
2
+ import { Font } from './Font';
3
+ import { BitmapImage2D, Stage } from '@awayjs/stage';
4
+ import { Rectangle } from '@awayjs/core';
5
+ import { GraphicsPathCommand } from '@awayjs/graphics';
6
+ import { TesselatedFontTable } from './TesselatedFontTable';
7
+ import { TesselatedFontChar } from './TesselatedFontChar';
8
+
9
+ class Bounds {
10
+ left: number = Number.MAX_VALUE;
11
+ right: number = Number.MIN_VALUE;
12
+ top: number = Number.MAX_VALUE;
13
+ bottom: number = Number.MIN_VALUE;
14
+
15
+ grown (x: number, y: number) {
16
+ if (x < this.left)
17
+ this.left = x;
18
+
19
+ if (x > this.right)
20
+ this.right = x;
21
+
22
+ if (y < this.top)
23
+ this.top = y;
24
+
25
+ if (y > this.bottom)
26
+ this.bottom = y;
27
+ }
28
+
29
+ toRect(): Rectangle {
30
+ return new Rectangle(this.left, this.top, this.right - this.left, this.bottom - this.top);
31
+ }
32
+ }
33
+
34
+ interface IPathContainer {
35
+ path: Path2D;
36
+ rect: Rectangle;
37
+ char?: TesselatedFontChar;
38
+ }
39
+
40
+ export class FNTGeneratorCanvas extends FNTGeneratorBase {
41
+ private static getCanvasContext (
42
+ width: number = 100,
43
+ height: number = 100,
44
+ debug: boolean = false
45
+ ): CanvasRenderingContext2D {
46
+ const canvas = ('OffscreenCanvas' in self && !debug)
47
+ ? new self.OffscreenCanvas(width, height)
48
+ : document.createElement('canvas');
49
+
50
+ canvas.width = width;
51
+ canvas.height = height;
52
+
53
+ return canvas.getContext('2d', { desynchronized: true, alpha: true }) as CanvasRenderingContext2D;
54
+ }
55
+
56
+ private static freeCanvasContext (ctx: CanvasRenderingContext2D) {
57
+ if (ctx.canvas instanceof self.OffscreenCanvas) {
58
+ return;
59
+ }
60
+
61
+ // safari bug with canvas leaks
62
+ ctx.canvas.width = 0;
63
+ ctx.canvas.height = 0;
64
+ }
65
+
66
+ private static transformChar (char: TesselatedFontChar): IPathContainer {
67
+ const fillData = char.fill_data_path;
68
+
69
+ // empty command
70
+ if (fillData.commands[0].length === 0 && fillData.commands.length === 1) {
71
+ return null;
72
+ }
73
+
74
+ const path = new Path2D();
75
+ const box = new Bounds();
76
+
77
+ for (let i = 0; i < fillData.commands.length; i++) {
78
+ const data = fillData.data[i];
79
+ const commands = fillData.commands[i];
80
+
81
+ let pointer = 0;
82
+ for (const command of commands) {
83
+ switch (command) {
84
+ case GraphicsPathCommand.MOVE_TO: {
85
+ // flash path in twips, transform it
86
+ const px = data[pointer++] / 20;
87
+ const py = data[pointer++] / 20;
88
+
89
+ box.grown(px, py);
90
+ path.moveTo(px, py);
91
+ break;
92
+ }
93
+
94
+ case GraphicsPathCommand.LINE_TO: {
95
+ const px = data[pointer++] / 20;
96
+ const py = data[pointer++] / 20;
97
+
98
+ box.grown(px, py);
99
+ path.lineTo(px, py);
100
+ break;
101
+ }
102
+
103
+ case GraphicsPathCommand.CURVE_TO: {
104
+ const cx = data[pointer++] / 20;
105
+ const cy = data[pointer++] / 20;
106
+ const dx = data[pointer++] / 20;
107
+ const dy = data[pointer++] / 20;
108
+
109
+ box.grown(cx, cy);
110
+ box.grown(dx, dy);
111
+
112
+ path.quadraticCurveTo(
113
+ cx, cy,
114
+ dx, dy
115
+ );
116
+ }
117
+ }
118
+ }
119
+ //path.closePath();
120
+ }
121
+
122
+ return {
123
+ path,
124
+ char,
125
+ rect: box.toRect(),
126
+ };
127
+ }
128
+
129
+ private static sortPatches (a: IPathContainer, b: IPathContainer): number {
130
+ return a.rect.height - b.rect.height;
131
+ }
132
+
133
+ private static render(
134
+ table: TesselatedFontTable,
135
+ context: CanvasRenderingContext2D,
136
+ size: number,
137
+ fontSize: number = 0,
138
+ padding: number = 0
139
+ ): void {
140
+ const chars = table.get_font_chars();
141
+ const patches: IPathContainer[] = [];
142
+ const doublePad = padding * 2;
143
+
144
+ let totalArea: number = 0;
145
+ let scale: number = 1;
146
+
147
+ for (const char of chars) {
148
+ const path = FNTGeneratorCanvas.transformChar(char);
149
+
150
+ if (!path) {
151
+ continue;
152
+ }
153
+
154
+ totalArea += (doublePad + path.rect.width) * (doublePad + path.rect.height);
155
+
156
+ patches.push(path);
157
+ }
158
+
159
+ patches.sort(FNTGeneratorCanvas.sortPatches);
160
+
161
+ // TODO compute valid scale when fontSize is 0
162
+ if (fontSize) {
163
+ table.initFontSize(fontSize);
164
+ scale = /* 20 * table._size_multiply **/ size / table.get_font_em_size();
165
+ }
166
+
167
+ /**
168
+ * Render glyphs onto canvas
169
+ * We will render glyphs as white and background as black transparent
170
+ */
171
+ context.save();
172
+ context.clearRect(0, 0, size, size);
173
+ context.fillStyle = '#fff';
174
+
175
+ let tx = padding;
176
+ let ty = padding;
177
+ let maxHeight = 0;
178
+
179
+ // pack glyph based on size
180
+ for (const path of patches) {
181
+ if (!path.path) {
182
+ continue;
183
+ }
184
+
185
+ // if overflow - jump next location
186
+ if (tx + path.rect.width + doublePad >= size / scale) {
187
+ tx = padding;
188
+ ty += maxHeight + doublePad;
189
+ maxHeight = 0;
190
+ }
191
+
192
+ /**
193
+ * @todo Support multiple channel table data
194
+ */
195
+ if (ty + path.rect.height > size / scale) {
196
+ console.warn('[FNTGeneratorCanvas] Font image overflow, font will corrupted!');
197
+ }
198
+
199
+ maxHeight = Math.max(path.rect.height, maxHeight);
200
+
201
+ context.setTransform(
202
+ scale, 0, 0, scale,
203
+ (tx - path.rect.x) * scale,
204
+ (ty - path.rect.y) * scale
205
+ );
206
+
207
+ context.fill(path.path);
208
+ //context.strokeRect(path.rect.x,path.rect.y, path.rect.width, path.rect.height);
209
+
210
+ path.char.fnt_rect = new Rectangle(
211
+ (path.rect.x) / path.char.char_width,
212
+ (path.rect.y) / table.get_font_em_size(),
213
+ path.rect.width / path.char.char_width,
214
+ path.rect.height / table.get_font_em_size()
215
+ );
216
+
217
+ path.char.fnt_uv = new Rectangle(
218
+ tx * scale / size,
219
+ ty * scale / size,
220
+ path.rect.width * scale / size,
221
+ path.rect.height * scale / size);
222
+ /*
223
+ path.char.fnt_uv = new Rectangle(
224
+ (tx - path.rect.x) * scale / size,
225
+ 1 - (ty - path.rect.y) * scale / scale,
226
+ path.rect.width * scale / size,
227
+ path.rect.height * scale / size
228
+ );*/
229
+
230
+ path.char.fnt_channel = 0;
231
+
232
+ tx += path.rect.width + doublePad;
233
+ }
234
+
235
+ context.restore();
236
+ }
237
+
238
+ /**
239
+ * Debug mode.
240
+ * Prevent OffscreenCanvas and prevent canvas clear.
241
+ * Store canvas
242
+ */
243
+ public debug: boolean = false;
244
+
245
+ /**
246
+ * Store canvas used for caching. Only in debug mode
247
+ */
248
+ public canvas: HTMLCanvasElement;
249
+
250
+ constructor(stage: Stage, debug = false) {
251
+ super(stage);
252
+
253
+ this.debug = debug;
254
+ }
255
+
256
+ public generate(font: Font, size: number, fontSize: number, padding: number): BitmapImage2D[] {
257
+ const context = FNTGeneratorCanvas.getCanvasContext(size, size, this.debug);
258
+ const bitmaps = [];
259
+
260
+ for (const key in font.font_styles) {
261
+ const table = <TesselatedFontTable> font.font_styles[key];
262
+ // render table in context
263
+ FNTGeneratorCanvas.render(
264
+ table,
265
+ context,
266
+ size,
267
+ fontSize,
268
+ padding
269
+ );
270
+
271
+ const data = context.getImageData(0, 0, size, size);
272
+ const image = new BitmapImage2D(size, size, true, 0, true, this._stage);
273
+
274
+ image.setPixels(new Rectangle(0, 0, size, size), data.data);
275
+
276
+ bitmaps.push(image);
277
+ table.addFNTChannel(image);
278
+ }
279
+
280
+ if (!this.debug) {
281
+ FNTGeneratorCanvas.freeCanvasContext(context);
282
+ } else {
283
+ this.canvas = context.canvas;
284
+ }
285
+
286
+ return bitmaps;
287
+ }
288
+ }
@@ -0,0 +1,11 @@
1
+ import { Stage, BitmapImage2D } from '@awayjs/stage';
2
+ import { Font } from './Font';
3
+
4
+ export abstract class FNTGeneratorBase {
5
+ constructor (
6
+ protected _stage: Stage
7
+ ) {
8
+ }
9
+
10
+ public abstract generate(font: Font, maxSize: number, fontSize: number, padding: number): BitmapImage2D[];
11
+ }
@@ -39,14 +39,13 @@ export class TesselatedFontChar {
39
39
 
40
40
  public fnt_rect: Rectangle;
41
41
  public fnt_uv: Rectangle;
42
- public fnt_channel: number=0;
42
+ public fnt_channel: number = 0;
43
43
 
44
44
  /**
45
- * Elements for this char
45
+ * What is scale was used for generating last tesselation
46
+ * Used for reduce a vertices count
46
47
  */
47
- public stroke_data: AttributesBuffer;
48
- public fill_data: AttributesBuffer;
49
- public fill_data_path: GraphicsPath;
48
+ public lastTesselatedScale: number = 0;
50
49
 
51
50
  /**
52
51
  * the char_codes that this geom has kerning set for
@@ -58,13 +57,10 @@ export class TesselatedFontChar {
58
57
  public kerningValues: Array<number>=[];
59
58
 
60
59
  constructor(
61
- fill_data: AttributesBuffer = null,
62
- stroke_data: AttributesBuffer = null,
63
- fill_data_path: GraphicsPath = null) {
64
-
65
- this.fill_data = fill_data;
66
- this.stroke_data = stroke_data;
67
- this.fill_data_path = fill_data_path;
60
+ public fill_data: AttributesBuffer = null,
61
+ public stroke_data: AttributesBuffer = null,
62
+ public fill_data_path: GraphicsPath = null
63
+ ) {
68
64
  }
69
65
 
70
66
  public dispose() {