babylon.proceduralTexture.ts 9.5 KB

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