babylon.prim2dBase.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.getActualZOffset = function () {
  242. return this._zOrder || 1 - (this._siblingDepthOffset + this._hierarchyDepthOffset);
  243. };
  244. Prim2DBase.prototype.onPrimBecomesDirty = function () {
  245. if (this._renderGroup) {
  246. this._renderGroup._addPrimToDirtyList(this);
  247. }
  248. };
  249. Prim2DBase.prototype.needPrepare = function () {
  250. return this._modelDirty || (this._instanceDirtyFlags !== 0) || (this._globalTransformProcessStep !== this._globalTransformStep);
  251. };
  252. Prim2DBase.prototype._prepareRender = function (context) {
  253. this._prepareRenderPre(context);
  254. this._prepareRenderPost(context);
  255. };
  256. Prim2DBase.prototype._prepareRenderPre = function (context) {
  257. };
  258. Prim2DBase.prototype._prepareRenderPost = function (context) {
  259. // Don't recurse if it's a renderable group, the content will be processed by the group itself
  260. if (this instanceof BABYLON.Group2D) {
  261. var self = this;
  262. if (self.isRenderableGroup) {
  263. return;
  264. }
  265. }
  266. // Check if we need to recurse the prepare to children primitives
  267. // - must have children
  268. // - the global transform of this level have changed, or
  269. // - the visible state of primitive has changed
  270. if (this._children.length > 0 && ((this._globalTransformProcessStep !== this._globalTransformStep) ||
  271. this.checkPropertiesDirty(Prim2DBase.isVisibleProperty.flagId))) {
  272. this._children.forEach(function (c) {
  273. // As usual stop the recursion if we meet a renderable group
  274. if (!(c instanceof BABYLON.Group2D && c.isRenderableGroup)) {
  275. c._prepareRender(context);
  276. }
  277. });
  278. }
  279. // Finally reset the dirty flags as we've processed everything
  280. this._modelDirty = false;
  281. this._instanceDirtyFlags = 0;
  282. };
  283. Prim2DBase.CheckParent = function (parent) {
  284. if (!parent) {
  285. 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)");
  286. }
  287. };
  288. Prim2DBase.prototype.updateGlobalTransVisOf = function (list, recurse) {
  289. for (var _i = 0; _i < list.length; _i++) {
  290. var cur = list[_i];
  291. cur.updateGlobalTransVis(recurse);
  292. }
  293. };
  294. Prim2DBase.prototype.updateGlobalTransVis = function (recurse) {
  295. // Check if the parent is synced
  296. if (this._parent && this._parent._globalTransformProcessStep !== this.owner._globalTransformProcessStep) {
  297. this._parent.updateGlobalTransVis(false);
  298. }
  299. // Check if we must update this prim
  300. if (this === this.owner || this._globalTransformProcessStep !== this.owner._globalTransformProcessStep) {
  301. this.isVisible = (!this._parent || this._parent.isVisible) && this.levelVisible;
  302. // Detect if either the parent or this node changed
  303. var tflags = Prim2DBase.positionProperty.flagId | Prim2DBase.rotationProperty.flagId | Prim2DBase.scaleProperty.flagId;
  304. if ((this._parent && this._parent._globalTransformStep !== this._parentTransformStep) || this.checkPropertiesDirty(tflags)) {
  305. var rot = BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(0, 0, 1), this._rotation);
  306. 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));
  307. this._globalTransform = this._parent ? local.multiply(this._parent._globalTransform) : local;
  308. this._invGlobalTransform = BABYLON.Matrix.Invert(this._globalTransform);
  309. this._globalTransformStep = this.owner._globalTransformProcessStep + 1;
  310. this._parentTransformStep = this._parent ? this._parent._globalTransformStep : 0;
  311. this.clearPropertiesDirty(tflags);
  312. }
  313. this._globalTransformProcessStep = this.owner._globalTransformProcessStep;
  314. }
  315. if (recurse) {
  316. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  317. var child = _a[_i];
  318. // Stop the recursion if we meet a renderable group
  319. child.updateGlobalTransVis(!(child instanceof BABYLON.Group2D && child.isRenderableGroup));
  320. }
  321. }
  322. };
  323. Prim2DBase.PRIM2DBASE_PROPCOUNT = 10;
  324. __decorate([
  325. BABYLON.instanceLevelProperty(1, function (pi) { return Prim2DBase.positionProperty = pi; }, false, true)
  326. ], Prim2DBase.prototype, "position", null);
  327. __decorate([
  328. BABYLON.instanceLevelProperty(2, function (pi) { return Prim2DBase.rotationProperty = pi; }, false, true)
  329. ], Prim2DBase.prototype, "rotation", null);
  330. __decorate([
  331. BABYLON.instanceLevelProperty(3, function (pi) { return Prim2DBase.scaleProperty = pi; }, false, true)
  332. ], Prim2DBase.prototype, "scale", null);
  333. __decorate([
  334. BABYLON.instanceLevelProperty(4, function (pi) { return Prim2DBase.originProperty = pi; }, false, true)
  335. ], Prim2DBase.prototype, "origin", null);
  336. __decorate([
  337. BABYLON.dynamicLevelProperty(5, function (pi) { return Prim2DBase.levelVisibleProperty = pi; })
  338. ], Prim2DBase.prototype, "levelVisible", null);
  339. __decorate([
  340. BABYLON.instanceLevelProperty(6, function (pi) { return Prim2DBase.isVisibleProperty = pi; })
  341. ], Prim2DBase.prototype, "isVisible", null);
  342. __decorate([
  343. BABYLON.instanceLevelProperty(7, function (pi) { return Prim2DBase.zOrderProperty = pi; })
  344. ], Prim2DBase.prototype, "zOrder", null);
  345. Prim2DBase = __decorate([
  346. BABYLON.className("Prim2DBase")
  347. ], Prim2DBase);
  348. return Prim2DBase;
  349. })(BABYLON.SmartPropertyPrim);
  350. BABYLON.Prim2DBase = Prim2DBase;
  351. })(BABYLON || (BABYLON = {}));