babylon.customProceduralTexture.js 5.4 KB

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