babylon.customProceduralTexture.ts 4.2 KB

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