babylon.proceduralTexture.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. var __extends = (this && 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. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var ProceduralTexture = (function (_super) {
  9. __extends(ProceduralTexture, _super);
  10. function ProceduralTexture(name, size, fragment, scene, fallbackTexture, generateMipMaps, isCube) {
  11. if (generateMipMaps === void 0) { generateMipMaps = true; }
  12. if (isCube === void 0) { isCube = false; }
  13. _super.call(this, null, scene, !generateMipMaps);
  14. this.isCube = isCube;
  15. this.isEnabled = true;
  16. this._currentRefreshId = -1;
  17. this._refreshRate = 1;
  18. this._vertexDeclaration = [2];
  19. this._vertexStrideSize = 2 * 4;
  20. this._uniforms = new Array();
  21. this._samplers = new Array();
  22. this._textures = new Array();
  23. this._floats = new Array();
  24. this._floatsArrays = {};
  25. this._colors3 = new Array();
  26. this._colors4 = new Array();
  27. this._vectors2 = new Array();
  28. this._vectors3 = new Array();
  29. this._matrices = new Array();
  30. this._fallbackTextureUsed = false;
  31. scene._proceduralTextures.push(this);
  32. this.name = name;
  33. this.isRenderTarget = true;
  34. this._size = size;
  35. this._generateMipMaps = generateMipMaps;
  36. this.setFragment(fragment);
  37. this._fallbackTexture = fallbackTexture;
  38. if (isCube) {
  39. this._texture = scene.getEngine().createRenderTargetCubeTexture(size, { generateMipMaps: generateMipMaps });
  40. this.setFloat("face", 0);
  41. }
  42. else {
  43. this._texture = scene.getEngine().createRenderTargetTexture(size, generateMipMaps);
  44. }
  45. // VBO
  46. var vertices = [];
  47. vertices.push(1, 1);
  48. vertices.push(-1, 1);
  49. vertices.push(-1, -1);
  50. vertices.push(1, -1);
  51. this._vertexBuffer = scene.getEngine().createVertexBuffer(vertices);
  52. // Indices
  53. var indices = [];
  54. indices.push(0);
  55. indices.push(1);
  56. indices.push(2);
  57. indices.push(0);
  58. indices.push(2);
  59. indices.push(3);
  60. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  61. }
  62. ProceduralTexture.prototype.reset = function () {
  63. if (this._effect === undefined) {
  64. return;
  65. }
  66. var engine = this.getScene().getEngine();
  67. engine._releaseEffect(this._effect);
  68. };
  69. ProceduralTexture.prototype.isReady = function () {
  70. var _this = this;
  71. var engine = this.getScene().getEngine();
  72. var shaders;
  73. if (!this._fragment) {
  74. return false;
  75. }
  76. if (this._fallbackTextureUsed) {
  77. return true;
  78. }
  79. if (this._fragment.fragmentElement !== undefined) {
  80. shaders = { vertex: "procedural", fragmentElement: this._fragment.fragmentElement };
  81. }
  82. else {
  83. shaders = { vertex: "procedural", fragment: this._fragment };
  84. }
  85. this._effect = engine.createEffect(shaders, ["position"], this._uniforms, this._samplers, "", null, null, function () {
  86. _this.releaseInternalTexture();
  87. if (_this._fallbackTexture) {
  88. _this._texture = _this._fallbackTexture._texture;
  89. _this._texture.references++;
  90. }
  91. _this._fallbackTextureUsed = true;
  92. });
  93. return this._effect.isReady();
  94. };
  95. ProceduralTexture.prototype.resetRefreshCounter = function () {
  96. this._currentRefreshId = -1;
  97. };
  98. ProceduralTexture.prototype.setFragment = function (fragment) {
  99. this._fragment = fragment;
  100. };
  101. Object.defineProperty(ProceduralTexture.prototype, "refreshRate", {
  102. get: function () {
  103. return this._refreshRate;
  104. },
  105. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  106. set: function (value) {
  107. this._refreshRate = value;
  108. this.resetRefreshCounter();
  109. },
  110. enumerable: true,
  111. configurable: true
  112. });
  113. ProceduralTexture.prototype._shouldRender = function () {
  114. if (!this.isEnabled || !this.isReady() || !this._texture) {
  115. return false;
  116. }
  117. if (this._fallbackTextureUsed) {
  118. return false;
  119. }
  120. if (this._currentRefreshId === -1) {
  121. this._currentRefreshId = 1;
  122. return true;
  123. }
  124. if (this.refreshRate === this._currentRefreshId) {
  125. this._currentRefreshId = 1;
  126. return true;
  127. }
  128. this._currentRefreshId++;
  129. return false;
  130. };
  131. ProceduralTexture.prototype.getRenderSize = function () {
  132. return this._size;
  133. };
  134. ProceduralTexture.prototype.resize = function (size, generateMipMaps) {
  135. if (this._fallbackTextureUsed) {
  136. return;
  137. }
  138. this.releaseInternalTexture();
  139. this._texture = this.getScene().getEngine().createRenderTargetTexture(size, generateMipMaps);
  140. };
  141. ProceduralTexture.prototype._checkUniform = function (uniformName) {
  142. if (this._uniforms.indexOf(uniformName) === -1) {
  143. this._uniforms.push(uniformName);
  144. }
  145. };
  146. ProceduralTexture.prototype.setTexture = function (name, texture) {
  147. if (this._samplers.indexOf(name) === -1) {
  148. this._samplers.push(name);
  149. }
  150. this._textures[name] = texture;
  151. return this;
  152. };
  153. ProceduralTexture.prototype.setFloat = function (name, value) {
  154. this._checkUniform(name);
  155. this._floats[name] = value;
  156. return this;
  157. };
  158. ProceduralTexture.prototype.setFloats = function (name, value) {
  159. this._checkUniform(name);
  160. this._floatsArrays[name] = value;
  161. return this;
  162. };
  163. ProceduralTexture.prototype.setColor3 = function (name, value) {
  164. this._checkUniform(name);
  165. this._colors3[name] = value;
  166. return this;
  167. };
  168. ProceduralTexture.prototype.setColor4 = function (name, value) {
  169. this._checkUniform(name);
  170. this._colors4[name] = value;
  171. return this;
  172. };
  173. ProceduralTexture.prototype.setVector2 = function (name, value) {
  174. this._checkUniform(name);
  175. this._vectors2[name] = value;
  176. return this;
  177. };
  178. ProceduralTexture.prototype.setVector3 = function (name, value) {
  179. this._checkUniform(name);
  180. this._vectors3[name] = value;
  181. return this;
  182. };
  183. ProceduralTexture.prototype.setMatrix = function (name, value) {
  184. this._checkUniform(name);
  185. this._matrices[name] = value;
  186. return this;
  187. };
  188. ProceduralTexture.prototype.render = function (useCameraPostProcess) {
  189. var scene = this.getScene();
  190. var engine = scene.getEngine();
  191. // Render
  192. engine.enableEffect(this._effect);
  193. engine.setState(false);
  194. // Texture
  195. for (var name in this._textures) {
  196. this._effect.setTexture(name, this._textures[name]);
  197. }
  198. // Float
  199. for (name in this._floats) {
  200. this._effect.setFloat(name, this._floats[name]);
  201. }
  202. // Floats
  203. for (name in this._floatsArrays) {
  204. this._effect.setArray(name, this._floatsArrays[name]);
  205. }
  206. // Color3
  207. for (name in this._colors3) {
  208. this._effect.setColor3(name, this._colors3[name]);
  209. }
  210. // Color4
  211. for (name in this._colors4) {
  212. var color = this._colors4[name];
  213. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  214. }
  215. // Vector2
  216. for (name in this._vectors2) {
  217. this._effect.setVector2(name, this._vectors2[name]);
  218. }
  219. // Vector3
  220. for (name in this._vectors3) {
  221. this._effect.setVector3(name, this._vectors3[name]);
  222. }
  223. // Matrix
  224. for (name in this._matrices) {
  225. this._effect.setMatrix(name, this._matrices[name]);
  226. }
  227. // VBOs
  228. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  229. if (this.isCube) {
  230. for (var face = 0; face < 6; face++) {
  231. engine.bindFramebuffer(this._texture, face);
  232. this._effect.setFloat("face", face);
  233. // Clear
  234. engine.clear(scene.clearColor, true, true);
  235. // Draw order
  236. engine.draw(true, 0, 6);
  237. // Mipmaps
  238. if (face === 5) {
  239. engine.generateMipMapsForCubemap(this._texture);
  240. }
  241. }
  242. }
  243. else {
  244. engine.bindFramebuffer(this._texture);
  245. // Clear
  246. engine.clear(scene.clearColor, true, true);
  247. // Draw order
  248. engine.draw(true, 0, 6);
  249. }
  250. // Unbind
  251. engine.unBindFramebuffer(this._texture, this.isCube);
  252. if (this.onGenerated) {
  253. this.onGenerated();
  254. }
  255. };
  256. ProceduralTexture.prototype.clone = function () {
  257. var textureSize = this.getSize();
  258. var newTexture = new ProceduralTexture(this.name, textureSize.width, this._fragment, this.getScene(), this._fallbackTexture, this._generateMipMaps);
  259. // Base texture
  260. newTexture.hasAlpha = this.hasAlpha;
  261. newTexture.level = this.level;
  262. // RenderTarget Texture
  263. newTexture.coordinatesMode = this.coordinatesMode;
  264. return newTexture;
  265. };
  266. ProceduralTexture.prototype.dispose = function () {
  267. var index = this.getScene()._proceduralTextures.indexOf(this);
  268. if (index >= 0) {
  269. this.getScene()._proceduralTextures.splice(index, 1);
  270. }
  271. _super.prototype.dispose.call(this);
  272. };
  273. return ProceduralTexture;
  274. })(BABYLON.Texture);
  275. BABYLON.ProceduralTexture = ProceduralTexture;
  276. })(BABYLON || (BABYLON = {}));