normalMapProceduralTexture.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { Effect, ProceduralTexture, Texture, Scene, serializeAsTexture, SerializationHelper } from "babylonjs";
  2. Effect.ShadersStore["normalMapProceduralTexturePixelShader"] = require("./normalMapProceduralTexture.fragment.fx");
  3. export class NormalMapProceduralTexture extends ProceduralTexture {
  4. private _baseTexture: Texture;
  5. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  6. super(name, size, "normalMapProceduralTexture", scene, fallbackTexture, generateMipMaps);
  7. this.updateShaderUniforms();
  8. }
  9. public updateShaderUniforms() {
  10. this.setTexture("baseSampler", this._baseTexture);
  11. this.setFloat("size", this.getRenderSize());
  12. }
  13. public render(useCameraPostProcess?: boolean) {
  14. super.render(useCameraPostProcess);
  15. }
  16. public resize(size: any, generateMipMaps: any): void {
  17. super.resize(size, generateMipMaps);
  18. // We need to update the "size" uniform
  19. this.updateShaderUniforms();
  20. }
  21. @serializeAsTexture()
  22. public get baseTexture(): Texture {
  23. return this._baseTexture;
  24. }
  25. public set baseTexture(texture: Texture) {
  26. this._baseTexture = texture;
  27. this.updateShaderUniforms();
  28. }
  29. /**
  30. * Serializes this normal map procedural texture
  31. * @returns a serialized normal map procedural texture object
  32. */
  33. public serialize(): any {
  34. var serializationObject = SerializationHelper.Serialize(this, super.serialize());
  35. serializationObject.customType = "BABYLON.NormalMapProceduralTexture";
  36. return serializationObject;
  37. }
  38. /**
  39. * Creates a Normal Map Procedural Texture from parsed normal map procedural texture data
  40. * @param parsedTexture defines parsed texture data
  41. * @param scene defines the current scene
  42. * @param rootUrl defines the root URL containing normal map procedural texture information
  43. * @returns a parsed Normal Map Procedural Texture
  44. */
  45. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): NormalMapProceduralTexture {
  46. var texture = SerializationHelper.Parse(() => new NormalMapProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps), parsedTexture, scene, rootUrl);
  47. return texture;
  48. }
  49. }