babylon.proceduralTexture.ts 9.5 KB

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