babylon.rectangle2d.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Rectangle2DRenderCache = (function (_super) {
  15. __extends(Rectangle2DRenderCache, _super);
  16. function Rectangle2DRenderCache(modelKey, isTransparent) {
  17. _super.call(this, modelKey, isTransparent);
  18. }
  19. Rectangle2DRenderCache.prototype.render = function (instanceInfo, context) {
  20. // Do nothing if the shader is still loading/preparing
  21. if ((this.effectFill && !this.effectFill.isReady()) || (this.effectBorder && !this.effectBorder.isReady())) {
  22. return false;
  23. }
  24. var engine = instanceInfo._owner.owner.engine;
  25. var depthFunction = 0;
  26. if (this.effectFill && this.effectBorder) {
  27. depthFunction = engine.getDepthFunction();
  28. engine.setDepthFunctionToLessOrEqual();
  29. }
  30. var cur;
  31. if (this.isTransparent) {
  32. cur = engine.getAlphaMode();
  33. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  34. }
  35. if (this.effectFill) {
  36. var partIndex = instanceInfo._partIndexFromId.get(BABYLON.Shape2D.SHAPE2D_FILLPARTID.toString());
  37. engine.enableEffect(this.effectFill);
  38. engine.bindBuffers(this.fillVB, this.fillIB, [1], 4, this.effectFill);
  39. var count = instanceInfo._instancesPartsData[partIndex].usedElementCount;
  40. if (instanceInfo._owner.owner.supportInstancedArray) {
  41. if (!this.instancingFillAttributes) {
  42. // Compute the offset locations of the attributes in the vertexshader that will be mapped to the instance buffer data
  43. this.instancingFillAttributes = this.loadInstancingAttributes(BABYLON.Shape2D.SHAPE2D_FILLPARTID, this.effectFill);
  44. }
  45. engine.updateAndBindInstancesBuffer(instanceInfo._instancesPartsBuffer[partIndex], null, this.instancingFillAttributes);
  46. engine.draw(true, 0, this.fillIndicesCount, count);
  47. engine.unBindInstancesBuffer(instanceInfo._instancesPartsBuffer[partIndex], this.instancingFillAttributes);
  48. }
  49. else {
  50. for (var i = 0; i < count; i++) {
  51. this.setupUniforms(this.effectFill, partIndex, instanceInfo._instancesPartsData[partIndex], i);
  52. engine.draw(true, 0, this.fillIndicesCount);
  53. }
  54. }
  55. }
  56. if (this.effectBorder) {
  57. var partIndex = instanceInfo._partIndexFromId.get(BABYLON.Shape2D.SHAPE2D_BORDERPARTID.toString());
  58. engine.enableEffect(this.effectBorder);
  59. engine.bindBuffers(this.borderVB, this.borderIB, [1], 4, this.effectBorder);
  60. var count = instanceInfo._instancesPartsData[partIndex].usedElementCount;
  61. if (instanceInfo._owner.owner.supportInstancedArray) {
  62. if (!this.instancingBorderAttributes) {
  63. this.instancingBorderAttributes = this.loadInstancingAttributes(BABYLON.Shape2D.SHAPE2D_BORDERPARTID, this.effectBorder);
  64. }
  65. engine.updateAndBindInstancesBuffer(instanceInfo._instancesPartsBuffer[partIndex], null, this.instancingBorderAttributes);
  66. engine.draw(true, 0, this.borderIndicesCount, count);
  67. engine.unBindInstancesBuffer(instanceInfo._instancesPartsBuffer[partIndex], this.instancingBorderAttributes);
  68. }
  69. else {
  70. for (var i = 0; i < count; i++) {
  71. this.setupUniforms(this.effectBorder, partIndex, instanceInfo._instancesPartsData[partIndex], i);
  72. engine.draw(true, 0, this.borderIndicesCount);
  73. }
  74. }
  75. }
  76. if (this.isTransparent) {
  77. engine.setAlphaMode(cur);
  78. }
  79. if (this.effectFill && this.effectBorder) {
  80. engine.setDepthFunction(depthFunction);
  81. }
  82. return true;
  83. };
  84. return Rectangle2DRenderCache;
  85. })(BABYLON.ModelRenderCache);
  86. BABYLON.Rectangle2DRenderCache = Rectangle2DRenderCache;
  87. var Rectangle2DInstanceData = (function (_super) {
  88. __extends(Rectangle2DInstanceData, _super);
  89. function Rectangle2DInstanceData(partId) {
  90. _super.call(this, partId, 1);
  91. }
  92. Object.defineProperty(Rectangle2DInstanceData.prototype, "properties", {
  93. get: function () {
  94. return null;
  95. },
  96. enumerable: true,
  97. configurable: true
  98. });
  99. __decorate([
  100. BABYLON.instanceData()
  101. ], Rectangle2DInstanceData.prototype, "properties", null);
  102. return Rectangle2DInstanceData;
  103. })(BABYLON.Shape2DInstanceData);
  104. BABYLON.Rectangle2DInstanceData = Rectangle2DInstanceData;
  105. var Rectangle2D = (function (_super) {
  106. __extends(Rectangle2D, _super);
  107. function Rectangle2D() {
  108. _super.apply(this, arguments);
  109. }
  110. Object.defineProperty(Rectangle2D.prototype, "size", {
  111. get: function () {
  112. return this._size;
  113. },
  114. set: function (value) {
  115. this._size = value;
  116. },
  117. enumerable: true,
  118. configurable: true
  119. });
  120. Object.defineProperty(Rectangle2D.prototype, "notRounded", {
  121. get: function () {
  122. return this._notRounded;
  123. },
  124. set: function (value) {
  125. this._notRounded = value;
  126. },
  127. enumerable: true,
  128. configurable: true
  129. });
  130. Object.defineProperty(Rectangle2D.prototype, "roundRadius", {
  131. get: function () {
  132. return this._roundRadius;
  133. },
  134. set: function (value) {
  135. this._roundRadius = value;
  136. this.notRounded = value === 0;
  137. },
  138. enumerable: true,
  139. configurable: true
  140. });
  141. Rectangle2D.prototype.updateLevelBoundingInfo = function () {
  142. BABYLON.BoundingInfo2D.CreateFromSizeToRef(this.size, this._levelBoundingInfo);
  143. };
  144. Rectangle2D.prototype.setupRectangle2D = function (owner, parent, id, position, size, roundRadius, fill, border, borderThickness) {
  145. if (roundRadius === void 0) { roundRadius = 0; }
  146. if (borderThickness === void 0) { borderThickness = 1; }
  147. this.setupShape2D(owner, parent, id, position, true, fill, border, borderThickness);
  148. this.size = size;
  149. this.notRounded = !roundRadius;
  150. this.roundRadius = roundRadius;
  151. };
  152. Rectangle2D.Create = function (parent, id, x, y, width, height, fill, border) {
  153. BABYLON.Prim2DBase.CheckParent(parent);
  154. var rect = new Rectangle2D();
  155. rect.setupRectangle2D(parent.owner, parent, id, new BABYLON.Vector2(x, y), new BABYLON.Size(width, height), null);
  156. rect.fill = fill;
  157. rect.border = border;
  158. return rect;
  159. };
  160. Rectangle2D.CreateRounded = function (parent, id, x, y, width, height, roundRadius, fill, border) {
  161. if (roundRadius === void 0) { roundRadius = 0; }
  162. BABYLON.Prim2DBase.CheckParent(parent);
  163. var rect = new Rectangle2D();
  164. rect.setupRectangle2D(parent.owner, parent, id, new BABYLON.Vector2(x, y), new BABYLON.Size(width, height), roundRadius);
  165. rect.fill = fill || BABYLON.Canvas2D.GetSolidColorBrushFromHex("#FFFFFFFF");
  166. rect.border = border;
  167. return rect;
  168. };
  169. Rectangle2D.prototype.createModelRenderCache = function (modelKey, isTransparent) {
  170. var renderCache = new Rectangle2DRenderCache(modelKey, isTransparent);
  171. return renderCache;
  172. };
  173. Rectangle2D.prototype.setupModelRenderCache = function (modelRenderCache) {
  174. var renderCache = modelRenderCache;
  175. var engine = this.owner.engine;
  176. // Need to create webgl resources for fill part?
  177. if (this.fill) {
  178. var vbSize = ((this.notRounded ? 1 : Rectangle2D.roundSubdivisions) * 4) + 1;
  179. var vb = new Float32Array(vbSize);
  180. for (var i = 0; i < vbSize; i++) {
  181. vb[i] = i;
  182. }
  183. renderCache.fillVB = engine.createVertexBuffer(vb);
  184. var triCount = vbSize - 1;
  185. var ib = new Float32Array(triCount * 3);
  186. for (var i = 0; i < triCount; i++) {
  187. ib[i * 3 + 0] = 0;
  188. ib[i * 3 + 2] = i + 1;
  189. ib[i * 3 + 1] = i + 2;
  190. }
  191. ib[triCount * 3 - 2] = 1;
  192. renderCache.fillIB = engine.createIndexBuffer(ib);
  193. renderCache.fillIndicesCount = triCount * 3;
  194. var ei = this.getDataPartEffectInfo(BABYLON.Shape2D.SHAPE2D_FILLPARTID, ["index"]);
  195. renderCache.effectFill = engine.createEffect({ vertex: "rect2d", fragment: "rect2d" }, ei.attributes, ei.uniforms, [], ei.defines, null, function (e) {
  196. // renderCache.setupUniformsLocation(e, ei.uniforms, Shape2D.SHAPE2D_FILLPARTID);
  197. });
  198. }
  199. // Need to create webgl resource for border part?
  200. if (this.border) {
  201. var vbSize = (this.notRounded ? 1 : Rectangle2D.roundSubdivisions) * 4 * 2;
  202. var vb = new Float32Array(vbSize);
  203. for (var i = 0; i < vbSize; i++) {
  204. vb[i] = i;
  205. }
  206. renderCache.borderVB = engine.createVertexBuffer(vb);
  207. var triCount = vbSize;
  208. var rs = triCount / 2;
  209. var ib = new Float32Array(triCount * 3);
  210. for (var i = 0; i < rs; i++) {
  211. var r0 = i;
  212. var r1 = (i + 1) % rs;
  213. ib[i * 6 + 0] = rs + r1;
  214. ib[i * 6 + 1] = rs + r0;
  215. ib[i * 6 + 2] = r0;
  216. ib[i * 6 + 3] = r1;
  217. ib[i * 6 + 4] = rs + r1;
  218. ib[i * 6 + 5] = r0;
  219. }
  220. renderCache.borderIB = engine.createIndexBuffer(ib);
  221. renderCache.borderIndicesCount = triCount * 3;
  222. var ei = this.getDataPartEffectInfo(BABYLON.Shape2D.SHAPE2D_BORDERPARTID, ["index"]);
  223. renderCache.effectBorder = engine.createEffect({ vertex: "rect2d", fragment: "rect2d" }, ei.attributes, ei.uniforms, [], ei.defines, null, function (e) {
  224. // renderCache.setupUniformsLocation(e, ei.uniforms, Shape2D.SHAPE2D_BORDERPARTID);
  225. });
  226. }
  227. return renderCache;
  228. };
  229. Rectangle2D.prototype.createInstanceDataParts = function () {
  230. var res = new Array();
  231. if (this.border) {
  232. res.push(new Rectangle2DInstanceData(BABYLON.Shape2D.SHAPE2D_BORDERPARTID));
  233. }
  234. if (this.fill) {
  235. res.push(new Rectangle2DInstanceData(BABYLON.Shape2D.SHAPE2D_FILLPARTID));
  236. }
  237. return res;
  238. };
  239. Rectangle2D.prototype.refreshInstanceDataPart = function (part) {
  240. if (!_super.prototype.refreshInstanceDataPart.call(this, part)) {
  241. return false;
  242. }
  243. if (part.id === BABYLON.Shape2D.SHAPE2D_BORDERPARTID) {
  244. var d = part;
  245. var size = this.size;
  246. d.properties = new BABYLON.Vector3(size.width, size.height, this.roundRadius || 0);
  247. }
  248. else if (part.id === BABYLON.Shape2D.SHAPE2D_FILLPARTID) {
  249. var d = part;
  250. var size = this.size;
  251. d.properties = new BABYLON.Vector3(size.width, size.height, this.roundRadius || 0);
  252. }
  253. return true;
  254. };
  255. Rectangle2D.roundSubdivisions = 16;
  256. __decorate([
  257. BABYLON.instanceLevelProperty(BABYLON.Shape2D.SHAPE2D_PROPCOUNT + 1, function (pi) { return Rectangle2D.sizeProperty = pi; }, false, true)
  258. ], Rectangle2D.prototype, "size", null);
  259. __decorate([
  260. BABYLON.modelLevelProperty(BABYLON.Shape2D.SHAPE2D_PROPCOUNT + 2, function (pi) { return Rectangle2D.notRoundedProperty = pi; })
  261. ], Rectangle2D.prototype, "notRounded", null);
  262. __decorate([
  263. BABYLON.instanceLevelProperty(BABYLON.Shape2D.SHAPE2D_PROPCOUNT + 3, function (pi) { return Rectangle2D.roundRadiusProperty = pi; })
  264. ], Rectangle2D.prototype, "roundRadius", null);
  265. Rectangle2D = __decorate([
  266. BABYLON.className("Rectangle2D")
  267. ], Rectangle2D);
  268. return Rectangle2D;
  269. })(BABYLON.Shape2D);
  270. BABYLON.Rectangle2D = Rectangle2D;
  271. })(BABYLON || (BABYLON = {}));