babylon.brickProceduralTexture.min.js 3.3 KB

1
  1. var BABYLON;!function(n){var r=function(r){function i(i,o,e,t,c){r.call(this,i,o,"brickProceduralTexture",e,t,c),this._numberOfBricksHeight=15,this._numberOfBricksWidth=5,this._jointColor=new n.Color3(.72,.72,.72),this._brickColor=new n.Color3(.77,.47,.4),this.updateShaderUniforms(),this.refreshRate=0}return __extends(i,r),i.prototype.updateShaderUniforms=function(){this.setFloat("numberOfBricksHeight",this._numberOfBricksHeight),this.setFloat("numberOfBricksWidth",this._numberOfBricksWidth),this.setColor3("brickColor",this._brickColor),this.setColor3("jointColor",this._jointColor)},Object.defineProperty(i.prototype,"numberOfBricksHeight",{get:function(){return this._numberOfBricksHeight},set:function(n){this._numberOfBricksHeight=n,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"numberOfBricksWidth",{get:function(){return this._numberOfBricksWidth},set:function(n){this._numberOfBricksWidth=n,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"jointColor",{get:function(){return this._jointColor},set:function(n){this._jointColor=n,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"brickColor",{get:function(){return this._brickColor},set:function(n){this._brickColor=n,this.updateShaderUniforms()},enumerable:!0,configurable:!0}),i}(n.ProceduralTexture);n.BrickProceduralTexture=r}(BABYLON||(BABYLON={})),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 return fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);\n}\n\nfloat noise(vec2 n) {\n const vec2 d = vec2(0.0, 1.0);\n vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));\n return 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 float total = 0.0, amplitude = 1.0;\n for (int i = 0; i < 4; i++) {\n total += noise(n) * amplitude;\n n += n;\n amplitude *= 0.5;\n }\n return total;\n}\n\nfloat round(float number){\n return sign(number)*floor(abs(number) + 0.5);\n}\n\nvoid main(void)\n{\n float brickW = 1.0 / numberOfBricksWidth;\n float brickH = 1.0 / numberOfBricksHeight;\n float jointWPercentage = 0.01;\n float jointHPercentage = 0.05;\n vec3 color = brickColor;\n float yi = vUV.y / brickH;\n float nyi = round(yi);\n float xi = vUV.x / brickW;\n\n if (mod(floor(yi), 2.0) == 0.0){\n xi = xi - 0.5;\n }\n\n float nxi = round(xi);\n vec2 brickvUV = vec2((xi - floor(xi)) / brickH, (yi - floor(yi)) / brickW);\n\n if (yi < nyi + jointHPercentage && yi > nyi - jointHPercentage){\n color = mix(jointColor, vec3(0.37, 0.25, 0.25), (yi - nyi) / jointHPercentage + 0.2);\n }\n else if (xi < nxi + jointWPercentage && xi > nxi - jointWPercentage){\n color = mix(jointColor, vec3(0.44, 0.44, 0.44), (xi - nxi) / jointWPercentage + 0.2);\n }\n else {\n float brickColorSwitch = mod(floor(yi) + floor(xi), 3.0);\n\n if (brickColorSwitch == 0.0)\n color = mix(color, vec3(0.33, 0.33, 0.33), 0.3);\n else if (brickColorSwitch == 2.0)\n color = mix(color, vec3(0.11, 0.11, 0.11), 0.3);\n }\n\n gl_FragColor = vec4(color, 1.0);\n}";