babylon.modelRenderCache.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 BABYLON;
  7. (function (BABYLON) {
  8. var GroupInstanceInfo = (function () {
  9. function GroupInstanceInfo(owner, mrc, partCount) {
  10. this._partCount = partCount;
  11. this.owner = owner;
  12. this.modelRenderCache = mrc;
  13. this.modelRenderCache.addRef();
  14. this.partIndexFromId = new BABYLON.StringDictionary();
  15. this._usedShaderCategories = new Array(partCount);
  16. this._strides = new Array(partCount);
  17. this._opaqueData = null;
  18. this._alphaTestData = null;
  19. this._transparentData = null;
  20. this.opaqueDirty = this.alphaTestDirty = this.transparentDirty = this.transparentOrderDirty = false;
  21. }
  22. GroupInstanceInfo.prototype.dispose = function () {
  23. if (this._isDisposed) {
  24. return false;
  25. }
  26. if (this.modelRenderCache) {
  27. this.modelRenderCache.dispose();
  28. this.modelRenderCache = null;
  29. }
  30. var engine = this.owner.owner.engine;
  31. if (this._opaqueData) {
  32. this._opaqueData.forEach(function (d) { return d.dispose(engine); });
  33. this._opaqueData = null;
  34. }
  35. if (this._alphaTestData) {
  36. this._alphaTestData.forEach(function (d) { return d.dispose(engine); });
  37. this._alphaTestData = null;
  38. }
  39. if (this._transparentData) {
  40. this._transparentData.forEach(function (d) { return d.dispose(engine); });
  41. this._transparentData = null;
  42. }
  43. this.partIndexFromId = null;
  44. this._isDisposed = true;
  45. return true;
  46. };
  47. Object.defineProperty(GroupInstanceInfo.prototype, "hasOpaqueData", {
  48. get: function () {
  49. return this._opaqueData != null;
  50. },
  51. enumerable: true,
  52. configurable: true
  53. });
  54. Object.defineProperty(GroupInstanceInfo.prototype, "hasAlphaTestData", {
  55. get: function () {
  56. return this._alphaTestData != null;
  57. },
  58. enumerable: true,
  59. configurable: true
  60. });
  61. Object.defineProperty(GroupInstanceInfo.prototype, "hasTransparentData", {
  62. get: function () {
  63. return this._transparentData != null;
  64. },
  65. enumerable: true,
  66. configurable: true
  67. });
  68. Object.defineProperty(GroupInstanceInfo.prototype, "opaqueData", {
  69. get: function () {
  70. if (!this._opaqueData) {
  71. this._opaqueData = new Array(this._partCount);
  72. for (var i = 0; i < this._partCount; i++) {
  73. this._opaqueData[i] = new GroupInfoPartData(this._strides[i]);
  74. }
  75. }
  76. return this._opaqueData;
  77. },
  78. enumerable: true,
  79. configurable: true
  80. });
  81. Object.defineProperty(GroupInstanceInfo.prototype, "alphaTestData", {
  82. get: function () {
  83. if (!this._alphaTestData) {
  84. this._alphaTestData = new Array(this._partCount);
  85. for (var i = 0; i < this._partCount; i++) {
  86. this._alphaTestData[i] = new GroupInfoPartData(this._strides[i]);
  87. }
  88. }
  89. return this._alphaTestData;
  90. },
  91. enumerable: true,
  92. configurable: true
  93. });
  94. Object.defineProperty(GroupInstanceInfo.prototype, "transparentData", {
  95. get: function () {
  96. if (!this._transparentData) {
  97. this._transparentData = new Array(this._partCount);
  98. for (var i = 0; i < this._partCount; i++) {
  99. var zoff = this.modelRenderCache._partData[i]._zBiasOffset;
  100. this._transparentData[i] = new TransparentGroupInfoPartData(this._strides[i], zoff);
  101. }
  102. }
  103. return this._transparentData;
  104. },
  105. enumerable: true,
  106. configurable: true
  107. });
  108. GroupInstanceInfo.prototype.sortTransparentData = function () {
  109. if (!this.transparentOrderDirty) {
  110. return;
  111. }
  112. for (var i = 0; i < this._transparentData.length; i++) {
  113. var td = this._transparentData[i];
  114. td._partData.sort();
  115. }
  116. this.transparentOrderDirty = false;
  117. };
  118. Object.defineProperty(GroupInstanceInfo.prototype, "usedShaderCategories", {
  119. get: function () {
  120. return this._usedShaderCategories;
  121. },
  122. enumerable: true,
  123. configurable: true
  124. });
  125. Object.defineProperty(GroupInstanceInfo.prototype, "strides", {
  126. get: function () {
  127. return this._strides;
  128. },
  129. enumerable: true,
  130. configurable: true
  131. });
  132. return GroupInstanceInfo;
  133. }());
  134. BABYLON.GroupInstanceInfo = GroupInstanceInfo;
  135. var TransparentSegment = (function () {
  136. function TransparentSegment() {
  137. this.groupInsanceInfo = null;
  138. this.startZ = 0;
  139. this.endZ = 0;
  140. this.startDataIndex = BABYLON.Prim2DBase._bigInt;
  141. this.endDataIndex = 0;
  142. this.partBuffers = null;
  143. }
  144. TransparentSegment.prototype.dispose = function (engine) {
  145. if (this.partBuffers) {
  146. this.partBuffers.forEach(function (b) { return engine._releaseBuffer(b); });
  147. this.partBuffers.splice(0);
  148. this.partBuffers = null;
  149. }
  150. };
  151. return TransparentSegment;
  152. }());
  153. BABYLON.TransparentSegment = TransparentSegment;
  154. var GroupInfoPartData = (function () {
  155. function GroupInfoPartData(stride) {
  156. this._partData = null;
  157. this._partBuffer = null;
  158. this._partBufferSize = 0;
  159. this._partData = new BABYLON.DynamicFloatArray(stride / 4, 50);
  160. this._isDisposed = false;
  161. }
  162. GroupInfoPartData.prototype.dispose = function (engine) {
  163. if (this._isDisposed) {
  164. return false;
  165. }
  166. if (this._partBuffer) {
  167. engine._releaseBuffer(this._partBuffer);
  168. this._partBuffer = null;
  169. }
  170. this._partData = null;
  171. this._isDisposed = true;
  172. };
  173. return GroupInfoPartData;
  174. }());
  175. BABYLON.GroupInfoPartData = GroupInfoPartData;
  176. var TransparentGroupInfoPartData = (function (_super) {
  177. __extends(TransparentGroupInfoPartData, _super);
  178. function TransparentGroupInfoPartData(stride, zoff) {
  179. _super.call(this, stride);
  180. this._partData.compareValueOffset = zoff;
  181. this._partData.sortingAscending = false;
  182. }
  183. return TransparentGroupInfoPartData;
  184. }(GroupInfoPartData));
  185. BABYLON.TransparentGroupInfoPartData = TransparentGroupInfoPartData;
  186. var ModelRenderCache = (function () {
  187. function ModelRenderCache(engine, modelKey) {
  188. this._engine = engine;
  189. this._modelKey = modelKey;
  190. this._nextKey = 1;
  191. this._refCounter = 1;
  192. this._partData = null;
  193. }
  194. ModelRenderCache.prototype.dispose = function () {
  195. if (--this._refCounter !== 0) {
  196. return false;
  197. }
  198. // Remove the Model Render Cache from the global dictionary
  199. var edata = this._engine.getExternalData("__BJSCANVAS2D__");
  200. if (edata) {
  201. edata.DisposeModelRenderCache(this);
  202. }
  203. return true;
  204. };
  205. Object.defineProperty(ModelRenderCache.prototype, "isDisposed", {
  206. get: function () {
  207. return this._refCounter <= 0;
  208. },
  209. enumerable: true,
  210. configurable: true
  211. });
  212. ModelRenderCache.prototype.addRef = function () {
  213. return ++this._refCounter;
  214. };
  215. Object.defineProperty(ModelRenderCache.prototype, "modelKey", {
  216. get: function () {
  217. return this._modelKey;
  218. },
  219. enumerable: true,
  220. configurable: true
  221. });
  222. /**
  223. * Render the model instances
  224. * @param instanceInfo
  225. * @param context
  226. * @return must return true is the rendering succeed, false if the rendering couldn't be done (asset's not yet ready, like Effect)
  227. */
  228. ModelRenderCache.prototype.render = function (instanceInfo, context) {
  229. return true;
  230. };
  231. ModelRenderCache.prototype.getPartIndexFromId = function (partId) {
  232. for (var i = 0; i < this._partData.length; i++) {
  233. if (this._partData[i]._partId === partId) {
  234. return i;
  235. }
  236. }
  237. return null;
  238. };
  239. ModelRenderCache.prototype.loadInstancingAttributes = function (partId, effect) {
  240. var i = this.getPartIndexFromId(partId);
  241. if (i === null) {
  242. return null;
  243. }
  244. var ci = this._partsClassInfo[i];
  245. var categories = this._partData[i]._partUsedCategories;
  246. var res = ci.classContent.getInstancingAttributeInfos(effect, categories);
  247. return res;
  248. };
  249. ModelRenderCache.prototype.setupUniforms = function (effect, partIndex, data, elementCount) {
  250. var pd = this._partData[partIndex];
  251. var offset = (pd._partDataStride / 4) * elementCount;
  252. var pci = this._partsClassInfo[partIndex];
  253. var self = this;
  254. pci.fullContent.forEach(function (k, v) {
  255. if (!v.category || pd._partUsedCategories.indexOf(v.category) !== -1) {
  256. switch (v.dataType) {
  257. case 4 /* float */:
  258. {
  259. var attribOffset = v.instanceOffset.get(pd._partJoinedUsedCategories);
  260. effect.setFloat(v.attributeName, data.buffer[offset + attribOffset]);
  261. break;
  262. }
  263. case 0 /* Vector2 */:
  264. {
  265. var attribOffset = v.instanceOffset.get(pd._partJoinedUsedCategories);
  266. ModelRenderCache.v2.x = data.buffer[offset + attribOffset + 0];
  267. ModelRenderCache.v2.y = data.buffer[offset + attribOffset + 1];
  268. effect.setVector2(v.attributeName, ModelRenderCache.v2);
  269. break;
  270. }
  271. case 5 /* Color3 */:
  272. case 1 /* Vector3 */:
  273. {
  274. var attribOffset = v.instanceOffset.get(pd._partJoinedUsedCategories);
  275. ModelRenderCache.v3.x = data.buffer[offset + attribOffset + 0];
  276. ModelRenderCache.v3.y = data.buffer[offset + attribOffset + 1];
  277. ModelRenderCache.v3.z = data.buffer[offset + attribOffset + 2];
  278. effect.setVector3(v.attributeName, ModelRenderCache.v3);
  279. break;
  280. }
  281. case 6 /* Color4 */:
  282. case 2 /* Vector4 */:
  283. {
  284. var attribOffset = v.instanceOffset.get(pd._partJoinedUsedCategories);
  285. ModelRenderCache.v4.x = data.buffer[offset + attribOffset + 0];
  286. ModelRenderCache.v4.y = data.buffer[offset + attribOffset + 1];
  287. ModelRenderCache.v4.z = data.buffer[offset + attribOffset + 2];
  288. ModelRenderCache.v4.w = data.buffer[offset + attribOffset + 3];
  289. effect.setVector4(v.attributeName, ModelRenderCache.v4);
  290. break;
  291. }
  292. default:
  293. }
  294. }
  295. });
  296. };
  297. //setupUniformsLocation(effect: Effect, uniforms: string[], partId: number) {
  298. // let i = this.getPartIndexFromId(partId);
  299. // if (i === null) {
  300. // return null;
  301. // }
  302. // let pci = this._partsClassInfo[i];
  303. // pci.fullContent.forEach((k, v) => {
  304. // if (uniforms.indexOf(v.attributeName) !== -1) {
  305. // v.uniformLocation = effect.getUniform(v.attributeName);
  306. // }
  307. // });
  308. //}
  309. ModelRenderCache.v2 = BABYLON.Vector2.Zero();
  310. ModelRenderCache.v3 = BABYLON.Vector3.Zero();
  311. ModelRenderCache.v4 = BABYLON.Vector4.Zero();
  312. return ModelRenderCache;
  313. }());
  314. BABYLON.ModelRenderCache = ModelRenderCache;
  315. var ModelRenderCachePartData = (function () {
  316. function ModelRenderCachePartData() {
  317. }
  318. return ModelRenderCachePartData;
  319. }());
  320. BABYLON.ModelRenderCachePartData = ModelRenderCachePartData;
  321. })(BABYLON || (BABYLON = {}));