marbleProceduralTexture.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { Effect, ProceduralTexture, Color3, Scene, Texture, serialize, SerializationHelper } from "babylonjs";
  2. Effect.ShadersStore["marbleProceduralTexturePixelShader"] = require("./marbleProceduralTexture.fragment.fx");
  3. export class MarbleProceduralTexture extends ProceduralTexture {
  4. private _numberOfTilesHeight: number = 3;
  5. private _numberOfTilesWidth: number = 3;
  6. private _amplitude: number = 9.0;
  7. private _jointColor = new Color3(0.72, 0.72, 0.72);
  8. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  9. super(name, size, "marbleProceduralTexture", scene, fallbackTexture, generateMipMaps);
  10. this.updateShaderUniforms();
  11. }
  12. public updateShaderUniforms() {
  13. this.setFloat("numberOfTilesHeight", this._numberOfTilesHeight);
  14. this.setFloat("numberOfTilesWidth", this._numberOfTilesWidth);
  15. this.setFloat("amplitude", this._amplitude);
  16. this.setColor3("jointColor", this._jointColor);
  17. }
  18. @serialize()
  19. public get numberOfTilesHeight(): number {
  20. return this._numberOfTilesHeight;
  21. }
  22. public set numberOfTilesHeight(value: number) {
  23. this._numberOfTilesHeight = value;
  24. this.updateShaderUniforms();
  25. }
  26. @serialize()
  27. public get amplitude(): number {
  28. return this._amplitude;
  29. }
  30. public set amplitude(value: number) {
  31. this._amplitude = value;
  32. this.updateShaderUniforms();
  33. }
  34. @serialize()
  35. public get numberOfTilesWidth(): number {
  36. return this._numberOfTilesWidth;
  37. }
  38. public set numberOfTilesWidth(value: number) {
  39. this._numberOfTilesWidth = value;
  40. this.updateShaderUniforms();
  41. }
  42. @serialize()
  43. public get jointColor(): Color3 {
  44. return this._jointColor;
  45. }
  46. public set jointColor(value: Color3) {
  47. this._jointColor = value;
  48. this.updateShaderUniforms();
  49. }
  50. /**
  51. * Serializes this marble procedural texture
  52. * @returns a serialized marble procedural texture object
  53. */
  54. public serialize(): any {
  55. var serializationObject = SerializationHelper.Serialize(this, super.serialize());
  56. serializationObject.customType = "BABYLON.MarbleProceduralTexture";
  57. return serializationObject;
  58. }
  59. /**
  60. * Creates a Marble Procedural Texture from parsed marble procedural texture data
  61. * @param parsedTexture defines parsed texture data
  62. * @param scene defines the current scene
  63. * @param rootUrl defines the root URL containing marble procedural texture information
  64. * @returns a parsed Marble Procedural Texture
  65. */
  66. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): MarbleProceduralTexture {
  67. var texture = SerializationHelper.Parse(() => new MarbleProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps), parsedTexture, scene, rootUrl);
  68. return texture;
  69. }
  70. }