babylon.prim2dBase.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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._parentTranformStep = 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. get: function () {
  122. return this._origin;
  123. },
  124. set: function (value) {
  125. this._origin = value;
  126. },
  127. enumerable: true,
  128. configurable: true
  129. });
  130. Object.defineProperty(Prim2DBase.prototype, "levelVisible", {
  131. get: function () {
  132. return this._levelVisible;
  133. },
  134. set: function (value) {
  135. this._levelVisible = value;
  136. },
  137. enumerable: true,
  138. configurable: true
  139. });
  140. Object.defineProperty(Prim2DBase.prototype, "isVisible", {
  141. get: function () {
  142. return this._isVisible;
  143. },
  144. set: function (value) {
  145. this._isVisible = value;
  146. },
  147. enumerable: true,
  148. configurable: true
  149. });
  150. Object.defineProperty(Prim2DBase.prototype, "zOrder", {
  151. get: function () {
  152. return this._zOrder;
  153. },
  154. set: function (value) {
  155. this._zOrder = value;
  156. },
  157. enumerable: true,
  158. configurable: true
  159. });
  160. Object.defineProperty(Prim2DBase.prototype, "hierarchyDepth", {
  161. get: function () {
  162. return this._hierarchyDepth;
  163. },
  164. enumerable: true,
  165. configurable: true
  166. });
  167. Object.defineProperty(Prim2DBase.prototype, "renderGroup", {
  168. get: function () {
  169. return this._renderGroup;
  170. },
  171. enumerable: true,
  172. configurable: true
  173. });
  174. Object.defineProperty(Prim2DBase.prototype, "globalTransform", {
  175. get: function () {
  176. return this._globalTransform;
  177. },
  178. enumerable: true,
  179. configurable: true
  180. });
  181. Object.defineProperty(Prim2DBase.prototype, "invGlobalTransform", {
  182. get: function () {
  183. return this._invGlobalTransform;
  184. },
  185. enumerable: true,
  186. configurable: true
  187. });
  188. Object.defineProperty(Prim2DBase.prototype, "boundingInfo", {
  189. get: function () {
  190. if (this._boundingInfoDirty) {
  191. this._boundingInfo = this.levelBoundingInfo.clone();
  192. var bi = this._boundingInfo;
  193. var localTransform = new BABYLON.Matrix();
  194. if (this.parent) {
  195. this.globalTransform.multiplyToRef(BABYLON.Matrix.Invert(this.parent.globalTransform), localTransform);
  196. }
  197. else {
  198. localTransform = this.globalTransform;
  199. }
  200. var invLocalTransform = BABYLON.Matrix.Invert(localTransform);
  201. this.levelBoundingInfo.transformToRef(localTransform, bi);
  202. var tps = new BABYLON.BoundingInfo2D();
  203. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  204. var curChild = _a[_i];
  205. curChild.boundingInfo.transformToRef(curChild.globalTransform.multiply(invLocalTransform), tps);
  206. bi.unionToRef(tps, bi);
  207. }
  208. this._boundingInfoDirty = false;
  209. }
  210. return this._boundingInfo;
  211. },
  212. enumerable: true,
  213. configurable: true
  214. });
  215. Prim2DBase.prototype.moveChild = function (child, previous) {
  216. if (child.parent !== this) {
  217. return false;
  218. }
  219. var prevOffset, nextOffset;
  220. var childIndex = this._children.indexOf(child);
  221. var prevIndex = previous ? this._children.indexOf(previous) : -1;
  222. // Move to first position
  223. if (!previous) {
  224. prevOffset = 0;
  225. nextOffset = this._children[1]._siblingDepthOffset;
  226. }
  227. else {
  228. prevOffset = this._children[prevIndex]._siblingDepthOffset;
  229. nextOffset = this._children[prevIndex + 1]._siblingDepthOffset;
  230. }
  231. child._siblingDepthOffset = (nextOffset - prevOffset) / 2;
  232. this._children.splice(prevIndex + 1, 0, this._children.splice(childIndex, 1)[0]);
  233. };
  234. Prim2DBase.prototype.addChild = function (child) {
  235. child._siblingDepthOffset = (this._children.length + 1) * this.owner.hierarchySiblingZDelta;
  236. this._children.push(child);
  237. };
  238. Prim2DBase.prototype.getActualZOffset = function () {
  239. return this._zOrder || this._siblingDepthOffset;
  240. };
  241. Prim2DBase.prototype.onPrimBecomesDirty = function () {
  242. if (this._renderGroup) {
  243. //if (this instanceof Group2D) {
  244. // var group: any= this;
  245. // if (group.isRenderableGroup) {
  246. // return;
  247. // }
  248. //}
  249. this._renderGroup._addPrimToDirtyList(this);
  250. }
  251. };
  252. Prim2DBase.prototype.needPrepare = function () {
  253. return this._modelDirty || (this._instanceDirtyFlags !== 0) || (this._globalTransformPreviousStep !== this._globalTransformStep);
  254. };
  255. Prim2DBase.prototype._buildChildContext = function (context) {
  256. var childContext = new Render2DContext();
  257. childContext.camera = context.camera;
  258. childContext.parentVisibleState = context.parentVisibleState && this.levelVisible;
  259. childContext.parentTransform = this._globalTransform;
  260. childContext.parentTransformStep = this._globalTransformStep;
  261. childContext.forceRefreshPrimitive = context.forceRefreshPrimitive;
  262. return childContext;
  263. };
  264. Prim2DBase.prototype._prepareRender = function (context) {
  265. this._prepareRenderPre(context);
  266. this._prepareRenderPost(context);
  267. };
  268. Prim2DBase.prototype._prepareRenderPre = function (context) {
  269. };
  270. Prim2DBase.prototype._prepareRenderPost = function (context) {
  271. // Don't recurse if it's a renderable group, the content will be processed by the group itself
  272. if (this instanceof BABYLON.Group2D) {
  273. var self = this;
  274. if (self.isRenderableGroup) {
  275. return;
  276. }
  277. }
  278. // Check if we need to recurse the prepare to children primitives
  279. // - must have children
  280. // - the global transform of this level have changed, or
  281. // - the visible state of primitive has changed
  282. if (this._children.length > 0 && ((this._globalTransformPreviousStep !== this._globalTransformStep) ||
  283. this.checkPropertiesDirty(Prim2DBase.isVisibleProperty.flagId))) {
  284. var childContext = this._buildChildContext(context);
  285. this._children.forEach(function (c) {
  286. // As usual stop the recursion if we meet a renderable group
  287. if (!(c instanceof BABYLON.Group2D && c.isRenderableGroup)) {
  288. c._prepareRender(childContext);
  289. }
  290. });
  291. }
  292. // Finally reset the dirty flags as we've processed everything
  293. this._modelDirty = false;
  294. this._instanceDirtyFlags = 0;
  295. };
  296. Prim2DBase.CheckParent = function (parent) {
  297. if (!parent) {
  298. 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)");
  299. }
  300. };
  301. Prim2DBase.prototype.updateGlobalTransVisOf = function (list, context, recurse) {
  302. for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
  303. var cur = list_1[_i];
  304. cur.updateGlobalTransVis(context, recurse);
  305. }
  306. };
  307. Prim2DBase.prototype.updateGlobalTransVis = function (context, recurse) {
  308. this._globalTransformPreviousStep = this._globalTransformStep;
  309. this.isVisible = context.parentVisibleState && this.levelVisible;
  310. // Detect if nothing changed
  311. var tflags = Prim2DBase.positionProperty.flagId | Prim2DBase.rotationProperty.flagId | Prim2DBase.scaleProperty.flagId;
  312. if ((context.parentTransformStep === this._parentTranformStep) && !this.checkPropertiesDirty(tflags)) {
  313. return;
  314. }
  315. var rot = BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(0, 0, 1), this._rotation);
  316. 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));
  317. this._globalTransform = context.parentTransform.multiply(local);
  318. this._invGlobalTransform = BABYLON.Matrix.Invert(this._globalTransform);
  319. ++this._globalTransformStep;
  320. this._parentTranformStep = context.parentTransformStep;
  321. this.clearPropertiesDirty(tflags);
  322. if (recurse) {
  323. var childrenContext = this._buildChildContext(context);
  324. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  325. var child = _a[_i];
  326. // Stop the recursion if we meet a renderable group
  327. child.updateGlobalTransVis(childrenContext, !(child instanceof BABYLON.Group2D && child.isRenderableGroup));
  328. }
  329. }
  330. };
  331. Prim2DBase.PRIM2DBASE_PROPCOUNT = 10;
  332. __decorate([
  333. BABYLON.instanceLevelProperty(1, function (pi) { return Prim2DBase.positionProperty = pi; }, false, true)
  334. ], Prim2DBase.prototype, "position", null);
  335. __decorate([
  336. BABYLON.instanceLevelProperty(2, function (pi) { return Prim2DBase.rotationProperty = pi; }, false, true)
  337. ], Prim2DBase.prototype, "rotation", null);
  338. __decorate([
  339. BABYLON.instanceLevelProperty(3, function (pi) { return Prim2DBase.scaleProperty = pi; }, false, true)
  340. ], Prim2DBase.prototype, "scale", null);
  341. __decorate([
  342. BABYLON.instanceLevelProperty(4, function (pi) { return Prim2DBase.originProperty = pi; }, false, true)
  343. ], Prim2DBase.prototype, "origin", null);
  344. __decorate([
  345. BABYLON.dynamicLevelProperty(5, function (pi) { return Prim2DBase.levelVisibleProperty = pi; })
  346. ], Prim2DBase.prototype, "levelVisible", null);
  347. __decorate([
  348. BABYLON.instanceLevelProperty(6, function (pi) { return Prim2DBase.isVisibleProperty = pi; })
  349. ], Prim2DBase.prototype, "isVisible", null);
  350. __decorate([
  351. BABYLON.instanceLevelProperty(7, function (pi) { return Prim2DBase.zOrderProperty = pi; })
  352. ], Prim2DBase.prototype, "zOrder", null);
  353. Prim2DBase = __decorate([
  354. BABYLON.className("Prim2DBase")
  355. ], Prim2DBase);
  356. return Prim2DBase;
  357. }(BABYLON.SmartPropertyPrim));
  358. BABYLON.Prim2DBase = Prim2DBase;
  359. })(BABYLON || (BABYLON = {}));
  360. //# sourceMappingURL=babylon.prim2dBase.js.map