babylon.proceduralTexture.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. var __extends = 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. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var ProceduralTexture = (function (_super) {
  10. __extends(ProceduralTexture, _super);
  11. function ProceduralTexture(name, size, fragment, scene, fallbackTexture, generateMipMaps) {
  12. if (typeof generateMipMaps === "undefined") { generateMipMaps = true; }
  13. _super.call(this, null, scene, !generateMipMaps);
  14. this._currentRefreshId = -1;
  15. this._refreshRate = 1;
  16. this._vertexDeclaration = [2];
  17. this._vertexStrideSize = 2 * 4;
  18. this._uniforms = new Array();
  19. this._samplers = new Array();
  20. this._textures = new Array();
  21. this._floats = new Array();
  22. this._floatsArrays = {};
  23. this._colors3 = new Array();
  24. this._colors4 = new Array();
  25. this._vectors2 = new Array();
  26. this._vectors3 = new Array();
  27. this._matrices = new Array();
  28. this._fallbackTextureUsed = false;
  29. scene._proceduralTextures.push(this);
  30. this.name = name;
  31. this.isRenderTarget = true;
  32. this._size = size;
  33. this._generateMipMaps = generateMipMaps;
  34. this.setFragment(fragment);
  35. this._fallbackTexture = fallbackTexture;
  36. this._texture = scene.getEngine().createRenderTargetTexture(size, generateMipMaps);
  37. // VBO
  38. var vertices = [];
  39. vertices.push(1, 1);
  40. vertices.push(-1, 1);
  41. vertices.push(-1, -1);
  42. vertices.push(1, -1);
  43. this._vertexBuffer = scene.getEngine().createVertexBuffer(vertices);
  44. // Indices
  45. var indices = [];
  46. indices.push(0);
  47. indices.push(1);
  48. indices.push(2);
  49. indices.push(0);
  50. indices.push(2);
  51. indices.push(3);
  52. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  53. }
  54. ProceduralTexture.prototype.reset = function () {
  55. if (this._effect === undefined) {
  56. return;
  57. }
  58. var engine = this.getScene().getEngine();
  59. engine._releaseEffect(this._effect);
  60. };
  61. ProceduralTexture.prototype.isReady = function () {
  62. var _this = this;
  63. var engine = this.getScene().getEngine();
  64. var shaders;
  65. if (!this._fragment) {
  66. return false;
  67. }
  68. if (this._fallbackTextureUsed) {
  69. return true;
  70. }
  71. if (this._fragment.fragmentElement !== undefined) {
  72. shaders = { vertex: "procedural", fragmentElement: this._fragment.fragmentElement };
  73. } else {
  74. shaders = { vertex: "procedural", fragment: this._fragment };
  75. }
  76. this._effect = engine.createEffect(shaders, ["position"], this._uniforms, this._samplers, "", null, null, function () {
  77. _this.releaseInternalTexture();
  78. if (_this._fallbackTexture) {
  79. _this._texture = _this._fallbackTexture._texture;
  80. _this._texture.references++;
  81. }
  82. _this._fallbackTextureUsed = true;
  83. });
  84. return this._effect.isReady();
  85. };
  86. ProceduralTexture.prototype.resetRefreshCounter = function () {
  87. this._currentRefreshId = -1;
  88. };
  89. ProceduralTexture.prototype.setFragment = function (fragment) {
  90. this._fragment = fragment;
  91. };
  92. Object.defineProperty(ProceduralTexture.prototype, "refreshRate", {
  93. get: function () {
  94. return this._refreshRate;
  95. },
  96. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  97. set: function (value) {
  98. this._refreshRate = value;
  99. this.resetRefreshCounter();
  100. },
  101. enumerable: true,
  102. configurable: true
  103. });
  104. ProceduralTexture.prototype._shouldRender = function () {
  105. if (!this.isReady() || !this._texture) {
  106. return false;
  107. }
  108. if (this._fallbackTextureUsed) {
  109. return false;
  110. }
  111. if (this._currentRefreshId === -1) {
  112. this._currentRefreshId = 1;
  113. return true;
  114. }
  115. if (this.refreshRate === this._currentRefreshId) {
  116. this._currentRefreshId = 1;
  117. return true;
  118. }
  119. this._currentRefreshId++;
  120. return false;
  121. };
  122. ProceduralTexture.prototype.getRenderSize = function () {
  123. return this._size;
  124. };
  125. ProceduralTexture.prototype.resize = function (size, generateMipMaps) {
  126. if (this._fallbackTextureUsed) {
  127. return;
  128. }
  129. this.releaseInternalTexture();
  130. this._texture = this.getScene().getEngine().createRenderTargetTexture(size, generateMipMaps);
  131. };
  132. ProceduralTexture.prototype._checkUniform = function (uniformName) {
  133. if (this._uniforms.indexOf(uniformName) === -1) {
  134. this._uniforms.push(uniformName);
  135. }
  136. };
  137. ProceduralTexture.prototype.setTexture = function (name, texture) {
  138. if (this._samplers.indexOf(name) === -1) {
  139. this._samplers.push(name);
  140. }
  141. this._textures[name] = texture;
  142. return this;
  143. };
  144. ProceduralTexture.prototype.setFloat = function (name, value) {
  145. this._checkUniform(name);
  146. this._floats[name] = value;
  147. return this;
  148. };
  149. ProceduralTexture.prototype.setFloats = function (name, value) {
  150. this._checkUniform(name);
  151. this._floatsArrays[name] = value;
  152. return this;
  153. };
  154. ProceduralTexture.prototype.setColor3 = function (name, value) {
  155. this._checkUniform(name);
  156. this._colors3[name] = value;
  157. return this;
  158. };
  159. ProceduralTexture.prototype.setColor4 = function (name, value) {
  160. this._checkUniform(name);
  161. this._colors4[name] = value;
  162. return this;
  163. };
  164. ProceduralTexture.prototype.setVector2 = function (name, value) {
  165. this._checkUniform(name);
  166. this._vectors2[name] = value;
  167. return this;
  168. };
  169. ProceduralTexture.prototype.setVector3 = function (name, value) {
  170. this._checkUniform(name);
  171. this._vectors3[name] = value;
  172. return this;
  173. };
  174. ProceduralTexture.prototype.setMatrix = function (name, value) {
  175. this._checkUniform(name);
  176. this._matrices[name] = value;
  177. return this;
  178. };
  179. ProceduralTexture.prototype.render = function (useCameraPostProcess) {
  180. var scene = this.getScene();
  181. var engine = scene.getEngine();
  182. engine.bindFramebuffer(this._texture);
  183. // Clear
  184. engine.clear(scene.clearColor, true, true);
  185. // Render
  186. engine.enableEffect(this._effect);
  187. engine.setState(false);
  188. for (var name in this._textures) {
  189. this._effect.setTexture(name, this._textures[name]);
  190. }
  191. for (name in this._floats) {
  192. this._effect.setFloat(name, this._floats[name]);
  193. }
  194. for (name in this._floatsArrays) {
  195. this._effect.setArray(name, this._floatsArrays[name]);
  196. }
  197. for (name in this._colors3) {
  198. this._effect.setColor3(name, this._colors3[name]);
  199. }
  200. for (name in this._colors4) {
  201. var color = this._colors4[name];
  202. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  203. }
  204. for (name in this._vectors2) {
  205. this._effect.setVector2(name, this._vectors2[name]);
  206. }
  207. for (name in this._vectors3) {
  208. this._effect.setVector3(name, this._vectors3[name]);
  209. }
  210. for (name in this._matrices) {
  211. this._effect.setMatrix(name, this._matrices[name]);
  212. }
  213. // VBOs
  214. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  215. // Draw order
  216. engine.draw(true, 0, 6);
  217. // Unbind
  218. engine.unBindFramebuffer(this._texture);
  219. };
  220. ProceduralTexture.prototype.clone = function () {
  221. var textureSize = this.getSize();
  222. var newTexture = new ProceduralTexture(this.name, textureSize.width, this._fragment, this.getScene(), this._fallbackTexture, this._generateMipMaps);
  223. // Base texture
  224. newTexture.hasAlpha = this.hasAlpha;
  225. newTexture.level = this.level;
  226. // RenderTarget Texture
  227. newTexture.coordinatesMode = this.coordinatesMode;
  228. return newTexture;
  229. };
  230. ProceduralTexture.prototype.dispose = function () {
  231. var index = this.getScene()._proceduralTextures.indexOf(this);
  232. if (index >= 0) {
  233. this.getScene()._proceduralTextures.splice(index, 1);
  234. }
  235. _super.prototype.dispose.call(this);
  236. };
  237. return ProceduralTexture;
  238. })(BABYLON.Texture);
  239. BABYLON.ProceduralTexture = ProceduralTexture;
  240. })(BABYLON || (BABYLON = {}));
  241. //# sourceMappingURL=babylon.proceduralTexture.js.map