babylon.proceduralTexture.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. module BABYLON {
  2. export class ProceduralTexture extends Texture {
  3. @serialize()
  4. private _size: number;
  5. /** @hidden */
  6. @serialize()
  7. public _generateMipMaps: boolean;
  8. @serialize()
  9. public isEnabled = true;
  10. private _currentRefreshId = -1;
  11. private _refreshRate = 1;
  12. public onGenerated: () => void;
  13. private _vertexBuffers: { [key: string]: Nullable<VertexBuffer> } = {};
  14. private _indexBuffer: Nullable<WebGLBuffer>;
  15. private _effect: Effect;
  16. private _uniforms = new Array<string>();
  17. private _samplers = new Array<string>();
  18. private _fragment: any;
  19. /** @hidden */
  20. public _textures: { [key: string]: Texture } = {};
  21. private _floats: { [key: string]: number } = {};
  22. private _ints: { [key: string]: number } = {};
  23. private _floatsArrays: { [key: string]: number[] } = {};
  24. private _colors3: { [key: string]: Color3 } = {};
  25. private _colors4: { [key: string]: Color4 } = {};
  26. private _vectors2: { [key: string]: Vector2 } = {};
  27. private _vectors3: { [key: string]: Vector3 } = {};
  28. private _matrices: { [key: string]: Matrix } = {};
  29. private _fallbackTexture: Nullable<Texture>;
  30. private _fallbackTextureUsed = false;
  31. private _engine: Engine;
  32. constructor(name: string, size: any, fragment: any, scene: Nullable<Scene>, fallbackTexture: Nullable<Texture> = null, generateMipMaps = true, public isCube = false) {
  33. super(null, scene, !generateMipMaps);
  34. scene = this.getScene()!;
  35. scene.proceduralTextures.push(this);
  36. this._engine = scene.getEngine();
  37. this.name = name;
  38. this.isRenderTarget = true;
  39. this._size = size;
  40. this._generateMipMaps = generateMipMaps;
  41. this.setFragment(fragment);
  42. this._fallbackTexture = fallbackTexture;
  43. if (isCube) {
  44. this._texture = this._engine.createRenderTargetCubeTexture(size, { generateMipMaps: generateMipMaps });
  45. this.setFloat("face", 0);
  46. }
  47. else {
  48. this._texture = this._engine.createRenderTargetTexture(size, generateMipMaps);
  49. }
  50. // VBO
  51. var vertices = [];
  52. vertices.push(1, 1);
  53. vertices.push(-1, 1);
  54. vertices.push(-1, -1);
  55. vertices.push(1, -1);
  56. this._vertexBuffers[VertexBuffer.PositionKind] = new VertexBuffer(this._engine, vertices, VertexBuffer.PositionKind, false, false, 2);
  57. this._createIndexBuffer();
  58. }
  59. private _createIndexBuffer(): void {
  60. var engine = this._engine;
  61. // Indices
  62. var indices = [];
  63. indices.push(0);
  64. indices.push(1);
  65. indices.push(2);
  66. indices.push(0);
  67. indices.push(2);
  68. indices.push(3);
  69. this._indexBuffer = engine.createIndexBuffer(indices);
  70. }
  71. /** @hidden */
  72. public _rebuild(): void {
  73. let vb = this._vertexBuffers[VertexBuffer.PositionKind];
  74. if (vb) {
  75. vb._rebuild();
  76. }
  77. this._createIndexBuffer();
  78. if (this.refreshRate === RenderTargetTexture.REFRESHRATE_RENDER_ONCE) {
  79. this.refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
  80. }
  81. }
  82. public reset(): void {
  83. if (this._effect === undefined) {
  84. return;
  85. }
  86. var engine = this._engine;
  87. engine._releaseEffect(this._effect);
  88. }
  89. protected _getDefines(): string {
  90. return "";
  91. }
  92. public isReady(): boolean {
  93. var engine = this._engine;
  94. var shaders;
  95. if (!this._fragment) {
  96. return false;
  97. }
  98. if (this._fallbackTextureUsed) {
  99. return true;
  100. }
  101. if (this._fragment.fragmentElement !== undefined) {
  102. shaders = { vertex: "procedural", fragmentElement: this._fragment.fragmentElement };
  103. }
  104. else {
  105. shaders = { vertex: "procedural", fragment: this._fragment };
  106. }
  107. let defines = this._getDefines();
  108. this._effect = engine.createEffect(shaders,
  109. [VertexBuffer.PositionKind],
  110. this._uniforms,
  111. this._samplers,
  112. defines, undefined, undefined, () => {
  113. this.releaseInternalTexture();
  114. if (this._fallbackTexture) {
  115. this._texture = this._fallbackTexture._texture;
  116. if (this._texture) {
  117. this._texture.incrementReferences();
  118. }
  119. }
  120. this._fallbackTextureUsed = true;
  121. });
  122. return this._effect.isReady();
  123. }
  124. public resetRefreshCounter(): void {
  125. this._currentRefreshId = -1;
  126. }
  127. public setFragment(fragment: any) {
  128. this._fragment = fragment;
  129. }
  130. @serialize()
  131. public get refreshRate(): number {
  132. return this._refreshRate;
  133. }
  134. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  135. public set refreshRate(value: number) {
  136. this._refreshRate = value;
  137. this.resetRefreshCounter();
  138. }
  139. /** @hidden */
  140. public _shouldRender(): boolean {
  141. if (!this.isEnabled || !this.isReady() || !this._texture) {
  142. return false;
  143. }
  144. if (this._fallbackTextureUsed) {
  145. return false;
  146. }
  147. if (this._currentRefreshId === -1) { // At least render once
  148. this._currentRefreshId = 1;
  149. return true;
  150. }
  151. if (this.refreshRate === this._currentRefreshId) {
  152. this._currentRefreshId = 1;
  153. return true;
  154. }
  155. this._currentRefreshId++;
  156. return false;
  157. }
  158. public getRenderSize(): number {
  159. return this._size;
  160. }
  161. public resize(size: number, generateMipMaps: boolean): void {
  162. if (this._fallbackTextureUsed) {
  163. return;
  164. }
  165. this.releaseInternalTexture();
  166. this._texture = this._engine.createRenderTargetTexture(size, generateMipMaps);
  167. // Update properties
  168. this._size = size;
  169. this._generateMipMaps = generateMipMaps;
  170. }
  171. private _checkUniform(uniformName: string): void {
  172. if (this._uniforms.indexOf(uniformName) === -1) {
  173. this._uniforms.push(uniformName);
  174. }
  175. }
  176. public setTexture(name: string, texture: Texture): ProceduralTexture {
  177. if (this._samplers.indexOf(name) === -1) {
  178. this._samplers.push(name);
  179. }
  180. this._textures[name] = texture;
  181. return this;
  182. }
  183. public setFloat(name: string, value: number): ProceduralTexture {
  184. this._checkUniform(name);
  185. this._floats[name] = value;
  186. return this;
  187. }
  188. /**
  189. * Set the value of an uniform to an integer value
  190. * @param name defines the name of the uniform
  191. * @param value defines the value to set
  192. * @returns the current procedural texture
  193. */
  194. public setInt(name: string, value: number): ProceduralTexture {
  195. this._checkUniform(name);
  196. this._ints[name] = value;
  197. return this;
  198. }
  199. public setFloats(name: string, value: number[]): ProceduralTexture {
  200. this._checkUniform(name);
  201. this._floatsArrays[name] = value;
  202. return this;
  203. }
  204. public setColor3(name: string, value: Color3): ProceduralTexture {
  205. this._checkUniform(name);
  206. this._colors3[name] = value;
  207. return this;
  208. }
  209. public setColor4(name: string, value: Color4): ProceduralTexture {
  210. this._checkUniform(name);
  211. this._colors4[name] = value;
  212. return this;
  213. }
  214. public setVector2(name: string, value: Vector2): ProceduralTexture {
  215. this._checkUniform(name);
  216. this._vectors2[name] = value;
  217. return this;
  218. }
  219. public setVector3(name: string, value: Vector3): ProceduralTexture {
  220. this._checkUniform(name);
  221. this._vectors3[name] = value;
  222. return this;
  223. }
  224. public setMatrix(name: string, value: Matrix): ProceduralTexture {
  225. this._checkUniform(name);
  226. this._matrices[name] = value;
  227. return this;
  228. }
  229. public render(useCameraPostProcess?: boolean): void {
  230. var scene = this.getScene();
  231. if (!scene) {
  232. return;
  233. }
  234. var engine = this._engine;
  235. // Render
  236. engine.enableEffect(this._effect);
  237. engine.setState(false);
  238. // Texture
  239. for (var name in this._textures) {
  240. this._effect.setTexture(name, this._textures[name]);
  241. }
  242. // Float
  243. for (name in this._ints) {
  244. this._effect.setInt(name, this._ints[name]);
  245. }
  246. // Float
  247. for (name in this._floats) {
  248. this._effect.setFloat(name, this._floats[name]);
  249. }
  250. // Floats
  251. for (name in this._floatsArrays) {
  252. this._effect.setArray(name, this._floatsArrays[name]);
  253. }
  254. // Color3
  255. for (name in this._colors3) {
  256. this._effect.setColor3(name, this._colors3[name]);
  257. }
  258. // Color4
  259. for (name in this._colors4) {
  260. var color = this._colors4[name];
  261. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  262. }
  263. // Vector2
  264. for (name in this._vectors2) {
  265. this._effect.setVector2(name, this._vectors2[name]);
  266. }
  267. // Vector3
  268. for (name in this._vectors3) {
  269. this._effect.setVector3(name, this._vectors3[name]);
  270. }
  271. // Matrix
  272. for (name in this._matrices) {
  273. this._effect.setMatrix(name, this._matrices[name]);
  274. }
  275. if (!this._texture) {
  276. return;
  277. }
  278. if (this.isCube) {
  279. for (var face = 0; face < 6; face++) {
  280. engine.bindFramebuffer(this._texture, face, undefined, undefined, true);
  281. // VBOs
  282. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, this._effect);
  283. this._effect.setFloat("face", face);
  284. // Clear
  285. engine.clear(scene.clearColor, true, true, true);
  286. // Draw order
  287. engine.drawElementsType(Material.TriangleFillMode, 0, 6);
  288. // Mipmaps
  289. if (face === 5) {
  290. engine.generateMipMapsForCubemap(this._texture);
  291. }
  292. }
  293. } else {
  294. engine.bindFramebuffer(this._texture, 0, undefined, undefined, true);
  295. // VBOs
  296. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, this._effect);
  297. // Clear
  298. engine.clear(scene.clearColor, true, true, true);
  299. // Draw order
  300. engine.drawElementsType(Material.TriangleFillMode, 0, 6);
  301. }
  302. // Unbind
  303. engine.unBindFramebuffer(this._texture, this.isCube);
  304. if (this.onGenerated) {
  305. this.onGenerated();
  306. }
  307. }
  308. public clone(): ProceduralTexture {
  309. var textureSize = this.getSize();
  310. var newTexture = new ProceduralTexture(this.name, textureSize.width, this._fragment, <Scene>this.getScene(), this._fallbackTexture, this._generateMipMaps);
  311. // Base texture
  312. newTexture.hasAlpha = this.hasAlpha;
  313. newTexture.level = this.level;
  314. // RenderTarget Texture
  315. newTexture.coordinatesMode = this.coordinatesMode;
  316. return newTexture;
  317. }
  318. public dispose(): void {
  319. let scene = this.getScene();
  320. if (!scene) {
  321. return;
  322. }
  323. var index = scene.proceduralTextures.indexOf(this);
  324. if (index >= 0) {
  325. scene.proceduralTextures.splice(index, 1);
  326. }
  327. var vertexBuffer = this._vertexBuffers[VertexBuffer.PositionKind];
  328. if (vertexBuffer) {
  329. vertexBuffer.dispose();
  330. this._vertexBuffers[VertexBuffer.PositionKind] = null;
  331. }
  332. if (this._indexBuffer && this._engine._releaseBuffer(this._indexBuffer)) {
  333. this._indexBuffer = null;
  334. }
  335. super.dispose();
  336. }
  337. }
  338. }