babylon.marbleProceduralTexture.js 7.3 KB

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