proceduralTexture.ts 21 KB

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