babylon.proceduralTexture.js 8.7 KB

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