babylon.customProceduralTexture.ts 5.2 KB

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