babylon.renderablePrim2d.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 InstanceClassInfo = (function () {
  15. function InstanceClassInfo(base) {
  16. this._baseInfo = base;
  17. this._nextOffset = new BABYLON.StringDictionary();
  18. this._attributes = new Array();
  19. }
  20. InstanceClassInfo.prototype.mapProperty = function (propInfo, push) {
  21. var curOff = this._nextOffset.getOrAdd(InstanceClassInfo._CurCategories, 0);
  22. propInfo.instanceOffset.add(InstanceClassInfo._CurCategories, this._getBaseOffset(InstanceClassInfo._CurCategories) + curOff);
  23. //console.log(`[${InstanceClassInfo._CurCategories}] New PropInfo. Category: ${propInfo.category}, Name: ${propInfo.attributeName}, Offset: ${propInfo.instanceOffset.get(InstanceClassInfo._CurCategories)}, Size: ${propInfo.size / 4}`);
  24. this._nextOffset.set(InstanceClassInfo._CurCategories, curOff + (propInfo.size / 4));
  25. if (push) {
  26. this._attributes.push(propInfo);
  27. }
  28. };
  29. InstanceClassInfo.prototype.getInstancingAttributeInfos = function (effect, categories) {
  30. var catInline = ";" + categories.join(";") + ";";
  31. var res = new Array();
  32. var curInfo = this;
  33. while (curInfo) {
  34. for (var _i = 0, _a = curInfo._attributes; _i < _a.length; _i++) {
  35. var attrib = _a[_i];
  36. // Only map if there's no category assigned to the instance data or if there's a category and it's in the given list
  37. if (!attrib.category || categories.indexOf(attrib.category) !== -1) {
  38. var index = effect.getAttributeLocationByName(attrib.attributeName);
  39. var iai = new BABYLON.InstancingAttributeInfo();
  40. iai.index = index;
  41. iai.attributeSize = attrib.size / 4; // attrib.size is in byte and we need to store in "component" (i.e float is 1, vec3 is 3)
  42. iai.offset = attrib.instanceOffset.get(catInline) * 4; // attrib.instanceOffset is in float, iai.offset must be in bytes
  43. iai.attributeName = attrib.attributeName;
  44. res.push(iai);
  45. }
  46. }
  47. curInfo = curInfo._baseInfo;
  48. }
  49. return res;
  50. };
  51. InstanceClassInfo.prototype.getShaderAttributes = function (categories) {
  52. var res = new Array();
  53. var curInfo = this;
  54. while (curInfo) {
  55. for (var _i = 0, _a = curInfo._attributes; _i < _a.length; _i++) {
  56. var attrib = _a[_i];
  57. // Only map if there's no category assigned to the instance data or if there's a category and it's in the given list
  58. if (!attrib.category || categories.indexOf(attrib.category) !== -1) {
  59. res.push(attrib.attributeName);
  60. }
  61. }
  62. curInfo = curInfo._baseInfo;
  63. }
  64. return res;
  65. };
  66. InstanceClassInfo.prototype._getBaseOffset = function (categories) {
  67. var curOffset = 0;
  68. var curBase = this._baseInfo;
  69. while (curBase) {
  70. curOffset += curBase._nextOffset.getOrAdd(categories, 0);
  71. curBase = curBase._baseInfo;
  72. }
  73. return curOffset;
  74. };
  75. return InstanceClassInfo;
  76. }());
  77. BABYLON.InstanceClassInfo = InstanceClassInfo;
  78. var InstancePropInfo = (function () {
  79. function InstancePropInfo() {
  80. this.instanceOffset = new BABYLON.StringDictionary();
  81. }
  82. InstancePropInfo.prototype.setSize = function (val) {
  83. if (val instanceof BABYLON.Vector2) {
  84. this.size = 8;
  85. this.dataType = 0 /* Vector2 */;
  86. return;
  87. }
  88. if (val instanceof BABYLON.Vector3) {
  89. this.size = 12;
  90. this.dataType = 1 /* Vector3 */;
  91. return;
  92. }
  93. if (val instanceof BABYLON.Vector4) {
  94. this.size = 16;
  95. this.dataType = 2 /* Vector4 */;
  96. return;
  97. }
  98. if (val instanceof BABYLON.Matrix) {
  99. throw new Error("Matrix type is not supported by WebGL Instance Buffer, you have to use four Vector4 properties instead");
  100. }
  101. if (typeof (val) === "number") {
  102. this.size = 4;
  103. this.dataType = 4 /* float */;
  104. return;
  105. }
  106. if (val instanceof BABYLON.Color3) {
  107. this.size = 12;
  108. this.dataType = 5 /* Color3 */;
  109. return;
  110. }
  111. if (val instanceof BABYLON.Color4) {
  112. this.size = 16;
  113. this.dataType = 6 /* Color4 */;
  114. return;
  115. }
  116. if (val instanceof BABYLON.Size) {
  117. this.size = 8;
  118. this.dataType = 7 /* Size */;
  119. return;
  120. }
  121. return;
  122. };
  123. InstancePropInfo.prototype.writeData = function (array, offset, val) {
  124. switch (this.dataType) {
  125. case 0 /* Vector2 */:
  126. {
  127. var v = val;
  128. array[offset + 0] = v.x;
  129. array[offset + 1] = v.y;
  130. break;
  131. }
  132. case 1 /* Vector3 */:
  133. {
  134. var v = val;
  135. array[offset + 0] = v.x;
  136. array[offset + 1] = v.y;
  137. array[offset + 2] = v.z;
  138. break;
  139. }
  140. case 2 /* Vector4 */:
  141. {
  142. var v = val;
  143. array[offset + 0] = v.x;
  144. array[offset + 1] = v.y;
  145. array[offset + 2] = v.z;
  146. array[offset + 3] = v.w;
  147. break;
  148. }
  149. case 5 /* Color3 */:
  150. {
  151. var v = val;
  152. array[offset + 0] = v.r;
  153. array[offset + 1] = v.g;
  154. array[offset + 2] = v.b;
  155. break;
  156. }
  157. case 6 /* Color4 */:
  158. {
  159. var v = val;
  160. array[offset + 0] = v.r;
  161. array[offset + 1] = v.g;
  162. array[offset + 2] = v.b;
  163. array[offset + 3] = v.a;
  164. break;
  165. }
  166. case 4 /* float */:
  167. {
  168. var v = val;
  169. array[offset] = v;
  170. break;
  171. }
  172. case 3 /* Matrix */:
  173. {
  174. var v = val;
  175. for (var i = 0; i < 16; i++) {
  176. array[offset + i] = v.m[i];
  177. }
  178. break;
  179. }
  180. case 7 /* Size */:
  181. {
  182. var s = val;
  183. array[offset + 0] = s.width;
  184. array[offset + 1] = s.height;
  185. break;
  186. }
  187. }
  188. };
  189. return InstancePropInfo;
  190. }());
  191. BABYLON.InstancePropInfo = InstancePropInfo;
  192. function instanceData(category, shaderAttributeName) {
  193. return function (target, propName, descriptor) {
  194. var dic = BABYLON.ClassTreeInfo.getOrRegister(target, function (base) { return new InstanceClassInfo(base); });
  195. var node = dic.getLevelOf(target);
  196. var instanceDataName = propName;
  197. shaderAttributeName = shaderAttributeName || instanceDataName;
  198. var info = node.levelContent.get(instanceDataName);
  199. if (info) {
  200. throw new Error("The ID " + instanceDataName + " is already taken by another instance data");
  201. }
  202. info = new InstancePropInfo();
  203. info.attributeName = shaderAttributeName;
  204. info.category = category || null;
  205. if (info.category) {
  206. info.delimitedCategory = ";" + info.category + ";";
  207. }
  208. node.levelContent.add(instanceDataName, info);
  209. descriptor.get = function () {
  210. return null;
  211. };
  212. descriptor.set = function (val) {
  213. // Check that we're not trying to set a property that belongs to a category that is not allowed (current)
  214. // Quit if it's the case, otherwise we could overwrite data somewhere...
  215. if (info.category && InstanceClassInfo._CurCategories.indexOf(info.delimitedCategory) === -1) {
  216. return;
  217. }
  218. if (!info.size) {
  219. info.setSize(val);
  220. node.classContent.mapProperty(info, true);
  221. }
  222. else if (!info.instanceOffset.contains(InstanceClassInfo._CurCategories)) {
  223. node.classContent.mapProperty(info, false);
  224. }
  225. var obj = this;
  226. if (obj.dataBuffer && obj.dataElements) {
  227. var offset = obj.dataElements[obj.curElement].offset + info.instanceOffset.get(InstanceClassInfo._CurCategories);
  228. info.writeData(obj.dataBuffer.buffer, offset, val);
  229. }
  230. };
  231. };
  232. }
  233. BABYLON.instanceData = instanceData;
  234. var InstanceDataBase = (function () {
  235. function InstanceDataBase(partId, dataElementCount) {
  236. this.id = partId;
  237. this.curElement = 0;
  238. this.dataElementCount = dataElementCount;
  239. }
  240. Object.defineProperty(InstanceDataBase.prototype, "zBias", {
  241. get: function () {
  242. return null;
  243. },
  244. enumerable: true,
  245. configurable: true
  246. });
  247. Object.defineProperty(InstanceDataBase.prototype, "transformX", {
  248. get: function () {
  249. return null;
  250. },
  251. enumerable: true,
  252. configurable: true
  253. });
  254. Object.defineProperty(InstanceDataBase.prototype, "transformY", {
  255. get: function () {
  256. return null;
  257. },
  258. enumerable: true,
  259. configurable: true
  260. });
  261. Object.defineProperty(InstanceDataBase.prototype, "origin", {
  262. get: function () {
  263. return null;
  264. },
  265. enumerable: true,
  266. configurable: true
  267. });
  268. InstanceDataBase.prototype.getClassTreeInfo = function () {
  269. if (!this.typeInfo) {
  270. this.typeInfo = BABYLON.ClassTreeInfo.get(Object.getPrototypeOf(this));
  271. }
  272. return this.typeInfo;
  273. };
  274. InstanceDataBase.prototype.allocElements = function () {
  275. if (!this.dataBuffer) {
  276. return;
  277. }
  278. var res = new Array(this.dataElementCount);
  279. for (var i = 0; i < this.dataElementCount; i++) {
  280. res[i] = this.dataBuffer.allocElement();
  281. }
  282. this.dataElements = res;
  283. };
  284. InstanceDataBase.prototype.freeElements = function () {
  285. if (!this.dataElements) {
  286. return;
  287. }
  288. for (var _i = 0, _a = this.dataElements; _i < _a.length; _i++) {
  289. var ei = _a[_i];
  290. this.dataBuffer.freeElement(ei);
  291. }
  292. this.dataElements = null;
  293. };
  294. Object.defineProperty(InstanceDataBase.prototype, "dataElementCount", {
  295. get: function () {
  296. return this._dataElementCount;
  297. },
  298. set: function (value) {
  299. if (value === this._dataElementCount) {
  300. return;
  301. }
  302. this.freeElements();
  303. this._dataElementCount = value;
  304. this.allocElements();
  305. },
  306. enumerable: true,
  307. configurable: true
  308. });
  309. __decorate([
  310. instanceData()
  311. ], InstanceDataBase.prototype, "zBias", null);
  312. __decorate([
  313. instanceData()
  314. ], InstanceDataBase.prototype, "transformX", null);
  315. __decorate([
  316. instanceData()
  317. ], InstanceDataBase.prototype, "transformY", null);
  318. __decorate([
  319. instanceData()
  320. ], InstanceDataBase.prototype, "origin", null);
  321. return InstanceDataBase;
  322. }());
  323. BABYLON.InstanceDataBase = InstanceDataBase;
  324. var RenderablePrim2D = (function (_super) {
  325. __extends(RenderablePrim2D, _super);
  326. function RenderablePrim2D() {
  327. _super.apply(this, arguments);
  328. }
  329. Object.defineProperty(RenderablePrim2D.prototype, "isTransparent", {
  330. get: function () {
  331. return this._isTransparent;
  332. },
  333. set: function (value) {
  334. this._isTransparent = value;
  335. },
  336. enumerable: true,
  337. configurable: true
  338. });
  339. RenderablePrim2D.prototype.setupRenderablePrim2D = function (owner, parent, id, position, origin, isVisible) {
  340. this.setupPrim2DBase(owner, parent, id, position, origin);
  341. this._isTransparent = false;
  342. };
  343. RenderablePrim2D.prototype.dispose = function () {
  344. if (!_super.prototype.dispose.call(this)) {
  345. return false;
  346. }
  347. if (this._modelRenderInstanceID) {
  348. this._modelRenderCache.removeInstanceData(this._modelRenderInstanceID);
  349. this._modelRenderInstanceID = null;
  350. }
  351. if (this._modelRenderCache) {
  352. this._modelRenderCache.dispose();
  353. this._modelRenderCache = null;
  354. }
  355. if (this._instanceDataParts) {
  356. this._instanceDataParts.forEach(function (p) {
  357. p.freeElements();
  358. });
  359. this._instanceDataParts = null;
  360. }
  361. return true;
  362. };
  363. RenderablePrim2D.prototype._prepareRenderPre = function (context) {
  364. var _this = this;
  365. _super.prototype._prepareRenderPre.call(this, context);
  366. // If the model changed and we have already an instance, we must remove this instance from the obsolete model
  367. if (this._modelDirty && this._modelRenderInstanceID) {
  368. this._modelRenderCache.removeInstanceData(this._modelRenderInstanceID);
  369. this._modelRenderInstanceID = null;
  370. }
  371. // Need to create the model?
  372. var setupModelRenderCache = false;
  373. if (!this._modelRenderCache || this._modelDirty) {
  374. if (this._modelRenderCache) {
  375. this._modelRenderCache.dispose();
  376. }
  377. this._modelRenderCache = this.owner._engineData.GetOrAddModelCache(this.modelKey, function (key) {
  378. var mrc = _this.createModelRenderCache(key, _this.isTransparent);
  379. setupModelRenderCache = true;
  380. return mrc;
  381. });
  382. this._modelDirty = false;
  383. // if this is still false it means the MRC already exists, so we add a reference to it
  384. if (!setupModelRenderCache) {
  385. this._modelRenderCache.addRef();
  386. }
  387. }
  388. var gii;
  389. var newInstance = false;
  390. // Need to create the instance data parts?
  391. if (!this._modelRenderInstanceID) {
  392. // Yes, flag it for later, more processing will have to be done
  393. newInstance = true;
  394. // Create the instance data parts of the primitive and store them
  395. var parts = this.createInstanceDataParts();
  396. this._instanceDataParts = parts;
  397. // Check if the ModelRenderCache for this particular instance is also brand new, initialize it if it's the case
  398. if (!this._modelRenderCache._partsDataStride) {
  399. var ctiArray = new Array();
  400. var dataStrides = new Array();
  401. var usedCatList = new Array();
  402. var partIdList = new Array();
  403. var joinedUsedCatList = new Array();
  404. for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
  405. var dataPart = parts_1[_i];
  406. var cat = this.getUsedShaderCategories(dataPart);
  407. var cti = dataPart.getClassTreeInfo();
  408. // Make sure the instance is visible other the properties won't be set and their size/offset wont be computed
  409. var curVisible = this.isVisible;
  410. this.isVisible = true;
  411. // We manually trigger refreshInstanceData for the only sake of evaluating each instance property size and offset in the instance data, this can only be made at runtime. Once it's done we have all the information to create the instance data buffer.
  412. //console.log("Build Prop Layout for " + Tools.getClassName(this._instanceDataParts[0]));
  413. var joinCat = ";" + cat.join(";") + ";";
  414. joinedUsedCatList.push(joinCat);
  415. InstanceClassInfo._CurCategories = joinCat;
  416. var obj = this.beforeRefreshForLayoutConstruction(dataPart);
  417. this.refreshInstanceDataPart(dataPart);
  418. this.afterRefreshForLayoutConstruction(dataPart, obj);
  419. this.isVisible = curVisible;
  420. var size = 0;
  421. cti.fullContent.forEach(function (k, v) {
  422. if (!v.category || cat.indexOf(v.category) !== -1) {
  423. if (!v.size) {
  424. console.log("ERROR: Couldn't detect the size of the Property " + v.attributeName + " from type " + BABYLON.Tools.getClassName(cti.type) + ". Property is ignored.");
  425. }
  426. else {
  427. size += v.size;
  428. }
  429. }
  430. });
  431. dataStrides.push(size);
  432. usedCatList.push(cat);
  433. ctiArray.push(cti);
  434. partIdList.push(dataPart.id);
  435. }
  436. this._modelRenderCache._partsDataStride = dataStrides;
  437. this._modelRenderCache._partsUsedCategories = usedCatList;
  438. this._modelRenderCache._partsJoinedUsedCategories = joinedUsedCatList;
  439. this._modelRenderCache._partsClassInfo = ctiArray;
  440. this._modelRenderCache._partIdList = partIdList;
  441. }
  442. // The Rendering resources (Effect, VB, IB, Textures) are stored in the ModelRenderCache
  443. // But it's the RenderGroup that will store all the Instanced related data to render all the primitive it owns.
  444. // So for a given ModelKey we getOrAdd a GroupInstanceInfo that will store all these data
  445. gii = this.renderGroup._renderGroupInstancesInfo.getOrAddWithFactory(this.modelKey, function (k) { return new BABYLON.GroupInstanceInfo(_this.renderGroup, _this._modelRenderCache); });
  446. // First time init of the GroupInstanceInfo
  447. if (gii._instancesPartsData.length === 0) {
  448. for (var j = 0; j < this._modelRenderCache._partsDataStride.length; j++) {
  449. var stride = this._modelRenderCache._partsDataStride[j];
  450. gii._instancesPartsData.push(new BABYLON.DynamicFloatArray(stride / 4, 50));
  451. gii._partIndexFromId.add(this._modelRenderCache._partIdList[j].toString(), j);
  452. for (var _a = 0, _b = this._instanceDataParts; _a < _b.length; _a++) {
  453. var part = _b[_a];
  454. gii._instancesPartsUsedShaderCategories[gii._partIndexFromId.get(part.id.toString())] = ";" + this.getUsedShaderCategories(part).join(";") + ";";
  455. }
  456. }
  457. }
  458. // For each instance data part of the primitive, allocate the instanced element it needs for render
  459. for (var i = 0; i < parts.length; i++) {
  460. var part = parts[i];
  461. part.dataBuffer = gii._instancesPartsData[i];
  462. part.allocElements();
  463. }
  464. // Add the instance data parts in the ModelRenderCache they belong, track them by storing their ID in the primitive in case we need to change the model later on, so we'll have to release the allocated instance data parts because they won't fit anymore
  465. this._modelRenderInstanceID = this._modelRenderCache.addInstanceDataParts(this._instanceDataParts);
  466. }
  467. // If the ModelRenderCache is brand new, now is the time to call the implementation's specific setup method to create the rendering resources
  468. if (setupModelRenderCache) {
  469. this.setupModelRenderCache(this._modelRenderCache);
  470. }
  471. // At this stage we have everything correctly initialized, ModelRenderCache is setup, Model Instance data are good too, they have allocated elements in the Instanced DynamicFloatArray.
  472. // The last thing to do is check if the instanced related data must be updated because a InstanceLevel property had changed or the primitive visibility changed.
  473. if (this._visibilityChanged || context.forceRefreshPrimitive || newInstance || (this._instanceDirtyFlags !== 0) || (this._globalTransformProcessStep !== this._globalTransformStep)) {
  474. // Fetch the GroupInstanceInfo if we don't already have it
  475. if (!gii) {
  476. gii = this.renderGroup._renderGroupInstancesInfo.get(this.modelKey);
  477. }
  478. // For each Instance Data part, refresh it to update the data in the DynamicFloatArray
  479. for (var _c = 0, _d = this._instanceDataParts; _c < _d.length; _c++) {
  480. var part = _d[_c];
  481. // Check if we need to allocate data elements (hidden prim which becomes visible again)
  482. if (this._visibilityChanged && !part.dataElements) {
  483. part.allocElements();
  484. }
  485. InstanceClassInfo._CurCategories = gii._instancesPartsUsedShaderCategories[gii._partIndexFromId.get(part.id.toString())];
  486. // Will return false if the instance should not be rendered (not visible or other any reasons)
  487. if (!this.refreshInstanceDataPart(part)) {
  488. // Free the data element
  489. if (part.dataElements) {
  490. part.freeElements();
  491. }
  492. }
  493. }
  494. this._instanceDirtyFlags = 0;
  495. gii._dirtyInstancesData = true;
  496. this._visibilityChanged = false; // Reset the flag as we've handled the case
  497. }
  498. };
  499. /**
  500. * Transform a given point using the Primitive's origin setting.
  501. * This method requires the Primitive's actualSize to be accurate
  502. * @param p the point to transform
  503. * @param originOffset an offset applied on the current origin before performing the transformation. Depending on which frame of reference your data is expressed you may have to apply a offset. (if you data is expressed from the bottom/left, no offset is required. If it's expressed from the center the a [-0.5;-0.5] offset has to be applied.
  504. * @param res an allocated Vector2 that will receive the transformed content
  505. */
  506. RenderablePrim2D.prototype.transformPointWithOriginByRef = function (p, originOffset, res) {
  507. var actualSize = this.actualSize;
  508. res.x = p.x - ((this.origin.x + (originOffset ? originOffset.x : 0)) * actualSize.width);
  509. res.y = p.y - ((this.origin.y + (originOffset ? originOffset.y : 0)) * actualSize.height);
  510. };
  511. RenderablePrim2D.prototype.transformPointWithOrigin = function (p, originOffset) {
  512. var res = new BABYLON.Vector2(0, 0);
  513. this.transformPointWithOriginByRef(p, originOffset, res);
  514. return res;
  515. };
  516. RenderablePrim2D.prototype.getDataPartEffectInfo = function (dataPartId, vertexBufferAttributes) {
  517. var dataPart = BABYLON.Tools.first(this._instanceDataParts, function (i) { return i.id === dataPartId; });
  518. if (!dataPart) {
  519. return null;
  520. }
  521. var instancedArray = this.owner.supportInstancedArray;
  522. var cti = dataPart.getClassTreeInfo();
  523. var categories = this.getUsedShaderCategories(dataPart);
  524. var att = cti.classContent.getShaderAttributes(categories);
  525. var defines = "";
  526. categories.forEach(function (c) { defines += "#define " + c + "\n"; });
  527. if (instancedArray) {
  528. defines += "#define Instanced\n";
  529. }
  530. return { attributes: instancedArray ? vertexBufferAttributes.concat(att) : vertexBufferAttributes, uniforms: instancedArray ? [] : att, defines: defines };
  531. };
  532. Object.defineProperty(RenderablePrim2D.prototype, "modelRenderCache", {
  533. get: function () {
  534. return this._modelRenderCache;
  535. },
  536. enumerable: true,
  537. configurable: true
  538. });
  539. RenderablePrim2D.prototype.createModelRenderCache = function (modelKey, isTransparent) {
  540. return null;
  541. };
  542. RenderablePrim2D.prototype.setupModelRenderCache = function (modelRenderCache) {
  543. };
  544. RenderablePrim2D.prototype.createInstanceDataParts = function () {
  545. return null;
  546. };
  547. RenderablePrim2D.prototype.getUsedShaderCategories = function (dataPart) {
  548. return [];
  549. };
  550. RenderablePrim2D.prototype.beforeRefreshForLayoutConstruction = function (part) {
  551. };
  552. RenderablePrim2D.prototype.afterRefreshForLayoutConstruction = function (part, obj) {
  553. };
  554. RenderablePrim2D.prototype.refreshInstanceDataPart = function (part) {
  555. if (!this.isVisible) {
  556. return false;
  557. }
  558. part.isVisible = this.isVisible;
  559. // Which means, if there's only one data element, we're update it from this method, otherwise it is the responsibility of the derived class to call updateInstanceDataPart as many times as needed, properly (look at Text2D's implementation for more information)
  560. if (part.dataElementCount === 1) {
  561. part.curElement = 0;
  562. this.updateInstanceDataPart(part);
  563. }
  564. return true;
  565. };
  566. /**
  567. * Update the instanceDataBase level properties of a part
  568. * @param part the part to update
  569. * @param positionOffset to use in multi part per primitive (e.g. the Text2D has N parts for N letter to display), this give the offset to apply (e.g. the position of the letter from the bottom/left corner of the text). You MUST also set customSize.
  570. * @param customSize to use in multi part per primitive, this is the size of the overall primitive to display (the bounding rect's size of the Text, for instance). This is mandatory to compute correct transformation based on the Primitive's origin property.
  571. */
  572. RenderablePrim2D.prototype.updateInstanceDataPart = function (part, positionOffset, customSize) {
  573. if (positionOffset === void 0) { positionOffset = null; }
  574. if (customSize === void 0) { customSize = null; }
  575. var t = this._globalTransform.multiply(this.renderGroup.invGlobalTransform);
  576. var size = this.renderGroup.viewportSize;
  577. var zBias = this.getActualZOffset();
  578. var offX = 0;
  579. var offY = 0;
  580. // If there's an offset, apply the global transformation matrix on it to get a global offset
  581. if (positionOffset && customSize) {
  582. offX = (positionOffset.x - (customSize.width * this.origin.x)) * t.m[0] + (positionOffset.y - (customSize.height * this.origin.y)) * t.m[4];
  583. offY = (positionOffset.x - (customSize.width * this.origin.x)) * t.m[1] + (positionOffset.y - (customSize.height * this.origin.y)) * t.m[5];
  584. }
  585. // Have to convert the coordinates to clip space which is ranged between [-1;1] on X and Y axis, with 0,0 being the left/bottom corner
  586. // Current coordinates are expressed in renderGroup coordinates ([0, renderGroup.actualSize.width|height]) with 0,0 being at the left/top corner
  587. // RenderGroup Width and Height are multiplied by zBias because the VertexShader will multiply X and Y by W, which is 1/zBias. As we divide our coordinate by these Width/Height, we will also divide by the zBias to compensate the operation made by the VertexShader.
  588. // So for X:
  589. // - tx.x = value * 2 / width: is to switch from [0, renderGroup.width] to [0, 2]
  590. // - tx.w = (value * 2 / width) - 1: w stores the translation in renderGroup coordinates so (value * 2 / width) to switch to a clip space translation value. - 1 is to offset the overall [0;2] to [-1;1]. Don't forget it's -(1/zBias) and not -1 because everything need to be scaled by 1/zBias.
  591. var w = size.width * zBias;
  592. var h = size.height * zBias;
  593. var invZBias = 1 / zBias;
  594. var tx = new BABYLON.Vector4(t.m[0] * 2 / w, t.m[4] * 2 / w, 0 /*t.m[8]*/, ((t.m[12] + offX) * 2 / w) - (invZBias));
  595. var ty = new BABYLON.Vector4(t.m[1] * 2 / h, t.m[5] * 2 / h, 0 /*t.m[9]*/, ((t.m[13] + offY) * 2 / h) - (invZBias));
  596. part.transformX = tx;
  597. part.transformY = ty;
  598. part.origin = this.origin;
  599. // Stores zBias and it's inverse value because that's needed to compute the clip space W coordinate (which is 1/Z, so 1/zBias)
  600. part.zBias = new BABYLON.Vector2(zBias, invZBias);
  601. };
  602. RenderablePrim2D.RENDERABLEPRIM2D_PROPCOUNT = BABYLON.Prim2DBase.PRIM2DBASE_PROPCOUNT + 5;
  603. __decorate([
  604. BABYLON.modelLevelProperty(BABYLON.Prim2DBase.PRIM2DBASE_PROPCOUNT + 1, function (pi) { return RenderablePrim2D.isTransparentProperty = pi; })
  605. ], RenderablePrim2D.prototype, "isTransparent", null);
  606. RenderablePrim2D = __decorate([
  607. BABYLON.className("RenderablePrim2D")
  608. ], RenderablePrim2D);
  609. return RenderablePrim2D;
  610. }(BABYLON.Prim2DBase));
  611. BABYLON.RenderablePrim2D = RenderablePrim2D;
  612. })(BABYLON || (BABYLON = {}));