marbleProceduralTexture.ts 3.3 KB

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