babylon.proceduralTexture.ts 8.6 KB

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