babylon.marbleProceduralTexture.js 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  3. var __extends = (this && this.__extends) || (function () {
  4. var 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 function (d, b) {
  8. extendStatics(d, b);
  9. function __() { this.constructor = d; }
  10. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  11. };
  12. })();
  13. var BABYLON;
  14. (function (BABYLON) {
  15. var MarbleProceduralTexture = /** @class */ (function (_super) {
  16. __extends(MarbleProceduralTexture, _super);
  17. function MarbleProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
  18. var _this = _super.call(this, name, size, "marbleProceduralTexture", scene, fallbackTexture, generateMipMaps) || this;
  19. _this._numberOfTilesHeight = 3;
  20. _this._numberOfTilesWidth = 3;
  21. _this._amplitude = 9.0;
  22. _this._jointColor = new BABYLON.Color3(0.72, 0.72, 0.72);
  23. _this.updateShaderUniforms();
  24. return _this;
  25. }
  26. MarbleProceduralTexture.prototype.updateShaderUniforms = function () {
  27. this.setFloat("numberOfTilesHeight", this._numberOfTilesHeight);
  28. this.setFloat("numberOfTilesWidth", this._numberOfTilesWidth);
  29. this.setFloat("amplitude", this._amplitude);
  30. this.setColor3("jointColor", this._jointColor);
  31. };
  32. Object.defineProperty(MarbleProceduralTexture.prototype, "numberOfTilesHeight", {
  33. get: function () {
  34. return this._numberOfTilesHeight;
  35. },
  36. set: function (value) {
  37. this._numberOfTilesHeight = value;
  38. this.updateShaderUniforms();
  39. },
  40. enumerable: true,
  41. configurable: true
  42. });
  43. Object.defineProperty(MarbleProceduralTexture.prototype, "amplitude", {
  44. get: function () {
  45. return this._amplitude;
  46. },
  47. set: function (value) {
  48. this._amplitude = value;
  49. this.updateShaderUniforms();
  50. },
  51. enumerable: true,
  52. configurable: true
  53. });
  54. Object.defineProperty(MarbleProceduralTexture.prototype, "numberOfTilesWidth", {
  55. get: function () {
  56. return this._numberOfTilesWidth;
  57. },
  58. set: function (value) {
  59. this._numberOfTilesWidth = value;
  60. this.updateShaderUniforms();
  61. },
  62. enumerable: true,
  63. configurable: true
  64. });
  65. Object.defineProperty(MarbleProceduralTexture.prototype, "jointColor", {
  66. get: function () {
  67. return this._jointColor;
  68. },
  69. set: function (value) {
  70. this._jointColor = value;
  71. this.updateShaderUniforms();
  72. },
  73. enumerable: true,
  74. configurable: true
  75. });
  76. return MarbleProceduralTexture;
  77. }(BABYLON.ProceduralTexture));
  78. BABYLON.MarbleProceduralTexture = MarbleProceduralTexture;
  79. })(BABYLON || (BABYLON = {}));
  80. //# sourceMappingURL=babylon.marbleProceduralTexture.js.map
  81. BABYLON.Effect.ShadersStore['marbleProceduralTexturePixelShader'] = "precision highp float;\nvarying vec2 vPosition;\nvarying vec2 vUV;\nuniform float numberOfTilesHeight;\nuniform float numberOfTilesWidth;\nuniform float amplitude;\nuniform vec3 marbleColor;\nuniform vec3 jointColor;\nconst vec3 tileSize=vec3(1.1,1.0,1.1);\nconst vec3 tilePct=vec3(0.98,1.0,0.98);\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 turbulence(vec2 P)\n{\nfloat val=0.0;\nfloat freq=1.0;\nfor (int i=0; i<4; i++)\n{\nval+=abs(noise(P*freq)/freq);\nfreq*=2.07;\n}\nreturn val;\n}\nfloat roundF(float number){\nreturn sign(number)*floor(abs(number)+0.5);\n}\nvec3 marble_color(float x)\n{\nvec3 col;\nx=0.5*(x+1.);\nx=sqrt(x); \nx=sqrt(x);\nx=sqrt(x);\ncol=vec3(.2+.75*x); \ncol.b*=0.95; \nreturn col;\n}\nvoid main()\n{\nfloat brickW=1.0/numberOfTilesWidth;\nfloat brickH=1.0/numberOfTilesHeight;\nfloat jointWPercentage=0.01;\nfloat jointHPercentage=0.01;\nvec3 color=marbleColor;\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 t=6.28*brickvUV.x/(tileSize.x+noise(vec2(vUV)*6.0));\nt+=amplitude*turbulence(brickvUV.xy);\nt=sin(t);\ncolor=marble_color(t);\n}\ngl_FragColor=vec4(color,0.0);\n}";