perlinNoiseProceduralTexture.ts 2.6 KB

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