babylon.brickProceduralTexture.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  16. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  17. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  18. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  19. return c > 3 && r && Object.defineProperty(target, key, r), r;
  20. };
  21. var BABYLON;
  22. (function (BABYLON) {
  23. var BrickProceduralTexture = /** @class */ (function (_super) {
  24. __extends(BrickProceduralTexture, _super);
  25. function BrickProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
  26. var _this = _super.call(this, name, size, "brickProceduralTexture", scene, fallbackTexture, generateMipMaps) || this;
  27. _this._numberOfBricksHeight = 15;
  28. _this._numberOfBricksWidth = 5;
  29. _this._jointColor = new BABYLON.Color3(0.72, 0.72, 0.72);
  30. _this._brickColor = new BABYLON.Color3(0.77, 0.47, 0.40);
  31. _this.updateShaderUniforms();
  32. return _this;
  33. }
  34. BrickProceduralTexture.prototype.updateShaderUniforms = function () {
  35. this.setFloat("numberOfBricksHeight", this._numberOfBricksHeight);
  36. this.setFloat("numberOfBricksWidth", this._numberOfBricksWidth);
  37. this.setColor3("brickColor", this._brickColor);
  38. this.setColor3("jointColor", this._jointColor);
  39. };
  40. Object.defineProperty(BrickProceduralTexture.prototype, "numberOfBricksHeight", {
  41. get: function () {
  42. return this._numberOfBricksHeight;
  43. },
  44. set: function (value) {
  45. this._numberOfBricksHeight = value;
  46. this.updateShaderUniforms();
  47. },
  48. enumerable: true,
  49. configurable: true
  50. });
  51. Object.defineProperty(BrickProceduralTexture.prototype, "numberOfBricksWidth", {
  52. get: function () {
  53. return this._numberOfBricksWidth;
  54. },
  55. set: function (value) {
  56. this._numberOfBricksWidth = value;
  57. this.updateShaderUniforms();
  58. },
  59. enumerable: true,
  60. configurable: true
  61. });
  62. Object.defineProperty(BrickProceduralTexture.prototype, "jointColor", {
  63. get: function () {
  64. return this._jointColor;
  65. },
  66. set: function (value) {
  67. this._jointColor = value;
  68. this.updateShaderUniforms();
  69. },
  70. enumerable: true,
  71. configurable: true
  72. });
  73. Object.defineProperty(BrickProceduralTexture.prototype, "brickColor", {
  74. get: function () {
  75. return this._brickColor;
  76. },
  77. set: function (value) {
  78. this._brickColor = value;
  79. this.updateShaderUniforms();
  80. },
  81. enumerable: true,
  82. configurable: true
  83. });
  84. /**
  85. * Serializes this brick procedural texture
  86. * @returns a serialized brick procedural texture object
  87. */
  88. BrickProceduralTexture.prototype.serialize = function () {
  89. var serializationObject = BABYLON.SerializationHelper.Serialize(this, _super.prototype.serialize.call(this));
  90. serializationObject.customType = "BABYLON.BrickProceduralTexture";
  91. return serializationObject;
  92. };
  93. /**
  94. * Creates a Brick Procedural Texture from parsed brick procedural texture data
  95. * @param parsedTexture defines parsed texture data
  96. * @param scene defines the current scene
  97. * @param rootUrl defines the root URL containing brick procedural texture information
  98. * @returns a parsed Brick Procedural Texture
  99. */
  100. BrickProceduralTexture.Parse = function (parsedTexture, scene, rootUrl) {
  101. var texture = BABYLON.SerializationHelper.Parse(function () { return new BrickProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps); }, parsedTexture, scene, rootUrl);
  102. return texture;
  103. };
  104. __decorate([
  105. BABYLON.serialize()
  106. ], BrickProceduralTexture.prototype, "numberOfBricksHeight", null);
  107. __decorate([
  108. BABYLON.serialize()
  109. ], BrickProceduralTexture.prototype, "numberOfBricksWidth", null);
  110. __decorate([
  111. BABYLON.serializeAsColor3()
  112. ], BrickProceduralTexture.prototype, "jointColor", null);
  113. __decorate([
  114. BABYLON.serializeAsColor3()
  115. ], BrickProceduralTexture.prototype, "brickColor", null);
  116. return BrickProceduralTexture;
  117. }(BABYLON.ProceduralTexture));
  118. BABYLON.BrickProceduralTexture = BrickProceduralTexture;
  119. })(BABYLON || (BABYLON = {}));
  120. //# sourceMappingURL=babylon.brickProceduralTexture.js.map
  121. 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}";