brickProceduralTexture.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { serialize, serializeAsColor3, SerializationHelper } from "babylonjs/Misc/decorators";
  2. import { Color3 } from "babylonjs/Maths/math";
  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 "./brickProceduralTexture.fragment";
  8. export class BrickProceduralTexture extends ProceduralTexture {
  9. private _numberOfBricksHeight: number = 15;
  10. private _numberOfBricksWidth: number = 5;
  11. private _jointColor = new Color3(0.72, 0.72, 0.72);
  12. private _brickColor = new Color3(0.77, 0.47, 0.40);
  13. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  14. super(name, size, "brickProceduralTexture", scene, fallbackTexture, generateMipMaps);
  15. this.updateShaderUniforms();
  16. }
  17. public updateShaderUniforms() {
  18. this.setFloat("numberOfBricksHeight", this._numberOfBricksHeight);
  19. this.setFloat("numberOfBricksWidth", this._numberOfBricksWidth);
  20. this.setColor3("brickColor", this._brickColor);
  21. this.setColor3("jointColor", this._jointColor);
  22. }
  23. @serialize()
  24. public get numberOfBricksHeight(): number {
  25. return this._numberOfBricksHeight;
  26. }
  27. public set numberOfBricksHeight(value: number) {
  28. this._numberOfBricksHeight = value;
  29. this.updateShaderUniforms();
  30. }
  31. @serialize()
  32. public get numberOfBricksWidth(): number {
  33. return this._numberOfBricksWidth;
  34. }
  35. public set numberOfBricksWidth(value: number) {
  36. this._numberOfBricksWidth = value;
  37. this.updateShaderUniforms();
  38. }
  39. @serializeAsColor3()
  40. public get jointColor(): Color3 {
  41. return this._jointColor;
  42. }
  43. public set jointColor(value: Color3) {
  44. this._jointColor = value;
  45. this.updateShaderUniforms();
  46. }
  47. @serializeAsColor3()
  48. public get brickColor(): Color3 {
  49. return this._brickColor;
  50. }
  51. public set brickColor(value: Color3) {
  52. this._brickColor = value;
  53. this.updateShaderUniforms();
  54. }
  55. /**
  56. * Serializes this brick procedural texture
  57. * @returns a serialized brick procedural texture object
  58. */
  59. public serialize(): any {
  60. var serializationObject = SerializationHelper.Serialize(this, super.serialize());
  61. serializationObject.customType = "BABYLON.BrickProceduralTexture";
  62. return serializationObject;
  63. }
  64. /**
  65. * Creates a Brick Procedural Texture from parsed brick 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 brick procedural texture information
  69. * @returns a parsed Brick Procedural Texture
  70. */
  71. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): BrickProceduralTexture {
  72. var texture = SerializationHelper.Parse(() => new BrickProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps), parsedTexture, scene, rootUrl);
  73. return texture;
  74. }
  75. }
  76. _TypeStore.RegisteredTypes["BABYLON.BrickProceduralTexture"] = BrickProceduralTexture;