grassProceduralTexture.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { serializeAsColor3, 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 "./grassProceduralTexture.fragment";
  8. export class GrassProceduralTexture extends ProceduralTexture {
  9. private _grassColors: Color3[];
  10. private _groundColor = new Color3(1, 1, 1);
  11. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  12. super(name, size, "grassProceduralTexture", scene, fallbackTexture, generateMipMaps);
  13. this._grassColors = [
  14. new Color3(0.29, 0.38, 0.02),
  15. new Color3(0.36, 0.49, 0.09),
  16. new Color3(0.51, 0.6, 0.28)
  17. ];
  18. this.updateShaderUniforms();
  19. }
  20. public updateShaderUniforms() {
  21. this.setColor3("herb1Color", this._grassColors[0]);
  22. this.setColor3("herb2Color", this._grassColors[1]);
  23. this.setColor3("herb3Color", this._grassColors[2]);
  24. this.setColor3("groundColor", this._groundColor);
  25. }
  26. public get grassColors(): Color3[] {
  27. return this._grassColors;
  28. }
  29. public set grassColors(value: Color3[]) {
  30. this._grassColors = value;
  31. this.updateShaderUniforms();
  32. }
  33. @serializeAsColor3()
  34. public get groundColor(): Color3 {
  35. return this._groundColor;
  36. }
  37. public set groundColor(value: Color3) {
  38. this._groundColor = value;
  39. this.updateShaderUniforms();
  40. }
  41. /**
  42. * Serializes this grass procedural texture
  43. * @returns a serialized grass procedural texture object
  44. */
  45. public serialize(): any {
  46. var serializationObject = SerializationHelper.Serialize(this, super.serialize());
  47. serializationObject.customType = "BABYLON.GrassProceduralTexture";
  48. serializationObject.grassColors = [];
  49. for (var i = 0; i < this._grassColors.length; i++) {
  50. serializationObject.grassColors.push(this._grassColors[i].asArray());
  51. }
  52. return serializationObject;
  53. }
  54. /**
  55. * Creates a Grass Procedural Texture from parsed grass procedural texture data
  56. * @param parsedTexture defines parsed texture data
  57. * @param scene defines the current scene
  58. * @param rootUrl defines the root URL containing grass procedural texture information
  59. * @returns a parsed Grass Procedural Texture
  60. */
  61. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): GrassProceduralTexture {
  62. var texture = SerializationHelper.Parse(() => new GrassProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps), parsedTexture, scene, rootUrl);
  63. var colors: Color3[] = [];
  64. for (var i = 0; i < parsedTexture.grassColors.length; i++) {
  65. colors.push(Color3.FromArray(parsedTexture.grassColors[i]));
  66. }
  67. texture.grassColors = colors;
  68. return texture;
  69. }
  70. }
  71. _TypeStore.RegisteredTypes["BABYLON.GrassProceduralTexture"] = GrassProceduralTexture;