babylon.customProceduralTexture.ts 4.5 KB

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