babylon.proceduralTexture.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 (generateMipMaps === void 0) { 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. }
  74. else {
  75. shaders = { vertex: "procedural", fragment: this._fragment };
  76. }
  77. this._effect = engine.createEffect(shaders, ["position"], this._uniforms, this._samplers, "", null, null, function () {
  78. _this.releaseInternalTexture();
  79. if (_this._fallbackTexture) {
  80. _this._texture = _this._fallbackTexture._texture;
  81. _this._texture.references++;
  82. }
  83. _this._fallbackTextureUsed = true;
  84. });
  85. return this._effect.isReady();
  86. };
  87. ProceduralTexture.prototype.resetRefreshCounter = function () {
  88. this._currentRefreshId = -1;
  89. };
  90. ProceduralTexture.prototype.setFragment = function (fragment) {
  91. this._fragment = fragment;
  92. };
  93. Object.defineProperty(ProceduralTexture.prototype, "refreshRate", {
  94. get: function () {
  95. return this._refreshRate;
  96. },
  97. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  98. set: function (value) {
  99. this._refreshRate = value;
  100. this.resetRefreshCounter();
  101. },
  102. enumerable: true,
  103. configurable: true
  104. });
  105. ProceduralTexture.prototype._shouldRender = function () {
  106. if (!this.isReady() || !this._texture) {
  107. return false;
  108. }
  109. if (this._fallbackTextureUsed) {
  110. return false;
  111. }
  112. if (this._currentRefreshId === -1) {
  113. this._currentRefreshId = 1;
  114. return true;
  115. }
  116. if (this.refreshRate === this._currentRefreshId) {
  117. this._currentRefreshId = 1;
  118. return true;
  119. }
  120. this._currentRefreshId++;
  121. return false;
  122. };
  123. ProceduralTexture.prototype.getRenderSize = function () {
  124. return this._size;
  125. };
  126. ProceduralTexture.prototype.resize = function (size, generateMipMaps) {
  127. if (this._fallbackTextureUsed) {
  128. return;
  129. }
  130. this.releaseInternalTexture();
  131. this._texture = this.getScene().getEngine().createRenderTargetTexture(size, generateMipMaps);
  132. };
  133. ProceduralTexture.prototype._checkUniform = function (uniformName) {
  134. if (this._uniforms.indexOf(uniformName) === -1) {
  135. this._uniforms.push(uniformName);
  136. }
  137. };
  138. ProceduralTexture.prototype.setTexture = function (name, texture) {
  139. if (this._samplers.indexOf(name) === -1) {
  140. this._samplers.push(name);
  141. }
  142. this._textures[name] = texture;
  143. return this;
  144. };
  145. ProceduralTexture.prototype.setFloat = function (name, value) {
  146. this._checkUniform(name);
  147. this._floats[name] = value;
  148. return this;
  149. };
  150. ProceduralTexture.prototype.setFloats = function (name, value) {
  151. this._checkUniform(name);
  152. this._floatsArrays[name] = value;
  153. return this;
  154. };
  155. ProceduralTexture.prototype.setColor3 = function (name, value) {
  156. this._checkUniform(name);
  157. this._colors3[name] = value;
  158. return this;
  159. };
  160. ProceduralTexture.prototype.setColor4 = function (name, value) {
  161. this._checkUniform(name);
  162. this._colors4[name] = value;
  163. return this;
  164. };
  165. ProceduralTexture.prototype.setVector2 = function (name, value) {
  166. this._checkUniform(name);
  167. this._vectors2[name] = value;
  168. return this;
  169. };
  170. ProceduralTexture.prototype.setVector3 = function (name, value) {
  171. this._checkUniform(name);
  172. this._vectors3[name] = value;
  173. return this;
  174. };
  175. ProceduralTexture.prototype.setMatrix = function (name, value) {
  176. this._checkUniform(name);
  177. this._matrices[name] = value;
  178. return this;
  179. };
  180. ProceduralTexture.prototype.render = function (useCameraPostProcess) {
  181. var scene = this.getScene();
  182. var engine = scene.getEngine();
  183. engine.bindFramebuffer(this._texture);
  184. // Clear
  185. engine.clear(scene.clearColor, true, true);
  186. // Render
  187. engine.enableEffect(this._effect);
  188. engine.setState(false);
  189. for (var name in this._textures) {
  190. this._effect.setTexture(name, this._textures[name]);
  191. }
  192. for (name in this._floats) {
  193. this._effect.setFloat(name, this._floats[name]);
  194. }
  195. for (name in this._floatsArrays) {
  196. this._effect.setArray(name, this._floatsArrays[name]);
  197. }
  198. for (name in this._colors3) {
  199. this._effect.setColor3(name, this._colors3[name]);
  200. }
  201. for (name in this._colors4) {
  202. var color = this._colors4[name];
  203. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  204. }
  205. for (name in this._vectors2) {
  206. this._effect.setVector2(name, this._vectors2[name]);
  207. }
  208. for (name in this._vectors3) {
  209. this._effect.setVector3(name, this._vectors3[name]);
  210. }
  211. for (name in this._matrices) {
  212. this._effect.setMatrix(name, this._matrices[name]);
  213. }
  214. // VBOs
  215. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  216. // Draw order
  217. engine.draw(true, 0, 6);
  218. // Unbind
  219. engine.unBindFramebuffer(this._texture);
  220. };
  221. ProceduralTexture.prototype.clone = function () {
  222. var textureSize = this.getSize();
  223. var newTexture = new ProceduralTexture(this.name, textureSize.width, this._fragment, this.getScene(), this._fallbackTexture, this._generateMipMaps);
  224. // Base texture
  225. newTexture.hasAlpha = this.hasAlpha;
  226. newTexture.level = this.level;
  227. // RenderTarget Texture
  228. newTexture.coordinatesMode = this.coordinatesMode;
  229. return newTexture;
  230. };
  231. ProceduralTexture.prototype.dispose = function () {
  232. var index = this.getScene()._proceduralTextures.indexOf(this);
  233. if (index >= 0) {
  234. this.getScene()._proceduralTextures.splice(index, 1);
  235. }
  236. _super.prototype.dispose.call(this);
  237. };
  238. return ProceduralTexture;
  239. })(BABYLON.Texture);
  240. BABYLON.ProceduralTexture = ProceduralTexture;
  241. })(BABYLON || (BABYLON = {}));
  242. //# sourceMappingURL=babylon.proceduralTexture.js.map