babylon.shape2d.js 12 KB

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