babylon.brickProceduralTexture.js 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var BrickProceduralTexture = (function (_super) {
  15. __extends(BrickProceduralTexture, _super);
  16. function BrickProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
  17. var _this = _super.call(this, name, size, "brickProceduralTexture", scene, fallbackTexture, generateMipMaps) || this;
  18. _this._numberOfBricksHeight = 15;
  19. _this._numberOfBricksWidth = 5;
  20. _this._jointColor = new BABYLON.Color3(0.72, 0.72, 0.72);
  21. _this._brickColor = new BABYLON.Color3(0.77, 0.47, 0.40);
  22. _this.updateShaderUniforms();
  23. return _this;
  24. }
  25. BrickProceduralTexture.prototype.updateShaderUniforms = function () {
  26. this.setFloat("numberOfBricksHeight", this._numberOfBricksHeight);
  27. this.setFloat("numberOfBricksWidth", this._numberOfBricksWidth);
  28. this.setColor3("brickColor", this._brickColor);
  29. this.setColor3("jointColor", this._jointColor);
  30. };
  31. Object.defineProperty(BrickProceduralTexture.prototype, "numberOfBricksHeight", {
  32. get: function () {
  33. return this._numberOfBricksHeight;
  34. },
  35. set: function (value) {
  36. this._numberOfBricksHeight = value;
  37. this.updateShaderUniforms();
  38. },
  39. enumerable: true,
  40. configurable: true
  41. });
  42. Object.defineProperty(BrickProceduralTexture.prototype, "numberOfBricksWidth", {
  43. get: function () {
  44. return this._numberOfBricksWidth;
  45. },
  46. set: function (value) {
  47. this._numberOfBricksWidth = value;
  48. this.updateShaderUniforms();
  49. },
  50. enumerable: true,
  51. configurable: true
  52. });
  53. Object.defineProperty(BrickProceduralTexture.prototype, "jointColor", {
  54. get: function () {
  55. return this._jointColor;
  56. },
  57. set: function (value) {
  58. this._jointColor = value;
  59. this.updateShaderUniforms();
  60. },
  61. enumerable: true,
  62. configurable: true
  63. });
  64. Object.defineProperty(BrickProceduralTexture.prototype, "brickColor", {
  65. get: function () {
  66. return this._brickColor;
  67. },
  68. set: function (value) {
  69. this._brickColor = value;
  70. this.updateShaderUniforms();
  71. },
  72. enumerable: true,
  73. configurable: true
  74. });
  75. return BrickProceduralTexture;
  76. }(BABYLON.ProceduralTexture));
  77. BABYLON.BrickProceduralTexture = BrickProceduralTexture;
  78. })(BABYLON || (BABYLON = {}));
  79. //# sourceMappingURL=babylon.brickProceduralTexture.js.map
  80. BABYLON.Effect.ShadersStore['brickProceduralTexturePixelShader'] = "precision highp float;\nvarying vec2 vPosition;\nvarying vec2 vUV;\nuniform float numberOfBricksHeight;\nuniform float numberOfBricksWidth;\nuniform vec3 brickColor;\nuniform vec3 jointColor;\nfloat rand(vec2 n) {\nreturn fract(cos(dot(n,vec2(12.9898,4.1414)))*43758.5453);\n}\nfloat noise(vec2 n) {\nconst vec2 d=vec2(0.0,1.0);\nvec2 b=floor(n),f=smoothstep(vec2(0.0),vec2(1.0),fract(n));\nreturn mix(mix(rand(b),rand(b+d.yx),f.x),mix(rand(b+d.xy),rand(b+d.yy),f.x),f.y);\n}\nfloat fbm(vec2 n) {\nfloat total=0.0,amplitude=1.0;\nfor (int i=0; i<4; i++) {\ntotal+=noise(n)*amplitude;\nn+=n;\namplitude*=0.5;\n}\nreturn total;\n}\nfloat roundF(float number){\nreturn sign(number)*floor(abs(number)+0.5);\n}\nvoid main(void)\n{\nfloat brickW=1.0/numberOfBricksWidth;\nfloat brickH=1.0/numberOfBricksHeight;\nfloat jointWPercentage=0.01;\nfloat jointHPercentage=0.05;\nvec3 color=brickColor;\nfloat yi=vUV.y/brickH;\nfloat nyi=roundF(yi);\nfloat xi=vUV.x/brickW;\nif (mod(floor(yi),2.0) == 0.0){\nxi=xi-0.5;\n}\nfloat nxi=roundF(xi);\nvec2 brickvUV=vec2((xi-floor(xi))/brickH,(yi-floor(yi))/brickW);\nif (yi<nyi+jointHPercentage && yi>nyi-jointHPercentage){\ncolor=mix(jointColor,vec3(0.37,0.25,0.25),(yi-nyi)/jointHPercentage+0.2);\n}\nelse if (xi<nxi+jointWPercentage && xi>nxi-jointWPercentage){\ncolor=mix(jointColor,vec3(0.44,0.44,0.44),(xi-nxi)/jointWPercentage+0.2);\n}\nelse {\nfloat brickColorSwitch=mod(floor(yi)+floor(xi),3.0);\nif (brickColorSwitch == 0.0)\ncolor=mix(color,vec3(0.33,0.33,0.33),0.3);\nelse if (brickColorSwitch == 2.0)\ncolor=mix(color,vec3(0.11,0.11,0.11),0.3);\n}\ngl_FragColor=vec4(color,1.0);\n}";