babylon.prim2dBase.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  7. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  8. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  9. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  10. return c > 3 && r && Object.defineProperty(target, key, r), r;
  11. };
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var Render2DContext = (function () {
  15. function Render2DContext() {
  16. }
  17. return Render2DContext;
  18. })();
  19. BABYLON.Render2DContext = Render2DContext;
  20. var Prim2DBase = (function (_super) {
  21. __extends(Prim2DBase, _super);
  22. function Prim2DBase() {
  23. _super.apply(this, arguments);
  24. }
  25. Prim2DBase.prototype.setupPrim2DBase = function (owner, parent, id, position, isVisible) {
  26. if (isVisible === void 0) { isVisible = true; }
  27. if (!(this instanceof BABYLON.Group2D) && !(this instanceof BABYLON.Sprite2D && id !== null && id.indexOf("__cachedSpriteOfGroup__") === 0) && (owner.cachingStrategy === BABYLON.Canvas2D.CACHESTRATEGY_TOPLEVELGROUPS) && (parent === owner)) {
  28. throw new Error("Can't create a primitive with the canvas as direct parent when the caching strategy is TOPLEVELGROUPS. You need to create a Group below the canvas and use it as the parent for the primitive");
  29. }
  30. this.setupSmartPropertyPrim();
  31. this._boundingInfoDirty = true;
  32. this._boundingInfo = new BABYLON.BoundingInfo2D();
  33. this._owner = owner;
  34. this._parent = parent;
  35. if (parent != null) {
  36. this._hierarchyDepth = parent._hierarchyDepth + 1;
  37. this._renderGroup = this.parent.traverseUp(function (p) { return p instanceof BABYLON.Group2D && p.isRenderableGroup; });
  38. parent.addChild(this);
  39. }
  40. else {
  41. this._hierarchyDepth = 0;
  42. this._renderGroup = null;
  43. }
  44. this._id = id;
  45. this.propertyChanged = new BABYLON.Observable();
  46. this._children = new Array();
  47. this._globalTransformProcessStep = 0;
  48. this._globalTransformStep = 0;
  49. if (this instanceof BABYLON.Group2D) {
  50. var group = this;
  51. group.detectGroupStates();
  52. }
  53. this.position = position;
  54. this.rotation = 0;
  55. this.scale = 1;
  56. this.levelVisible = isVisible;
  57. this.origin = new BABYLON.Vector2(0.5, 0.5);
  58. };
  59. Prim2DBase.prototype.traverseUp = function (predicate) {
  60. var p = this;
  61. while (p != null) {
  62. if (predicate(p)) {
  63. return p;
  64. }
  65. p = p._parent;
  66. }
  67. return null;
  68. };
  69. Object.defineProperty(Prim2DBase.prototype, "owner", {
  70. get: function () {
  71. return this._owner;
  72. },
  73. enumerable: true,
  74. configurable: true
  75. });
  76. Object.defineProperty(Prim2DBase.prototype, "parent", {
  77. get: function () {
  78. return this._parent;
  79. },
  80. enumerable: true,
  81. configurable: true
  82. });
  83. Object.defineProperty(Prim2DBase.prototype, "id", {
  84. get: function () {
  85. return this._id;
  86. },
  87. enumerable: true,
  88. configurable: true
  89. });
  90. Object.defineProperty(Prim2DBase.prototype, "position", {
  91. get: function () {
  92. return this._position;
  93. },
  94. set: function (value) {
  95. this._position = value;
  96. },
  97. enumerable: true,
  98. configurable: true
  99. });
  100. Object.defineProperty(Prim2DBase.prototype, "rotation", {
  101. get: function () {
  102. return this._rotation;
  103. },
  104. set: function (value) {
  105. this._rotation = value;
  106. },
  107. enumerable: true,
  108. configurable: true
  109. });
  110. Object.defineProperty(Prim2DBase.prototype, "scale", {
  111. get: function () {
  112. return this._scale;
  113. },
  114. set: function (value) {
  115. this._scale = value;
  116. },
  117. enumerable: true,
  118. configurable: true
  119. });
  120. Object.defineProperty(Prim2DBase.prototype, "origin", {
  121. /**
  122. * The origin defines the normalized coordinate of the center of the primitive, from the top/left corner.
  123. * The origin is used only to compute transformation of the primitive, it has no meaning in the primitive local frame of reference
  124. * For instance:
  125. * 0,0 means the center is top/left
  126. * 0.5,0.5 means the center is at the center of the primtive
  127. * 0,1 means the center is bottom/left
  128. * @returns The normalized center.
  129. */
  130. get: function () {
  131. return this._origin;
  132. },
  133. set: function (value) {
  134. this._origin = value;
  135. },
  136. enumerable: true,
  137. configurable: true
  138. });
  139. Object.defineProperty(Prim2DBase.prototype, "levelVisible", {
  140. get: function () {
  141. return this._levelVisible;
  142. },
  143. set: function (value) {
  144. this._levelVisible = value;
  145. },
  146. enumerable: true,
  147. configurable: true
  148. });
  149. Object.defineProperty(Prim2DBase.prototype, "isVisible", {
  150. get: function () {
  151. return this._isVisible;
  152. },
  153. set: function (value) {
  154. this._isVisible = value;
  155. },
  156. enumerable: true,
  157. configurable: true
  158. });
  159. Object.defineProperty(Prim2DBase.prototype, "zOrder", {
  160. get: function () {
  161. return this._zOrder;
  162. },
  163. set: function (value) {
  164. this._zOrder = value;
  165. },
  166. enumerable: true,
  167. configurable: true
  168. });
  169. Object.defineProperty(Prim2DBase.prototype, "hierarchyDepth", {
  170. get: function () {
  171. return this._hierarchyDepth;
  172. },
  173. enumerable: true,
  174. configurable: true
  175. });
  176. Object.defineProperty(Prim2DBase.prototype, "renderGroup", {
  177. get: function () {
  178. return this._renderGroup;
  179. },
  180. enumerable: true,
  181. configurable: true
  182. });
  183. Object.defineProperty(Prim2DBase.prototype, "globalTransform", {
  184. get: function () {
  185. return this._globalTransform;
  186. },
  187. enumerable: true,
  188. configurable: true
  189. });
  190. Object.defineProperty(Prim2DBase.prototype, "invGlobalTransform", {
  191. get: function () {
  192. return this._invGlobalTransform;
  193. },
  194. enumerable: true,
  195. configurable: true
  196. });
  197. Object.defineProperty(Prim2DBase.prototype, "boundingInfo", {
  198. get: function () {
  199. if (this._boundingInfoDirty) {
  200. this._boundingInfo = this.levelBoundingInfo.clone();
  201. var bi = this._boundingInfo;
  202. var tps = new BABYLON.BoundingInfo2D();
  203. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  204. var curChild = _a[_i];
  205. var t = curChild.globalTransform.multiply(this.invGlobalTransform);
  206. curChild.boundingInfo.transformToRef(t, curChild.origin, tps);
  207. bi.unionToRef(tps, bi);
  208. }
  209. this._boundingInfoDirty = false;
  210. }
  211. return this._boundingInfo;
  212. },
  213. enumerable: true,
  214. configurable: true
  215. });
  216. Prim2DBase.prototype.moveChild = function (child, previous) {
  217. if (child.parent !== this) {
  218. return false;
  219. }
  220. var prevOffset, nextOffset;
  221. var childIndex = this._children.indexOf(child);
  222. var prevIndex = previous ? this._children.indexOf(previous) : -1;
  223. // Move to first position
  224. if (!previous) {
  225. prevOffset = 1;
  226. nextOffset = this._children[1]._siblingDepthOffset;
  227. }
  228. else {
  229. prevOffset = this._children[prevIndex]._siblingDepthOffset;
  230. nextOffset = this._children[prevIndex + 1]._siblingDepthOffset;
  231. }
  232. child._siblingDepthOffset = (nextOffset - prevOffset) / 2;
  233. this._children.splice(prevIndex + 1, 0, this._children.splice(childIndex, 1)[0]);
  234. };
  235. Prim2DBase.prototype.addChild = function (child) {
  236. child._siblingDepthOffset = (this._children.length + 1) * this.owner.hierarchySiblingZDelta;
  237. child._depthLevel = this._depthLevel + 1;
  238. child._hierarchyDepthOffset = child._depthLevel * this.owner.hierarchyLevelZFactor;
  239. this._children.push(child);
  240. };
  241. Prim2DBase.prototype.dispose = function () {
  242. if (!_super.prototype.dispose.call(this)) {
  243. return false;
  244. }
  245. // If there's a parent, remove this object from its parent list
  246. if (this._parent) {
  247. var i = this._parent._children.indexOf(this);
  248. if (i !== undefined) {
  249. this._parent._children.splice(i, 1);
  250. }
  251. this._parent = null;
  252. }
  253. // Recurse dispose to children
  254. if (this._children) {
  255. while (this._children.length > 0) {
  256. this._children[this._children.length - 1].dispose();
  257. }
  258. }
  259. return true;
  260. };
  261. Prim2DBase.prototype.getActualZOffset = function () {
  262. return this._zOrder || 1 - (this._siblingDepthOffset + this._hierarchyDepthOffset);
  263. };
  264. Prim2DBase.prototype.onPrimBecomesDirty = function () {
  265. if (this._renderGroup) {
  266. this._renderGroup._addPrimToDirtyList(this);
  267. }
  268. };
  269. Prim2DBase.prototype.needPrepare = function () {
  270. return (this.isVisible || this._visibilityChanged) && (this._modelDirty || (this._instanceDirtyFlags !== 0) || (this._globalTransformProcessStep !== this._globalTransformStep));
  271. };
  272. Prim2DBase.prototype._prepareRender = function (context) {
  273. this._prepareRenderPre(context);
  274. this._prepareRenderPost(context);
  275. };
  276. Prim2DBase.prototype._prepareRenderPre = function (context) {
  277. };
  278. Prim2DBase.prototype._prepareRenderPost = function (context) {
  279. // Don't recurse if it's a renderable group, the content will be processed by the group itself
  280. if (this instanceof BABYLON.Group2D) {
  281. var self = this;
  282. if (self.isRenderableGroup) {
  283. return;
  284. }
  285. }
  286. // Check if we need to recurse the prepare to children primitives
  287. // - must have children
  288. // - the global transform of this level have changed, or
  289. // - the visible state of primitive has changed
  290. if (this._children.length > 0 && ((this._globalTransformProcessStep !== this._globalTransformStep) ||
  291. this.checkPropertiesDirty(Prim2DBase.isVisibleProperty.flagId))) {
  292. this._children.forEach(function (c) {
  293. // As usual stop the recursion if we meet a renderable group
  294. if (!(c instanceof BABYLON.Group2D && c.isRenderableGroup)) {
  295. c._prepareRender(context);
  296. }
  297. });
  298. }
  299. // Finally reset the dirty flags as we've processed everything
  300. this._modelDirty = false;
  301. this._instanceDirtyFlags = 0;
  302. };
  303. Prim2DBase.CheckParent = function (parent) {
  304. if (!parent) {
  305. throw new Error("A Primitive needs a valid Parent, it can be any kind of Primitives based types, even the Canvas (with the exception that only Group2D can be direct child of a Canvas if the cache strategy used is TOPLEVELGROUPS)");
  306. }
  307. };
  308. Prim2DBase.prototype.updateGlobalTransVisOf = function (list, recurse) {
  309. for (var _i = 0; _i < list.length; _i++) {
  310. var cur = list[_i];
  311. cur.updateGlobalTransVis(recurse);
  312. }
  313. };
  314. Prim2DBase.prototype.updateGlobalTransVis = function (recurse) {
  315. if (this.isDisposed) {
  316. return;
  317. }
  318. // Check if the parent is synced
  319. if (this._parent && this._parent._globalTransformProcessStep !== this.owner._globalTransformProcessStep) {
  320. this._parent.updateGlobalTransVis(false);
  321. }
  322. // Check if we must update this prim
  323. if (this === this.owner || this._globalTransformProcessStep !== this.owner._globalTransformProcessStep) {
  324. var curVisibleState = this.isVisible;
  325. this.isVisible = (!this._parent || this._parent.isVisible) && this.levelVisible;
  326. // Detect a change of visibility
  327. this._visibilityChanged = (curVisibleState !== undefined) && curVisibleState !== this.isVisible;
  328. // Detect if either the parent or this node changed
  329. var tflags = Prim2DBase.positionProperty.flagId | Prim2DBase.rotationProperty.flagId | Prim2DBase.scaleProperty.flagId;
  330. if (this.isVisible && (this._parent && this._parent._globalTransformStep !== this._parentTransformStep) || this.checkPropertiesDirty(tflags)) {
  331. var rot = BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(0, 0, 1), this._rotation);
  332. var local = BABYLON.Matrix.Compose(new BABYLON.Vector3(this._scale, this._scale, this._scale), rot, new BABYLON.Vector3(this._position.x, this._position.y, 0));
  333. this._globalTransform = this._parent ? local.multiply(this._parent._globalTransform) : local;
  334. this._invGlobalTransform = BABYLON.Matrix.Invert(this._globalTransform);
  335. this._globalTransformStep = this.owner._globalTransformProcessStep + 1;
  336. this._parentTransformStep = this._parent ? this._parent._globalTransformStep : 0;
  337. this.clearPropertiesDirty(tflags);
  338. }
  339. this._globalTransformProcessStep = this.owner._globalTransformProcessStep;
  340. }
  341. if (recurse) {
  342. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  343. var child = _a[_i];
  344. // Stop the recursion if we meet a renderable group
  345. child.updateGlobalTransVis(!(child instanceof BABYLON.Group2D && child.isRenderableGroup));
  346. }
  347. }
  348. };
  349. Prim2DBase.PRIM2DBASE_PROPCOUNT = 10;
  350. __decorate([
  351. BABYLON.instanceLevelProperty(1, function (pi) { return Prim2DBase.positionProperty = pi; }, false, true)
  352. ], Prim2DBase.prototype, "position", null);
  353. __decorate([
  354. BABYLON.instanceLevelProperty(2, function (pi) { return Prim2DBase.rotationProperty = pi; }, false, true)
  355. ], Prim2DBase.prototype, "rotation", null);
  356. __decorate([
  357. BABYLON.instanceLevelProperty(3, function (pi) { return Prim2DBase.scaleProperty = pi; }, false, true)
  358. ], Prim2DBase.prototype, "scale", null);
  359. __decorate([
  360. BABYLON.instanceLevelProperty(4, function (pi) { return Prim2DBase.originProperty = pi; }, false, true)
  361. ], Prim2DBase.prototype, "origin", null);
  362. __decorate([
  363. BABYLON.dynamicLevelProperty(5, function (pi) { return Prim2DBase.levelVisibleProperty = pi; })
  364. ], Prim2DBase.prototype, "levelVisible", null);
  365. __decorate([
  366. BABYLON.instanceLevelProperty(6, function (pi) { return Prim2DBase.isVisibleProperty = pi; })
  367. ], Prim2DBase.prototype, "isVisible", null);
  368. __decorate([
  369. BABYLON.instanceLevelProperty(7, function (pi) { return Prim2DBase.zOrderProperty = pi; })
  370. ], Prim2DBase.prototype, "zOrder", null);
  371. Prim2DBase = __decorate([
  372. BABYLON.className("Prim2DBase")
  373. ], Prim2DBase);
  374. return Prim2DBase;
  375. })(BABYLON.SmartPropertyPrim);
  376. BABYLON.Prim2DBase = Prim2DBase;
  377. })(BABYLON || (BABYLON = {}));