babylon.brickProceduralTexture.js 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. }
  14. BrickProceduralTexture.prototype.updateShaderUniforms = function () {
  15. this.setFloat("numberOfBricksHeight", this._numberOfBricksHeight);
  16. this.setFloat("numberOfBricksWidth", this._numberOfBricksWidth);
  17. this.setColor3("brickColor", this._brickColor);
  18. this.setColor3("jointColor", this._jointColor);
  19. };
  20. Object.defineProperty(BrickProceduralTexture.prototype, "numberOfBricksHeight", {
  21. get: function () {
  22. return this._numberOfBricksHeight;
  23. },
  24. set: function (value) {
  25. this._numberOfBricksHeight = value;
  26. this.updateShaderUniforms();
  27. },
  28. enumerable: true,
  29. configurable: true
  30. });
  31. Object.defineProperty(BrickProceduralTexture.prototype, "numberOfBricksWidth", {
  32. get: function () {
  33. return this._numberOfBricksWidth;
  34. },
  35. set: function (value) {
  36. this._numberOfBricksWidth = value;
  37. this.updateShaderUniforms();
  38. },
  39. enumerable: true,
  40. configurable: true
  41. });
  42. Object.defineProperty(BrickProceduralTexture.prototype, "jointColor", {
  43. get: function () {
  44. return this._jointColor;
  45. },
  46. set: function (value) {
  47. this._jointColor = value;
  48. this.updateShaderUniforms();
  49. },
  50. enumerable: true,
  51. configurable: true
  52. });
  53. Object.defineProperty(BrickProceduralTexture.prototype, "brickColor", {
  54. get: function () {
  55. return this._brickColor;
  56. },
  57. set: function (value) {
  58. this._brickColor = value;
  59. this.updateShaderUniforms();
  60. },
  61. enumerable: true,
  62. configurable: true
  63. });
  64. return BrickProceduralTexture;
  65. })(BABYLON.ProceduralTexture);
  66. BABYLON.BrickProceduralTexture = BrickProceduralTexture;
  67. })(BABYLON || (BABYLON = {}));
  68. 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}";