babylon.customProceduralTexture.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  21. this.refreshRate = 1;
  22. }
  23. CustomProceduralTexture.prototype.loadJson = function (jsonUrl) {
  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._updateTexture = true;
  36. that._shaderLoaded = true;
  37. } catch (ex) {
  38. noConfigFile();
  39. }
  40. } else {
  41. noConfigFile();
  42. }
  43. }, false);
  44. xhr.addEventListener("error", function (event) {
  45. noConfigFile();
  46. }, false);
  47. try {
  48. xhr.send();
  49. } catch (ex) {
  50. BABYLON.Tools.Error("Error on XHR send request.");
  51. }
  52. };
  53. CustomProceduralTexture.prototype.render = function (useCameraPostProcess) {
  54. //if config and shader not loaded, do not render
  55. if (!this._shaderLoaded)
  56. return;
  57. if (this._updateTexture) {
  58. this.reset();
  59. this.setFragment(this._texturePath + "/custom");
  60. this.updateTextures();
  61. this.updateShaderUniforms();
  62. this._shaderLoaded = true;
  63. this._animate = this._config.animate;
  64. this.refreshRate = this._config.refreshrate;
  65. this.isReady();
  66. this._updateTexture = false;
  67. return;
  68. }
  69. if (this._animate) {
  70. this._time += this.getScene().getAnimationRatio() * 0.03;
  71. this.updateShaderUniforms();
  72. }
  73. _super.prototype.render.call(this, useCameraPostProcess);
  74. };
  75. CustomProceduralTexture.prototype.updateTextures = function () {
  76. for (var i = 0; i < this._config.texture2Ds.length; i++) {
  77. 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));
  78. }
  79. };
  80. CustomProceduralTexture.prototype.updateShaderUniforms = function () {
  81. for (var j = 0; j < this._config.uniforms.length; j++) {
  82. var uniform = this._config.uniforms[j];
  83. switch (uniform.type) {
  84. case "float":
  85. this.setFloat(uniform.name, uniform.value);
  86. break;
  87. case "color3":
  88. this.setColor3(uniform.name, new BABYLON.Color3(uniform.r, uniform.g, uniform.b));
  89. break;
  90. case "color4":
  91. this.setColor4(uniform.name, new BABYLON.Color4(uniform.r, uniform.g, uniform.b, uniform.a));
  92. break;
  93. case "vector2":
  94. this.setVector2(uniform.name, new BABYLON.Vector2(uniform.x, uniform.y));
  95. break;
  96. case "vector3":
  97. this.setVector3(uniform.name, new BABYLON.Vector3(uniform.x, uniform.y, uniform.z));
  98. break;
  99. }
  100. }
  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