proceduralTexture.ts 21 KB

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