babylon.customProceduralTexture.js 4.8 KB

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