proceduralTexture.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. import { serialize } from "Misc/decorators";
  2. import { Observable } from "Misc/observable";
  3. import { Nullable } from "types";
  4. import { Scene } from "scene";
  5. import { Matrix, Vector3, Vector2, Color3, Color4 } from "Maths/math";
  6. import { Engine } from "Engines/engine";
  7. import { VertexBuffer } from "Meshes/buffer";
  8. import { SceneComponentConstants } from "sceneComponent";
  9. import { _TimeToken } from "Instrumentation/timeToken";
  10. import { _DepthCullingState, _StencilState, _AlphaState } from "States";
  11. import { Material } from "Materials/material";
  12. import { Effect } from "Materials/effect";
  13. import { Texture } from "Materials/Textures/texture";
  14. import { RenderTargetTexture } from "Materials/Textures/renderTargetTexture";
  15. import { ProceduralTextureSceneComponent } from "./proceduralTextureSceneComponent";
  16. import "Shaders/procedural.vertex";
  17. /**
  18. * Procedural texturing is a way to programmatically create a texture. There are 2 types of procedural textures: code-only, and code that references some classic 2D images, sometimes calmpler' images.
  19. * This is the base class of any Procedural texture and contains most of the shareable code.
  20. * @see http://doc.babylonjs.com/how_to/how_to_use_procedural_textures
  21. */
  22. export class ProceduralTexture extends Texture {
  23. /**
  24. * Define if the texture is enabled or not (disabled texture will not render)
  25. */
  26. @serialize()
  27. public isEnabled = true;
  28. /**
  29. * Define if the texture must be cleared before rendering (default is true)
  30. */
  31. @serialize()
  32. public autoClear = true;
  33. /**
  34. * Callback called when the texture is generated
  35. */
  36. public onGenerated: () => void;
  37. /**
  38. * Event raised when the texture is generated
  39. */
  40. public onGeneratedObservable = new Observable<ProceduralTexture>();
  41. /** @hidden */
  42. @serialize()
  43. public _generateMipMaps: boolean;
  44. /** @hidden **/
  45. public _effect: Effect;
  46. /** @hidden */
  47. public _textures: { [key: string]: Texture } = {};
  48. @serialize()
  49. private _size: number;
  50. private _currentRefreshId = -1;
  51. private _refreshRate = 1;
  52. private _vertexBuffers: { [key: string]: Nullable<VertexBuffer> } = {};
  53. private _indexBuffer: Nullable<WebGLBuffer>;
  54. private _uniforms = new Array<string>();
  55. private _samplers = new Array<string>();
  56. private _fragment: any;
  57. private _floats: { [key: string]: number } = {};
  58. private _ints: { [key: string]: number } = {};
  59. private _floatsArrays: { [key: string]: number[] } = {};
  60. private _colors3: { [key: string]: Color3 } = {};
  61. private _colors4: { [key: string]: Color4 } = {};
  62. private _vectors2: { [key: string]: Vector2 } = {};
  63. private _vectors3: { [key: string]: Vector3 } = {};
  64. private _matrices: { [key: string]: Matrix } = {};
  65. private _fallbackTexture: Nullable<Texture>;
  66. private _fallbackTextureUsed = false;
  67. private _engine: Engine;
  68. private _cachedDefines = "";
  69. private _contentUpdateId = -1;
  70. private _contentData: Nullable<ArrayBufferView>;
  71. /**
  72. * Instantiates a new procedural texture.
  73. * Procedural texturing is a way to programmatically create a texture. There are 2 types of procedural textures: code-only, and code that references some classic 2D images, sometimes called 'refMaps' or 'sampler' images.
  74. * This is the base class of any Procedural texture and contains most of the shareable code.
  75. * @see http://doc.babylonjs.com/how_to/how_to_use_procedural_textures
  76. * @param name Define the name of the texture
  77. * @param size Define the size of the texture to create
  78. * @param fragment Define the fragment shader to use to generate the texture or null if it is defined later
  79. * @param scene Define the scene the texture belongs to
  80. * @param fallbackTexture Define a fallback texture in case there were issues to create the custom texture
  81. * @param generateMipMaps Define if the texture should creates mip maps or not
  82. * @param isCube Define if the texture is a cube texture or not (this will render each faces of the cube)
  83. */
  84. constructor(name: string, size: any, fragment: any, scene: Nullable<Scene>, fallbackTexture: Nullable<Texture> = null, generateMipMaps = true, public isCube = false) {
  85. super(null, scene, !generateMipMaps);
  86. scene = this.getScene()!;
  87. let component = scene._getComponent(SceneComponentConstants.NAME_PROCEDURALTEXTURE);
  88. if (!component) {
  89. component = new ProceduralTextureSceneComponent(scene);
  90. scene._addComponent(component);
  91. }
  92. scene.proceduralTextures.push(this);
  93. this._engine = scene.getEngine();
  94. this.name = name;
  95. this.isRenderTarget = true;
  96. this._size = size;
  97. this._generateMipMaps = generateMipMaps;
  98. this.setFragment(fragment);
  99. this._fallbackTexture = fallbackTexture;
  100. if (isCube) {
  101. this._texture = this._engine.createRenderTargetCubeTexture(size, { generateMipMaps: generateMipMaps, generateDepthBuffer: false, generateStencilBuffer: false });
  102. this.setFloat("face", 0);
  103. }
  104. else {
  105. this._texture = this._engine.createRenderTargetTexture(size, { generateMipMaps: generateMipMaps, generateDepthBuffer: false, generateStencilBuffer: false });
  106. }
  107. // VBO
  108. var vertices = [];
  109. vertices.push(1, 1);
  110. vertices.push(-1, 1);
  111. vertices.push(-1, -1);
  112. vertices.push(1, -1);
  113. this._vertexBuffers[VertexBuffer.PositionKind] = new VertexBuffer(this._engine, vertices, VertexBuffer.PositionKind, false, false, 2);
  114. this._createIndexBuffer();
  115. }
  116. /**
  117. * The effect that is created when initializing the post process.
  118. * @returns The created effect corrisponding the the postprocess.
  119. */
  120. public getEffect(): Effect {
  121. return this._effect;
  122. }
  123. /**
  124. * Gets texture content (Use this function wisely as reading from a texture can be slow)
  125. * @returns an ArrayBufferView (Uint8Array or Float32Array)
  126. */
  127. public getContent(): Nullable<ArrayBufferView> {
  128. if (this._contentData && this._currentRefreshId == this._contentUpdateId) {
  129. return this._contentData;
  130. }
  131. this._contentData = this.readPixels(0, 0, this._contentData);
  132. this._contentUpdateId = this._currentRefreshId;
  133. return this._contentData;
  134. }
  135. private _createIndexBuffer(): void {
  136. var engine = this._engine;
  137. // Indices
  138. var indices = [];
  139. indices.push(0);
  140. indices.push(1);
  141. indices.push(2);
  142. indices.push(0);
  143. indices.push(2);
  144. indices.push(3);
  145. this._indexBuffer = engine.createIndexBuffer(indices);
  146. }
  147. /** @hidden */
  148. public _rebuild(): void {
  149. let vb = this._vertexBuffers[VertexBuffer.PositionKind];
  150. if (vb) {
  151. vb._rebuild();
  152. }
  153. this._createIndexBuffer();
  154. if (this.refreshRate === RenderTargetTexture.REFRESHRATE_RENDER_ONCE) {
  155. this.refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
  156. }
  157. }
  158. /**
  159. * Resets the texture in order to recreate its associated resources.
  160. * This can be called in case of context loss
  161. */
  162. public reset(): void {
  163. if (this._effect === undefined) {
  164. return;
  165. }
  166. var engine = this._engine;
  167. engine._releaseEffect(this._effect);
  168. }
  169. protected _getDefines(): string {
  170. return "";
  171. }
  172. /**
  173. * Is the texture ready to be used ? (rendered at least once)
  174. * @returns true if ready, otherwise, false.
  175. */
  176. public isReady(): boolean {
  177. var engine = this._engine;
  178. var shaders;
  179. if (!this._fragment) {
  180. return false;
  181. }
  182. if (this._fallbackTextureUsed) {
  183. return true;
  184. }
  185. let defines = this._getDefines();
  186. if (this._effect && defines === this._cachedDefines && this._effect.isReady()) {
  187. return true;
  188. }
  189. if (this._fragment.fragmentElement !== undefined) {
  190. shaders = { vertex: "procedural", fragmentElement: this._fragment.fragmentElement };
  191. }
  192. else {
  193. shaders = { vertex: "procedural", fragment: this._fragment };
  194. }
  195. this._cachedDefines = defines;
  196. this._effect = engine.createEffect(shaders,
  197. [VertexBuffer.PositionKind],
  198. this._uniforms,
  199. this._samplers,
  200. defines, undefined, undefined, () => {
  201. this.releaseInternalTexture();
  202. if (this._fallbackTexture) {
  203. this._texture = this._fallbackTexture._texture;
  204. if (this._texture) {
  205. this._texture.incrementReferences();
  206. }
  207. }
  208. this._fallbackTextureUsed = true;
  209. });
  210. return this._effect.isReady();
  211. }
  212. /**
  213. * Resets the refresh counter of the texture and start bak from scratch.
  214. * Could be usefull to regenerate the texture if it is setup to render only once.
  215. */
  216. public resetRefreshCounter(): void {
  217. this._currentRefreshId = -1;
  218. }
  219. /**
  220. * Set the fragment shader to use in order to render the texture.
  221. * @param fragment This can be set to a path (into the shader store) or to a json object containing a fragmentElement property.
  222. */
  223. public setFragment(fragment: any) {
  224. this._fragment = fragment;
  225. }
  226. /**
  227. * Define the refresh rate of the texture or the rendering frequency.
  228. * Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  229. */
  230. @serialize()
  231. public get refreshRate(): number {
  232. return this._refreshRate;
  233. }
  234. public set refreshRate(value: number) {
  235. this._refreshRate = value;
  236. this.resetRefreshCounter();
  237. }
  238. /** @hidden */
  239. public _shouldRender(): boolean {
  240. if (!this.isEnabled || !this.isReady() || !this._texture) {
  241. if (this._texture) {
  242. this._texture.isReady = false;
  243. }
  244. return false;
  245. }
  246. if (this._fallbackTextureUsed) {
  247. return false;
  248. }
  249. if (this._currentRefreshId === -1) { // At least render once
  250. this._currentRefreshId = 1;
  251. return true;
  252. }
  253. if (this.refreshRate === this._currentRefreshId) {
  254. this._currentRefreshId = 1;
  255. return true;
  256. }
  257. this._currentRefreshId++;
  258. return false;
  259. }
  260. /**
  261. * Get the size the texture is rendering at.
  262. * @returns the size (texture is always squared)
  263. */
  264. public getRenderSize(): number {
  265. return this._size;
  266. }
  267. /**
  268. * Resize the texture to new value.
  269. * @param size Define the new size the texture should have
  270. * @param generateMipMaps Define whether the new texture should create mip maps
  271. */
  272. public resize(size: number, generateMipMaps: boolean): void {
  273. if (this._fallbackTextureUsed) {
  274. return;
  275. }
  276. this.releaseInternalTexture();
  277. this._texture = this._engine.createRenderTargetTexture(size, generateMipMaps);
  278. // Update properties
  279. this._size = size;
  280. this._generateMipMaps = generateMipMaps;
  281. }
  282. private _checkUniform(uniformName: string): void {
  283. if (this._uniforms.indexOf(uniformName) === -1) {
  284. this._uniforms.push(uniformName);
  285. }
  286. }
  287. /**
  288. * Set a texture in the shader program used to render.
  289. * @param name Define the name of the uniform samplers as defined in the shader
  290. * @param texture Define the texture to bind to this sampler
  291. * @return the texture itself allowing "fluent" like uniform updates
  292. */
  293. public setTexture(name: string, texture: Texture): ProceduralTexture {
  294. if (this._samplers.indexOf(name) === -1) {
  295. this._samplers.push(name);
  296. }
  297. this._textures[name] = texture;
  298. return this;
  299. }
  300. /**
  301. * Set a float in the shader.
  302. * @param name Define the name of the uniform as defined in the shader
  303. * @param value Define the value to give to the uniform
  304. * @return the texture itself allowing "fluent" like uniform updates
  305. */
  306. public setFloat(name: string, value: number): ProceduralTexture {
  307. this._checkUniform(name);
  308. this._floats[name] = value;
  309. return this;
  310. }
  311. /**
  312. * Set a int in the shader.
  313. * @param name Define the name of the uniform as defined in the shader
  314. * @param value Define the value to give to the uniform
  315. * @return the texture itself allowing "fluent" like uniform updates
  316. */
  317. public setInt(name: string, value: number): ProceduralTexture {
  318. this._checkUniform(name);
  319. this._ints[name] = value;
  320. return this;
  321. }
  322. /**
  323. * Set an array of floats in the shader.
  324. * @param name Define the name of the uniform as defined in the shader
  325. * @param value Define the value to give to the uniform
  326. * @return the texture itself allowing "fluent" like uniform updates
  327. */
  328. public setFloats(name: string, value: number[]): ProceduralTexture {
  329. this._checkUniform(name);
  330. this._floatsArrays[name] = value;
  331. return this;
  332. }
  333. /**
  334. * Set a vec3 in the shader from a Color3.
  335. * @param name Define the name of the uniform as defined in the shader
  336. * @param value Define the value to give to the uniform
  337. * @return the texture itself allowing "fluent" like uniform updates
  338. */
  339. public setColor3(name: string, value: Color3): ProceduralTexture {
  340. this._checkUniform(name);
  341. this._colors3[name] = value;
  342. return this;
  343. }
  344. /**
  345. * Set a vec4 in the shader from a Color4.
  346. * @param name Define the name of the uniform as defined in the shader
  347. * @param value Define the value to give to the uniform
  348. * @return the texture itself allowing "fluent" like uniform updates
  349. */
  350. public setColor4(name: string, value: Color4): ProceduralTexture {
  351. this._checkUniform(name);
  352. this._colors4[name] = value;
  353. return this;
  354. }
  355. /**
  356. * Set a vec2 in the shader from a Vector2.
  357. * @param name Define the name of the uniform as defined in the shader
  358. * @param value Define the value to give to the uniform
  359. * @return the texture itself allowing "fluent" like uniform updates
  360. */
  361. public setVector2(name: string, value: Vector2): ProceduralTexture {
  362. this._checkUniform(name);
  363. this._vectors2[name] = value;
  364. return this;
  365. }
  366. /**
  367. * Set a vec3 in the shader from a Vector3.
  368. * @param name Define the name of the uniform as defined in the shader
  369. * @param value Define the value to give to the uniform
  370. * @return the texture itself allowing "fluent" like uniform updates
  371. */
  372. public setVector3(name: string, value: Vector3): ProceduralTexture {
  373. this._checkUniform(name);
  374. this._vectors3[name] = value;
  375. return this;
  376. }
  377. /**
  378. * Set a mat4 in the shader from a MAtrix.
  379. * @param name Define the name of the uniform as defined in the shader
  380. * @param value Define the value to give to the uniform
  381. * @return the texture itself allowing "fluent" like uniform updates
  382. */
  383. public setMatrix(name: string, value: Matrix): ProceduralTexture {
  384. this._checkUniform(name);
  385. this._matrices[name] = value;
  386. return this;
  387. }
  388. /**
  389. * Render the texture to its associated render target.
  390. * @param useCameraPostProcess Define if camera post process should be applied to the texture
  391. */
  392. public render(useCameraPostProcess?: boolean): void {
  393. var scene = this.getScene();
  394. if (!scene) {
  395. return;
  396. }
  397. var engine = this._engine;
  398. // Render
  399. engine.enableEffect(this._effect);
  400. engine.setState(false);
  401. // Texture
  402. for (var name in this._textures) {
  403. this._effect.setTexture(name, this._textures[name]);
  404. }
  405. // Float
  406. for (name in this._ints) {
  407. this._effect.setInt(name, this._ints[name]);
  408. }
  409. // Float
  410. for (name in this._floats) {
  411. this._effect.setFloat(name, this._floats[name]);
  412. }
  413. // Floats
  414. for (name in this._floatsArrays) {
  415. this._effect.setArray(name, this._floatsArrays[name]);
  416. }
  417. // Color3
  418. for (name in this._colors3) {
  419. this._effect.setColor3(name, this._colors3[name]);
  420. }
  421. // Color4
  422. for (name in this._colors4) {
  423. var color = this._colors4[name];
  424. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  425. }
  426. // Vector2
  427. for (name in this._vectors2) {
  428. this._effect.setVector2(name, this._vectors2[name]);
  429. }
  430. // Vector3
  431. for (name in this._vectors3) {
  432. this._effect.setVector3(name, this._vectors3[name]);
  433. }
  434. // Matrix
  435. for (name in this._matrices) {
  436. this._effect.setMatrix(name, this._matrices[name]);
  437. }
  438. if (!this._texture) {
  439. return;
  440. }
  441. if (this.isCube) {
  442. for (var face = 0; face < 6; face++) {
  443. engine.bindFramebuffer(this._texture, face, undefined, undefined, true);
  444. // VBOs
  445. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, this._effect);
  446. this._effect.setFloat("face", face);
  447. // Clear
  448. if (this.autoClear) {
  449. engine.clear(scene.clearColor, true, false, false);
  450. }
  451. // Draw order
  452. engine.drawElementsType(Material.TriangleFillMode, 0, 6);
  453. // Mipmaps
  454. if (face === 5) {
  455. engine.generateMipMapsForCubemap(this._texture);
  456. }
  457. }
  458. } else {
  459. engine.bindFramebuffer(this._texture, 0, undefined, undefined, true);
  460. // VBOs
  461. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, this._effect);
  462. // Clear
  463. if (this.autoClear) {
  464. engine.clear(scene.clearColor, true, false, false);
  465. }
  466. // Draw order
  467. engine.drawElementsType(Material.TriangleFillMode, 0, 6);
  468. }
  469. // Unbind
  470. engine.unBindFramebuffer(this._texture, this.isCube);
  471. if (this.onGenerated) {
  472. this.onGenerated();
  473. }
  474. this.onGeneratedObservable.notifyObservers(this);
  475. }
  476. /**
  477. * Clone the texture.
  478. * @returns the cloned texture
  479. */
  480. public clone(): ProceduralTexture {
  481. var textureSize = this.getSize();
  482. var newTexture = new ProceduralTexture(this.name, textureSize.width, this._fragment, <Scene>this.getScene(), this._fallbackTexture, this._generateMipMaps);
  483. // Base texture
  484. newTexture.hasAlpha = this.hasAlpha;
  485. newTexture.level = this.level;
  486. // RenderTarget Texture
  487. newTexture.coordinatesMode = this.coordinatesMode;
  488. return newTexture;
  489. }
  490. /**
  491. * Dispose the texture and release its asoociated resources.
  492. */
  493. public dispose(): void {
  494. let scene = this.getScene();
  495. if (!scene) {
  496. return;
  497. }
  498. var index = scene.proceduralTextures.indexOf(this);
  499. if (index >= 0) {
  500. scene.proceduralTextures.splice(index, 1);
  501. }
  502. var vertexBuffer = this._vertexBuffers[VertexBuffer.PositionKind];
  503. if (vertexBuffer) {
  504. vertexBuffer.dispose();
  505. this._vertexBuffers[VertexBuffer.PositionKind] = null;
  506. }
  507. if (this._indexBuffer && this._engine._releaseBuffer(this._indexBuffer)) {
  508. this._indexBuffer = null;
  509. }
  510. super.dispose();
  511. }
  512. }