babylon.skyMaterial.min.js 11 KB

1
  1. var __decorate=this&&this.__decorate||function(e,n,t,r){if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)return Reflect.decorate(e,n,t,r);switch(arguments.length){case 2:return e.reduceRight(function(e,n){return n&&n(e)||e},n);case 3:return e.reduceRight(function(e,r){return void(r&&r(n,t))},void 0);case 4:return e.reduceRight(function(e,r){return r&&r(n,t,e)||e},r)}},BABYLON;!function(e){var n=function(e){function n(){e.call(this),this.CLIPPLANE=!1,this.POINTSIZE=!1,this.FOG=!1,this.VERTEXCOLOR=!1,this.VERTEXALPHA=!1,this._keys=Object.keys(this)}return __extends(n,e),n}(e.MaterialDefines),t=function(t){function r(r,i){t.call(this,r,i),this.luminance=1,this.turbidity=10,this.rayleigh=2,this.mieCoefficient=.005,this.mieDirectionalG=.8,this.distance=500,this.inclination=.49,this.azimuth=.25,this._sunPosition=e.Vector3.Zero(),this._defines=new n,this._cachedDefines=new n}return __extends(r,t),r.prototype.needAlphaBlending=function(){return this.alpha<1},r.prototype.needAlphaTesting=function(){return!1},r.prototype.getAlphaTestTexture=function(){return null},r.prototype._checkCache=function(e,n,t){return n?n._materialDefines&&n._materialDefines.isEqual(this._defines)?!0:!1:!0},r.prototype.isReady=function(t,r){if(this.checkReadyOnlyOnce&&this._wasPreviouslyReady)return!0;var i=this.getScene();if(!this.checkReadyOnEveryCall&&this._renderId===i.getRenderId()&&this._checkCache(i,t,r))return!0;i.getEngine();if(this._defines.reset(),i.clipPlane&&(this._defines.CLIPPLANE=!0),(this.pointsCloud||i.forcePointsCloud)&&(this._defines.POINTSIZE=!0),i.fogEnabled&&t&&t.applyFog&&i.fogMode!==e.Scene.FOGMODE_NONE&&this.fogEnabled&&(this._defines.FOG=!0),t&&t.useVertexColors&&t.isVerticesDataPresent(e.VertexBuffer.ColorKind)&&(this._defines.VERTEXCOLOR=!0,t.hasVertexAlpha&&(this._defines.VERTEXALPHA=!0)),!this._defines.isEqual(this._cachedDefines)||!this._effect){this._defines.cloneTo(this._cachedDefines),i.resetCachedMaterial();var o=new e.EffectFallbacks;this._defines.FOG&&o.addFallback(1,"FOG");var a=[e.VertexBuffer.PositionKind];this._defines.VERTEXCOLOR&&a.push(e.VertexBuffer.ColorKind);var s="sky",c=this._defines.toString();this._effect=i.getEngine().createEffect(s,a,["world","viewProjection","view","vFogInfos","vFogColor","pointSize","vClipPlane","luminance","turbidity","rayleigh","mieCoefficient","mieDirectionalG","sunPosition"],[],c,o,this.onCompiled,this.onError)}return this._effect.isReady()?(this._renderId=i.getRenderId(),this._wasPreviouslyReady=!0,t&&(t._materialDefines||(t._materialDefines=new n),this._defines.cloneTo(t._materialDefines)),!0):!1},r.prototype.bindOnlyWorldMatrix=function(e){this._effect.setMatrix("world",e)},r.prototype.bind=function(n,r){var i=this.getScene();if(this.bindOnlyWorldMatrix(n),this._effect.setMatrix("viewProjection",i.getTransformMatrix()),i.getCachedMaterial()!==this){if(i.clipPlane){var o=i.clipPlane;this._effect.setFloat4("vClipPlane",o.normal.x,o.normal.y,o.normal.z,o.d)}this.pointsCloud&&this._effect.setFloat("pointSize",this.pointSize)}i.fogEnabled&&r.applyFog&&i.fogMode!==e.Scene.FOGMODE_NONE&&this._effect.setMatrix("view",i.getViewMatrix()),e.MaterialHelper.BindFogParameters(i,r,this._effect),this._effect.setFloat("luminance",this.luminance),this._effect.setFloat("turbidity",this.turbidity),this._effect.setFloat("rayleigh",this.rayleigh),this._effect.setFloat("mieCoefficient",this.mieCoefficient),this._effect.setFloat("mieDirectionalG",this.mieDirectionalG);var a=Math.PI*(this.inclination-.5),s=2*Math.PI*(this.azimuth-.5);this._sunPosition.x=this.distance*Math.cos(s),this._sunPosition.y=this.distance*Math.sin(s)*Math.sin(a),this._sunPosition.z=this.distance*Math.sin(s)*Math.cos(a),this._effect.setVector3("sunPosition",this._sunPosition),t.prototype.bind.call(this,n,r)},r.prototype.getAnimatables=function(){return[]},r.prototype.dispose=function(e){t.prototype.dispose.call(this,e)},r.prototype.clone=function(n){var t=this;return e.SerializationHelper.Clone(function(){return new r(n,t.getScene())},this)},r.prototype.serialize=function(){var n=e.SerializationHelper.Serialize(this);return n.customType="BABYLON.SkyMaterial",n},r.Parse=function(n,t,i){return e.SerializationHelper.Parse(function(){return new r(n.name,t)},n,t,i)},__decorate([e.serialize()],r.prototype,"luminance"),__decorate([e.serialize()],r.prototype,"turbidity"),__decorate([e.serialize()],r.prototype,"rayleigh"),__decorate([e.serialize()],r.prototype,"mieCoefficient"),__decorate([e.serialize()],r.prototype,"mieDirectionalG"),__decorate([e.serialize()],r.prototype,"distance"),__decorate([e.serialize()],r.prototype,"inclination"),__decorate([e.serialize()],r.prototype,"azimuth"),r}(e.Material);e.SkyMaterial=t}(BABYLON||(BABYLON={})),BABYLON.Effect.ShadersStore.skyVertexShader="precision highp float;\r\n\r\n// Attributes\r\nattribute vec3 position;\r\n\r\n#ifdef VERTEXCOLOR\r\nattribute vec4 color;\r\n#endif\r\n\r\n// Uniforms\r\nuniform mat4 world;\r\nuniform mat4 view;\r\nuniform mat4 viewProjection;\r\n\r\n#ifdef POINTSIZE\r\nuniform float pointSize;\r\n#endif\r\n\r\n// Output\r\nvarying vec3 vPositionW;\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n#include<clipPlaneVertexDeclaration>\r\n#include<fogVertexDeclaration>\r\n\r\nvoid main(void) {\r\n gl_Position = viewProjection * world * vec4(position, 1.0);\r\n \r\n vec4 worldPos = world * vec4(position, 1.0);\r\n vPositionW = vec3(worldPos);\r\n\r\n // Clip plane\r\n#include<clipPlaneVertex>\r\n\r\n // Fog\r\n#include<fogVertex>\r\n\r\n // Vertex color\r\n#ifdef VERTEXCOLOR\r\n vColor = color;\r\n#endif\r\n\r\n // Point size\r\n#ifdef POINTSIZE\r\n gl_PointSize = pointSize;\r\n#endif\r\n}\r\n",BABYLON.Effect.ShadersStore.skyPixelShader="precision highp float;\r\n\r\n// Input\r\nvarying vec3 vPositionW;\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n#include<clipPlaneFragmentDeclaration>\r\n\r\n// Sky\r\nuniform float luminance;\r\nuniform float turbidity;\r\nuniform float rayleigh;\r\nuniform float mieCoefficient;\r\nuniform float mieDirectionalG;\r\nuniform vec3 sunPosition;\r\n\r\n// Fog\r\n#include<fogFragmentDeclaration>\r\n\r\n// Constants\r\nconst float e = 2.71828182845904523536028747135266249775724709369995957;\r\nconst float pi = 3.141592653589793238462643383279502884197169;\r\nconst float n = 1.0003;\r\nconst float N = 2.545E25;\r\nconst float pn = 0.035;\r\n\r\nconst vec3 lambda = vec3(680E-9, 550E-9, 450E-9);\r\n\r\nconst vec3 K = vec3(0.686, 0.678, 0.666);\r\nconst float v = 4.0;\r\n\r\nconst float rayleighZenithLength = 8.4E3;\r\nconst float mieZenithLength = 1.25E3;\r\nconst vec3 up = vec3(0.0, 1.0, 0.0);\r\n\r\nconst float EE = 1000.0;\r\nconst float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324;\r\n\r\nconst float cutoffAngle = pi/1.95;\r\nconst float steepness = 1.5;\r\n\r\nvec3 totalRayleigh(vec3 lambda)\r\n{\r\n return (8.0 * pow(pi, 3.0) * pow(pow(n, 2.0) - 1.0, 2.0) * (6.0 + 3.0 * pn)) / (3.0 * N * pow(lambda, vec3(4.0)) * (6.0 - 7.0 * pn));\r\n}\r\n\r\nvec3 simplifiedRayleigh()\r\n{\r\n return 0.0005 / vec3(94, 40, 18);\r\n}\r\n\r\nfloat rayleighPhase(float cosTheta)\r\n{ \r\n return (3.0 / (16.0*pi)) * (1.0 + pow(cosTheta, 2.0));\r\n}\r\n\r\nvec3 totalMie(vec3 lambda, vec3 K, float T)\r\n{\r\n float c = (0.2 * T ) * 10E-18;\r\n return 0.434 * c * pi * pow((2.0 * pi) / lambda, vec3(v - 2.0)) * K;\r\n}\r\n\r\nfloat hgPhase(float cosTheta, float g)\r\n{\r\n return (1.0 / (4.0*pi)) * ((1.0 - pow(g, 2.0)) / pow(1.0 - 2.0*g*cosTheta + pow(g, 2.0), 1.5));\r\n}\r\n\r\nfloat sunIntensity(float zenithAngleCos)\r\n{\r\n return EE * max(0.0, 1.0 - exp(-((cutoffAngle - acos(zenithAngleCos))/steepness)));\r\n}\r\n\r\nfloat A = 0.15;\r\nfloat B = 0.50;\r\nfloat C = 0.10;\r\nfloat D = 0.20;\r\nfloat EEE = 0.02;\r\nfloat F = 0.30;\r\nfloat W = 1000.0;\r\n\r\nvec3 Uncharted2Tonemap(vec3 x)\r\n{\r\n return ((x*(A*x+C*B)+D*EEE)/(x*(A*x+B)+D*F))-EEE/F;\r\n}\r\n\r\nvoid main(void) {\r\n // Clip plane\r\n#include<clipPlaneFragment>\r\n\r\n /**\r\n *--------------------------------------------------------------------------------------------------\r\n * Sky Color\r\n *--------------------------------------------------------------------------------------------------\r\n */\r\n const vec3 cameraPos = vec3(0.0, 0.0, 0.0);\r\n float sunfade = 1.0 - clamp(1.0 - exp((sunPosition.y / 450000.0)), 0.0, 1.0);\r\n float rayleighCoefficient = rayleigh - (1.0 * (1.0 - sunfade));\r\n vec3 sunDirection = normalize(sunPosition);\r\n float sunE = sunIntensity(dot(sunDirection, up));\r\n vec3 betaR = simplifiedRayleigh() * rayleighCoefficient;\r\n vec3 betaM = totalMie(lambda, K, turbidity) * mieCoefficient;\r\n float zenithAngle = acos(max(0.0, dot(up, normalize(vPositionW - cameraPos))));\r\n float sR = rayleighZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));\r\n float sM = mieZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));\r\n vec3 Fex = exp(-(betaR * sR + betaM * sM));\r\n float cosTheta = dot(normalize(vPositionW - cameraPos), sunDirection);\r\n float rPhase = rayleighPhase(cosTheta*0.5+0.5);\r\n vec3 betaRTheta = betaR * rPhase;\r\n float mPhase = hgPhase(cosTheta, mieDirectionalG);\r\n vec3 betaMTheta = betaM * mPhase;\r\n \r\n vec3 Lin = pow(sunE * ((betaRTheta + betaMTheta) / (betaR + betaM)) * (1.0 - Fex),vec3(1.5));\r\n Lin *= mix(vec3(1.0), pow(sunE * ((betaRTheta + betaMTheta) / (betaR + betaM)) * Fex, vec3(1.0 / 2.0)), clamp(pow(1.0-dot(up, sunDirection), 5.0), 0.0, 1.0));\r\n\r\n vec3 direction = normalize(vPositionW - cameraPos);\r\n float theta = acos(direction.y);\r\n float phi = atan(direction.z, direction.x);\r\n vec2 uv = vec2(phi, theta) / vec2(2.0 * pi, pi) + vec2(0.5, 0.0);\r\n vec3 L0 = vec3(0.1) * Fex;\r\n \r\n float sundisk = smoothstep(sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta);\r\n L0 += (sunE * 19000.0 * Fex) * sundisk;\r\n \r\n vec3 whiteScale = 1.0/Uncharted2Tonemap(vec3(W));\r\n vec3 texColor = (Lin+L0); \r\n texColor *= 0.04 ;\r\n texColor += vec3(0.0,0.001,0.0025)*0.3;\r\n\r\n float g_fMaxLuminance = 1.0;\r\n float fLumScaled = 0.1 / luminance; \r\n float fLumCompressed = (fLumScaled * (1.0 + (fLumScaled / (g_fMaxLuminance * g_fMaxLuminance)))) / (1.0 + fLumScaled); \r\n\r\n float ExposureBias = fLumCompressed;\r\n\r\n vec3 curr = Uncharted2Tonemap((log2(2.0/pow(luminance,4.0)))*texColor);\r\n vec3 skyColor = curr * whiteScale;\r\n\r\n vec3 retColor = pow(skyColor,vec3(1.0/(1.2+(1.2*sunfade))));\r\n \r\n vec4 baseColor = vec4(retColor, 1.0);\r\n /**\r\n *--------------------------------------------------------------------------------------------------\r\n * Sky Color\r\n *--------------------------------------------------------------------------------------------------\r\n */\r\n \r\n // Alpha\r\n float alpha = 1.0;\r\n\r\n#ifdef VERTEXCOLOR\r\n baseColor.rgb *= vColor.rgb;\r\n#endif\r\n\r\n // Lighting\r\n vec3 diffuseBase = vec3(1.0, 1.0, 1.0);\r\n\r\n#ifdef VERTEXALPHA\r\n alpha *= vColor.a;\r\n#endif\r\n\r\n // Composition\r\n vec4 color = vec4(baseColor.rgb, alpha);\r\n\r\n // Fog\r\n#include<fogFragment>\r\n\r\n gl_FragColor = color;\r\n}";