babylon.brickProceduralTexture.js 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var BrickProceduralTexture = (function (_super) {
  5. __extends(BrickProceduralTexture, _super);
  6. function BrickProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
  7. _super.call(this, name, size, "brickProceduralTexture", scene, fallbackTexture, generateMipMaps);
  8. this._numberOfBricksHeight = 15;
  9. this._numberOfBricksWidth = 5;
  10. this._jointColor = new BABYLON.Color3(0.72, 0.72, 0.72);
  11. this._brickColor = new BABYLON.Color3(0.77, 0.47, 0.40);
  12. this.updateShaderUniforms();
  13. this.refreshRate = 0;
  14. }
  15. BrickProceduralTexture.prototype.updateShaderUniforms = function () {
  16. this.setFloat("numberOfBricksHeight", this._numberOfBricksHeight);
  17. this.setFloat("numberOfBricksWidth", this._numberOfBricksWidth);
  18. this.setColor3("brickColor", this._brickColor);
  19. this.setColor3("jointColor", this._jointColor);
  20. };
  21. Object.defineProperty(BrickProceduralTexture.prototype, "numberOfBricksHeight", {
  22. get: function () {
  23. return this._numberOfBricksHeight;
  24. },
  25. set: function (value) {
  26. this._numberOfBricksHeight = value;
  27. this.updateShaderUniforms();
  28. },
  29. enumerable: true,
  30. configurable: true
  31. });
  32. Object.defineProperty(BrickProceduralTexture.prototype, "numberOfBricksWidth", {
  33. get: function () {
  34. return this._numberOfBricksWidth;
  35. },
  36. set: function (value) {
  37. this._numberOfBricksWidth = value;
  38. this.updateShaderUniforms();
  39. },
  40. enumerable: true,
  41. configurable: true
  42. });
  43. Object.defineProperty(BrickProceduralTexture.prototype, "jointColor", {
  44. get: function () {
  45. return this._jointColor;
  46. },
  47. set: function (value) {
  48. this._jointColor = value;
  49. this.updateShaderUniforms();
  50. },
  51. enumerable: true,
  52. configurable: true
  53. });
  54. Object.defineProperty(BrickProceduralTexture.prototype, "brickColor", {
  55. get: function () {
  56. return this._brickColor;
  57. },
  58. set: function (value) {
  59. this._brickColor = value;
  60. this.updateShaderUniforms();
  61. },
  62. enumerable: true,
  63. configurable: true
  64. });
  65. return BrickProceduralTexture;
  66. })(BABYLON.ProceduralTexture);
  67. BABYLON.BrickProceduralTexture = BrickProceduralTexture;
  68. })(BABYLON || (BABYLON = {}));
  69. BABYLON.Effect.ShadersStore['brickProceduralTexturePixelShader'] = "precision highp float;\r\n\r\nvarying vec2 vPosition;\r\nvarying vec2 vUV;\r\n\r\nuniform float numberOfBricksHeight;\r\nuniform float numberOfBricksWidth;\r\nuniform vec3 brickColor;\r\nuniform vec3 jointColor;\r\n\r\nfloat rand(vec2 n) {\r\n\treturn fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);\r\n}\r\n\r\nfloat noise(vec2 n) {\r\n\tconst vec2 d = vec2(0.0, 1.0);\r\n\tvec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));\r\n\treturn mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);\r\n}\r\n\r\nfloat fbm(vec2 n) {\r\n\tfloat total = 0.0, amplitude = 1.0;\r\n\tfor (int i = 0; i < 4; i++) {\r\n\t\ttotal += noise(n) * amplitude;\r\n\t\tn += n;\r\n\t\tamplitude *= 0.5;\r\n\t}\r\n\treturn total;\r\n}\r\n\r\nfloat round(float number){\r\n\treturn sign(number)*floor(abs(number) + 0.5);\r\n}\r\n\r\nvoid main(void)\r\n{\r\n\tfloat brickW = 1.0 / numberOfBricksWidth;\r\n\tfloat brickH = 1.0 / numberOfBricksHeight;\r\n\tfloat jointWPercentage = 0.01;\r\n\tfloat jointHPercentage = 0.05;\r\n\tvec3 color = brickColor;\r\n\tfloat yi = vUV.y / brickH;\r\n\tfloat nyi = round(yi);\r\n\tfloat xi = vUV.x / brickW;\r\n\r\n\tif (mod(floor(yi), 2.0) == 0.0){\r\n\t\txi = xi - 0.5;\r\n\t}\r\n\r\n\tfloat nxi = round(xi);\r\n\tvec2 brickvUV = vec2((xi - floor(xi)) / brickH, (yi - floor(yi)) / brickW);\r\n\r\n\tif (yi < nyi + jointHPercentage && yi > nyi - jointHPercentage){\r\n\t\tcolor = mix(jointColor, vec3(0.37, 0.25, 0.25), (yi - nyi) / jointHPercentage + 0.2);\r\n\t}\r\n\telse if (xi < nxi + jointWPercentage && xi > nxi - jointWPercentage){\r\n\t\tcolor = mix(jointColor, vec3(0.44, 0.44, 0.44), (xi - nxi) / jointWPercentage + 0.2);\r\n\t}\r\n\telse {\r\n\t\tfloat brickColorSwitch = mod(floor(yi) + floor(xi), 3.0);\r\n\r\n\t\tif (brickColorSwitch == 0.0)\r\n\t\t\tcolor = mix(color, vec3(0.33, 0.33, 0.33), 0.3);\r\n\t\telse if (brickColorSwitch == 2.0)\r\n\t\t\tcolor = mix(color, vec3(0.11, 0.11, 0.11), 0.3);\r\n\t}\r\n\r\n\tgl_FragColor = vec4(color, 1.0);\r\n}";