babylon.customProceduralTexture.js 5.4 KB

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