babylon.customProceduralTexture.js 5.4 KB

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