@awayjs/scene 0.13.265 → 0.13.267

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 (268) hide show
  1. package/.eslintrc +100 -100
  2. package/README.md +61 -61
  3. package/bundle/awayjs-scene.umd.js +2 -2
  4. package/bundle/awayjs-scene.umd.js.map +1 -1
  5. package/copyVersionToIndex.js +23 -23
  6. package/dist/index.d.ts +76 -75
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +67 -66
  9. package/dist/lib/Scene.d.ts +84 -84
  10. package/dist/lib/Scene.js +288 -288
  11. package/dist/lib/Settings.d.ts +16 -16
  12. package/dist/lib/Settings.js +60 -60
  13. package/dist/lib/adapters/IDisplayObjectAdapter.d.ts +13 -13
  14. package/dist/lib/adapters/IDisplayObjectAdapter.js +1 -1
  15. package/dist/lib/adapters/IFilter.d.ts +24 -24
  16. package/dist/lib/adapters/IFilter.js +1 -1
  17. package/dist/lib/adapters/IMovieClipAdapter.d.ts +14 -14
  18. package/dist/lib/adapters/IMovieClipAdapter.js +1 -1
  19. package/dist/lib/adapters/ITextfieldAdapter.d.ts +4 -4
  20. package/dist/lib/adapters/ITextfieldAdapter.js +1 -1
  21. package/dist/lib/base/IBitmapDrawable.d.ts +11 -11
  22. package/dist/lib/base/IBitmapDrawable.js +1 -1
  23. package/dist/lib/base/IFrameLabel.d.ts +4 -4
  24. package/dist/lib/base/IFrameLabel.js +1 -1
  25. package/dist/lib/base/IFrameScript.d.ts +5 -5
  26. package/dist/lib/base/IFrameScript.js +1 -1
  27. package/dist/lib/base/MouseButtons.d.ts +26 -22
  28. package/dist/lib/base/MouseButtons.d.ts.map +1 -1
  29. package/dist/lib/base/MouseButtons.js +1 -1
  30. package/dist/lib/base/Timeline.d.ts +148 -148
  31. package/dist/lib/base/Timeline.js +535 -535
  32. package/dist/lib/base/TimelineActionType.d.ts +18 -18
  33. package/dist/lib/base/TimelineActionType.js +1 -1
  34. package/dist/lib/controllers/ControllerBase.d.ts +14 -14
  35. package/dist/lib/controllers/ControllerBase.js +60 -60
  36. package/dist/lib/controllers/FirstPersonController.d.ts +78 -78
  37. package/dist/lib/controllers/FirstPersonController.js +228 -228
  38. package/dist/lib/controllers/FollowController.d.ts +12 -12
  39. package/dist/lib/controllers/FollowController.js +27 -27
  40. package/dist/lib/controllers/HoverController.d.ts +105 -105
  41. package/dist/lib/controllers/HoverController.js +302 -302
  42. package/dist/lib/controllers/LookAtController.d.ts +14 -14
  43. package/dist/lib/controllers/LookAtController.js +68 -68
  44. package/dist/lib/controllers/SpringController.d.ts +38 -38
  45. package/dist/lib/controllers/SpringController.js +59 -59
  46. package/dist/lib/display/Billboard.d.ts +147 -147
  47. package/dist/lib/display/Billboard.js +426 -426
  48. package/dist/lib/display/Camera.d.ts +40 -40
  49. package/dist/lib/display/Camera.js +90 -90
  50. package/dist/lib/display/DisplayObject.d.ts +938 -938
  51. package/dist/lib/display/DisplayObject.js +1641 -1641
  52. package/dist/lib/display/DisplayObjectContainer.d.ts +325 -325
  53. package/dist/lib/display/DisplayObjectContainer.js +480 -480
  54. package/dist/lib/display/LineSegment.d.ts +84 -84
  55. package/dist/lib/display/LineSegment.js +215 -215
  56. package/dist/lib/display/LoaderContainer.d.ts +504 -504
  57. package/dist/lib/display/LoaderContainer.js +603 -603
  58. package/dist/lib/display/MorphSprite.d.ts +20 -20
  59. package/dist/lib/display/MorphSprite.js +254 -254
  60. package/dist/lib/display/MovieClip.d.ts +152 -152
  61. package/dist/lib/display/MovieClip.js +735 -735
  62. package/dist/lib/display/SimpleButton.d.ts +158 -158
  63. package/dist/lib/display/SimpleButton.js +228 -228
  64. package/dist/lib/display/Skybox.d.ts +156 -156
  65. package/dist/lib/display/Skybox.js +309 -309
  66. package/dist/lib/display/Sprite.d.ts +70 -70
  67. package/dist/lib/display/Sprite.js +144 -144
  68. package/dist/lib/display/TextField.d.ts +1109 -1109
  69. package/dist/lib/display/TextField.js +3109 -3109
  70. package/dist/lib/display/TextSprite.d.ts +10 -10
  71. package/dist/lib/display/TextSprite.js +25 -25
  72. package/dist/lib/elements/SkyboxElements.d.ts +29 -29
  73. package/dist/lib/elements/SkyboxElements.js +127 -127
  74. package/dist/lib/errors/CastError.d.ts +4 -4
  75. package/dist/lib/errors/CastError.js +10 -10
  76. package/dist/lib/events/CameraEvent.d.ts +16 -16
  77. package/dist/lib/events/CameraEvent.js +30 -30
  78. package/dist/lib/events/FocusEvent.d.ts +42 -42
  79. package/dist/lib/events/FocusEvent.js +43 -43
  80. package/dist/lib/events/KeyboardEvent.d.ts +21 -21
  81. package/dist/lib/events/KeyboardEvent.js +43 -43
  82. package/dist/lib/events/MouseEvent.d.ts +91 -184
  83. package/dist/lib/events/MouseEvent.d.ts.map +1 -1
  84. package/dist/lib/events/MouseEvent.js +122 -191
  85. package/dist/lib/events/PointerEvent.d.ts +94 -0
  86. package/dist/lib/events/PointerEvent.d.ts.map +1 -0
  87. package/dist/lib/events/PointerEvent.js +73 -0
  88. package/dist/lib/events/ResizeEvent.d.ts +15 -15
  89. package/dist/lib/events/ResizeEvent.js +38 -38
  90. package/dist/lib/events/TextfieldEvent.d.ts +19 -19
  91. package/dist/lib/events/TextfieldEvent.js +28 -28
  92. package/dist/lib/events/TouchEvent.d.ts +55 -0
  93. package/dist/lib/events/TouchEvent.d.ts.map +1 -0
  94. package/dist/lib/events/TouchEvent.js +89 -0
  95. package/dist/lib/factories/DefaultSceneGraphFactory.d.ts +26 -26
  96. package/dist/lib/factories/DefaultSceneGraphFactory.js +54 -54
  97. package/dist/lib/factories/ISceneGraphFactory.d.ts +20 -20
  98. package/dist/lib/factories/ISceneGraphFactory.js +1 -1
  99. package/dist/lib/image/SceneImage2D.d.ts +236 -236
  100. package/dist/lib/image/SceneImage2D.js +687 -687
  101. package/dist/lib/managers/DefaultFontManager.d.ts +14 -14
  102. package/dist/lib/managers/DefaultFontManager.js +101 -101
  103. package/dist/lib/managers/DeviceFontManager.d.ts +8 -8
  104. package/dist/lib/managers/DeviceFontManager.js +44 -44
  105. package/dist/lib/managers/FrameScriptManager.d.ts +36 -36
  106. package/dist/lib/managers/FrameScriptManager.js +377 -377
  107. package/dist/lib/managers/IInputRecorder.d.ts +3 -3
  108. package/dist/lib/managers/IInputRecorder.js +1 -1
  109. package/dist/lib/managers/MouseManager.d.ts +76 -89
  110. package/dist/lib/managers/MouseManager.d.ts.map +1 -1
  111. package/dist/lib/managers/MouseManager.js +812 -761
  112. package/dist/lib/prefabs/PrefabBase.d.ts +19 -19
  113. package/dist/lib/prefabs/PrefabBase.js +42 -42
  114. package/dist/lib/prefabs/PrimitiveCapsulePrefab.d.ts +56 -56
  115. package/dist/lib/prefabs/PrimitiveCapsulePrefab.js +269 -269
  116. package/dist/lib/prefabs/PrimitiveConePrefab.d.ts +21 -21
  117. package/dist/lib/prefabs/PrimitiveConePrefab.js +43 -43
  118. package/dist/lib/prefabs/PrimitiveCubePrefab.d.ts +74 -74
  119. package/dist/lib/prefabs/PrimitiveCubePrefab.js +545 -545
  120. package/dist/lib/prefabs/PrimitiveCylinderPrefab.d.ts +79 -79
  121. package/dist/lib/prefabs/PrimitiveCylinderPrefab.js +584 -584
  122. package/dist/lib/prefabs/PrimitivePlanePrefab.d.ts +65 -65
  123. package/dist/lib/prefabs/PrimitivePlanePrefab.js +300 -300
  124. package/dist/lib/prefabs/PrimitivePolygonPrefab.d.ts +29 -29
  125. package/dist/lib/prefabs/PrimitivePolygonPrefab.js +64 -64
  126. package/dist/lib/prefabs/PrimitivePrefabBase.d.ts +67 -67
  127. package/dist/lib/prefabs/PrimitivePrefabBase.js +158 -158
  128. package/dist/lib/prefabs/PrimitiveSpherePrefab.d.ts +48 -48
  129. package/dist/lib/prefabs/PrimitiveSpherePrefab.js +294 -294
  130. package/dist/lib/prefabs/PrimitiveTorusPrefab.d.ts +55 -55
  131. package/dist/lib/prefabs/PrimitiveTorusPrefab.js +260 -260
  132. package/dist/lib/text/AntiAliasType.d.ts +24 -24
  133. package/dist/lib/text/AntiAliasType.js +28 -28
  134. package/dist/lib/text/BitmapFontChar.d.ts +39 -39
  135. package/dist/lib/text/BitmapFontChar.js +42 -42
  136. package/dist/lib/text/BitmapFontTable.d.ts +80 -80
  137. package/dist/lib/text/BitmapFontTable.js +203 -203
  138. package/dist/lib/text/FNTGenerator.d.ts +10 -10
  139. package/dist/lib/text/FNTGenerator.js +74 -74
  140. package/dist/lib/text/FNTGeneratorCanvas.d.ts +22 -22
  141. package/dist/lib/text/FNTGeneratorCanvas.js +205 -205
  142. package/dist/lib/text/FNTGeneratrorBase.d.ts +7 -7
  143. package/dist/lib/text/FNTGeneratrorBase.js +7 -7
  144. package/dist/lib/text/Font.d.ts +38 -38
  145. package/dist/lib/text/Font.js +109 -109
  146. package/dist/lib/text/FontLookUpMode.d.ts +5 -5
  147. package/dist/lib/text/FontLookUpMode.js +7 -7
  148. package/dist/lib/text/FontStyleName.d.ts +6 -6
  149. package/dist/lib/text/FontStyleName.js +7 -7
  150. package/dist/lib/text/GridFitType.d.ts +31 -31
  151. package/dist/lib/text/GridFitType.js +35 -35
  152. package/dist/lib/text/HTMLTextProcessor.d.ts +8 -8
  153. package/dist/lib/text/HTMLTextProcessor.js +423 -423
  154. package/dist/lib/text/IFontTable.d.ts +16 -16
  155. package/dist/lib/text/IFontTable.js +1 -1
  156. package/dist/lib/text/TesselatedFontChar.d.ts +58 -58
  157. package/dist/lib/text/TesselatedFontChar.js +66 -66
  158. package/dist/lib/text/TesselatedFontTable.d.ts +95 -95
  159. package/dist/lib/text/TesselatedFontTable.js +992 -992
  160. package/dist/lib/text/TextFieldAutoSize.d.ts +28 -28
  161. package/dist/lib/text/TextFieldAutoSize.js +32 -32
  162. package/dist/lib/text/TextFieldType.d.ts +20 -20
  163. package/dist/lib/text/TextFieldType.js +24 -24
  164. package/dist/lib/text/TextFormat.d.ts +273 -273
  165. package/dist/lib/text/TextFormat.js +440 -440
  166. package/dist/lib/text/TextFormatAlign.d.ts +26 -26
  167. package/dist/lib/text/TextFormatAlign.js +30 -30
  168. package/dist/lib/text/TextInteractionMode.d.ts +21 -21
  169. package/dist/lib/text/TextInteractionMode.js +25 -25
  170. package/dist/lib/text/TextLineMetrics.d.ts +61 -61
  171. package/dist/lib/text/TextLineMetrics.js +42 -42
  172. package/dist/lib/text/TextShape.d.ts +20 -20
  173. package/dist/lib/text/TextShape.js +75 -75
  174. package/dist/lib/tools/Merge.d.ts +81 -81
  175. package/dist/lib/tools/Merge.js +266 -266
  176. package/dist/lib/utils/Cast.d.ts +20 -20
  177. package/dist/lib/utils/Cast.js +271 -271
  178. package/index.ts +92 -91
  179. package/lib/Scene.ts +343 -343
  180. package/lib/Settings.ts +83 -83
  181. package/lib/adapters/IDisplayObjectAdapter.ts +23 -23
  182. package/lib/adapters/IFilter.ts +25 -25
  183. package/lib/adapters/IMovieClipAdapter.ts +24 -24
  184. package/lib/adapters/ITextfieldAdapter.ts +6 -6
  185. package/lib/base/IBitmapDrawable.ts +13 -13
  186. package/lib/base/IFrameLabel.ts +3 -3
  187. package/lib/base/IFrameScript.ts +4 -4
  188. package/lib/base/MouseButtons.ts +31 -26
  189. package/lib/base/Timeline.ts +727 -727
  190. package/lib/base/TimelineActionType.ts +18 -18
  191. package/lib/controllers/ControllerBase.ts +66 -66
  192. package/lib/controllers/FirstPersonController.ts +241 -241
  193. package/lib/controllers/FollowController.ts +22 -22
  194. package/lib/controllers/HoverController.ts +304 -304
  195. package/lib/controllers/LookAtController.ts +75 -75
  196. package/lib/controllers/SpringController.ts +91 -91
  197. package/lib/display/Billboard.ts +504 -504
  198. package/lib/display/Camera.ts +89 -89
  199. package/lib/display/DisplayObject.ts +1893 -1893
  200. package/lib/display/DisplayObjectContainer.ts +538 -538
  201. package/lib/display/LineSegment.ts +248 -248
  202. package/lib/display/LoaderContainer.ts +651 -651
  203. package/lib/display/MorphSprite.ts +347 -347
  204. package/lib/display/MovieClip.ts +879 -879
  205. package/lib/display/SimpleButton.ts +224 -224
  206. package/lib/display/Skybox.ts +379 -379
  207. package/lib/display/Sprite.ts +165 -165
  208. package/lib/display/TextField.ts +3958 -3958
  209. package/lib/display/TextSprite.ts +22 -22
  210. package/lib/elements/SkyboxElements.ts +141 -141
  211. package/lib/errors/CastError.ts +6 -6
  212. package/lib/events/CameraEvent.ts +29 -29
  213. package/lib/events/FocusEvent.ts +69 -69
  214. package/lib/events/KeyboardEvent.ts +40 -40
  215. package/lib/events/MouseEvent.ts +147 -287
  216. package/lib/events/PointerEvent.ts +146 -0
  217. package/lib/events/ResizeEvent.ts +31 -31
  218. package/lib/events/TextfieldEvent.ts +28 -28
  219. package/lib/events/TouchEvent.ts +107 -0
  220. package/lib/factories/DefaultSceneGraphFactory.ts +56 -56
  221. package/lib/factories/ISceneGraphFactory.ts +28 -28
  222. package/lib/image/SceneImage2D.ts +1020 -1020
  223. package/lib/managers/DefaultFontManager.ts +124 -124
  224. package/lib/managers/DeviceFontManager.ts +55 -55
  225. package/lib/managers/FrameScriptManager.ts +431 -431
  226. package/lib/managers/IInputRecorder.ts +2 -2
  227. package/lib/managers/MouseManager.ts +948 -884
  228. package/lib/prefabs/PrefabBase.ts +47 -47
  229. package/lib/prefabs/PrimitiveCapsulePrefab.ts +286 -286
  230. package/lib/prefabs/PrimitiveConePrefab.ts +37 -37
  231. package/lib/prefabs/PrimitiveCubePrefab.ts +630 -630
  232. package/lib/prefabs/PrimitiveCylinderPrefab.ts +657 -657
  233. package/lib/prefabs/PrimitivePlanePrefab.ts +352 -352
  234. package/lib/prefabs/PrimitivePolygonPrefab.ts +53 -53
  235. package/lib/prefabs/PrimitivePrefabBase.ts +177 -177
  236. package/lib/prefabs/PrimitiveSpherePrefab.ts +336 -336
  237. package/lib/prefabs/PrimitiveTorusPrefab.ts +282 -282
  238. package/lib/text/AntiAliasType.ts +24 -24
  239. package/lib/text/BitmapFontChar.ts +53 -53
  240. package/lib/text/BitmapFontTable.ts +232 -232
  241. package/lib/text/FNTGenerator.ts +100 -100
  242. package/lib/text/FNTGeneratorCanvas.ts +287 -287
  243. package/lib/text/FNTGeneratrorBase.ts +10 -10
  244. package/lib/text/Font.ts +114 -114
  245. package/lib/text/FontLookUpMode.ts +5 -5
  246. package/lib/text/FontStyleName.ts +6 -6
  247. package/lib/text/GridFitType.ts +32 -32
  248. package/lib/text/HTMLTextProcessor.ts +418 -418
  249. package/lib/text/IFontTable.ts +17 -17
  250. package/lib/text/TesselatedFontChar.ts +80 -80
  251. package/lib/text/TesselatedFontTable.ts +1221 -1221
  252. package/lib/text/TextFieldAutoSize.ts +30 -30
  253. package/lib/text/TextFieldType.ts +21 -21
  254. package/lib/text/TextFormat.ts +601 -601
  255. package/lib/text/TextFormatAlign.ts +28 -28
  256. package/lib/text/TextInteractionMode.ts +21 -21
  257. package/lib/text/TextLineMetrics.ts +74 -74
  258. package/lib/text/TextShape.ts +90 -90
  259. package/lib/tools/Merge.ts +311 -311
  260. package/lib/utils/Cast.ts +304 -304
  261. package/package.json +74 -74
  262. package/rollup.config.js +33 -33
  263. package/scripts/build.sh +2 -2
  264. package/scripts/gendocs.sh +14 -14
  265. package/scripts/genexamples.sh +14 -14
  266. package/scripts/genfull.sh +14 -14
  267. package/system.config.js +51 -51
  268. package/tsconfig.json +18 -18
