babylon.proceduralTexture.js 8.8 KB

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