babylon.group2d.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. module BABYLON {
  2. @className("Group2D")
  3. export class Group2D extends Prim2DBase {
  4. static GROUP2D_PROPCOUNT: number = Prim2DBase.PRIM2DBASE_PROPCOUNT + 5;
  5. public static sizeProperty: Prim2DPropInfo;
  6. public static actualSizeProperty: Prim2DPropInfo;
  7. /**
  8. * Default behavior, the group will use the caching strategy defined at the Canvas Level
  9. */
  10. public static GROUPCACHEBEHAVIOR_FOLLOWCACHESTRATEGY = 0;
  11. /**
  12. * When used, this group's content won't be cached, no matter which strategy used.
  13. * If the group is part of a WorldSpace Canvas, its content will be drawn in the Canvas cache bitmap.
  14. */
  15. public static GROUPCACHEBEHAVIOR_DONTCACHEOVERRIDE = 1;
  16. /**
  17. * When used, the group's content will be cached in the nearest cached parent group/canvas
  18. */
  19. public static GROUPCACHEBEHAVIOR_CACHEINPARENTGROUP = 2;
  20. constructor() {
  21. super();
  22. this._primDirtyList = new Array<Prim2DBase>();
  23. this._childrenRenderableGroups = new Array<Group2D>();
  24. this.groupRenderInfo = new StringDictionary<GroupInstanceInfo>();
  25. }
  26. static CreateGroup2D(parent: Prim2DBase, id: string, position: Vector2, size?: Size, cacheBehabior: number = Group2D.GROUPCACHEBEHAVIOR_FOLLOWCACHESTRATEGY): Group2D {
  27. Prim2DBase.CheckParent(parent);
  28. var g = new Group2D();
  29. g.setupGroup2D(parent.owner, parent, id, position, size, cacheBehabior);
  30. return g;
  31. }
  32. applyCachedTexture(vertexData: VertexData, material: StandardMaterial) {
  33. this._bindCacheTarget();
  34. var uv = vertexData.uvs;
  35. let nodeuv = this._cacheNode.UVs;
  36. for (let i = 0; i < 4; i++) {
  37. uv[i * 2 + 0] = nodeuv[i].x;
  38. uv[i * 2 + 1] = nodeuv[i].y;
  39. }
  40. material.diffuseTexture = this._cacheTexture;
  41. material.emissiveColor = new Color3(1, 1, 1);
  42. this._cacheTexture.hasAlpha = true;
  43. this._unbindCacheTarget();
  44. }
  45. public dispose(): boolean {
  46. if (!super.dispose()) {
  47. return false;
  48. }
  49. if (this._cacheRenderSprite) {
  50. this._cacheRenderSprite.dispose();
  51. this._cacheRenderSprite = null;
  52. }
  53. if (this._cacheTexture && this._cacheNode) {
  54. this._cacheTexture.freeRect(this._cacheNode);
  55. this._cacheTexture = null;
  56. this._cacheNode = null;
  57. }
  58. if(this._primDirtyList) {
  59. this._primDirtyList.splice(0);
  60. this._primDirtyList = null;
  61. }
  62. if (this.groupRenderInfo) {
  63. this.groupRenderInfo.forEach((k, v) => {
  64. v.dispose();
  65. });
  66. this.groupRenderInfo = null;
  67. }
  68. return true;
  69. }
  70. /**
  71. * Create an instance of the Group Primitive.
  72. * A group act as a container for many sub primitives, if features:
  73. * - Maintain a size, not setting one will determine it based on its content.
  74. * - Play an essential role in the rendering pipeline. A group and its content can be cached into a bitmap to enhance rendering performance (at the cost of memory storage in GPU)
  75. * @param owner
  76. * @param id
  77. * @param position
  78. * @param size
  79. * @param dontcache
  80. */
  81. protected setupGroup2D(owner: Canvas2D,
  82. parent: Prim2DBase,
  83. id: string,
  84. position: Vector2,
  85. size?: Size,
  86. cacheBehavior: number = Group2D.GROUPCACHEBEHAVIOR_FOLLOWCACHESTRATEGY) {
  87. this._cacheBehavior = cacheBehavior;
  88. this.setupPrim2DBase(owner, parent, id, position);
  89. this.size = size;
  90. this._viewportPosition = Vector2.Zero();
  91. }
  92. public get isRenderableGroup(): boolean {
  93. return this._isRenderableGroup;
  94. }
  95. public get isCachedGroup(): boolean {
  96. return this._isCachedGroup;
  97. }
  98. @instanceLevelProperty(Prim2DBase.PRIM2DBASE_PROPCOUNT + 1, pi => Group2D.sizeProperty = pi, false, true)
  99. public get size(): Size {
  100. return this._size;
  101. }
  102. public set size(val: Size) {
  103. this._size = val;
  104. }
  105. public get viewportSize(): ISize {
  106. return this._viewportSize;
  107. }
  108. @instanceLevelProperty(Prim2DBase.PRIM2DBASE_PROPCOUNT + 2, pi => Group2D.actualSizeProperty = pi)
  109. public get actualSize(): Size {
  110. // Return the size if set by the user
  111. if (this._size) {
  112. return this._size;
  113. }
  114. // Otherwise the size is computed based on the boundingInfo
  115. let m = this.boundingInfo.max();
  116. return new Size(m.x, m.y);
  117. }
  118. public get cacheBehavior(): number {
  119. return this._cacheBehavior;
  120. }
  121. public _addPrimToDirtyList(prim: Prim2DBase) {
  122. this._primDirtyList.push(prim);
  123. }
  124. protected updateLevelBoundingInfo() {
  125. let size: Size;
  126. // If the size is set by the user, the boundingInfo is computed from this value
  127. if (this.size) {
  128. size = this.size;
  129. }
  130. // Otherwise the group's level bouding info is "collapsed"
  131. else {
  132. size = new Size(0, 0);
  133. }
  134. BoundingInfo2D.CreateFromSizeToRef(size, this._levelBoundingInfo);
  135. }
  136. // Method called only on renderable groups to prepare the rendering
  137. protected _prepareGroupRender(context: Render2DContext) {
  138. let sortedDirtyList: Prim2DBase[] = null;
  139. // Update the Global Transformation and visibility status of the changed primitives
  140. if ((this._primDirtyList.length > 0) || context.forceRefreshPrimitive) {
  141. sortedDirtyList = this._primDirtyList.sort((a, b) => a.hierarchyDepth - b.hierarchyDepth);
  142. this.updateGlobalTransVisOf(sortedDirtyList, true);
  143. }
  144. // Setup the size of the rendering viewport
  145. // In non cache mode, we're rendering directly to the rendering canvas, in this case we have to detect if the canvas size changed since the previous iteration, if it's the case all primitives must be preprared again because their transformation must be recompute
  146. if (!this._isCachedGroup) {
  147. // Compute the WebGL viewport's location/size
  148. let t = this._globalTransform.getTranslation();
  149. let s = this.actualSize.clone();
  150. let rs = this.owner._renderingSize;
  151. s.height = Math.min(s.height, rs.height - t.y);
  152. s.width = Math.min(s.width, rs.width - t.x);
  153. let x = t.x;
  154. let y = (rs.height - s.height) - t.y;
  155. // The viewport where we're rendering must be the size of the canvas if this one fit in the rendering screen or clipped to the screen dimensions if needed
  156. this._viewportPosition.x = x;
  157. this._viewportPosition.y = y;
  158. let vw = s.width;
  159. let vh = s.height;
  160. if (!this._viewportSize) {
  161. this._viewportSize = new Size(vw, vh);
  162. } else {
  163. if (this._viewportSize.width !== vw || this._viewportSize.height !== vh) {
  164. context.forceRefreshPrimitive = true;
  165. }
  166. this._viewportSize.width = vw;
  167. this._viewportSize.height = vh;
  168. }
  169. } else {
  170. this._viewportSize = this.actualSize;
  171. }
  172. if ((this._primDirtyList.length > 0) || context.forceRefreshPrimitive) {
  173. // If the group is cached, set the dirty flag to true because of the incoming changes
  174. this._cacheGroupDirty = this._isCachedGroup;
  175. // If it's a force refresh, prepare all the children
  176. if (context.forceRefreshPrimitive) {
  177. for (let p of this._children) {
  178. p._prepareRender(context);
  179. }
  180. } else {
  181. // Each primitive that changed at least once was added into the primDirtyList, we have to sort this level using
  182. // the hierarchyDepth in order to prepare primitives from top to bottom
  183. if (!sortedDirtyList) {
  184. sortedDirtyList = this._primDirtyList.sort((a, b) => a.hierarchyDepth - b.hierarchyDepth);
  185. }
  186. sortedDirtyList.forEach(p => {
  187. // We need to check if prepare is needed because even if the primitive is in the dirtyList, its parent primitive may also have been modified, then prepared, then recurse on its children primitives (this one for instance) if the changes where impacting them.
  188. // For instance: a Rect's position change, the position of its children primitives will also change so a prepare will be call on them. If a child was in the dirtyList we will avoid a second prepare by making this check.
  189. if (!p.isDisposed && p.needPrepare()) {
  190. p._prepareRender(context);
  191. }
  192. });
  193. // Everything is updated, clear the dirty list
  194. this._primDirtyList.splice(0);
  195. }
  196. }
  197. // A renderable group has a list of direct children that are also renderable groups, we recurse on them to also prepare them
  198. this._childrenRenderableGroups.forEach(g => {
  199. g._prepareGroupRender(context);
  200. });
  201. }
  202. protected _groupRender(context: Render2DContext) {
  203. let engine = this.owner.engine;
  204. let failedCount = 0;
  205. // First recurse to children render group to render them (in their cache or on screen)
  206. for (let childGroup of this._childrenRenderableGroups) {
  207. childGroup._groupRender(context);
  208. }
  209. // Render the primitives if needed: either if we don't cache the content or if the content is cached but has changed
  210. if (!this.isCachedGroup || this._cacheGroupDirty) {
  211. if (this.isCachedGroup) {
  212. this._bindCacheTarget();
  213. } else {
  214. var curVP = engine.setDirectViewport(this._viewportPosition.x, this._viewportPosition.y, this._viewportSize.width, this._viewportSize.height);
  215. }
  216. // For each different model of primitive to render
  217. let totalRenderCount = 0;
  218. this.groupRenderInfo.forEach((k, v) => {
  219. // This part will pack the dynfloatarray and update the instanced array WebGLBufffer
  220. // Skip it if instanced arrays are not supported
  221. if (this.owner.supportInstancedArray) {
  222. for (let i = 0; i < v._instancesPartsData.length; i++) {
  223. // If the instances of the model was changed, pack the data
  224. let array = v._instancesPartsData[i];
  225. let instanceData = array.pack();
  226. totalRenderCount += array.usedElementCount;
  227. // Compute the size the instance buffer should have
  228. let neededSize = array.usedElementCount * array.stride * 4;
  229. // Check if we have to (re)create the instancesBuffer because there's none or the size is too small
  230. if (!v._instancesPartsBuffer[i] || (v._instancesPartsBufferSize[i] <= neededSize)) {
  231. if (v._instancesPartsBuffer[i]) {
  232. engine.deleteInstancesBuffer(v._instancesPartsBuffer[i]);
  233. }
  234. v._instancesPartsBuffer[i] = engine.createInstancesBuffer(neededSize);
  235. v._instancesPartsBufferSize[i] = neededSize;
  236. v._dirtyInstancesData = true;
  237. // Update the WebGL buffer to match the new content of the instances data
  238. engine._gl.bufferSubData(engine._gl.ARRAY_BUFFER, 0, instanceData);
  239. } else if (v._dirtyInstancesData) {
  240. // Update the WebGL buffer to match the new content of the instances data
  241. engine._gl.bindBuffer(engine._gl.ARRAY_BUFFER, v._instancesPartsBuffer[i]);
  242. engine._gl.bufferSubData(engine._gl.ARRAY_BUFFER, 0, instanceData);
  243. v._dirtyInstancesData = false;
  244. }
  245. }
  246. }
  247. // Submit render only if we have something to render (everything may be hiden and the floatarray empty)
  248. if (!this.owner.supportInstancedArray || totalRenderCount > 0) {
  249. // render all the instances of this model, if the render method returns true then our instances are no longer dirty
  250. let renderFailed = !v._modelCache.render(v, context);
  251. // Update dirty flag/related
  252. v._dirtyInstancesData = renderFailed;
  253. failedCount += renderFailed ? 1 : 0;
  254. }
  255. });
  256. // The group's content is no longer dirty
  257. this._cacheGroupDirty = failedCount !== 0;
  258. if (this.isCachedGroup) {
  259. this._unbindCacheTarget();
  260. } else {
  261. if (curVP) {
  262. engine.setViewport(curVP);
  263. }
  264. }
  265. }
  266. }
  267. private _bindCacheTarget() {
  268. // Check if we have to allocate a rendering zone in the global cache texture
  269. if (!this._cacheNode) {
  270. var res = this.owner._allocateGroupCache(this);
  271. this._cacheNode = res.node;
  272. this._cacheTexture = res.texture;
  273. this._cacheRenderSprite = res.sprite;
  274. }
  275. let n = this._cacheNode;
  276. this._cacheTexture.bindTextureForRect(n, true);
  277. }
  278. private _unbindCacheTarget() {
  279. if (this._cacheTexture) {
  280. this._cacheTexture.unbindTexture();
  281. }
  282. }
  283. protected handleGroupChanged(prop: Prim2DPropInfo) {
  284. // This method is only for cachedGroup
  285. if (!this.isCachedGroup || !this._cacheRenderSprite) {
  286. return;
  287. }
  288. // For now we only support these property changes
  289. // TODO: add more! :)
  290. if (prop.id === Prim2DBase.positionProperty.id) {
  291. this._cacheRenderSprite.position = this.position.clone();
  292. } else if (prop.id === Prim2DBase.rotationProperty.id) {
  293. this._cacheRenderSprite.rotation = this.rotation;
  294. } else if (prop.id === Prim2DBase.scaleProperty.id) {
  295. this._cacheRenderSprite.scale = this.scale;
  296. }
  297. }
  298. private detectGroupStates() {
  299. var isCanvas = this instanceof Canvas2D;
  300. var canvasStrat = this.owner.cachingStrategy;
  301. // In Don't Cache mode, only the canvas is renderable, all the other groups are logical. There are not a single cached group.
  302. if (canvasStrat === Canvas2D.CACHESTRATEGY_DONTCACHE) {
  303. this._isRenderableGroup = isCanvas;
  304. this._isCachedGroup = false;
  305. }
  306. // In Canvas cached only mode, only the Canvas is cached and renderable, all other groups are logicals
  307. else if (canvasStrat === Canvas2D.CACHESTRATEGY_CANVAS) {
  308. if (isCanvas) {
  309. this._isRenderableGroup = true;
  310. this._isCachedGroup = true;
  311. } else {
  312. this._isRenderableGroup = false;
  313. this._isCachedGroup = false;
  314. }
  315. }
  316. // Top Level Groups cached only mode, the canvas is a renderable/not cached, its direct Groups are cached/renderable, all other group are logicals
  317. else if (canvasStrat === Canvas2D.CACHESTRATEGY_TOPLEVELGROUPS) {
  318. if (isCanvas) {
  319. this._isRenderableGroup = true;
  320. this._isCachedGroup = false;
  321. } else {
  322. if (this.hierarchyDepth === 1) {
  323. this._isRenderableGroup = true;
  324. this._isCachedGroup = true;
  325. } else {
  326. this._isRenderableGroup = false;
  327. this._isCachedGroup = false;
  328. }
  329. }
  330. }
  331. // All Group cached mode, all groups are renderable/cached, including the Canvas, groups with the behavior DONTCACHE are renderable/not cached, groups with CACHEINPARENT are logical ones
  332. else if (canvasStrat === Canvas2D.CACHESTRATEGY_ALLGROUPS) {
  333. var gcb = this.cacheBehavior;
  334. if ((gcb === Group2D.GROUPCACHEBEHAVIOR_DONTCACHEOVERRIDE) || (gcb === Group2D.GROUPCACHEBEHAVIOR_CACHEINPARENTGROUP)) {
  335. this._isRenderableGroup = gcb === Group2D.GROUPCACHEBEHAVIOR_DONTCACHEOVERRIDE;
  336. this._isCachedGroup = false;
  337. }
  338. if (gcb === Group2D.GROUPCACHEBEHAVIOR_FOLLOWCACHESTRATEGY) {
  339. this._isRenderableGroup = true;
  340. this._isCachedGroup = true;
  341. }
  342. }
  343. // If the group is tagged as renderable we add it to the renderable tree
  344. if (this._isCachedGroup) {
  345. let cur = this.parent;
  346. while (cur) {
  347. if (cur instanceof Group2D && cur._isRenderableGroup) {
  348. cur._childrenRenderableGroups.push(this);
  349. break;
  350. }
  351. cur = cur.parent;
  352. }
  353. }
  354. }
  355. protected _isRenderableGroup: boolean;
  356. protected _isCachedGroup: boolean;
  357. private _cacheGroupDirty: boolean;
  358. protected _childrenRenderableGroups: Array<Group2D>;
  359. private _size: Size;
  360. private _cacheBehavior: number;
  361. private _primDirtyList: Array<Prim2DBase>;
  362. private _cacheNode: PackedRect;
  363. private _cacheTexture: MapTexture;
  364. private _cacheRenderSprite: Sprite2D;
  365. private _viewportPosition: Vector2;
  366. private _viewportSize: Size;
  367. groupRenderInfo: StringDictionary<GroupInstanceInfo>;
  368. }
  369. }