perlinNoiseProceduralTexture.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { serialize, SerializationHelper } from "babylonjs/Misc/decorators";
  2. import { Texture } from "babylonjs/Materials/Textures/texture";
  3. import { ProceduralTexture } from "babylonjs/Materials/Textures/Procedurals/proceduralTexture";
  4. import { Scene } from "babylonjs/scene";
  5. import { _TypeStore } from 'babylonjs/Misc/typeStore';
  6. import "./perlinNoiseProceduralTexture.fragment";
  7. export class PerlinNoiseProceduralTexture extends ProceduralTexture {
  8. @serialize()
  9. public time: number = 0.0;
  10. @serialize()
  11. public timeScale: number = 1.0;
  12. @serialize()
  13. public translationSpeed: number = 1.0;
  14. private _currentTranslation: number = 0;
  15. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  16. super(name, size, "perlinNoiseProceduralTexture", scene, fallbackTexture, generateMipMaps);
  17. this.updateShaderUniforms();
  18. }
  19. public updateShaderUniforms() {
  20. this.setFloat("size", this.getRenderSize() as number);
  21. let scene = this.getScene();
  22. if (!scene) {
  23. return;
  24. }
  25. var deltaTime = scene.getEngine().getDeltaTime();
  26. this.time += deltaTime;
  27. this.setFloat("time", this.time * this.timeScale / 1000);
  28. this._currentTranslation += deltaTime * this.translationSpeed / 1000.0;
  29. this.setFloat("translationSpeed", this._currentTranslation);
  30. }
  31. public render(useCameraPostProcess?: boolean) {
  32. this.updateShaderUniforms();
  33. super.render(useCameraPostProcess);
  34. }
  35. public resize(size: any, generateMipMaps: any): void {
  36. super.resize(size, generateMipMaps);
  37. }
  38. /**
  39. * Serializes this perlin noise procedural texture
  40. * @returns a serialized perlin noise procedural texture object
  41. */
  42. public serialize(): any {
  43. var serializationObject = SerializationHelper.Serialize(this, super.serialize());
  44. serializationObject.customType = "BABYLON.PerlinNoiseProceduralTexture";
  45. return serializationObject;
  46. }
  47. /**
  48. * Creates a Perlin Noise Procedural Texture from parsed perlin noise procedural texture data
  49. * @param parsedTexture defines parsed texture data
  50. * @param scene defines the current scene
  51. * @param rootUrl defines the root URL containing perlin noise procedural texture information
  52. * @returns a parsed Perlin Noise Procedural Texture
  53. */
  54. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): PerlinNoiseProceduralTexture {
  55. var texture = SerializationHelper.Parse(() => new PerlinNoiseProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps), parsedTexture, scene, rootUrl);
  56. return texture;
  57. }
  58. }
  59. _TypeStore.RegisteredTypes["BABYLON.PerlinNoiseProceduralTexture"] = PerlinNoiseProceduralTexture;