babylon.group2d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. module BABYLON {
  2. @className("Group2D")
  3. export class Group2D extends Prim2DBase {
  4. static GROUP2D_PROPCOUNT: number = Prim2DBase.PRIM2DBASE_PROPCOUNT + 10;
  5. /**
  6. * Default behavior, the group will use the caching strategy defined at the Canvas Level
  7. */
  8. public static GROUPCACHEBEHAVIOR_FOLLOWCACHESTRATEGY = 0;
  9. /**
  10. * When used, this group's content won't be cached, no matter which strategy used.
  11. * If the group is part of a WorldSpace Canvas, its content will be drawn in the Canvas cache bitmap.
  12. */
  13. public static GROUPCACHEBEHAVIOR_DONTCACHEOVERRIDE = 1;
  14. /**
  15. * When used, the group's content will be cached in the nearest cached parent group/canvas
  16. */
  17. public static GROUPCACHEBEHAVIOR_CACHEINPARENTGROUP = 2;
  18. constructor() {
  19. super();
  20. this._primDirtyList = new Array<Prim2DBase>();
  21. this._childrenRenderableGroups = new Array<Group2D>();
  22. this.groupRenderInfo = new StringDictionary<GroupInstanceInfo>();
  23. }
  24. static CreateGroup2D(parent: Prim2DBase, id: string, position: Vector2, size?: Size, cacheBehabior: number = Group2D.GROUPCACHEBEHAVIOR_FOLLOWCACHESTRATEGY): Group2D {
  25. Prim2DBase.CheckParent(parent);
  26. var g = new Group2D();
  27. g.setupGroup2D(parent.owner, parent, id, position, size, cacheBehabior);
  28. return g;
  29. }
  30. /**
  31. * Create an instance of the Group Primitive.
  32. * A group act as a container for many sub primitives, if features:
  33. * - Maintain a size, not setting one will determine it based on its content.
  34. * - 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)
  35. * @param owner
  36. * @param id
  37. * @param position
  38. * @param size
  39. * @param dontcache
  40. */
  41. protected setupGroup2D(owner: Canvas2D,
  42. parent: Prim2DBase,
  43. id: string,
  44. position: Vector2,
  45. size?: Size,
  46. cacheBehavior: number = Group2D.GROUPCACHEBEHAVIOR_FOLLOWCACHESTRATEGY) {
  47. this._cacheBehavior = cacheBehavior;
  48. this.setupPrim2DBase(owner, parent, id, position);
  49. this.size = size;
  50. this._viewportPosition = Vector2.Zero();
  51. }
  52. public get isRenderableGroup(): boolean {
  53. return this._isRenderableGroup;
  54. }
  55. public get isCachedGroup(): boolean {
  56. return this._isCachedGroup;
  57. }
  58. public static sizeProperty: Prim2DPropInfo;
  59. public static actualSizeProperty: Prim2DPropInfo;
  60. @instanceLevelProperty(Prim2DBase.PRIM2DBASE_PROPCOUNT + 1, pi => Group2D.sizeProperty = pi, false, true)
  61. public get size(): Size {
  62. return this._size;
  63. }
  64. public set size(val: Size) {
  65. this._size = val;
  66. }
  67. public get viewportSize(): ISize {
  68. return this._viewportSize;
  69. }
  70. @instanceLevelProperty(Prim2DBase.PRIM2DBASE_PROPCOUNT + 2, pi => Group2D.actualSizeProperty = pi)
  71. public get actualSize(): Size {
  72. // Return the size if set by the user
  73. if (this._size) {
  74. return this._size;
  75. }
  76. // Otherwise the size is computed based on the boundingInfo
  77. let size = this.boundingInfo.extent.clone();
  78. return size;
  79. }
  80. public get cacheBehavior(): number {
  81. return this._cacheBehavior;
  82. }
  83. _addPrimToDirtyList(prim: Prim2DBase) {
  84. this._primDirtyList.push(prim);
  85. }
  86. protected updateLevelBoundingInfo() {
  87. let size: Size;
  88. // If the size is set by the user, the boundingInfo is compute from this value
  89. if (this.size) {
  90. size = this.size;
  91. }
  92. // Otherwise the group's level bouding info is "collapsed"
  93. else {
  94. size = new Size(0, 0);
  95. }
  96. this._levelBoundingInfo.radius = Math.sqrt(size.width * size.width + size.height * size.height);
  97. this._levelBoundingInfo.extent = size.clone();
  98. }
  99. // Method called only on renderable groups to prepare the rendering
  100. protected _prepareGroupRender(context: Render2DContext) {
  101. var childrenContext = this._buildChildContext(context);
  102. let sortedDirtyList: Prim2DBase[] = null;
  103. // Update the Global Transformation and visibility status of the changed primitives
  104. if ((this._primDirtyList.length > 0) || context.forceRefreshPrimitive) {
  105. sortedDirtyList = this._primDirtyList.sort((a, b) => a.hierarchyDepth - b.hierarchyDepth);
  106. this.updateGlobalTransVisOf(sortedDirtyList, childrenContext, true);
  107. }
  108. // Setup the size of the rendering viewport
  109. // 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
  110. if (!this._isCachedGroup) {
  111. // Compute the WebGL viewport's location/size
  112. let t = this._globalTransform.getTranslation();
  113. let s = this.actualSize.clone();
  114. let rs = this.owner._renderingSize;
  115. s.height = Math.min(s.height, rs.height - t.y);
  116. s.width = Math.min(s.width, rs.width - t.x);
  117. let x = t.x;
  118. let y = (rs.height - s.height) - t.y;
  119. // 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
  120. this._viewportPosition.x = x;
  121. this._viewportPosition.y = y;
  122. let vw = s.width;
  123. let vh = s.height;
  124. if (!this._viewportSize) {
  125. this._viewportSize = new Size(vw, vh);
  126. } else {
  127. if (this._viewportSize.width !== vw || this._viewportSize.height !== vh) {
  128. context.forceRefreshPrimitive = true;
  129. }
  130. this._viewportSize.width = vw;
  131. this._viewportSize.height = vh;
  132. }
  133. } else {
  134. this._viewportSize = this.actualSize;
  135. }
  136. if ((this._primDirtyList.length > 0) || context.forceRefreshPrimitive) {
  137. // If the group is cached, set the dirty flag to true because of the incoming changes
  138. this._cacheGroupDirty = this._isCachedGroup;
  139. // If it's a force refresh, prepare all the children
  140. if (context.forceRefreshPrimitive) {
  141. for (let p of this._children) {
  142. p._prepareRender(childrenContext);
  143. }
  144. } else {
  145. // Each primitive that changed at least once was added into the primDirtyList, we have to sort this level using
  146. // the hierarchyDepth in order to prepare primitives from top to bottom
  147. if (!sortedDirtyList) {
  148. sortedDirtyList = this._primDirtyList.sort((a, b) => a.hierarchyDepth - b.hierarchyDepth);
  149. }
  150. sortedDirtyList.forEach(p => {
  151. // 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.
  152. // 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.
  153. if (p.needPrepare()) {
  154. p._prepareRender(childrenContext);
  155. }
  156. });
  157. // Everything is updated, clear the dirty list
  158. this._primDirtyList.splice(0);
  159. }
  160. }
  161. // A renderable group has a list of direct children that are also renderable groups, we recurse on them to also prepare them
  162. this._childrenRenderableGroups.forEach(g => {
  163. g._prepareGroupRender(childrenContext);
  164. });
  165. }
  166. protected _groupRender(context: Render2DContext) {
  167. let engine = this.owner.engine;
  168. let failedCount = 0;
  169. // First recurse to children render group to render them (in their cache or on screen)
  170. var childrenContext = this._buildChildContext(context);
  171. for (let childGroup of this._childrenRenderableGroups) {
  172. childGroup._groupRender(childrenContext);
  173. }
  174. // Render the primitives if needed: either if we don't cache the content or if the content is cached but has changed
  175. if (!this.isCachedGroup || this._cacheGroupDirty) {
  176. if (this.isCachedGroup) {
  177. this._bindCacheTarget();
  178. } else {
  179. var curVP = engine.setDirectViewport(this._viewportPosition.x, this._viewportPosition.y, this._viewportSize.width, this._viewportSize.height);
  180. }
  181. // For each different model of primitive to render
  182. this.groupRenderInfo.forEach((k, v) => {
  183. // If the instances of the model was changed, pack the data
  184. let instanceData = v._instancesData.pack();
  185. // Compute the size the instance buffer should have
  186. let neededSize = v._instancesData.usedElementCount * v._instancesData.stride * 4;
  187. // Check if we have to (re)create the instancesBuffer because there's none or the size doesn't match
  188. if (!v._instancesBuffer || (v._instancesBufferSize !== neededSize)) {
  189. if (v._instancesBuffer) {
  190. engine.deleteInstancesBuffer(v._instancesBuffer);
  191. }
  192. v._instancesBuffer = engine.createInstancesBuffer(neededSize);
  193. v._instancesBufferSize = neededSize;
  194. v._dirtyInstancesData = true;
  195. // Update the WebGL buffer to match the new content of the instances data
  196. engine._gl.bufferSubData(engine._gl.ARRAY_BUFFER, 0, instanceData);
  197. } else if (v._dirtyInstancesData) {
  198. // Update the WebGL buffer to match the new content of the instances data
  199. engine._gl.bindBuffer(engine._gl.ARRAY_BUFFER, v._instancesBuffer);
  200. engine._gl.bufferSubData(engine._gl.ARRAY_BUFFER, 0, instanceData);
  201. v._dirtyInstancesData = false;
  202. }
  203. // render all the instances of this model, if the render method returns true then our instances are no longer dirty
  204. let renderFailed = !v._modelCache.render(v, context);
  205. // Update dirty flag/related
  206. v._dirtyInstancesData = renderFailed;
  207. failedCount += renderFailed ? 1 : 0;
  208. });
  209. // The group's content is no longer dirty
  210. this._cacheGroupDirty = failedCount !== 0;
  211. if (this.isCachedGroup) {
  212. this._unbindCacheTarget();
  213. } else {
  214. if (curVP) {
  215. engine.setViewport(curVP);
  216. }
  217. }
  218. }
  219. }
  220. private _bindCacheTarget() {
  221. // Check if we have to allocate a rendering zone in the global cache texture
  222. if (!this._cacheNode) {
  223. var res = this.owner._allocateGroupCache(this);
  224. this._cacheNode = res.node;
  225. this._cacheTexture = res.texture;
  226. }
  227. let n = this._cacheNode;
  228. this._cacheTexture.bindTextureForRect(n);
  229. }
  230. private _unbindCacheTarget() {
  231. if (this._cacheTexture) {
  232. this._cacheTexture.unbindTexture();
  233. }
  234. }
  235. private detectGroupStates() {
  236. var isCanvas = this instanceof Canvas2D;
  237. var canvasStrat = this.owner.cachingStrategy;
  238. // In Don't Cache mode, only the canvas is renderable, all the other groups are logical. There are not a single cached group.
  239. if (canvasStrat === Canvas2D.CACHESTRATEGY_DONTCACHE) {
  240. this._isRenderableGroup = isCanvas;
  241. this._isCachedGroup = false;
  242. }
  243. // In Canvas cached only mode, only the Canvas is cached and renderable, all other groups are logicals
  244. else if (canvasStrat === Canvas2D.CACHESTRATEGY_CANVAS) {
  245. if (isCanvas) {
  246. this._isRenderableGroup = true;
  247. this._isCachedGroup = true;
  248. } else {
  249. this._isRenderableGroup = false;
  250. this._isCachedGroup = false;
  251. }
  252. }
  253. // Top Level Groups cached only mode, the canvas is a renderable/not cached, its direct Groups are cached/renderable, all other group are logicals
  254. else if (canvasStrat === Canvas2D.CACHESTRATEGY_TOPLEVELGROUPS) {
  255. if (isCanvas) {
  256. this._isRenderableGroup = true;
  257. this._isCachedGroup = false;
  258. } else {
  259. if (this.hierarchyDepth === 1) {
  260. this._isRenderableGroup = true;
  261. this._isCachedGroup = true;
  262. } else {
  263. this._isRenderableGroup = false;
  264. this._isCachedGroup = false;
  265. }
  266. }
  267. }
  268. // 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
  269. else if (canvasStrat === Canvas2D.CACHESTRATEGY_ALLGROUPS) {
  270. var gcb = this.cacheBehavior;
  271. if ((gcb === Group2D.GROUPCACHEBEHAVIOR_DONTCACHEOVERRIDE) || (gcb === Group2D.GROUPCACHEBEHAVIOR_CACHEINPARENTGROUP)) {
  272. this._isRenderableGroup = gcb === Group2D.GROUPCACHEBEHAVIOR_DONTCACHEOVERRIDE;
  273. this._isCachedGroup = false;
  274. }
  275. if (gcb === Group2D.GROUPCACHEBEHAVIOR_FOLLOWCACHESTRATEGY) {
  276. this._isRenderableGroup = true;
  277. this._isCachedGroup = true;
  278. }
  279. }
  280. // If the group is tagged as renderable we add it to the renderable tree
  281. if (this._isCachedGroup) {
  282. let cur = this.parent;
  283. while (cur) {
  284. if (cur instanceof Group2D && cur._isRenderableGroup) {
  285. cur._childrenRenderableGroups.push(this);
  286. break;
  287. }
  288. cur = cur.parent;
  289. }
  290. }
  291. }
  292. protected _isRenderableGroup: boolean;
  293. protected _isCachedGroup: boolean;
  294. private _cacheGroupDirty: boolean;
  295. protected _childrenRenderableGroups: Array<Group2D>;
  296. private _size: Size;
  297. private _cacheBehavior: number;
  298. private _primDirtyList: Array<Prim2DBase>;
  299. private _cacheNode: PackedRect;
  300. private _cacheTexture: MapTexture;
  301. private _viewportPosition: Vector2;
  302. private _viewportSize: Size;
  303. groupRenderInfo: StringDictionary<GroupInstanceInfo>;
  304. }
  305. }