@@ -1,1021 +1,1021 @@
1
-
2
- import { ContainerNode, View } from '@awayjs/view';
3
- import { MaterialBase, MethodMaterial } from '@awayjs/materials';
4
- import { DisplayObjectContainer } from '../display/DisplayObjectContainer';
5
- import { DisplayObject } from '../display/DisplayObject';
6
- import { Billboard } from '../display/Billboard';
7
- import { Settings } from '../Settings';
8
-
9
- import {
10
- ColorTransform,
11
- Matrix,
12
- Rectangle,
13
- Point,
14
- PerspectiveProjection,
15
- CoordinateSystem,
16
- Vector3D,
17
- Transform,
18
- ColorUtils
19
- } from '@awayjs/core';
20
-
21
- import { Stage,
22
- BitmapImage2D,
23
- _Stage_BitmapImage2D,
24
- BlendMode,
25
- ContextWebGL,
26
- Image2D
27
- } from '@awayjs/stage';
28
-
29
- import { DefaultRenderer, RenderGroup, Style } from '@awayjs/renderer';
30
-
31
- // empty matrix for transform reset
32
- const TMP_POINT = new Point(0,0);
33
-
34
- /**
35
- *
36
- */
37
- export class SceneImage2D extends BitmapImage2D {
38
- public static assetType: string = '[image SceneImage2D]';
39
-
40
- public static getImage(
41
- width: number, height: number, transparent: boolean = true,
42
- fillColor: number = 0xffffffff,
43
- powerOfTwo: boolean = true, stage: Stage = null, msaa = false
44
- ) {
45
- const result = new SceneImage2D(
46
- width,
47
- height,
48
- transparent,
49
- fillColor,
50
- powerOfTwo,
51
- stage
52
- );
53
-
54
- if (!msaa) {
55
- result._msaaNeedDrop = true;
56
- result._antialiasQuality = 0;
57
- } else {
58
- result._enforceMSAASupport = true;
59
- }
60
-
61
- return result;
62
- }
63
-
64
- public static tryStoreImage(image: Image2D, stage: Stage): void {
65
- stage.filterManager.pushTemp(image);
66
- }
67
-
68
- private static getTemp(width: number, height: number, stage: Stage, msaa = false) {
69
- const image = stage.filterManager.popTemp(width, height, msaa && Settings.ALLOW_FORCE_MSAA > 1);
70
-
71
- image.antialiasQuality = msaa ? Settings.ALLOW_FORCE_MSAA : 0;
72
- return image;
73
- }
74
-
75
- private static _renderer: DefaultRenderer;
76
- private static _billboardRenderer: DefaultRenderer;
77
- private static _view: View;
78
- private static _billboardView: View;
79
- private static _root: DisplayObjectContainer;
80
- private static _rootNode: ContainerNode;
81
- private static _billboardRoot: DisplayObjectContainer;
82
- private static _billboard: Billboard;
83
-
84
- private _imageDataDirty: boolean;
85
-
86
- private _asyncRead: Promise<boolean>;
87
-
88
- private _initalFillColor: number = null;
89
-
90
- private _lastUsedFill: number = null;
91
-
92
- private _internalSync: boolean = false;
93
-
94
- protected _msaaNeedDrop: boolean = false;
95
-
96
- protected _enforceMSAASupport: boolean = false;
97
-
98
- protected _dropMSAA() {
99
- if (this._msaaNeedDrop || !this.canUseMSAAInternaly) {
100
- return;
101
- }
102
-
103
- this._enforceMSAASupport = false;
104
- this._msaaNeedDrop = true;
105
-
106
- // force dispose texture and buffers
107
- // MSAA not stored in pool
108
- if (this.wasUpload) {
109
- // super unload not call sync, we call it
110
- super.unload();
111
- }
112
-
113
- console.debug(
114
- '[SceneImage2D Experemental] Drop MSAA support because a setPixel* operation called after upload.',
115
- this.id
116
- );
117
- }
118
-
119
- public get canUseMSAAInternaly () {
120
- if (this._enforceMSAASupport) {
121
- return true;
122
- }
123
-
124
- let minW = Settings.MSAA_MINIMAL_IMAGE_SIZE;
125
- let minH = Settings.MSAA_MINIMAL_IMAGE_SIZE;
126
-
127
- if (this._stage) {
128
- minH = Math.min(this._stage.height, minH);
129
- minW = Math.min(this._stage.width, minW);
130
- }
131
-
132
- return (
133
- this.width >= minW &&
134
- this.height >= minH &&
135
- !this._msaaNeedDrop
136
- );
137
- }
138
-
139
- /*private*/ _antialiasQuality: number = Settings.ALLOW_FORCE_MSAA;
140
- public get antialiasQuality() {
141
- return this.canUseMSAAInternaly ? this._antialiasQuality : 0;
142
- }
143
-
144
- protected syncData(async = false): boolean | Promise<boolean> {
145
-
146
- if (async && this._asyncRead) {
147
- return this._asyncRead;
148
- }
149
-
150
- if (!async && this._asyncRead) {
151
- throw '[SceneImage2D] Synced read not allowed while async read is requested!';
152
- }
153
-
154
- this.applySymbol();
155
-
156
- // update data from pixels from GPU
157
- if (!this._imageDataDirty) {
158
- return async ? Promise.resolve(false) : false;
159
- }
160
-
161
- const context = <ContextWebGL> this._stage.context;
162
-
163
- this._stage.setRenderTarget(this, false);
164
-
165
- // when we call syncData, we already loose other data
166
- // not require apply symbol etc, because it already should be applied
167
- if (!this._data) {
168
- this._data = new Uint8ClampedArray(this.width * this.height * 4);
169
- }
170
-
171
- // mark that this internal call, avoid reqursion loop
172
- this._internalSync = true;
173
- this._asyncRead = context.drawToBitmapImage2D(this, false, async);
174
- this._internalSync = false;
175
-
176
- this._stage.setRenderTarget(null);
177
-
178
- if (!async) {
179
- this._imageDataDirty = false;
180
- // we store pixel buffer already as PMA.
181
- // we should prevent unpack what already is PMA
182
- this._unpackPMA = false;
183
- return true;
184
- }
185
-
186
- return this._asyncRead.then((_status: boolean) => {
187
- this._imageDataDirty = false;
188
- this._unpackPMA = false;
189
- this._asyncRead = null;
190
-
191
- return true;
192
- });
193
- }
194
-
195
- /* overide internal */getDataInternal (constructEmpty = true, skipSync = false) {
196
-
197
- // if sync called, check that this is requried
198
- if (!skipSync && this._imageDataDirty && !this._internalSync) {
199
- // sync data already should fill _data
200
- this.syncData(false);
201
-
202
- return this._data;
203
- }
204
-
205
- if (this._initalFillColor === null) {
206
- return super.getDataInternal(constructEmpty, true);
207
- }
208
-
209
- // disable empty buffer filling
210
- // and check that buffer is empyt (has now symbol or alpha)
211
- const data = super.getDataInternal(false, true);
212
-
213
- // if it empty, fill with initlal value
214
- if (!data) {
215
-
216
- // fill rect constuct buffer inside
217
- super.fillRect(this.rect, this._initalFillColor);
218
- this._initalFillColor = null;
219
-
220
- return this._data;
221
- }
222
-
223
- return data;
224
- }
225
-
226
- /**
227
- *
228
- * @returns {string}
229
- */
230
- public get assetType(): string {
231
- return SceneImage2D.assetType;
232
- }
233
-
234
- /**
235
- * Creates a BitmapImage2D object with a specified width and height. If you
236
- * specify a value for the <code>fillColor</code> parameter, every pixel in
237
- * the bitmap is set to that color.
238
- *
239
- * <p>By default, the bitmap is created as transparent, unless you pass
240
- * the value <code>false</code> for the transparent parameter. After you
241
- * create an opaque bitmap, you cannot change it to a transparent bitmap.
242
- * Every pixel in an opaque bitmap uses only 24 bits of color channel
243
- * information. If you define the bitmap as transparent, every pixel uses 32
244
- * bits of color channel information, including an alpha transparency
245
- * channel.</p>
246
- *
247
- * @param width The width of the bitmap image in pixels.
248
- * @param height The height of the bitmap image in pixels.
249
- * @param transparent Specifies whether the bitmap image supports per-pixel
250
- * transparency. The default value is <code>true</code>
251
- * (transparent). To create a fully transparent bitmap,
252
- * set the value of the <code>transparent</code>
253
- * parameter to <code>true</code> and the value of the
254
- * <code>fillColor</code> parameter to 0x00000000(or to
255
- * 0). Setting the <code>transparent</code> property to
256
- * <code>false</code> can result in minor improvements
257
- * in rendering performance.
258
- * @param fillColor A 32-bit ARGB color value that you use to fill the
259
- * bitmap image area. The default value is
260
- * 0xFFFFFFFF(solid white).
261
- */
262
- constructor(
263
- width: number, height: number, transparent: boolean = true,
264
- fillColor: number = 0xffffffff, powerOfTwo: boolean = true, stage: Stage = null) {
265
-
266
- super(width, height, transparent, null, powerOfTwo, stage);
267
-
268
- this._initalFillColor = fillColor;
269
- this._lastUsedFill = fillColor;
270
- }
271
-
272
- private createRenderer() {
273
- //create the projection
274
- const projection = new PerspectiveProjection();
275
- projection.coordinateSystem = CoordinateSystem.RIGHT_HANDED;
276
- projection.originX = -1;
277
- projection.originY = 1;
278
- projection.transform = new Transform();
279
- projection.transform.scaleTo(1, -1, 1);
280
- projection.transform.moveTo(0, 0, -1000);
281
-
282
- //create the view
283
- SceneImage2D._view = new View(projection, this._stage);
284
- SceneImage2D._root = new DisplayObjectContainer();
285
- SceneImage2D._rootNode = SceneImage2D._view.getNode(SceneImage2D._root);
286
- SceneImage2D._renderer = <DefaultRenderer> RenderGroup
287
- .getInstance(DefaultRenderer)
288
- .getRenderer(SceneImage2D._rootNode.partition);
289
-
290
- //set the view properties
291
- SceneImage2D._view.backgroundAlpha = 0;
292
- SceneImage2D._view.backgroundColor = 0x0;
293
-
294
- //set the renderer properties
295
- SceneImage2D._renderer.disableClear = true;
296
- SceneImage2D._renderer.renderableSorter = null;//new RenderableSort2D();
297
- //SceneImage2D._renderer.antiAlias = Settings.ALLOW_FORCE_MSAA;
298
- }
299
-
300
- private createBillboardRenderer(): void {
301
- //create the projection
302
- const projection = new PerspectiveProjection();
303
- projection.coordinateSystem = CoordinateSystem.RIGHT_HANDED;
304
- projection.originX = -1;
305
- projection.originY = 1;
306
- projection.transform = new Transform();
307
- projection.transform.moveTo(0, 0, -1000);
308
- projection.transform.lookAt(new Vector3D());
309
-
310
- //create the view
311
- SceneImage2D._billboardView = new View(projection, this._stage);
312
- SceneImage2D._billboardRoot = new DisplayObjectContainer();
313
- SceneImage2D._billboardRenderer = <DefaultRenderer> RenderGroup
314
- .getInstance(DefaultRenderer)
315
- .getRenderer(SceneImage2D._billboardView.getNode(SceneImage2D._billboardRoot).partition);
316
-
317
- //SceneImage2D._billboardRoot.partition = SceneImage2D._billboardRenderer.partition;
318
-
319
- //SceneImage2D._renderer.antiAlias = Settings.ALLOW_FORCE_MSAA;
320
- //set the view properties
321
- SceneImage2D._billboardView.backgroundAlpha = 0;
322
- SceneImage2D._billboardView.backgroundColor = 0x0;
323
-
324
- //set the renderer properties
325
- SceneImage2D._billboardRenderer.disableClear = true;
326
- SceneImage2D._billboardRenderer.renderableSorter = null;//new RenderableSort2D();
327
-
328
- const mat: MethodMaterial = new MethodMaterial(new BitmapImage2D(128, 128, true, 0x0));
329
- mat.bothSides = true;
330
- mat.alphaBlending = true;
331
-
332
- SceneImage2D._billboard = new Billboard(mat);
333
- SceneImage2D._billboard.style = new Style();
334
-
335
- SceneImage2D._billboardRoot.addChild(SceneImage2D._billboard);
336
- }
337
-
338
- /**
339
- * Frees memory that is used to store the BitmapImage2D object.
340
- *
341
- * <p>When the <code>dispose()</code> method is called on an image, the width
342
- * and height of the image are set to 0. All subsequent calls to methods or
343
- * properties of this BitmapImage2D instance fail, and an exception is thrown.
344
- * </p>
345
- *
346
- * <p><code>BitmapImage2D.dispose()</code> releases the memory occupied by the
347
- * actual bitmap data, immediately(a bitmap can consume up to 64 MB of
348
- * memory). After using <code>BitmapImage2D.dispose()</code>, the BitmapImage2D
349
- * object is no longer usable and an exception may be thrown if
350
- * you call functions on the BitmapImage2D object. However,
351
- * <code>BitmapImage2D.dispose()</code> does not garbage collect the BitmapImage2D
352
- * object(approximately 128 bytes); the memory occupied by the actual
353
- * BitmapImage2D object is released at the time the BitmapImage2D object is
354
- * collected by the garbage collector.</p>
355
- *
356
- */
357
- public dispose(): void {
358
- this._clearFromDispose = true;
359
-
360
- this.dropAllReferences();
361
- this.unmarkToUnload();
362
- this.unuseWeakRef();
363
-
364
- // drop buffer, because is big
365
- this._data = null;
366
- this._locked = false;
367
- super.dispose();
368
-
369
- this._clearFromDispose = false;
370
- }
371
-
372
- public unload() {
373
- // query async unload
374
- const t = this.syncData(true);
375
-
376
- // strict quard
377
- if (typeof t !== 'boolean') {
378
- t.then(() => super.unload());
379
- return;
380
- }
381
-
382
- super.unload();
383
- }
384
-
385
- protected deepClone(from: BitmapImage2D) {
386
- this.copyPixels(from, this._rect, new Point(0,0));
387
- }
388
-
389
- /**
390
- * Fills a rectangular area of pixels with a specified ARGB color.
391
- *
392
- * @param rect The rectangular area to fill.
393
- * @param color The ARGB color value that fills the area. ARGB colors are
394
- * often specified in hexadecimal format; for example,
395
- * 0xFF336699.
396
- * @throws TypeError The rect is null.
397
- */
398
- public fillRect(rect: Rectangle, color: number): void {
399
- this.dropAllReferences();
400
-
401
- const argb = ColorUtils.float32ColorToARGB(color);
402
- const alpha = this._transparent ? argb[0] / 255 : 1;
403
- const isCrop = rect !== this._rect && !this._rect.equals(rect);
404
-
405
- this._stage.setRenderTarget(this, true, 0, 0, true);
406
- this._stage.setScissor(rect);
407
-
408
- // we shure that color is fully filled when there are not any crops
409
- this._lastUsedFill = isCrop ? null : color;
410
-
411
- this._stage.clear(
412
- (argb[1] / 0xff) * alpha,
413
- (argb[2] / 0xff) * alpha,
414
- (argb[3] / 0xff) * alpha,
415
- alpha
416
- );
417
-
418
- this._stage.setScissor(null);
419
- this._imageDataDirty = true;
420
- }
421
-
422
- /**
423
- * Provides a fast routine to perform pixel manipulation between images with
424
- * no stretching, rotation, or color effects. This method copies a
425
- * rectangular area of a source image to a rectangular area of the same size
426
- * at the destination point of the destination BitmapImage2D object.
427
- *
428
- * <p>If you include the <code>alphaBitmap</code> and <code>alphaPoint</code>
429
- * parameters, you can use a secondary image as an alpha source for the
430
- * source image. If the source image has alpha data, both sets of alpha data
431
- * are used to composite pixels from the source image to the destination
432
- * image. The <code>alphaPoint</code> parameter is the point in the alpha
433
- * image that corresponds to the upper-left corner of the source rectangle.
434
- * Any pixels outside the intersection of the source image and alpha image
435
- * are not copied to the destination image.</p>
436
- *
437
- * <p>The <code>mergeAlpha</code> property controls whether or not the alpha
438
- * channel is used when a transparent image is copied onto another
439
- * transparent image. To copy pixels with the alpha channel data, set the
440
- * <code>mergeAlpha</code> property to <code>true</code>. By default, the
441
- * <code>mergeAlpha</code> property is <code>false</code>.</p>
442
- *
443
- * @param source The input bitmap image from which to copy pixels.
444
- * The source image can be a different BitmapImage2D
445
- * instance, or it can refer to the current
446
- * BitmapImage2D instance.
447
- * @param sourceRect A rectangle that defines the area of the source
448
- * image to use as input.
449
- * @param destPoint The destination point that represents the
450
- * upper-left corner of the rectangular area where
451
- * the new pixels are placed.
452
- * @param alphaBitmapData A secondary, alpha BitmapImage2D object source.
453
- * @param alphaPoint The point in the alpha BitmapImage2D object source
454
- * that corresponds to the upper-left corner of the
455
- * <code>sourceRect</code> parameter.
456
- * @param mergeAlpha To use the alpha channel, set the value to
457
- * <code>true</code>. To copy pixels with no alpha
458
- * channel, set the value to <code>false</code>.
459
- * @throws TypeError The sourceBitmapImage2D, sourceRect, destPoint are null.
460
- */
461
- public copyPixels(
462
- source: BitmapImage2D, sourceRect: Rectangle, destPoint: Point,
463
- alphaBitmapData?: BitmapImage2D, alphaPoint?: Point, mergeAlpha?: boolean): void {
464
-
465
- this._lastUsedFill = null;
466
- this.dropAllReferences();
467
- this.unmarkToUnload();
468
-
469
- // need drop alpha from source when target is not has alpha
470
- mergeAlpha = this.transparent !== source.transparent || mergeAlpha;
471
-
472
- // CPU based copy, not require run GPU based copy
473
- // block is equal, one of image not uploaded yet
474
- // and source image no require SYNC
475
- if (!(<SceneImage2D>source)._imageDataDirty &&
476
- sourceRect.equals(this._rect) &&
477
- this._rect.equals(source.rect) &&
478
- !mergeAlpha && (!this.wasUpload || !source.wasUpload)
479
- ) {
480
- const data = source.getDataInternal(true);
481
-
482
- // inline, instead of setPixels, because it use a lot of checks
483
- if (this._data) {
484
- this._data.set(data);
485
- } else {
486
- this._data = data.slice();
487
- }
488
-
489
- // we should sync this, because we can apply PMA twice, that not needed for us
490
- this._unpackPMA = (<any> source)._unpackPMA;
491
- // we should reset initial color, because not require fill it after copyPixel
492
- this._initalFillColor = null;
493
- this._imageDataDirty = false;
494
- this.invalidateGPU();
495
- return;
496
- }
497
-
498
- if (source.width * source.height <= Settings.CPU_COPY_PIXELS_COUNT && !mergeAlpha) {
499
- // todo Not implemented yet, need to implement and check performance change
500
- // i think that increase some small operation performance,
501
- // because copy by GPU required upload array to VRAM,
502
- // and games that use set/get pixels and copyPixels only for math process not required use GPU copy
503
- }
504
-
505
- if (this._initalFillColor !== null) {
506
- this.fillRect(this._rect, this._initalFillColor);
507
- this._initalFillColor = null;
508
- }
509
-
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
-
551
- this._imageDataDirty = true;
552
- }
553
-
554
- public threshold(
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 {
564
-
565
- this._lastUsedFill = null;
566
- this.dropAllReferences();
567
- this.unmarkToUnload();
568
-
569
- this._stage.threshold(source, this, sourceRect, destPoint, operation, threshold, color, mask, copySource);
570
- this._imageDataDirty = true;
571
- }
572
-
573
- public applyFilter (source: BitmapImage2D, sourceRect: Rectangle, destPoint: Point, filter: any): boolean {
574
- if (!Settings.USE_UNSAFE_FILTERS || !filter || !filter.filterName) {
575
- return false;
576
- }
577
-
578
- this.dropAllReferences(false);
579
-
580
- const result = this._stage.filterManager.applyFilter (
581
- source,
582
- this,
583
- sourceRect,
584
- destPoint,
585
- filter.filterName, filter);
586
-
587
- this._imageDataDirty = result;
588
- return result;
589
- }
590
-
591
- public colorTransform(rect: Rectangle, colorTransform: ColorTransform): void {
592
- this.dropAllReferences();
593
- this.unmarkToUnload();
594
-
595
- this._lastUsedFill = null;
596
-
597
- this._stage.colorTransform(this, this, rect, colorTransform);
598
-
599
- this._imageDataDirty = true;
600
- }
601
-
602
- public setPixel(x: number, y: number, color: number) {
603
- // we can't upload buffer in MSAA texture after creating - only render it and get
604
- if (this.canUseMSAAInternaly) {
605
- this._dropMSAA();
606
- }
607
-
608
- super.setPixel(x, y, color);
609
- }
610
-
611
- public setPixel32(x: number, y: number, color: number) {
612
- // we can't upload buffer in MSAA texture after creating - only render it and get
613
- if (this.canUseMSAAInternaly) {
614
- this._dropMSAA();
615
- }
616
-
617
- super.setPixel32(x, y, color);
618
- }
619
-
620
- public setPixels(rect: Rectangle, buffer: Uint8ClampedArray) {
621
- // we can't upload buffer in MSAA texture after creating - only render it and get
622
- if (this.wasUpload && this.canUseMSAAInternaly) {
623
- this._dropMSAA();
624
- }
625
-
626
- super.setPixels(rect, buffer);
627
- }
628
-
629
- private _clearFromDispose = false;
630
- public clear() {
631
- // we call clear in parent class from dispose, call direct
632
- if (this._clearFromDispose) {
633
- super.clear();
634
- return;
635
- }
636
-
637
- // clear should drop abstraction, but we can't doing this if unloaded
638
- const t = this.syncData(true);
639
-
640
- // not require unload, we already doings this
641
- this.unmarkToUnload();
642
- this.lastUsedTime = -1;
643
-
644
- if (typeof t === 'boolean') {
645
- this.wasUpload = false;
646
- super.clear();
647
- return;
648
- }
649
-
650
- t.then(() => {
651
- this.wasUpload = false;
652
- super.clear();
653
- });
654
- }
655
-
656
- /**
657
- * @inheritdoc
658
- */
659
- public getPixel32(x: number, y: number): number {
660
- this.syncData();
661
-
662
- return super.getPixel32(x, y);
663
- }
664
-
665
- /**
666
- * @inheritdoc
667
- */
668
- public getPixel(x: number, y: number): number {
669
- const result = this.getPixel32(x, y);
670
-
671
- return result & 0x00ffffff;
672
- }
673
-
674
- /**
675
- * Draws the <code>source</code> display object onto the bitmap image, using
676
- * the NME software renderer. You can specify <code>matrix</code>,
677
- * <code>colorTransform</code>, <code>blendMode</code>, and a destination
678
- * <code>clipRect</code> parameter to control how the rendering performs.
679
- * Optionally, you can specify whether the bitmap should be smoothed when
680
- * scaled(this works only if the source object is a BitmapImage2D object).
681
- *
682
- * <p>The source display object does not use any of its applied
683
- * transformations for this call. It is treated as it exists in the library
684
- * or file, with no matrix transform, no color transform, and no blend mode.
685
- * To draw a display object(such as a movie clip) by using its own transform
686
- * properties, you can copy its <code>transform</code> property object to the
687
- * <code>transform</code> property of the Bitmap object that uses the
688
- * BitmapImage2D object.</p>
689
- *
690
- * @param source The display object or BitmapImage2D object to draw to
691
- * the BitmapImage2D object.(The DisplayObject and
692
- * BitmapImage2D classes implement the IBitmapDrawable
693
- * interface.)
694
- * @param matrix A Matrix object used to scale, rotate, or translate
695
- * the coordinates of the bitmap. If you do not want to
696
- * apply a matrix transformation to the image, set this
697
- * parameter to an identity matrix, created with the
698
- * default <code>new Matrix()</code> constructor, or
699
- * pass a <code>null</code> value.
700
- * @param colorTransform A ColorTransform object that you use to adjust the
701
- * color values of the bitmap. If no object is
702
- * supplied, the bitmap image's colors are not
703
- * transformed. If you must pass this parameter but you
704
- * do not want to transform the image, set this
705
- * parameter to a ColorTransform object created with
706
- * the default <code>new ColorTransform()</code>
707
- * constructor.
708
- * @param blendMode A string value, from the flash.display.BlendMode
709
- * class, specifying the blend mode to be applied to
710
- * the resulting bitmap.
711
- * @param clipRect A Rectangle object that defines the area of the
712
- * source object to draw. If you do not supply this
713
- * value, no clipping occurs and the entire source
714
- * object is drawn.
715
- * @param smoothing A Boolean value that determines whether a BitmapImage2D
716
- * object is smoothed when scaled or rotated, due to a
717
- * scaling or rotation in the <code>matrix</code>
718
- * parameter. The <code>smoothing</code> parameter only
719
- * applies if the <code>source</code> parameter is a
720
- * BitmapImage2D object. With <code>smoothing</code> set
721
- * to <code>false</code>, the rotated or scaled
722
- * BitmapImage2D image can appear pixelated or jagged. For
723
- * example, the following two images use the same
724
- * BitmapImage2D object for the <code>source</code>
725
- * parameter, but the <code>smoothing</code> parameter
726
- * is set to <code>true</code> on the left and
727
- * <code>false</code> on the right:
728
- *
729
- * <p>Drawing a bitmap with <code>smoothing</code> set
730
- * to <code>true</code> takes longer than doing so with
731
- * <code>smoothing</code> set to
732
- * <code>false</code>.</p>
733
- * @throws ArgumentError The <code>source</code> parameter is not a
734
- * BitmapImage2D or DisplayObject object.
735
- * @throws ArgumentError The source is null or not a valid IBitmapDrawable
736
- * object.
737
- * @throws SecurityError The <code>source</code> object and(in the case of a
738
- * Sprite or MovieClip object) all of its child objects
739
- * do not come from the same domain as the caller, or
740
- * are not in a content that is accessible to the
741
- * caller by having called the
742
- * <code>Security.allowDomain()</code> method. This
743
- * restriction does not apply to AIR content in the
744
- * application security sandbox.
745
- */
746
-
747
- /* eslint-disable */
748
- public draw(source: DisplayObject, matrix?: Matrix, colorTransform?: ColorTransform, blendMode?: string, clipRect?: Rectangle, smoothing?: boolean);
749
- public draw(source: BitmapImage2D, matrix?: Matrix, colorTransform?: ColorTransform, blendMode?: string, clipRect?: Rectangle, smoothing?: boolean);
750
- public draw(source: any, matrix?: Matrix, colorTransform?: ColorTransform, blendMode?: string, clipRect?: Rectangle, smoothing?: boolean): void
751
- {
752
- /* eslint-enable */
753
-
754
- this.dropAllReferences();
755
- this.unmarkToUnload();
756
-
757
- if (source instanceof DisplayObject) {
758
- this._drawAsDisplay(source, matrix, colorTransform, blendMode, clipRect, smoothing);
759
- } else {
760
- this._drawAsBitmap(source, matrix, colorTransform, blendMode, clipRect, smoothing);
761
- }
762
-
763
- this._lastUsedFill = null;
764
- this._imageDataDirty = true;
765
- this.invalidate();
766
- }
767
-
768
- private static _mapSupportedBlendMode(blendMode: string = ''): string {
769
- switch (blendMode) {
770
- case null:
771
- case '':
772
- case BlendMode.NORMAL:
773
- case BlendMode.LAYER:
774
- return BlendMode.LAYER;
775
- case BlendMode.MULTIPLY:
776
- case BlendMode.ADD:
777
- case BlendMode.ALPHA:
778
- return blendMode;
779
- }
780
-
781
- //console.debug("[ImageBitmap] Unsupport BlendMode", blendMode);
782
- return BlendMode.LAYER;
783
- }
784
-
785
- private _drawAsBitmap(
786
- source: BitmapImage2D, matrix?: Matrix, colorTransform?: ColorTransform,
787
- blendMode?: string, _clipRect?: Rectangle, smoothing?: boolean
788
- ) {
789
-
790
- if (!SceneImage2D._billboardRenderer) {
791
- this.createBillboardRenderer();
792
- }
793
-
794
- if (this._initalFillColor !== null) {
795
- this.fillRect(this._rect, this._initalFillColor);
796
- this._initalFillColor = null;
797
- }
798
-
799
- const stage = this._stage;
800
- const mappedBlend = SceneImage2D._mapSupportedBlendMode(blendMode);
801
- const supportNativeBlend = !blendMode || mappedBlend !== BlendMode.LAYER || blendMode == BlendMode.LAYER;
802
- const useTmp = (!supportNativeBlend || this === source);
803
- const target = useTmp ?
804
- stage.filterManager.popTemp(this.width, this.height, false)
805
- : this;
806
-
807
- const renderer = SceneImage2D._billboardRenderer;
808
- const root = SceneImage2D._billboardRoot;
809
- const billboard = SceneImage2D._billboard;
810
-
811
- billboard.sampler.smooth = smoothing;
812
-
813
- renderer.disableClear = !useTmp;
814
- renderer.view.target = target;
815
- renderer.view.projection.scale = 1000 / target.height;
816
-
817
- billboard.material.style.image = source;
818
-
819
- // not all blend modes can be used for rendering
820
- billboard.material.blendMode = !useTmp ? SceneImage2D._mapSupportedBlendMode(blendMode) : BlendMode.LAYER;
821
-
822
- (<MaterialBase> billboard.material).useColorTransform = !!colorTransform;
823
-
824
- if (matrix) {
825
- const m = root.transform.matrix3D;
826
-
827
- m.identity();
828
-
829
- m._rawData[0] = matrix.a;
830
- m._rawData[1] = -matrix.b;
831
- m._rawData[4] = matrix.c;
832
- m._rawData[5] = -matrix.d;
833
- m._rawData[12] = matrix.tx;
834
- m._rawData[13] = target.height - matrix.ty;
835
-
836
- root.transform.invalidateComponents();
837
- } else {
838
-
839
- root.transform.rotateTo(0,0,0);
840
- root.transform.scaleTo(1, -1, 1);
841
- root.transform.moveTo(0, target.height,0);
842
- }
843
-
844
- root.transform.colorTransform = colorTransform;
845
-
846
- //render
847
- renderer.render();
848
-
849
- //
850
- if (useTmp) {
851
- stage.filterManager.copyPixels(
852
- target,
853
- this,
854
- this.rect,
855
- TMP_POINT,
856
- true,
857
- blendMode
858
- );
859
-
860
- stage.filterManager.pushTemp(target);
861
- }
862
-
863
- }
864
-
865
- private _drawAsDisplay(
866
- source: DisplayObject, matrix?: Matrix, colorTransform?: ColorTransform,
867
- blendMode: string = '', _clipRect?: Rectangle, _smoothing?: boolean
868
- ) {
869
- // default global blend mode
870
- blendMode = blendMode || BlendMode.LAYER;
871
-
872
- if (!SceneImage2D._renderer) {
873
- this.createRenderer();
874
- }
875
-
876
- const root = SceneImage2D._root;
877
- const rootNode = SceneImage2D._rootNode;
878
- const renderer = SceneImage2D._renderer;
879
-
880
- // when we should use MSAA, we will create temporary image and draw to it
881
- const internal = this.canUseMSAAInternaly;
882
- const nativeMSAA = (
883
- !source.isAsset(Billboard) &&
884
- this._stage.context.glVersion === 2 && // can be used because a webgl2
885
- Settings.ALLOW_FORCE_MSAA > 1 && // because a quality is more that 1
886
- !internal); // and not internal
887
-
888
- // target image for rendering.
889
- let target: Image2D = this;
890
-
891
- // we should run compositor when blendMode !== LAYER (default)
892
- // or when we use MSAA + fill is not flat.
893
- const useBlend = blendMode !== BlendMode.LAYER || this._lastUsedFill === null;
894
-
895
- const useTemp = useBlend || nativeMSAA;
896
-
897
- // lazy filling
898
- // we require fill image with initial color, because we not doing this immediate
899
- // and when blend is required, because we should blend with vald color
900
- if ((!nativeMSAA || useBlend) && this._initalFillColor !== null) {
901
- this.fillRect(this._rect, this._initalFillColor);
902
- this._initalFillColor = null;
903
- }
904
-
905
- if (useTemp) {
906
- target = SceneImage2D.getTemp(this.width, this.height, this._stage, nativeMSAA);
907
-
908
- // because target image is stupid filled - it not require merging
909
- // BUT when blend is required - we shpuld skip color clear and use empty TMP
910
- if (!useBlend && this._lastUsedFill !== null) {
911
- // bitmap was filled plain, go clear TMP to this color too
912
- renderer.disableClear = false;
913
- renderer.view.backgroundColor = this._lastUsedFill & 0xffffff;
914
- renderer.view.backgroundAlpha = this._transparent ? (this._lastUsedFill >>> 24 & 0xff) / 0xff : 1;
915
- } else {
916
- // we clear TMP and render to it, prepare to composing
917
- renderer.disableClear = false;
918
- renderer.view.backgroundColor = 0x0;
919
- renderer.view.backgroundAlpha = 0;
920
-
921
- // copy from source to tmp
922
- // TMP_POINT.setTo(0,0);
923
- // this._stage.copyPixels(this, target, this._rect, TMP_POINT, null, null, false);
924
- }
925
- }
926
-
927
- const transform = renderer.view.projection.transform;
928
- const mat3d = transform.matrix3D;
929
- mat3d.identity();
930
-
931
- if (matrix) {
932
- const raw = mat3d._rawData;
933
- raw[0] = matrix.a;
934
- raw[1] = matrix.b;
935
- raw[4] = matrix.c;
936
- raw[5] = matrix.d;
937
- raw[10] = 1;
938
- raw[12] = matrix.tx;
939
- raw[13] = matrix.ty;
940
- }
941
-
942
- // todo By this line we flip normals, and cull will be broken
943
- // NEED FIX THIS ASAP, or flip cull state
944
- mat3d.appendScale(1, -1, 1);
945
- mat3d.appendTranslation(0, this._rect.height, 1000);
946
-
947
- mat3d.invert();
948
- transform.matrix3D = mat3d;
949
-
950
- renderer.antiAlias = (internal ? this.antialiasQuality : (<any>target).antialiasQuality) || 0;
951
-
952
- renderer.view.target = target;
953
- renderer.view.projection.scale = 1000 / this._rect.height;
954
-
955
- // shift view, because target can be more
956
- renderer.view.projection.ratio = (this._rect.width / this._rect.height);
957
- renderer.view.x = -(target.width - this.width);
958
- renderer.view.y = -(target.height - this.height);
959
- renderer.view.width = this.width;
960
- renderer.view.height = this.height;
961
-
962
- const sourceNode: ContainerNode = rootNode.addChildAt(source, 0);
963
- const transformDisabled = sourceNode.transformDisabled;
964
-
965
- sourceNode.transformDisabled = true;
966
- // color transform should be enabled!
967
- sourceNode.colorTransformDisabled = false;
968
-
969
- root.transform.colorTransform = colorTransform;
970
- // anyway we not support this =))
971
- root.blendMode = SceneImage2D._mapSupportedBlendMode(blendMode);
972
-
973
- //save snapshot if unlocked
974
- //if (!this._locked)
975
- //SceneImage2D.scene.view.target=this;
976
- //SceneImage2D.scene.renderer.disableClear = !this._locked;
977
-
978
- renderer.render();
979
-
980
- // reset render to default value
981
- renderer.antiAlias = 0;
982
- renderer.disableClear = true;
983
-
984
- rootNode.removeChildAt(0);
985
-
986
- sourceNode.transformDisabled = transformDisabled;
987
-
988
- if (useTemp) {
989
- // because we copy MSAA into no msaa, it should passed as BLIT
990
- this._stage.filterManager.copyPixels(
991
- target,
992
- this,
993
- this._rect,
994
- TMP_POINT,
995
- useBlend,
996
- blendMode // apply blend mode if this needed.
997
- );
998
-
999
- SceneImage2D.tryStoreImage(target, this._stage);
1000
- }
1001
- }
1002
-
1003
- public reset() {}
1004
-
1005
- public clone(): SceneImage2D {
1006
- const image = SceneImage2D.getImage(
1007
- this.width,
1008
- this.height,
1009
- this.transparent,
1010
- null,
1011
- false,
1012
- this._stage,
1013
- false
1014
- );
1015
-
1016
- image.deepClone(this);
1017
- return image;
1018
- }
1019
- }
1020
-
1
+
2
+ import { ContainerNode, View } from '@awayjs/view';
3
+ import { MaterialBase, MethodMaterial } from '@awayjs/materials';
4
+ import { DisplayObjectContainer } from '../display/DisplayObjectContainer';
5
+ import { DisplayObject } from '../display/DisplayObject';
6
+ import { Billboard } from '../display/Billboard';
7
+ import { Settings } from '../Settings';
8
+
9
+ import {
10
+ ColorTransform,
11
+ Matrix,
12
+ Rectangle,
13
+ Point,
14
+ PerspectiveProjection,
15
+ CoordinateSystem,
16
+ Vector3D,
17
+ Transform,
18
+ ColorUtils
19
+ } from '@awayjs/core';
20
+
21
+ import { Stage,
22
+ BitmapImage2D,
23
+ _Stage_BitmapImage2D,
24
+ BlendMode,
25
+ ContextWebGL,
26
+ Image2D
27
+ } from '@awayjs/stage';
28
+
29
+ import { DefaultRenderer, RenderGroup, Style } from '@awayjs/renderer';
30
+
31
+ // empty matrix for transform reset
32
+ const TMP_POINT = new Point(0,0);
33
+
34
+ /**
35
+ *
36
+ */
37
+ export class SceneImage2D extends BitmapImage2D {
38
+ public static assetType: string = '[image SceneImage2D]';
39
+
40
+ public static getImage(
41
+ width: number, height: number, transparent: boolean = true,
42
+ fillColor: number = 0xffffffff,
43
+ powerOfTwo: boolean = true, stage: Stage = null, msaa = false
44
+ ) {
45
+ const result = new SceneImage2D(
46
+ width,
47
+ height,
48
+ transparent,
49
+ fillColor,
50
+ powerOfTwo,
51
+ stage
52
+ );
53
+
54
+ if (!msaa) {
55
+ result._msaaNeedDrop = true;
56
+ result._antialiasQuality = 0;
57
+ } else {
58
+ result._enforceMSAASupport = true;
59
+ }
60
+
61
+ return result;
62
+ }
63
+
64
+ public static tryStoreImage(image: Image2D, stage: Stage): void {
65
+ stage.filterManager.pushTemp(image);
66
+ }
67
+
68
+ private static getTemp(width: number, height: number, stage: Stage, msaa = false) {
69
+ const image = stage.filterManager.popTemp(width, height, msaa && Settings.ALLOW_FORCE_MSAA > 1);
70
+
71
+ image.antialiasQuality = msaa ? Settings.ALLOW_FORCE_MSAA : 0;
72
+ return image;
73
+ }
74
+
75
+ private static _renderer: DefaultRenderer;
76
+ private static _billboardRenderer: DefaultRenderer;
77
+ private static _view: View;
78
+ private static _billboardView: View;
79
+ private static _root: DisplayObjectContainer;
80
+ private static _rootNode: ContainerNode;
81
+ private static _billboardRoot: DisplayObjectContainer;
82
+ private static _billboard: Billboard;
83
+
84
+ private _imageDataDirty: boolean;
85
+
86
+ private _asyncRead: Promise<boolean>;
87
+
88
+ private _initalFillColor: number = null;
89
+
90
+ private _lastUsedFill: number = null;
91
+
92
+ private _internalSync: boolean = false;
93
+
94
+ protected _msaaNeedDrop: boolean = false;
95
+
96
+ protected _enforceMSAASupport: boolean = false;
97
+
98
+ protected _dropMSAA() {
99
+ if (this._msaaNeedDrop || !this.canUseMSAAInternaly) {
100
+ return;
101
+ }
102
+
103
+ this._enforceMSAASupport = false;
104
+ this._msaaNeedDrop = true;
105
+
106
+ // force dispose texture and buffers
107
+ // MSAA not stored in pool
108
+ if (this.wasUpload) {
109
+ // super unload not call sync, we call it
110
+ super.unload();
111
+ }
112
+
113
+ console.debug(
114
+ '[SceneImage2D Experemental] Drop MSAA support because a setPixel* operation called after upload.',
115
+ this.id
116
+ );
117
+ }
118
+
119
+ public get canUseMSAAInternaly () {
120
+ if (this._enforceMSAASupport) {
121
+ return true;
122
+ }
123
+
124
+ let minW = Settings.MSAA_MINIMAL_IMAGE_SIZE;
125
+ let minH = Settings.MSAA_MINIMAL_IMAGE_SIZE;
126
+
127
+ if (this._stage) {
128
+ minH = Math.min(this._stage.height, minH);
129
+ minW = Math.min(this._stage.width, minW);
130
+ }
131
+
132
+ return (
133
+ this.width >= minW &&
134
+ this.height >= minH &&
135
+ !this._msaaNeedDrop
136
+ );
137
+ }
138
+
139
+ /*private*/ _antialiasQuality: number = Settings.ALLOW_FORCE_MSAA;
140
+ public get antialiasQuality() {
141
+ return this.canUseMSAAInternaly ? this._antialiasQuality : 0;
142
+ }
143
+
144
+ protected syncData(async = false): boolean | Promise<boolean> {
145
+
146
+ if (async && this._asyncRead) {
147
+ return this._asyncRead;
148
+ }
149
+
150
+ if (!async && this._asyncRead) {
151
+ throw '[SceneImage2D] Synced read not allowed while async read is requested!';
152
+ }
153
+
154
+ this.applySymbol();
155
+
156
+ // update data from pixels from GPU
157
+ if (!this._imageDataDirty) {
158
+ return async ? Promise.resolve(false) : false;
159
+ }
160
+
161
+ const context = <ContextWebGL> this._stage.context;
162
+
163
+ this._stage.setRenderTarget(this, false);
164
+
165
+ // when we call syncData, we already loose other data
166
+ // not require apply symbol etc, because it already should be applied
167
+ if (!this._data) {
168
+ this._data = new Uint8ClampedArray(this.width * this.height * 4);
169
+ }
170
+
171
+ // mark that this internal call, avoid reqursion loop
172
+ this._internalSync = true;
173
+ this._asyncRead = context.drawToBitmapImage2D(this, false, async);
174
+ this._internalSync = false;
175
+
176
+ this._stage.setRenderTarget(null);
177
+
178
+ if (!async) {
179
+ this._imageDataDirty = false;
180
+ // we store pixel buffer already as PMA.
181
+ // we should prevent unpack what already is PMA
182
+ this._unpackPMA = false;
183
+ return true;
184
+ }
185
+
186
+ return this._asyncRead.then((_status: boolean) => {
187
+ this._imageDataDirty = false;
188
+ this._unpackPMA = false;
189
+ this._asyncRead = null;
190
+
191
+ return true;
192
+ });
193
+ }
194
+
195
+ /* overide internal */getDataInternal (constructEmpty = true, skipSync = false) {
196
+
197
+ // if sync called, check that this is requried
198
+ if (!skipSync && this._imageDataDirty && !this._internalSync) {
199
+ // sync data already should fill _data
200
+ this.syncData(false);
201
+
202
+ return this._data;
203
+ }
204
+
205
+ if (this._initalFillColor === null) {
206
+ return super.getDataInternal(constructEmpty, true);
207
+ }
208
+
209
+ // disable empty buffer filling
210
+ // and check that buffer is empyt (has now symbol or alpha)
211
+ const data = super.getDataInternal(false, true);
212
+
213
+ // if it empty, fill with initlal value
214
+ if (!data) {
215
+
216
+ // fill rect constuct buffer inside
217
+ super.fillRect(this.rect, this._initalFillColor);
218
+ this._initalFillColor = null;
219
+
220
+ return this._data;
221
+ }
222
+
223
+ return data;
224
+ }
225
+
226
+ /**
227
+ *
228
+ * @returns {string}
229
+ */
230
+ public get assetType(): string {
231
+ return SceneImage2D.assetType;
232
+ }
233
+
234
+ /**
235
+ * Creates a BitmapImage2D object with a specified width and height. If you
236
+ * specify a value for the <code>fillColor</code> parameter, every pixel in
237
+ * the bitmap is set to that color.
238
+ *
239
+ * <p>By default, the bitmap is created as transparent, unless you pass
240
+ * the value <code>false</code> for the transparent parameter. After you
241
+ * create an opaque bitmap, you cannot change it to a transparent bitmap.
242
+ * Every pixel in an opaque bitmap uses only 24 bits of color channel
243
+ * information. If you define the bitmap as transparent, every pixel uses 32
244
+ * bits of color channel information, including an alpha transparency
245
+ * channel.</p>
246
+ *
247
+ * @param width The width of the bitmap image in pixels.
248
+ * @param height The height of the bitmap image in pixels.
249
+ * @param transparent Specifies whether the bitmap image supports per-pixel
250
+ * transparency. The default value is <code>true</code>
251
+ * (transparent). To create a fully transparent bitmap,
252
+ * set the value of the <code>transparent</code>
253
+ * parameter to <code>true</code> and the value of the
254
+ * <code>fillColor</code> parameter to 0x00000000(or to
255
+ * 0). Setting the <code>transparent</code> property to
256
+ * <code>false</code> can result in minor improvements
257
+ * in rendering performance.
258
+ * @param fillColor A 32-bit ARGB color value that you use to fill the
259
+ * bitmap image area. The default value is
260
+ * 0xFFFFFFFF(solid white).
261
+ */
262
+ constructor(
263
+ width: number, height: number, transparent: boolean = true,
264
+ fillColor: number = 0xffffffff, powerOfTwo: boolean = true, stage: Stage = null) {
265
+
266
+ super(width, height, transparent, null, powerOfTwo, stage);
267
+
268
+ this._initalFillColor = fillColor;
269
+ this._lastUsedFill = fillColor;
270
+ }
271
+
272
+ private createRenderer() {
273
+ //create the projection
274
+ const projection = new PerspectiveProjection();
275
+ projection.coordinateSystem = CoordinateSystem.RIGHT_HANDED;
276
+ projection.originX = -1;
277
+ projection.originY = 1;
278
+ projection.transform = new Transform();
279
+ projection.transform.scaleTo(1, -1, 1);
280
+ projection.transform.moveTo(0, 0, -1000);
281
+
282
+ //create the view
283
+ SceneImage2D._view = new View(projection, this._stage);
284
+ SceneImage2D._root = new DisplayObjectContainer();
285
+ SceneImage2D._rootNode = SceneImage2D._view.getNode(SceneImage2D._root);
286
+ SceneImage2D._renderer = <DefaultRenderer> RenderGroup
287
+ .getInstance(DefaultRenderer)
288
+ .getRenderer(SceneImage2D._rootNode.partition);
289
+
290
+ //set the view properties
291
+ SceneImage2D._view.backgroundAlpha = 0;
292
+ SceneImage2D._view.backgroundColor = 0x0;
293
+
294
+ //set the renderer properties
295
+ SceneImage2D._renderer.disableClear = true;
296
+ SceneImage2D._renderer.renderableSorter = null;//new RenderableSort2D();
297
+ //SceneImage2D._renderer.antiAlias = Settings.ALLOW_FORCE_MSAA;
298
+ }
299
+
300
+ private createBillboardRenderer(): void {
301
+ //create the projection
302
+ const projection = new PerspectiveProjection();
303
+ projection.coordinateSystem = CoordinateSystem.RIGHT_HANDED;
304
+ projection.originX = -1;
305
+ projection.originY = 1;
306
+ projection.transform = new Transform();
307
+ projection.transform.moveTo(0, 0, -1000);
308
+ projection.transform.lookAt(new Vector3D());
309
+
310
+ //create the view
311
+ SceneImage2D._billboardView = new View(projection, this._stage);
312
+ SceneImage2D._billboardRoot = new DisplayObjectContainer();
313
+ SceneImage2D._billboardRenderer = <DefaultRenderer> RenderGroup
314
+ .getInstance(DefaultRenderer)
315
+ .getRenderer(SceneImage2D._billboardView.getNode(SceneImage2D._billboardRoot).partition);
316
+
317
+ //SceneImage2D._billboardRoot.partition = SceneImage2D._billboardRenderer.partition;
318
+
319
+ //SceneImage2D._renderer.antiAlias = Settings.ALLOW_FORCE_MSAA;
320
+ //set the view properties
321
+ SceneImage2D._billboardView.backgroundAlpha = 0;
322
+ SceneImage2D._billboardView.backgroundColor = 0x0;
323
+
324
+ //set the renderer properties
325
+ SceneImage2D._billboardRenderer.disableClear = true;
326
+ SceneImage2D._billboardRenderer.renderableSorter = null;//new RenderableSort2D();
327
+
328
+ const mat: MethodMaterial = new MethodMaterial(new BitmapImage2D(128, 128, true, 0x0));
329
+ mat.bothSides = true;
330
+ mat.alphaBlending = true;
331
+
332
+ SceneImage2D._billboard = new Billboard(mat);
333
+ SceneImage2D._billboard.style = new Style();
334
+
335
+ SceneImage2D._billboardRoot.addChild(SceneImage2D._billboard);
336
+ }
337
+
338
+ /**
339
+ * Frees memory that is used to store the BitmapImage2D object.
340
+ *
341
+ * <p>When the <code>dispose()</code> method is called on an image, the width
342
+ * and height of the image are set to 0. All subsequent calls to methods or
343
+ * properties of this BitmapImage2D instance fail, and an exception is thrown.
344
+ * </p>
345
+ *
346
+ * <p><code>BitmapImage2D.dispose()</code> releases the memory occupied by the
347
+ * actual bitmap data, immediately(a bitmap can consume up to 64 MB of
348
+ * memory). After using <code>BitmapImage2D.dispose()</code>, the BitmapImage2D
349
+ * object is no longer usable and an exception may be thrown if
350
+ * you call functions on the BitmapImage2D object. However,
351
+ * <code>BitmapImage2D.dispose()</code> does not garbage collect the BitmapImage2D
352
+ * object(approximately 128 bytes); the memory occupied by the actual
353
+ * BitmapImage2D object is released at the time the BitmapImage2D object is
354
+ * collected by the garbage collector.</p>
355
+ *
356
+ */
357
+ public dispose(): void {
358
+ this._clearFromDispose = true;
359
+
360
+ this.dropAllReferences();
361
+ this.unmarkToUnload();
362
+ this.unuseWeakRef();
363
+
364
+ // drop buffer, because is big
365
+ this._data = null;
366
+ this._locked = false;
367
+ super.dispose();
368
+
369
+ this._clearFromDispose = false;
370
+ }
371
+
372
+ public unload() {
373
+ // query async unload
374
+ const t = this.syncData(true);
375
+
376
+ // strict quard
377
+ if (typeof t !== 'boolean') {
378
+ t.then(() => super.unload());
379
+ return;
380
+ }
381
+
382
+ super.unload();
383
+ }
384
+
385
+ protected deepClone(from: BitmapImage2D) {
386
+ this.copyPixels(from, this._rect, new Point(0,0));
387
+ }
388
+
389
+ /**
390
+ * Fills a rectangular area of pixels with a specified ARGB color.
391
+ *
392
+ * @param rect The rectangular area to fill.
393
+ * @param color The ARGB color value that fills the area. ARGB colors are
394
+ * often specified in hexadecimal format; for example,
395
+ * 0xFF336699.
396
+ * @throws TypeError The rect is null.
397
+ */
398
+ public fillRect(rect: Rectangle, color: number): void {
399
+ this.dropAllReferences();
400
+
401
+ const argb = ColorUtils.float32ColorToARGB(color);
402
+ const alpha = this._transparent ? argb[0] / 255 : 1;
403
+ const isCrop = rect !== this._rect && !this._rect.equals(rect);
404
+
405
+ this._stage.setRenderTarget(this, true, 0, 0, true);
406
+ this._stage.setScissor(rect);
407
+
408
+ // we shure that color is fully filled when there are not any crops
409
+ this._lastUsedFill = isCrop ? null : color;
410
+
411
+ this._stage.clear(
412
+ (argb[1] / 0xff) * alpha,
413
+ (argb[2] / 0xff) * alpha,
414
+ (argb[3] / 0xff) * alpha,
415
+ alpha
416
+ );
417
+
418
+ this._stage.setScissor(null);
419
+ this._imageDataDirty = true;
420
+ }
421
+
422
+ /**
423
+ * Provides a fast routine to perform pixel manipulation between images with
424
+ * no stretching, rotation, or color effects. This method copies a
425
+ * rectangular area of a source image to a rectangular area of the same size
426
+ * at the destination point of the destination BitmapImage2D object.
427
+ *
428
+ * <p>If you include the <code>alphaBitmap</code> and <code>alphaPoint</code>
429
+ * parameters, you can use a secondary image as an alpha source for the
430
+ * source image. If the source image has alpha data, both sets of alpha data
431
+ * are used to composite pixels from the source image to the destination
432
+ * image. The <code>alphaPoint</code> parameter is the point in the alpha
433
+ * image that corresponds to the upper-left corner of the source rectangle.
434
+ * Any pixels outside the intersection of the source image and alpha image
435
+ * are not copied to the destination image.</p>
436
+ *
437
+ * <p>The <code>mergeAlpha</code> property controls whether or not the alpha
438
+ * channel is used when a transparent image is copied onto another
439
+ * transparent image. To copy pixels with the alpha channel data, set the
440
+ * <code>mergeAlpha</code> property to <code>true</code>. By default, the
441
+ * <code>mergeAlpha</code> property is <code>false</code>.</p>
442
+ *
443
+ * @param source The input bitmap image from which to copy pixels.
444
+ * The source image can be a different BitmapImage2D
445
+ * instance, or it can refer to the current
446
+ * BitmapImage2D instance.
447
+ * @param sourceRect A rectangle that defines the area of the source
448
+ * image to use as input.
449
+ * @param destPoint The destination point that represents the
450
+ * upper-left corner of the rectangular area where
451
+ * the new pixels are placed.
452
+ * @param alphaBitmapData A secondary, alpha BitmapImage2D object source.
453
+ * @param alphaPoint The point in the alpha BitmapImage2D object source
454
+ * that corresponds to the upper-left corner of the
455
+ * <code>sourceRect</code> parameter.
456
+ * @param mergeAlpha To use the alpha channel, set the value to
457
+ * <code>true</code>. To copy pixels with no alpha
458
+ * channel, set the value to <code>false</code>.
459
+ * @throws TypeError The sourceBitmapImage2D, sourceRect, destPoint are null.
460
+ */
461
+ public copyPixels(
462
+ source: BitmapImage2D, sourceRect: Rectangle, destPoint: Point,
463
+ alphaBitmapData?: BitmapImage2D, alphaPoint?: Point, mergeAlpha?: boolean): void {
464
+
465
+ this._lastUsedFill = null;
466
+ this.dropAllReferences();
467
+ this.unmarkToUnload();
468
+
469
+ // need drop alpha from source when target is not has alpha
470
+ mergeAlpha = this.transparent !== source.transparent || mergeAlpha;
471
+
472
+ // CPU based copy, not require run GPU based copy
473
+ // block is equal, one of image not uploaded yet
474
+ // and source image no require SYNC
475
+ if (!(<SceneImage2D>source)._imageDataDirty &&
476
+ sourceRect.equals(this._rect) &&
477
+ this._rect.equals(source.rect) &&
478
+ !mergeAlpha && (!this.wasUpload || !source.wasUpload)
479
+ ) {
480
+ const data = source.getDataInternal(true);
481
+
482
+ // inline, instead of setPixels, because it use a lot of checks
483
+ if (this._data) {
484
+ this._data.set(data);
485
+ } else {
486
+ this._data = data.slice();
487
+ }
488
+
489
+ // we should sync this, because we can apply PMA twice, that not needed for us
490
+ this._unpackPMA = (<any> source)._unpackPMA;
491
+ // we should reset initial color, because not require fill it after copyPixel
492
+ this._initalFillColor = null;
493
+ this._imageDataDirty = false;
494
+ this.invalidateGPU();
495
+ return;
496
+ }
497
+
498
+ if (source.width * source.height <= Settings.CPU_COPY_PIXELS_COUNT && !mergeAlpha) {
499
+ // todo Not implemented yet, need to implement and check performance change
500
+ // i think that increase some small operation performance,
501
+ // because copy by GPU required upload array to VRAM,
502
+ // and games that use set/get pixels and copyPixels only for math process not required use GPU copy
503
+ }
504
+
505
+ if (this._initalFillColor !== null) {
506
+ this.fillRect(this._rect, this._initalFillColor);
507
+ this._initalFillColor = null;
508
+ }
509
+
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
+
551
+ this._imageDataDirty = true;
552
+ }
553
+
554
+ public threshold(
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 {
564
+
565
+ this._lastUsedFill = null;
566
+ this.dropAllReferences();
567
+ this.unmarkToUnload();
568
+
569
+ this._stage.threshold(source, this, sourceRect, destPoint, operation, threshold, color, mask, copySource);
570
+ this._imageDataDirty = true;
571
+ }
572
+
573
+ public applyFilter (source: BitmapImage2D, sourceRect: Rectangle, destPoint: Point, filter: any): boolean {
574
+ if (!Settings.USE_UNSAFE_FILTERS || !filter || !filter.filterName) {
575
+ return false;
576
+ }
577
+
578
+ this.dropAllReferences(false);
579
+
580
+ const result = this._stage.filterManager.applyFilter (
581
+ source,
582
+ this,
583
+ sourceRect,
584
+ destPoint,
585
+ filter.filterName, filter);
586
+
587
+ this._imageDataDirty = result;
588
+ return result;
589
+ }
590
+
591
+ public colorTransform(rect: Rectangle, colorTransform: ColorTransform): void {
592
+ this.dropAllReferences();
593
+ this.unmarkToUnload();
594
+
595
+ this._lastUsedFill = null;
596
+
597
+ this._stage.colorTransform(this, this, rect, colorTransform);
598
+
599
+ this._imageDataDirty = true;
600
+ }
601
+
602
+ public setPixel(x: number, y: number, color: number) {
603
+ // we can't upload buffer in MSAA texture after creating - only render it and get
604
+ if (this.canUseMSAAInternaly) {
605
+ this._dropMSAA();
606
+ }
607
+
608
+ super.setPixel(x, y, color);
609
+ }
610
+
611
+ public setPixel32(x: number, y: number, color: number) {
612
+ // we can't upload buffer in MSAA texture after creating - only render it and get
613
+ if (this.canUseMSAAInternaly) {
614
+ this._dropMSAA();
615
+ }
616
+
617
+ super.setPixel32(x, y, color);
618
+ }
619
+
620
+ public setPixels(rect: Rectangle, buffer: Uint8ClampedArray) {
621
+ // we can't upload buffer in MSAA texture after creating - only render it and get
622
+ if (this.wasUpload && this.canUseMSAAInternaly) {
623
+ this._dropMSAA();
624
+ }
625
+
626
+ super.setPixels(rect, buffer);
627
+ }
628
+
629
+ private _clearFromDispose = false;
630
+ public clear() {
631
+ // we call clear in parent class from dispose, call direct
632
+ if (this._clearFromDispose) {
633
+ super.clear();
634
+ return;
635
+ }
636
+
637
+ // clear should drop abstraction, but we can't doing this if unloaded
638
+ const t = this.syncData(true);
639
+
640
+ // not require unload, we already doings this
641
+ this.unmarkToUnload();
642
+ this.lastUsedTime = -1;
643
+
644
+ if (typeof t === 'boolean') {
645
+ this.wasUpload = false;
646
+ super.clear();
647
+ return;
648
+ }
649
+
650
+ t.then(() => {
651
+ this.wasUpload = false;
652
+ super.clear();
653
+ });
654
+ }
655
+
656
+ /**
657
+ * @inheritdoc
658
+ */
659
+ public getPixel32(x: number, y: number): number {
660
+ this.syncData();
661
+
662
+ return super.getPixel32(x, y);
663
+ }
664
+
665
+ /**
666
+ * @inheritdoc
667
+ */
668
+ public getPixel(x: number, y: number): number {
669
+ const result = this.getPixel32(x, y);
670
+
671
+ return result & 0x00ffffff;
672
+ }
673
+
674
+ /**
675
+ * Draws the <code>source</code> display object onto the bitmap image, using
676
+ * the NME software renderer. You can specify <code>matrix</code>,
677
+ * <code>colorTransform</code>, <code>blendMode</code>, and a destination
678
+ * <code>clipRect</code> parameter to control how the rendering performs.
679
+ * Optionally, you can specify whether the bitmap should be smoothed when
680
+ * scaled(this works only if the source object is a BitmapImage2D object).
681
+ *
682
+ * <p>The source display object does not use any of its applied
683
+ * transformations for this call. It is treated as it exists in the library
684
+ * or file, with no matrix transform, no color transform, and no blend mode.
685
+ * To draw a display object(such as a movie clip) by using its own transform
686
+ * properties, you can copy its <code>transform</code> property object to the
687
+ * <code>transform</code> property of the Bitmap object that uses the
688
+ * BitmapImage2D object.</p>
689
+ *
690
+ * @param source The display object or BitmapImage2D object to draw to
691
+ * the BitmapImage2D object.(The DisplayObject and
692
+ * BitmapImage2D classes implement the IBitmapDrawable
693
+ * interface.)
694
+ * @param matrix A Matrix object used to scale, rotate, or translate
695
+ * the coordinates of the bitmap. If you do not want to
696
+ * apply a matrix transformation to the image, set this
697
+ * parameter to an identity matrix, created with the
698
+ * default <code>new Matrix()</code> constructor, or
699
+ * pass a <code>null</code> value.
700
+ * @param colorTransform A ColorTransform object that you use to adjust the
701
+ * color values of the bitmap. If no object is
702
+ * supplied, the bitmap image's colors are not
703
+ * transformed. If you must pass this parameter but you
704
+ * do not want to transform the image, set this
705
+ * parameter to a ColorTransform object created with
706
+ * the default <code>new ColorTransform()</code>
707
+ * constructor.
708
+ * @param blendMode A string value, from the flash.display.BlendMode
709
+ * class, specifying the blend mode to be applied to
710
+ * the resulting bitmap.
711
+ * @param clipRect A Rectangle object that defines the area of the
712
+ * source object to draw. If you do not supply this
713
+ * value, no clipping occurs and the entire source
714
+ * object is drawn.
715
+ * @param smoothing A Boolean value that determines whether a BitmapImage2D
716
+ * object is smoothed when scaled or rotated, due to a
717
+ * scaling or rotation in the <code>matrix</code>
718
+ * parameter. The <code>smoothing</code> parameter only
719
+ * applies if the <code>source</code> parameter is a
720
+ * BitmapImage2D object. With <code>smoothing</code> set
721
+ * to <code>false</code>, the rotated or scaled
722
+ * BitmapImage2D image can appear pixelated or jagged. For
723
+ * example, the following two images use the same
724
+ * BitmapImage2D object for the <code>source</code>
725
+ * parameter, but the <code>smoothing</code> parameter
726
+ * is set to <code>true</code> on the left and
727
+ * <code>false</code> on the right:
728
+ *
729
+ * <p>Drawing a bitmap with <code>smoothing</code> set
730
+ * to <code>true</code> takes longer than doing so with
731
+ * <code>smoothing</code> set to
732
+ * <code>false</code>.</p>
733
+ * @throws ArgumentError The <code>source</code> parameter is not a
734
+ * BitmapImage2D or DisplayObject object.
735
+ * @throws ArgumentError The source is null or not a valid IBitmapDrawable
736
+ * object.
737
+ * @throws SecurityError The <code>source</code> object and(in the case of a
738
+ * Sprite or MovieClip object) all of its child objects
739
+ * do not come from the same domain as the caller, or
740
+ * are not in a content that is accessible to the
741
+ * caller by having called the
742
+ * <code>Security.allowDomain()</code> method. This
743
+ * restriction does not apply to AIR content in the
744
+ * application security sandbox.
745
+ */
746
+
747
+ /* eslint-disable */
748
+ public draw(source: DisplayObject, matrix?: Matrix, colorTransform?: ColorTransform, blendMode?: string, clipRect?: Rectangle, smoothing?: boolean);
749
+ public draw(source: BitmapImage2D, matrix?: Matrix, colorTransform?: ColorTransform, blendMode?: string, clipRect?: Rectangle, smoothing?: boolean);
750
+ public draw(source: any, matrix?: Matrix, colorTransform?: ColorTransform, blendMode?: string, clipRect?: Rectangle, smoothing?: boolean): void
751
+ {
752
+ /* eslint-enable */
753
+
754
+ this.dropAllReferences();
755
+ this.unmarkToUnload();
756
+
757
+ if (source instanceof DisplayObject) {
758
+ this._drawAsDisplay(source, matrix, colorTransform, blendMode, clipRect, smoothing);
759
+ } else {
760
+ this._drawAsBitmap(source, matrix, colorTransform, blendMode, clipRect, smoothing);
761
+ }
762
+
763
+ this._lastUsedFill = null;
764
+ this._imageDataDirty = true;
765
+ this.invalidate();
766
+ }
767
+
768
+ private static _mapSupportedBlendMode(blendMode: string = ''): string {
769
+ switch (blendMode) {
770
+ case null:
771
+ case '':
772
+ case BlendMode.NORMAL:
773
+ case BlendMode.LAYER:
774
+ return BlendMode.LAYER;
775
+ case BlendMode.MULTIPLY:
776
+ case BlendMode.ADD:
777
+ case BlendMode.ALPHA:
778
+ return blendMode;
779
+ }
780
+
781
+ //console.debug("[ImageBitmap] Unsupport BlendMode", blendMode);
782
+ return BlendMode.LAYER;
783
+ }
784
+
785
+ private _drawAsBitmap(
786
+ source: BitmapImage2D, matrix?: Matrix, colorTransform?: ColorTransform,
787
+ blendMode?: string, _clipRect?: Rectangle, smoothing?: boolean
788
+ ) {
789
+
790
+ if (!SceneImage2D._billboardRenderer) {
791
+ this.createBillboardRenderer();
792
+ }
793
+
794
+ if (this._initalFillColor !== null) {
795
+ this.fillRect(this._rect, this._initalFillColor);
796
+ this._initalFillColor = null;
797
+ }
798
+
799
+ const stage = this._stage;
800
+ const mappedBlend = SceneImage2D._mapSupportedBlendMode(blendMode);
801
+ const supportNativeBlend = !blendMode || mappedBlend !== BlendMode.LAYER || blendMode == BlendMode.LAYER;
802
+ const useTmp = (!supportNativeBlend || this === source);
803
+ const target = useTmp ?
804
+ stage.filterManager.popTemp(this.width, this.height, false)
805
+ : this;
806
+
807
+ const renderer = SceneImage2D._billboardRenderer;
808
+ const root = SceneImage2D._billboardRoot;
809
+ const billboard = SceneImage2D._billboard;
810
+
811
+ billboard.sampler.smooth = smoothing;
812
+
813
+ renderer.disableClear = !useTmp;
814
+ renderer.view.target = target;
815
+ renderer.view.projection.scale = 1000 / target.height;
816
+
817
+ billboard.material.style.image = source;
818
+
819
+ // not all blend modes can be used for rendering
820
+ billboard.material.blendMode = !useTmp ? SceneImage2D._mapSupportedBlendMode(blendMode) : BlendMode.LAYER;
821
+
822
+ (<MaterialBase> billboard.material).useColorTransform = !!colorTransform;
823
+
824
+ if (matrix) {
825
+ const m = root.transform.matrix3D;
826
+
827
+ m.identity();
828
+
829
+ m._rawData[0] = matrix.a;
830
+ m._rawData[1] = -matrix.b;
831
+ m._rawData[4] = matrix.c;
832
+ m._rawData[5] = -matrix.d;
833
+ m._rawData[12] = matrix.tx;
834
+ m._rawData[13] = target.height - matrix.ty;
835
+
836
+ root.transform.invalidateComponents();
837
+ } else {
838
+
839
+ root.transform.rotateTo(0,0,0);
840
+ root.transform.scaleTo(1, -1, 1);
841
+ root.transform.moveTo(0, target.height,0);
842
+ }
843
+
844
+ root.transform.colorTransform = colorTransform;
845
+
846
+ //render
847
+ renderer.render();
848
+
849
+ //
850
+ if (useTmp) {
851
+ stage.filterManager.copyPixels(
852
+ target,
853
+ this,
854
+ this.rect,
855
+ TMP_POINT,
856
+ true,
857
+ blendMode
858
+ );
859
+
860
+ stage.filterManager.pushTemp(target);
861
+ }
862
+
863
+ }
864
+
865
+ private _drawAsDisplay(
866
+ source: DisplayObject, matrix?: Matrix, colorTransform?: ColorTransform,
867
+ blendMode: string = '', _clipRect?: Rectangle, _smoothing?: boolean
868
+ ) {
869
+ // default global blend mode
870
+ blendMode = blendMode || BlendMode.LAYER;
871
+
872
+ if (!SceneImage2D._renderer) {
873
+ this.createRenderer();
874
+ }
875
+
876
+ const root = SceneImage2D._root;
877
+ const rootNode = SceneImage2D._rootNode;
878
+ const renderer = SceneImage2D._renderer;
879
+
880
+ // when we should use MSAA, we will create temporary image and draw to it
881
+ const internal = this.canUseMSAAInternaly;
882
+ const nativeMSAA = (
883
+ !source.isAsset(Billboard) &&
884
+ this._stage.context.glVersion === 2 && // can be used because a webgl2
885
+ Settings.ALLOW_FORCE_MSAA > 1 && // because a quality is more that 1
886
+ !internal); // and not internal
887
+
888
+ // target image for rendering.
889
+ let target: Image2D = this;
890
+
891
+ // we should run compositor when blendMode !== LAYER (default)
892
+ // or when we use MSAA + fill is not flat.
893
+ const useBlend = blendMode !== BlendMode.LAYER || this._lastUsedFill === null;
894
+
895
+ const useTemp = useBlend || nativeMSAA;
896
+
897
+ // lazy filling
898
+ // we require fill image with initial color, because we not doing this immediate
899
+ // and when blend is required, because we should blend with vald color
900
+ if ((!nativeMSAA || useBlend) && this._initalFillColor !== null) {
901
+ this.fillRect(this._rect, this._initalFillColor);
902
+ this._initalFillColor = null;
903
+ }
904
+
905
+ if (useTemp) {
906
+ target = SceneImage2D.getTemp(this.width, this.height, this._stage, nativeMSAA);
907
+
908
+ // because target image is stupid filled - it not require merging
909
+ // BUT when blend is required - we shpuld skip color clear and use empty TMP
910
+ if (!useBlend && this._lastUsedFill !== null) {
911
+ // bitmap was filled plain, go clear TMP to this color too
912
+ renderer.disableClear = false;
913
+ renderer.view.backgroundColor = this._lastUsedFill & 0xffffff;
914
+ renderer.view.backgroundAlpha = this._transparent ? (this._lastUsedFill >>> 24 & 0xff) / 0xff : 1;
915
+ } else {
916
+ // we clear TMP and render to it, prepare to composing
917
+ renderer.disableClear = false;
918
+ renderer.view.backgroundColor = 0x0;
919
+ renderer.view.backgroundAlpha = 0;
920
+
921
+ // copy from source to tmp
922
+ // TMP_POINT.setTo(0,0);
923
+ // this._stage.copyPixels(this, target, this._rect, TMP_POINT, null, null, false);
924
+ }
925
+ }
926
+
927
+ const transform = renderer.view.projection.transform;
928
+ const mat3d = transform.matrix3D;
929
+ mat3d.identity();
930
+
931
+ if (matrix) {
932
+ const raw = mat3d._rawData;
933
+ raw[0] = matrix.a;
934
+ raw[1] = matrix.b;
935
+ raw[4] = matrix.c;
936
+ raw[5] = matrix.d;
937
+ raw[10] = 1;
938
+ raw[12] = matrix.tx;
939
+ raw[13] = matrix.ty;
940
+ }
941
+
942
+ // todo By this line we flip normals, and cull will be broken
943
+ // NEED FIX THIS ASAP, or flip cull state
944
+ mat3d.appendScale(1, -1, 1);
945
+ mat3d.appendTranslation(0, this._rect.height, 1000);
946
+
947
+ mat3d.invert();
948
+ transform.matrix3D = mat3d;
949
+
950
+ renderer.antiAlias = (internal ? this.antialiasQuality : (<any>target).antialiasQuality) || 0;
951
+
952
+ renderer.view.target = target;
953
+ renderer.view.projection.scale = 1000 / this._rect.height;
954
+
955
+ // shift view, because target can be more
956
+ renderer.view.projection.ratio = (this._rect.width / this._rect.height);
957
+ renderer.view.x = -(target.width - this.width);
958
+ renderer.view.y = -(target.height - this.height);
959
+ renderer.view.width = this.width;
960
+ renderer.view.height = this.height;
961
+
962
+ const sourceNode: ContainerNode = rootNode.addChildAt(source, 0);
963
+ const transformDisabled = sourceNode.transformDisabled;
964
+
965
+ sourceNode.transformDisabled = true;
966
+ // color transform should be enabled!
967
+ sourceNode.colorTransformDisabled = false;
968
+
969
+ root.transform.colorTransform = colorTransform;
970
+ // anyway we not support this =))
971
+ root.blendMode = SceneImage2D._mapSupportedBlendMode(blendMode);
972
+
973
+ //save snapshot if unlocked
974
+ //if (!this._locked)
975
+ //SceneImage2D.scene.view.target=this;
976
+ //SceneImage2D.scene.renderer.disableClear = !this._locked;
977
+
978
+ renderer.render();
979
+
980
+ // reset render to default value
981
+ renderer.antiAlias = 0;
982
+ renderer.disableClear = true;
983
+
984
+ rootNode.removeChildAt(0);
985
+
986
+ sourceNode.transformDisabled = transformDisabled;
987
+
988
+ if (useTemp) {
989
+ // because we copy MSAA into no msaa, it should passed as BLIT
990
+ this._stage.filterManager.copyPixels(
991
+ target,
992
+ this,
993
+ this._rect,
994
+ TMP_POINT,
995
+ useBlend,
996
+ blendMode // apply blend mode if this needed.
997
+ );
998
+
999
+ SceneImage2D.tryStoreImage(target, this._stage);
1000
+ }
1001
+ }
1002
+
1003
+ public reset() {}
1004
+
1005
+ public clone(): SceneImage2D {
1006
+ const image = SceneImage2D.getImage(
1007
+ this.width,
1008
+ this.height,
1009
+ this.transparent,
1010
+ null,
1011
+ false,
1012
+ this._stage,
1013
+ false
1014
+ );
1015
+
1016
+ image.deepClone(this);
1017
+ return image;
1018
+ }
1019
+ }
1020
+
1021
1021
  Stage.registerAbstraction(_Stage_BitmapImage2D, SceneImage2D);