babylon.proceduralTexture.ts 11 KB

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