babylon.proceduralTexture.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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, 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._texture = scene.getEngine().createRenderTargetTexture(size, generateMipMaps);
  34. // VBO
  35. var vertices = [];
  36. vertices.push(1, 1);
  37. vertices.push(-1, 1);
  38. vertices.push(-1, -1);
  39. vertices.push(1, -1);
  40. this._vertexBuffer = scene.getEngine().createVertexBuffer(vertices);
  41. // Indices
  42. var indices = [];
  43. indices.push(0);
  44. indices.push(1);
  45. indices.push(2);
  46. indices.push(0);
  47. indices.push(2);
  48. indices.push(3);
  49. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  50. }
  51. ProceduralTexture.prototype.isReady = function () {
  52. var engine = this.getScene().getEngine();
  53. this._effect = engine.createEffect({ vertex: "procedural", fragment: this._fragment }, ["position"], this._uniforms, this._samplers, "");
  54. return this._effect.isReady();
  55. };
  56. ProceduralTexture.prototype.resetRefreshCounter = function () {
  57. this._currentRefreshId = -1;
  58. };
  59. Object.defineProperty(ProceduralTexture.prototype, "refreshRate", {
  60. get: function () {
  61. return this._refreshRate;
  62. },
  63. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  64. set: function (value) {
  65. this._refreshRate = value;
  66. this.resetRefreshCounter();
  67. },
  68. enumerable: true,
  69. configurable: true
  70. });
  71. ProceduralTexture.prototype._shouldRender = function () {
  72. if (this._currentRefreshId === -1) {
  73. this._currentRefreshId = 1;
  74. return true;
  75. }
  76. if (this.refreshRate == this._currentRefreshId) {
  77. this._currentRefreshId = 1;
  78. return true;
  79. }
  80. this._currentRefreshId++;
  81. return false;
  82. };
  83. ProceduralTexture.prototype.getRenderSize = function () {
  84. return this._size;
  85. };
  86. ProceduralTexture.prototype.resize = function (size, generateMipMaps) {
  87. this.releaseInternalTexture();
  88. this._texture = this.getScene().getEngine().createRenderTargetTexture(size, generateMipMaps);
  89. };
  90. ProceduralTexture.prototype._checkUniform = function (uniformName) {
  91. if (this._uniforms.indexOf(uniformName) === -1) {
  92. this._uniforms.push(uniformName);
  93. }
  94. };
  95. ProceduralTexture.prototype.setTexture = function (name, texture) {
  96. if (this._samplers.indexOf(name) === -1) {
  97. this._samplers.push(name);
  98. }
  99. this._textures[name] = texture;
  100. return this;
  101. };
  102. ProceduralTexture.prototype.setFloat = function (name, value) {
  103. this._checkUniform(name);
  104. this._floats[name] = value;
  105. return this;
  106. };
  107. ProceduralTexture.prototype.setFloats = function (name, value) {
  108. this._checkUniform(name);
  109. this._floatsArrays[name] = value;
  110. return this;
  111. };
  112. ProceduralTexture.prototype.setColor3 = function (name, value) {
  113. this._checkUniform(name);
  114. this._colors3[name] = value;
  115. return this;
  116. };
  117. ProceduralTexture.prototype.setColor4 = function (name, value) {
  118. this._checkUniform(name);
  119. this._colors4[name] = value;
  120. return this;
  121. };
  122. ProceduralTexture.prototype.setVector2 = function (name, value) {
  123. this._checkUniform(name);
  124. this._vectors2[name] = value;
  125. return this;
  126. };
  127. ProceduralTexture.prototype.setVector3 = function (name, value) {
  128. this._checkUniform(name);
  129. this._vectors3[name] = value;
  130. return this;
  131. };
  132. ProceduralTexture.prototype.setMatrix = function (name, value) {
  133. this._checkUniform(name);
  134. this._matrices[name] = value;
  135. return this;
  136. };
  137. ProceduralTexture.prototype.render = function (useCameraPostProcess) {
  138. if (!this.isReady() || !this._texture)
  139. return;
  140. var scene = this.getScene();
  141. var engine = scene.getEngine();
  142. engine.bindFramebuffer(this._texture);
  143. // Clear
  144. engine.clear(scene.clearColor, true, true);
  145. // Render
  146. engine.enableEffect(this._effect);
  147. engine.setState(false);
  148. // VBOs
  149. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  150. // Draw order
  151. engine.draw(true, 0, 6);
  152. // Unbind
  153. engine.unBindFramebuffer(this._texture);
  154. };
  155. ProceduralTexture.prototype.clone = function () {
  156. var textureSize = this.getSize();
  157. var newTexture = new BABYLON.ProceduralTexture(this.name, textureSize.width, this._fragment, this.getScene(), this._generateMipMaps);
  158. // Base texture
  159. newTexture.hasAlpha = this.hasAlpha;
  160. newTexture.level = this.level;
  161. // RenderTarget Texture
  162. newTexture.coordinatesMode = this.coordinatesMode;
  163. return newTexture;
  164. };
  165. ProceduralTexture.prototype.dispose = function () {
  166. var index = this.getScene()._proceduralTextures.indexOf(this);
  167. if (index >= 0) {
  168. this.getScene()._proceduralTextures.splice(index, 1);
  169. }
  170. _super.prototype.dispose.call(this);
  171. };
  172. return ProceduralTexture;
  173. })(BABYLON.Texture);
  174. BABYLON.ProceduralTexture = ProceduralTexture;
  175. })(BABYLON || (BABYLON = {}));
  176. //# sourceMappingURL=babylon.proceduralTexture.js.map