babylon.shape2d.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 Shape2D = (function (_super) {
  15. __extends(Shape2D, _super);
  16. function Shape2D(settings) {
  17. _super.call(this, settings);
  18. if (!settings) {
  19. settings = {};
  20. }
  21. var borderBrush = null;
  22. if (settings.border) {
  23. if (typeof (settings.border) === "string") {
  24. borderBrush = BABYLON.Canvas2D.GetBrushFromString(settings.border);
  25. }
  26. else {
  27. borderBrush = settings.border;
  28. }
  29. }
  30. var fillBrush = null;
  31. if (settings.fill) {
  32. if (typeof (settings.fill) === "string") {
  33. fillBrush = BABYLON.Canvas2D.GetBrushFromString(settings.fill);
  34. }
  35. else {
  36. fillBrush = settings.fill;
  37. }
  38. }
  39. this._isTransparent = false;
  40. this._oldTransparent = false;
  41. this.border = borderBrush;
  42. this.fill = fillBrush;
  43. this._updateTransparencyStatus();
  44. this.borderThickness = settings.borderThickness;
  45. }
  46. Object.defineProperty(Shape2D.prototype, "border", {
  47. get: function () {
  48. return this._border;
  49. },
  50. set: function (value) {
  51. this._border = value;
  52. this._updateTransparencyStatus();
  53. },
  54. enumerable: true,
  55. configurable: true
  56. });
  57. Object.defineProperty(Shape2D.prototype, "fill", {
  58. /**
  59. * Get/set the brush to render the Fill part of the Primitive
  60. */
  61. get: function () {
  62. return this._fill;
  63. },
  64. set: function (value) {
  65. this._fill = value;
  66. this._updateTransparencyStatus();
  67. },
  68. enumerable: true,
  69. configurable: true
  70. });
  71. Object.defineProperty(Shape2D.prototype, "borderThickness", {
  72. get: function () {
  73. return this._borderThickness;
  74. },
  75. set: function (value) {
  76. this._borderThickness = value;
  77. },
  78. enumerable: true,
  79. configurable: true
  80. });
  81. Shape2D.prototype.getUsedShaderCategories = function (dataPart) {
  82. var cat = _super.prototype.getUsedShaderCategories.call(this, dataPart);
  83. // Fill Part
  84. if (dataPart.id === Shape2D.SHAPE2D_FILLPARTID) {
  85. var fill = this.fill;
  86. if (fill instanceof BABYLON.SolidColorBrush2D) {
  87. cat.push(Shape2D.SHAPE2D_CATEGORY_FILLSOLID);
  88. }
  89. if (fill instanceof BABYLON.GradientColorBrush2D) {
  90. cat.push(Shape2D.SHAPE2D_CATEGORY_FILLGRADIENT);
  91. }
  92. }
  93. // Border Part
  94. if (dataPart.id === Shape2D.SHAPE2D_BORDERPARTID) {
  95. cat.push(Shape2D.SHAPE2D_CATEGORY_BORDER);
  96. var border = this.border;
  97. if (border instanceof BABYLON.SolidColorBrush2D) {
  98. cat.push(Shape2D.SHAPE2D_CATEGORY_BORDERSOLID);
  99. }
  100. if (border instanceof BABYLON.GradientColorBrush2D) {
  101. cat.push(Shape2D.SHAPE2D_CATEGORY_BORDERGRADIENT);
  102. }
  103. }
  104. return cat;
  105. };
  106. Shape2D.prototype.applyActualScaleOnTransform = function () {
  107. return false;
  108. };
  109. Shape2D.prototype.refreshInstanceDataPart = function (part) {
  110. if (!_super.prototype.refreshInstanceDataPart.call(this, part)) {
  111. return false;
  112. }
  113. // Fill Part
  114. if (part.id === Shape2D.SHAPE2D_FILLPARTID) {
  115. var d = part;
  116. if (this.fill) {
  117. var fill = this.fill;
  118. if (fill instanceof BABYLON.SolidColorBrush2D) {
  119. d.fillSolidColor = fill.color;
  120. }
  121. else if (fill instanceof BABYLON.GradientColorBrush2D) {
  122. d.fillGradientColor1 = fill.color1;
  123. d.fillGradientColor2 = fill.color2;
  124. var t = BABYLON.Matrix.Compose(new BABYLON.Vector3(fill.scale, fill.scale, fill.scale), BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(0, 0, 1), fill.rotation), new BABYLON.Vector3(fill.translation.x, fill.translation.y, 0));
  125. var ty = new BABYLON.Vector4(t.m[1], t.m[5], t.m[9], t.m[13]);
  126. d.fillGradientTY = ty;
  127. }
  128. }
  129. }
  130. else if (part.id === Shape2D.SHAPE2D_BORDERPARTID) {
  131. var d = part;
  132. if (this.border) {
  133. d.borderThickness = this.borderThickness;
  134. var border = this.border;
  135. if (border instanceof BABYLON.SolidColorBrush2D) {
  136. d.borderSolidColor = border.color;
  137. }
  138. else if (border instanceof BABYLON.GradientColorBrush2D) {
  139. d.borderGradientColor1 = border.color1;
  140. d.borderGradientColor2 = border.color2;
  141. var t = BABYLON.Matrix.Compose(new BABYLON.Vector3(border.scale, border.scale, border.scale), BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(0, 0, 1), border.rotation), new BABYLON.Vector3(border.translation.x, border.translation.y, 0));
  142. var ty = new BABYLON.Vector4(t.m[1], t.m[5], t.m[9], t.m[13]);
  143. d.borderGradientTY = ty;
  144. }
  145. }
  146. }
  147. return true;
  148. };
  149. Shape2D.prototype._updateTransparencyStatus = function () {
  150. this._isTransparent = (this._border && this._border.isTransparent()) || (this._fill && this._fill.isTransparent()) || (this.actualOpacity < 1);
  151. if (this._isTransparent !== this._oldTransparent) {
  152. this._oldTransparent = this._isTransparent;
  153. this._updateRenderMode();
  154. }
  155. };
  156. Shape2D.prototype._mustUpdateInstance = function () {
  157. var res = this._oldTransparent !== this._isTransparent;
  158. if (res) {
  159. this._updateRenderMode();
  160. this._oldTransparent = this._isTransparent;
  161. }
  162. return res;
  163. };
  164. Shape2D.prototype._isPrimTransparent = function () {
  165. return this._isTransparent;
  166. };
  167. Shape2D.SHAPE2D_BORDERPARTID = 1;
  168. Shape2D.SHAPE2D_FILLPARTID = 2;
  169. Shape2D.SHAPE2D_CATEGORY_BORDER = "Border";
  170. Shape2D.SHAPE2D_CATEGORY_BORDERSOLID = "BorderSolid";
  171. Shape2D.SHAPE2D_CATEGORY_BORDERGRADIENT = "BorderGradient";
  172. Shape2D.SHAPE2D_CATEGORY_FILLSOLID = "FillSolid";
  173. Shape2D.SHAPE2D_CATEGORY_FILLGRADIENT = "FillGradient";
  174. Shape2D.SHAPE2D_PROPCOUNT = BABYLON.RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 5;
  175. __decorate([
  176. BABYLON.modelLevelProperty(BABYLON.RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 1, function (pi) { return Shape2D.borderProperty = pi; }, true)
  177. ], Shape2D.prototype, "border", null);
  178. __decorate([
  179. BABYLON.modelLevelProperty(BABYLON.RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 2, function (pi) { return Shape2D.fillProperty = pi; }, true)
  180. ], Shape2D.prototype, "fill", null);
  181. __decorate([
  182. BABYLON.instanceLevelProperty(BABYLON.RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT + 3, function (pi) { return Shape2D.borderThicknessProperty = pi; })
  183. ], Shape2D.prototype, "borderThickness", null);
  184. Shape2D = __decorate([
  185. BABYLON.className("Shape2D", "BABYLON")
  186. ], Shape2D);
  187. return Shape2D;
  188. }(BABYLON.RenderablePrim2D));
  189. BABYLON.Shape2D = Shape2D;
  190. var Shape2DInstanceData = (function (_super) {
  191. __extends(Shape2DInstanceData, _super);
  192. function Shape2DInstanceData() {
  193. _super.apply(this, arguments);
  194. }
  195. Object.defineProperty(Shape2DInstanceData.prototype, "fillSolidColor", {
  196. // FILL ATTRIBUTES
  197. get: function () {
  198. return null;
  199. },
  200. set: function (value) {
  201. },
  202. enumerable: true,
  203. configurable: true
  204. });
  205. Object.defineProperty(Shape2DInstanceData.prototype, "fillGradientColor1", {
  206. get: function () {
  207. return null;
  208. },
  209. set: function (value) {
  210. },
  211. enumerable: true,
  212. configurable: true
  213. });
  214. Object.defineProperty(Shape2DInstanceData.prototype, "fillGradientColor2", {
  215. get: function () {
  216. return null;
  217. },
  218. set: function (value) {
  219. },
  220. enumerable: true,
  221. configurable: true
  222. });
  223. Object.defineProperty(Shape2DInstanceData.prototype, "fillGradientTY", {
  224. get: function () {
  225. return null;
  226. },
  227. set: function (value) {
  228. },
  229. enumerable: true,
  230. configurable: true
  231. });
  232. Object.defineProperty(Shape2DInstanceData.prototype, "borderThickness", {
  233. // BORDER ATTRIBUTES
  234. get: function () {
  235. return null;
  236. },
  237. set: function (value) {
  238. },
  239. enumerable: true,
  240. configurable: true
  241. });
  242. Object.defineProperty(Shape2DInstanceData.prototype, "borderSolidColor", {
  243. get: function () {
  244. return null;
  245. },
  246. set: function (value) {
  247. },
  248. enumerable: true,
  249. configurable: true
  250. });
  251. Object.defineProperty(Shape2DInstanceData.prototype, "borderGradientColor1", {
  252. get: function () {
  253. return null;
  254. },
  255. set: function (value) {
  256. },
  257. enumerable: true,
  258. configurable: true
  259. });
  260. Object.defineProperty(Shape2DInstanceData.prototype, "borderGradientColor2", {
  261. get: function () {
  262. return null;
  263. },
  264. set: function (value) {
  265. },
  266. enumerable: true,
  267. configurable: true
  268. });
  269. Object.defineProperty(Shape2DInstanceData.prototype, "borderGradientTY", {
  270. get: function () {
  271. return null;
  272. },
  273. set: function (value) {
  274. },
  275. enumerable: true,
  276. configurable: true
  277. });
  278. __decorate([
  279. BABYLON.instanceData(Shape2D.SHAPE2D_CATEGORY_FILLSOLID)
  280. ], Shape2DInstanceData.prototype, "fillSolidColor", null);
  281. __decorate([
  282. BABYLON.instanceData(Shape2D.SHAPE2D_CATEGORY_FILLGRADIENT)
  283. ], Shape2DInstanceData.prototype, "fillGradientColor1", null);
  284. __decorate([
  285. BABYLON.instanceData(Shape2D.SHAPE2D_CATEGORY_FILLGRADIENT)
  286. ], Shape2DInstanceData.prototype, "fillGradientColor2", null);
  287. __decorate([
  288. BABYLON.instanceData(Shape2D.SHAPE2D_CATEGORY_FILLGRADIENT)
  289. ], Shape2DInstanceData.prototype, "fillGradientTY", null);
  290. __decorate([
  291. BABYLON.instanceData(Shape2D.SHAPE2D_CATEGORY_BORDER)
  292. ], Shape2DInstanceData.prototype, "borderThickness", null);
  293. __decorate([
  294. BABYLON.instanceData(Shape2D.SHAPE2D_CATEGORY_BORDERSOLID)
  295. ], Shape2DInstanceData.prototype, "borderSolidColor", null);
  296. __decorate([
  297. BABYLON.instanceData(Shape2D.SHAPE2D_CATEGORY_BORDERGRADIENT)
  298. ], Shape2DInstanceData.prototype, "borderGradientColor1", null);
  299. __decorate([
  300. BABYLON.instanceData(Shape2D.SHAPE2D_CATEGORY_BORDERGRADIENT)
  301. ], Shape2DInstanceData.prototype, "borderGradientColor2", null);
  302. __decorate([
  303. BABYLON.instanceData(Shape2D.SHAPE2D_CATEGORY_BORDERGRADIENT)
  304. ], Shape2DInstanceData.prototype, "borderGradientTY", null);
  305. return Shape2DInstanceData;
  306. }(BABYLON.InstanceDataBase));
  307. BABYLON.Shape2DInstanceData = Shape2DInstanceData;
  308. })(BABYLON || (BABYLON = {}));