babylon.customProceduralTexture.js 5.1 KB

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