babylon.brickProceduralTexture.js 4.7 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;\n\nvarying vec2 vPosition;\nvarying vec2 vUV;\n\nuniform float numberOfBricksHeight;\nuniform float numberOfBricksWidth;\nuniform vec3 brickColor;\nuniform vec3 jointColor;\n\nfloat rand(vec2 n) {\n\treturn fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);\n}\n\nfloat noise(vec2 n) {\n\tconst vec2 d = vec2(0.0, 1.0);\n\tvec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));\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);\n}\n\nfloat fbm(vec2 n) {\n\tfloat total = 0.0, amplitude = 1.0;\n\tfor (int i = 0; i < 4; i++) {\n\t\ttotal += noise(n) * amplitude;\n\t\tn += n;\n\t\tamplitude *= 0.5;\n\t}\n\treturn total;\n}\n\nfloat round(float number){\n\treturn sign(number)*floor(abs(number) + 0.5);\n}\n\nvoid main(void)\n{\n\tfloat brickW = 1.0 / numberOfBricksWidth;\n\tfloat brickH = 1.0 / numberOfBricksHeight;\n\tfloat jointWPercentage = 0.01;\n\tfloat jointHPercentage = 0.05;\n\tvec3 color = brickColor;\n\tfloat yi = vUV.y / brickH;\n\tfloat nyi = round(yi);\n\tfloat xi = vUV.x / brickW;\n\n\tif (mod(floor(yi), 2.0) == 0.0){\n\t\txi = xi - 0.5;\n\t}\n\n\tfloat nxi = round(xi);\n\tvec2 brickvUV = vec2((xi - floor(xi)) / brickH, (yi - floor(yi)) / brickW);\n\n\tif (yi < nyi + jointHPercentage && yi > nyi - jointHPercentage){\n\t\tcolor = mix(jointColor, vec3(0.37, 0.25, 0.25), (yi - nyi) / jointHPercentage + 0.2);\n\t}\n\telse if (xi < nxi + jointWPercentage && xi > nxi - jointWPercentage){\n\t\tcolor = mix(jointColor, vec3(0.44, 0.44, 0.44), (xi - nxi) / jointWPercentage + 0.2);\n\t}\n\telse {\n\t\tfloat brickColorSwitch = mod(floor(yi) + floor(xi), 3.0);\n\n\t\tif (brickColorSwitch == 0.0)\n\t\t\tcolor = mix(color, vec3(0.33, 0.33, 0.33), 0.3);\n\t\telse if (brickColorSwitch == 2.0)\n\t\t\tcolor = mix(color, vec3(0.11, 0.11, 0.11), 0.3);\n\t}\n\n\tgl_FragColor = vec4(color, 1.0);\n}";