babylon.customProceduralTexture.ts 4.8 KB

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