babylon.customProceduralTexture.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. module BABYLON {
  2. export class CustomProceduralTexture extends ProceduralTexture {
  3. private _animate: boolean = true;
  4. private _time: number = 0;
  5. private _config: any;
  6. private _texturePath: any;
  7. constructor(name: string, texturePath: any, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  8. super(name, size, null, scene, fallbackTexture, generateMipMaps);
  9. this._texturePath = texturePath;
  10. //Try to load json
  11. this.loadJson(texturePath);
  12. this.refreshRate = 1;
  13. }
  14. private loadJson(jsonUrl: string): void {
  15. var that = this;
  16. function noConfigFile() {
  17. BABYLON.Tools.Log("No config file found in " + jsonUrl + " trying to use ShaderStore or DOM element");
  18. try {
  19. that.setFragment(that._texturePath);
  20. }
  21. catch (ex) {
  22. BABYLON.Tools.Error("No json or ShaderStore or DOM element found for CustomProceduralTexture");
  23. }
  24. }
  25. var configFileUrl = jsonUrl + "/config.json";
  26. var xhr: XMLHttpRequest = new XMLHttpRequest();
  27. xhr.open("GET", configFileUrl, true);
  28. xhr.addEventListener("load", () => {
  29. if (xhr.status === 200 || BABYLON.Tools.ValidateXHRData(xhr, 1)) {
  30. try {
  31. this._config = JSON.parse(xhr.response);
  32. this.updateShaderUniforms();
  33. this.updateTextures();
  34. this.setFragment(this._texturePath + "/custom");
  35. this._animate = this._config.animate;
  36. this.refreshRate = this._config.refreshrate;
  37. }
  38. catch (ex) {
  39. noConfigFile();
  40. }
  41. }
  42. else {
  43. noConfigFile();
  44. }
  45. }, false);
  46. xhr.addEventListener("error", event => {
  47. noConfigFile();
  48. }, false);
  49. try {
  50. xhr.send();
  51. }
  52. catch (ex) {
  53. BABYLON.Tools.Error("CustomProceduralTexture: Error on XHR send request.");
  54. }
  55. }
  56. public isReady(): boolean {
  57. if (!super.isReady()) {
  58. return false;
  59. }
  60. for (var name in this._textures) {
  61. var texture = this._textures[name];
  62. if (!texture.isReady()) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. public render(useCameraPostProcess?: boolean): void {
  69. if (this._animate) {
  70. this._time += this.getScene().getAnimationRatio() * 0.03;
  71. this.updateShaderUniforms();
  72. }
  73. super.render(useCameraPostProcess);
  74. }
  75. public updateTextures(): void {
  76. for (var i = 0; i < this._config.sampler2Ds.length; i++) {
  77. this.setTexture(this._config.sampler2Ds[i].sample2Dname, new Texture(this._texturePath + "/" + this._config.sampler2Ds[i].textureRelativeUrl, this.getScene()));
  78. }
  79. }
  80. public updateShaderUniforms(): void {
  81. if (this._config) {
  82. for (var j = 0; j < this._config.uniforms.length; j++) {
  83. var uniform = this._config.uniforms[j];
  84. switch (uniform.type) {
  85. case "float":
  86. this.setFloat(uniform.name, uniform.value);
  87. break;
  88. case "color3":
  89. this.setColor3(uniform.name, new Color3(uniform.r, uniform.g, uniform.b));
  90. break;
  91. case "color4":
  92. this.setColor4(uniform.name, new Color4(uniform.r, uniform.g, uniform.b, uniform.a));
  93. break;
  94. case "vector2":
  95. this.setVector2(uniform.name, new Vector2(uniform.x, uniform.y));
  96. break;
  97. case "vector3":
  98. this.setVector3(uniform.name, new Vector3(uniform.x, uniform.y, uniform.z));
  99. break;
  100. }
  101. }
  102. }
  103. this.setFloat("time", this._time);
  104. }
  105. public get animate(): boolean {
  106. return this._animate;
  107. }
  108. public set animate(value: boolean) {
  109. this._animate = value;
  110. }
  111. }
  112. }