babylon.proceduralTexture.js 8.6 KB

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