babylon.renderTargetTexture.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 RenderTargetTexture = (function (_super) {
  9. __extends(RenderTargetTexture, _super);
  10. function RenderTargetTexture(name, size, scene, generateMipMaps, doNotChangeAspectRatio, type, isCube) {
  11. if (doNotChangeAspectRatio === void 0) { doNotChangeAspectRatio = true; }
  12. if (type === void 0) { type = BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT; }
  13. if (isCube === void 0) { isCube = false; }
  14. _super.call(this, null, scene, !generateMipMaps);
  15. this.isCube = isCube;
  16. this.renderList = new Array();
  17. this.renderParticles = true;
  18. this.renderSprites = false;
  19. this.coordinatesMode = BABYLON.Texture.PROJECTION_MODE;
  20. // Events
  21. /**
  22. * An event triggered when the texture is unbind.
  23. * @type {BABYLON.Observable}
  24. */
  25. this.onAfterUnbindObservable = new BABYLON.Observable();
  26. /**
  27. * An event triggered before rendering the texture
  28. * @type {BABYLON.Observable}
  29. */
  30. this.onBeforeRenderObservable = new BABYLON.Observable();
  31. /**
  32. * An event triggered after rendering the texture
  33. * @type {BABYLON.Observable}
  34. */
  35. this.onAfterRenderObservable = new BABYLON.Observable();
  36. /**
  37. * An event triggered after the texture clear
  38. * @type {BABYLON.Observable}
  39. */
  40. this.onClearObservable = new BABYLON.Observable();
  41. this._currentRefreshId = -1;
  42. this._refreshRate = 1;
  43. this.name = name;
  44. this.isRenderTarget = true;
  45. this._size = size;
  46. this._generateMipMaps = generateMipMaps;
  47. this._doNotChangeAspectRatio = doNotChangeAspectRatio;
  48. if (isCube) {
  49. this._texture = scene.getEngine().createRenderTargetCubeTexture(size, { generateMipMaps: generateMipMaps });
  50. this.coordinatesMode = BABYLON.Texture.INVCUBIC_MODE;
  51. this._textureMatrix = BABYLON.Matrix.Identity();
  52. }
  53. else {
  54. this._texture = scene.getEngine().createRenderTargetTexture(size, { generateMipMaps: generateMipMaps, type: type });
  55. }
  56. // Rendering groups
  57. this._renderingManager = new BABYLON.RenderingManager(scene);
  58. }
  59. Object.defineProperty(RenderTargetTexture, "REFRESHRATE_RENDER_ONCE", {
  60. get: function () {
  61. return RenderTargetTexture._REFRESHRATE_RENDER_ONCE;
  62. },
  63. enumerable: true,
  64. configurable: true
  65. });
  66. Object.defineProperty(RenderTargetTexture, "REFRESHRATE_RENDER_ONEVERYFRAME", {
  67. get: function () {
  68. return RenderTargetTexture._REFRESHRATE_RENDER_ONEVERYFRAME;
  69. },
  70. enumerable: true,
  71. configurable: true
  72. });
  73. Object.defineProperty(RenderTargetTexture, "REFRESHRATE_RENDER_ONEVERYTWOFRAMES", {
  74. get: function () {
  75. return RenderTargetTexture._REFRESHRATE_RENDER_ONEVERYTWOFRAMES;
  76. },
  77. enumerable: true,
  78. configurable: true
  79. });
  80. Object.defineProperty(RenderTargetTexture.prototype, "onAfterUnbind", {
  81. set: function (callback) {
  82. if (this._onAfterUnbindObserver) {
  83. this.onAfterUnbindObservable.remove(this._onAfterUnbindObserver);
  84. }
  85. this._onAfterUnbindObserver = this.onAfterUnbindObservable.add(callback);
  86. },
  87. enumerable: true,
  88. configurable: true
  89. });
  90. Object.defineProperty(RenderTargetTexture.prototype, "onBeforeRender", {
  91. set: function (callback) {
  92. if (this._onBeforeRenderObserver) {
  93. this.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  94. }
  95. this._onBeforeRenderObserver = this.onBeforeRenderObservable.add(callback);
  96. },
  97. enumerable: true,
  98. configurable: true
  99. });
  100. Object.defineProperty(RenderTargetTexture.prototype, "onAfterRender", {
  101. set: function (callback) {
  102. if (this._onAfterRenderObserver) {
  103. this.onAfterRenderObservable.remove(this._onAfterRenderObserver);
  104. }
  105. this._onAfterRenderObserver = this.onAfterRenderObservable.add(callback);
  106. },
  107. enumerable: true,
  108. configurable: true
  109. });
  110. Object.defineProperty(RenderTargetTexture.prototype, "onClear", {
  111. set: function (callback) {
  112. if (this._onClearObserver) {
  113. this.onClearObservable.remove(this._onClearObserver);
  114. }
  115. this._onClearObserver = this.onClearObservable.add(callback);
  116. },
  117. enumerable: true,
  118. configurable: true
  119. });
  120. RenderTargetTexture.prototype.resetRefreshCounter = function () {
  121. this._currentRefreshId = -1;
  122. };
  123. Object.defineProperty(RenderTargetTexture.prototype, "refreshRate", {
  124. get: function () {
  125. return this._refreshRate;
  126. },
  127. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  128. set: function (value) {
  129. this._refreshRate = value;
  130. this.resetRefreshCounter();
  131. },
  132. enumerable: true,
  133. configurable: true
  134. });
  135. RenderTargetTexture.prototype._shouldRender = function () {
  136. if (this._currentRefreshId === -1) {
  137. this._currentRefreshId = 1;
  138. return true;
  139. }
  140. if (this.refreshRate === this._currentRefreshId) {
  141. this._currentRefreshId = 1;
  142. return true;
  143. }
  144. this._currentRefreshId++;
  145. return false;
  146. };
  147. RenderTargetTexture.prototype.isReady = function () {
  148. if (!this.getScene().renderTargetsEnabled) {
  149. return false;
  150. }
  151. return _super.prototype.isReady.call(this);
  152. };
  153. RenderTargetTexture.prototype.getRenderSize = function () {
  154. return this._size;
  155. };
  156. Object.defineProperty(RenderTargetTexture.prototype, "canRescale", {
  157. get: function () {
  158. return true;
  159. },
  160. enumerable: true,
  161. configurable: true
  162. });
  163. RenderTargetTexture.prototype.scale = function (ratio) {
  164. var newSize = this._size * ratio;
  165. this.resize(newSize, this._generateMipMaps);
  166. };
  167. RenderTargetTexture.prototype.getReflectionTextureMatrix = function () {
  168. if (this.isCube) {
  169. return this._textureMatrix;
  170. }
  171. return _super.prototype.getReflectionTextureMatrix.call(this);
  172. };
  173. RenderTargetTexture.prototype.resize = function (size, generateMipMaps) {
  174. this.releaseInternalTexture();
  175. if (this.isCube) {
  176. this._texture = this.getScene().getEngine().createRenderTargetCubeTexture(size);
  177. }
  178. else {
  179. this._texture = this.getScene().getEngine().createRenderTargetTexture(size, generateMipMaps);
  180. }
  181. };
  182. RenderTargetTexture.prototype.render = function (useCameraPostProcess, dumpForDebug) {
  183. var scene = this.getScene();
  184. if (this.activeCamera && this.activeCamera !== scene.activeCamera) {
  185. scene.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix(true));
  186. }
  187. if (this._waitingRenderList) {
  188. this.renderList = [];
  189. for (var index = 0; index < this._waitingRenderList.length; index++) {
  190. var id = this._waitingRenderList[index];
  191. this.renderList.push(scene.getMeshByID(id));
  192. }
  193. delete this._waitingRenderList;
  194. }
  195. if (this.renderList && this.renderList.length === 0) {
  196. return;
  197. }
  198. // Prepare renderingManager
  199. this._renderingManager.reset();
  200. var currentRenderList = this.renderList ? this.renderList : scene.getActiveMeshes().data;
  201. var sceneRenderId = scene.getRenderId();
  202. for (var meshIndex = 0; meshIndex < currentRenderList.length; meshIndex++) {
  203. var mesh = currentRenderList[meshIndex];
  204. if (mesh) {
  205. if (!mesh.isReady()) {
  206. // Reset _currentRefreshId
  207. this.resetRefreshCounter();
  208. continue;
  209. }
  210. mesh._preActivateForIntermediateRendering(sceneRenderId);
  211. if (mesh.isEnabled() && mesh.isVisible && mesh.subMeshes && ((mesh.layerMask & scene.activeCamera.layerMask) !== 0)) {
  212. mesh._activate(sceneRenderId);
  213. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  214. var subMesh = mesh.subMeshes[subIndex];
  215. scene._activeIndices += subMesh.indexCount;
  216. this._renderingManager.dispatch(subMesh);
  217. }
  218. }
  219. }
  220. }
  221. if (this.isCube) {
  222. for (var face = 0; face < 6; face++) {
  223. this.renderToTarget(face, currentRenderList, useCameraPostProcess, dumpForDebug);
  224. scene.incrementRenderId();
  225. scene.resetCachedMaterial();
  226. }
  227. }
  228. else {
  229. this.renderToTarget(0, currentRenderList, useCameraPostProcess, dumpForDebug);
  230. }
  231. this.onAfterUnbindObservable.notifyObservers(this);
  232. if (this.activeCamera && this.activeCamera !== scene.activeCamera) {
  233. scene.setTransformMatrix(scene.activeCamera.getViewMatrix(), scene.activeCamera.getProjectionMatrix(true));
  234. }
  235. scene.resetCachedMaterial();
  236. };
  237. RenderTargetTexture.prototype.renderToTarget = function (faceIndex, currentRenderList, useCameraPostProcess, dumpForDebug) {
  238. var scene = this.getScene();
  239. var engine = scene.getEngine();
  240. // Bind
  241. if (!useCameraPostProcess || !scene.postProcessManager._prepareFrame(this._texture)) {
  242. if (this.isCube) {
  243. engine.bindFramebuffer(this._texture, faceIndex);
  244. }
  245. else {
  246. engine.bindFramebuffer(this._texture);
  247. }
  248. }
  249. this.onBeforeRenderObservable.notifyObservers(faceIndex);
  250. // Clear
  251. if (this.onClearObservable.hasObservers()) {
  252. this.onClearObservable.notifyObservers(engine);
  253. }
  254. else {
  255. engine.clear(scene.clearColor, true, true);
  256. }
  257. if (!this._doNotChangeAspectRatio) {
  258. scene.updateTransformMatrix(true);
  259. }
  260. // Render
  261. this._renderingManager.render(this.customRenderFunction, currentRenderList, this.renderParticles, this.renderSprites);
  262. if (useCameraPostProcess) {
  263. scene.postProcessManager._finalizeFrame(false, this._texture, faceIndex);
  264. }
  265. if (!this._doNotChangeAspectRatio) {
  266. scene.updateTransformMatrix(true);
  267. }
  268. this.onAfterRenderObservable.notifyObservers(faceIndex);
  269. // Dump ?
  270. if (dumpForDebug) {
  271. BABYLON.Tools.DumpFramebuffer(this._size, this._size, engine);
  272. }
  273. // Unbind
  274. if (!this.isCube || faceIndex === 5) {
  275. if (this.isCube) {
  276. if (faceIndex === 5) {
  277. engine.generateMipMapsForCubemap(this._texture);
  278. }
  279. }
  280. engine.unBindFramebuffer(this._texture, this.isCube);
  281. }
  282. };
  283. RenderTargetTexture.prototype.clone = function () {
  284. var textureSize = this.getSize();
  285. var newTexture = new RenderTargetTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
  286. // Base texture
  287. newTexture.hasAlpha = this.hasAlpha;
  288. newTexture.level = this.level;
  289. // RenderTarget Texture
  290. newTexture.coordinatesMode = this.coordinatesMode;
  291. newTexture.renderList = this.renderList.slice(0);
  292. return newTexture;
  293. };
  294. RenderTargetTexture.prototype.serialize = function () {
  295. if (!this.name) {
  296. return null;
  297. }
  298. var serializationObject = _super.prototype.serialize.call(this);
  299. serializationObject.renderTargetSize = this.getRenderSize();
  300. serializationObject.renderList = [];
  301. for (var index = 0; index < this.renderList.length; index++) {
  302. serializationObject.renderList.push(this.renderList[index].id);
  303. }
  304. return serializationObject;
  305. };
  306. RenderTargetTexture._REFRESHRATE_RENDER_ONCE = 0;
  307. RenderTargetTexture._REFRESHRATE_RENDER_ONEVERYFRAME = 1;
  308. RenderTargetTexture._REFRESHRATE_RENDER_ONEVERYTWOFRAMES = 2;
  309. return RenderTargetTexture;
  310. }(BABYLON.Texture));
  311. BABYLON.RenderTargetTexture = RenderTargetTexture;
  312. })(BABYLON || (BABYLON = {}));