@onerjs/addons 8.37.7 → 8.37.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -64,8 +64,10 @@ vec2 getTransmittanceUV(float radius,float cosAngleLightToZenith,out float dista
64
64
  vec4 sampleTransmittanceLut(sampler2D transmittanceLut,float positionRadius,float cosAngleLightToZenith) {float distanceToHorizon;vec2 uv=getTransmittanceUV(positionRadius,cosAngleLightToZenith,distanceToHorizon);float weight=smoothstep(TransmittanceMinOccludedU,TransmittanceMaxUnoccludedU,uv.x);return weight*textureLod(transmittanceLut,uv,0.);}
65
65
  #endif
66
66
  #ifndef EXCLUDE_RAY_MARCHING_FUNCTIONS
67
+ #ifndef COMPUTE_MULTI_SCATTERING
67
68
  #define inline
68
69
  vec3 sampleMultiScatteringLut(sampler2D multiScatteringLut,float radius,float cosAngleLightToZenith) {vec2 unit=vec2(0.5+0.5*cosAngleLightToZenith,(radius-planetRadius)/atmosphereThickness);vec2 uv=unitToUV(unit,MultiScatteringLutDomainInUVSpace,MultiScatteringLutHalfTexelSize);vec3 multiScattering=textureLod(multiScatteringLut,uv,0.).rgb;return max(minMultiScattering,multiScattering);}
70
+ #endif
69
71
  const float uniformPhase=RECIPROCAL_PI4;
70
72
  #define inline
71
73
  vec3 integrateScatteredRadiance(
@@ -1 +1 @@
1
- {"version":3,"file":"atmosphereFunctions.js","sourceRoot":"","sources":["../../../../../../dev/addons/src/atmosphere/Shaders/ShadersInclude/atmosphereFunctions.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,OAAO,EAAE,WAAW,EAAE,4CAAiC;AACvD,sEAA2D;AAE3D,MAAM,IAAI,GAAG,qBAAqB,CAAC;AACnC,MAAM,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyOd,CAAC;AACF,aAAa;AACb,IAAI,CAAC,WAAW,CAAC,oBAAoB,CAAC,IAAI,CAAC,EAAE,CAAC;IAC1C,WAAW,CAAC,oBAAoB,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC;AACpD,CAAC;AACD,gBAAgB;AAChB,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC","sourcesContent":["// Do not edit.\nimport { ShaderStore } from \"core/Engines/shaderStore\";\nimport \"core/Shaders/ShadersInclude/intersectionFunctions\";\n\nconst name = \"atmosphereFunctions\";\nconst shader = `#include<intersectionFunctions>\nconst vec2 MultiScatteringLutSize=vec2(32.0,32.0);const vec2 MultiScatteringLutDomainInUVSpace=(MultiScatteringLutSize-vec2(1.0))/MultiScatteringLutSize;const vec2 MultiScatteringLutHalfTexelSize=vec2(0.5)/MultiScatteringLutSize;const float NumAerialPerspectiveLutLayers=32.0;const vec3 AerialPerspectiveLutSize=vec3(16.0,64.0,NumAerialPerspectiveLutLayers);const vec2 DiffuseSkyIrradianceLutSize=vec2(64.0,16.0);const vec2 DiffuseSkyIrradianceLutDomainInUVSpace=(DiffuseSkyIrradianceLutSize-vec2(1.0))/DiffuseSkyIrradianceLutSize;const vec2 DiffuseSkyIrradianceLutHalfTexelSize=vec2(0.5)/DiffuseSkyIrradianceLutSize;const vec2 SkyViewLutSize=vec2(128.0,128.0);const vec2 SkyViewLutDomainInUVSpace=(SkyViewLutSize-vec2(1.0))/SkyViewLutSize;const vec2 SkyViewLutHalfTexelSize=vec2(0.5)/SkyViewLutSize;const float AerialPerspectiveLutKMPerSlice=4.0;const float AerialPerspectiveLutRangeKM=AerialPerspectiveLutKMPerSlice*NumAerialPerspectiveLutLayers;const float TransmittanceSampleCount=128.0;const float SkyViewLutSampleCount=30.0;const vec2 TransmittanceLutSize=vec2(256.,64.);const vec2 TransmittanceLutDomainInUVSpace=(TransmittanceLutSize-vec2(1.))/TransmittanceLutSize;const vec2 TransmittanceLutHalfTexelSize=vec2(0.5)/TransmittanceLutSize;const float TransmittanceHorizonRange=2.*TransmittanceLutHalfTexelSize.x;const float TransmittanceMaxUnoccludedU=1.-0.5*TransmittanceHorizonRange;const float TransmittanceMinOccludedU=1.+0.5*TransmittanceHorizonRange;vec2 uvToUnit(vec2 uv,vec2 domainInUVSpace,vec2 halfTexelSize) {return (uv-halfTexelSize)/domainInUVSpace;}\nvec2 unitToUV(vec2 unit,vec2 domainInUVSpace,vec2 halfTexelSize) {return unit*domainInUVSpace+halfTexelSize;}\nfloat sphereIntersectNearest(vec3 rayOrigin,vec3 rayDirection,float sphereRadius) {vec2 result=sphereIntersectFromOrigin(rayOrigin,rayDirection,sphereRadius);float c=dot(rayOrigin,rayOrigin)-sphereRadius*sphereRadius;return c>=0.0 ?\nresult.y :\nresult.x;}\nvoid moveToTopAtmosphere(\nvec3 cameraPosition,\nfloat positionRadius,\nvec3 positionGeocentricNormal,\nvec3 rayDirection,\nout bool intersectsAtmosphere,\nout vec3 cameraPositionClampedToTopOfAtmosphere) {intersectsAtmosphere=true;cameraPositionClampedToTopOfAtmosphere=cameraPosition;if (positionRadius>atmosphereRadius) {float tTop=sphereIntersectNearest(cameraPosition,rayDirection,atmosphereRadius);if (tTop>=0.0) {vec3 upOffset=-planetRadiusOffset*positionGeocentricNormal;cameraPositionClampedToTopOfAtmosphere=cameraPosition+rayDirection*tTop+upOffset;} else {intersectsAtmosphere=false;}}}\nvoid getSkyViewUVFromParameters(\nbool intersectsGround,\nfloat cosHorizonAngleFromZenith,\nfloat cosAngleBetweenViewAndZenith,\nfloat cosAngleBetweenViewAndLightOnPlane,\nout vec2 uv)\n{vec2 unit=vec2(0.0);if (intersectsGround) {float coord=(cosAngleBetweenViewAndZenith+1.0)/(cosHorizonAngleFromZenith+1.0);coord=sqrtClamped(coord); \nunit.y=0.5*coord; } else {float coord=(cosAngleBetweenViewAndZenith-cosHorizonAngleFromZenith)/(1.0-cosHorizonAngleFromZenith);coord=sqrtClamped(coord); \nunit.y=0.5*coord+0.5; }\n{float coord=0.5-0.5*cosAngleBetweenViewAndLightOnPlane;unit.x=coord;}\nuv=unitToUV(unit,SkyViewLutDomainInUVSpace,SkyViewLutHalfTexelSize);}\n#if USE_SKY_VIEW_LUT && SAMPLE_SKY_VIEW_LUT\n#define inline\nvec4 sampleSkyViewLut(\nsampler2D skyViewLut,\nfloat positionRadius,\nvec3 geocentricNormal,\nvec3 rayDirection,\nvec3 directionToLight,\nfloat cosHorizonAngleFromZenith,\nout float cosAngleBetweenViewAndZenith,\nout bool isRayIntersectingGround) {cosAngleBetweenViewAndZenith=dot(rayDirection,geocentricNormal);if (positionRadius>atmosphereRadius) {float sinAngleBetweenViewAndNadir=sqrtClamped(1.-cosAngleBetweenViewAndZenith*cosAngleBetweenViewAndZenith);if (sinAngleBetweenViewAndNadir>sinCameraAtmosphereHorizonAngleFromNadir) {isRayIntersectingGround=false;return vec4(0.);}}\nvec3 sideVector=normalize(cross(geocentricNormal,rayDirection));vec3 forwardVector=normalize(cross(sideVector,geocentricNormal));vec2 lightOnPlane=normalize(vec2(dot(directionToLight,forwardVector),dot(directionToLight,sideVector)));float cosAngleBetweenViewAndLightOnPlane=lightOnPlane.x;float rayIntersectionScale=mix(0.95,1.,saturate((positionRadius-planetRadius)/atmosphereThickness));isRayIntersectingGround =\npositionRadius>planetRadius &&\n(rayIntersectionScale*cosAngleBetweenViewAndZenith)<=cosHorizonAngleFromZenith;vec2 uv;getSkyViewUVFromParameters(\nisRayIntersectingGround,\ncosHorizonAngleFromZenith,\ncosAngleBetweenViewAndZenith,\ncosAngleBetweenViewAndLightOnPlane,\nuv);return textureLod(skyViewLut,uv,0.);}\n#endif\nfloat computeRayleighPhase(float onePlusCosThetaSq) {return 0.0596831037*onePlusCosThetaSq;}\nfloat computeMiePhaseCornetteShanks(float cosTheta,float onePlusCosThetaSq) {const float g=0.8;const float gSquared=g*g;const float oneMinusGSquared=1.-gSquared;const float onePlusGSquared=1.+gSquared;const float twoPlusGSquared=2.+gSquared;const float twoG=2.*g;const float threeOverEightPi=3./(8.*PI);return threeOverEightPi*oneMinusGSquared*onePlusCosThetaSq/(twoPlusGSquared*pow(onePlusGSquared-twoG*cosTheta,1.5));}\nfloat computeOzoneDensity(float normalizedViewHeight) {const float MinOzoneDensity=0.135;const float OneMinusMinOzoneDensity=1.-MinOzoneDensity;const float OzoneStartHeight=.15; \nconst float PeakOzoneHeight=.25;const float MaxOzoneHeight=0.6;const float InverseRampupDistance=1./(PeakOzoneHeight-OzoneStartHeight);const float InverseRampdownDistance=1./(MaxOzoneHeight-PeakOzoneHeight);float lowerAtmosphereDensity=MinOzoneDensity+OneMinusMinOzoneDensity*max(0.,normalizedViewHeight-OzoneStartHeight)*InverseRampupDistance;float sqrtUpperAtmosphereDensity=max(0.,1.-(normalizedViewHeight-PeakOzoneHeight)*InverseRampdownDistance);float upperAtmosphereDensity=sqrtUpperAtmosphereDensity*sqrtUpperAtmosphereDensity;float densityOzone=normalizedViewHeight<PeakOzoneHeight ? lowerAtmosphereDensity : upperAtmosphereDensity;return densityOzone;}\nvoid sampleMediumRGB(\nfloat viewHeight,\nout vec3 scatteringRayleigh,\nout vec3 scatteringMie,\nout vec3 extinction,\nout vec3 scattering) {float normalizedViewHeight=saturate(viewHeight*inverseAtmosphereThickness);float densityMie=exp(-83.333*normalizedViewHeight);float densityRayleigh=exp(-12.5*normalizedViewHeight);float densityOzone=computeOzoneDensity(normalizedViewHeight);scatteringRayleigh=densityRayleigh*peakRayleighScattering;scatteringMie=densityMie*peakMieScattering;scattering=scatteringMie+scatteringRayleigh;vec3 extinctionRayleigh=scatteringRayleigh;vec3 extinctionMie=densityMie*peakMieExtinction;vec3 extinctionOzone=densityOzone*peakOzoneAbsorption;extinction=extinctionRayleigh+extinctionMie+extinctionOzone;}\nvec3 computeTransmittance(vec3 rayOriginGlobal,vec3 rayDirection,float tMax,float sampleCount) {vec3 opticalDepth=vec3(0.);float t=0.;float sampleSegmentWeight=tMax/sampleCount;const float sampleSegmentT=0.3;for (float s=0.; s<sampleCount; s+=1.) {float newT=sampleSegmentWeight*(s+sampleSegmentT);float dt=newT-t;t=newT;vec3 scatteringRayleigh,scatteringMie,extinction,scattering;vec3 samplePositionGlobal=rayOriginGlobal+t*rayDirection;sampleMediumRGB(length(samplePositionGlobal)-planetRadius,scatteringRayleigh,scatteringMie,extinction,scattering);opticalDepth+=extinction*dt;}\nreturn exp(-opticalDepth);}\n#if defined(SAMPLE_TRANSMITTANCE_LUT) || !defined(EXCLUDE_RAY_MARCHING_FUNCTIONS)\nvec2 getTransmittanceUV(float radius,float cosAngleLightToZenith,out float distanceToHorizon) {float radiusSquared=radius*radius;distanceToHorizon=sqrtClamped(radiusSquared-planetRadiusSquared);float cosAngleLightToZenithSquared=cosAngleLightToZenith*cosAngleLightToZenith;float discriminant=radiusSquared*(cosAngleLightToZenithSquared-1.)+atmosphereRadiusSquared;float distanceToAtmosphereEdge=max(0.,-radius*cosAngleLightToZenith+sqrtClamped(discriminant));float minDistanceToAtmosphereEdge=max(0.,atmosphereRadius-radius);float maxDistanceToAtmosphereEdge=distanceToHorizon+horizonDistanceToAtmosphereEdge;float cosAngleLightToZenithCoordinate=(distanceToAtmosphereEdge-minDistanceToAtmosphereEdge)/max(0.000001,maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);float distanceToHorizonCoordinate=distanceToHorizon/max(0.000001,horizonDistanceToAtmosphereEdge);vec2 unit=vec2(cosAngleLightToZenithCoordinate,distanceToHorizonCoordinate);return unit*TransmittanceLutDomainInUVSpace+TransmittanceLutHalfTexelSize; }\n#define inline\nvec4 sampleTransmittanceLut(sampler2D transmittanceLut,float positionRadius,float cosAngleLightToZenith) {float distanceToHorizon;vec2 uv=getTransmittanceUV(positionRadius,cosAngleLightToZenith,distanceToHorizon);float weight=smoothstep(TransmittanceMinOccludedU,TransmittanceMaxUnoccludedU,uv.x);return weight*textureLod(transmittanceLut,uv,0.);}\n#endif\n#ifndef EXCLUDE_RAY_MARCHING_FUNCTIONS\n#define inline\nvec3 sampleMultiScatteringLut(sampler2D multiScatteringLut,float radius,float cosAngleLightToZenith) {vec2 unit=vec2(0.5+0.5*cosAngleLightToZenith,(radius-planetRadius)/atmosphereThickness);vec2 uv=unitToUV(unit,MultiScatteringLutDomainInUVSpace,MultiScatteringLutHalfTexelSize);vec3 multiScattering=textureLod(multiScatteringLut,uv,0.).rgb;return max(minMultiScattering,multiScattering);}\nconst float uniformPhase=RECIPROCAL_PI4;\n#define inline\nvec3 integrateScatteredRadiance(\nbool isAerialPerspectiveLut,\nfloat lightIntensity,\nsampler2D transmittanceLut,\n#ifndef COMPUTE_MULTI_SCATTERING\nsampler2D multiScatteringLut,\nfloat multiScatteringIntensity,\n#endif\nvec3 rayOriginGlobal,\nvec3 rayDirection,\nvec3 directionToLight,\nfloat tMaxMax,\nfloat sampleCount,\nfloat distanceToSurface,\nout vec3 transmittance\n#if COMPUTE_MULTI_SCATTERING\n,out vec3 multiScattering\n#endif\n) {vec3 radiance=vec3(0.);transmittance=vec3(1.);\n#if COMPUTE_MULTI_SCATTERING\nmultiScattering=vec3(0.);\n#endif\nfloat tBottom=sphereIntersectNearest(rayOriginGlobal,rayDirection,planetRadius);float tTop=sphereIntersectNearest(rayOriginGlobal,rayDirection,atmosphereRadius);float tMax=0.;if (tBottom<0.) {if (tTop<0.) {return radiance;} else {tMax=tTop;}} else {if (tTop>0.) {if (isAerialPerspectiveLut) {tMax=tTop;} else {tMax=min(tBottom,tTop);}}}\nif (distanceToSurface>0. && distanceToSurface<tMax) {tMax=distanceToSurface;}\ntMax=min(tMax,tMaxMax);\n#ifndef COMPUTE_MULTI_SCATTERING\nfloat cosTheta=dot(rayDirection,directionToLight);float onePlusCosThetaSq=1.+cosTheta*cosTheta;float rayleighPhase=computeRayleighPhase(onePlusCosThetaSq);float miePhase=computeMiePhaseCornetteShanks(cosTheta,onePlusCosThetaSq);\n#endif\nfloat transmittanceScale=isAerialPerspectiveLut ? aerialPerspectiveTransmittanceScale : 1.;float t=0.;float sampleSegmentWeight=tMax/sampleCount;const float sampleSegmentT=0.3;for (float s=0.; s<sampleCount; s+=1.) {float newT=sampleSegmentWeight*(s+sampleSegmentT);float dt=newT-t;t=newT;vec3 samplePositionGlobal=rayOriginGlobal+t*rayDirection;float sampleRadiusGlobal=length(samplePositionGlobal);vec3 sampleGeocentricNormal=samplePositionGlobal/sampleRadiusGlobal;float sampleCosAngleLightToZenith=dot(directionToLight,sampleGeocentricNormal);vec3 scatteringRayleigh,scatteringMie,extinction,scattering;sampleMediumRGB(sampleRadiusGlobal-planetRadius,scatteringRayleigh,scatteringMie,extinction,scattering);vec3 transmittanceToLight=sampleTransmittanceLut(transmittanceLut,sampleRadiusGlobal,sampleCosAngleLightToZenith).rgb;\n#if COMPUTE_MULTI_SCATTERING\nvec3 phaseTimesScattering=uniformPhase*scattering;vec3 S=transmittanceToLight*phaseTimesScattering;\n#else\nvec3 phaseTimesScattering=scatteringMie*miePhase+scatteringRayleigh*rayleighPhase;vec3 multiScatteredRadiance=sampleMultiScatteringLut(multiScatteringLut,sampleRadiusGlobal,sampleCosAngleLightToZenith);vec3 S=transmittanceScale*transmittanceToLight*phaseTimesScattering+multiScatteringIntensity*multiScatteredRadiance*scattering;\n#endif\nvec3 sampleOpticalDepth=extinction*dt;vec3 sampleTransmittance=exp(-sampleOpticalDepth);vec3 clampedExtinction=max(vec3(0.0000001),extinction);vec3 SInt=(S-S*sampleTransmittance)/clampedExtinction;radiance+=transmittance*SInt;\n#if COMPUTE_MULTI_SCATTERING\nvec3 MSInt=(scattering-scattering*sampleTransmittance)/clampedExtinction;multiScattering+=transmittance*MSInt;\n#endif\ntransmittance*=sampleTransmittance;}\n#if USE_GROUND_ALBEDO\nif (tMax==tBottom && tBottom>0.) {vec3 planetPos=rayOriginGlobal+tBottom*rayDirection;float planetPosRadius=length(planetPos);vec3 planetPosGeocentricNormal=planetPos/planetPosRadius;float nDotL=dot(directionToLight,planetPosGeocentricNormal);vec3 lightTransmittance=sampleTransmittanceLut(transmittanceLut,planetPosRadius,nDotL).rgb;const float diffuseBrdf=RECIPROCAL_PI;radiance+=lightTransmittance*transmittance*groundAlbedo*(nDotL*diffuseBrdf);}\n#endif\nradiance*=lightIntensity;return radiance;}\n#endif\nfloat layerIdxToAerialPerspectiveLayer(float layerIdx) {float layer=(layerIdx+1.)/NumAerialPerspectiveLutLayers;layer*=layer; \nlayer*=NumAerialPerspectiveLutLayers;return layer;}\nfloat toAerialPerspectiveDepth(float layer) {return layer*AerialPerspectiveLutKMPerSlice;}\nfloat toAerialPerspectiveLayer(float distance,float aerialPerspectiveLutDistancePerSlice) {return distance/aerialPerspectiveLutDistancePerSlice;}\nvec4 applyAerialPerspectiveSaturation(vec4 aerialPerspective) {float previousRadiance=getLuminance(aerialPerspective.rgb);aerialPerspective.rgb=mix(vec3(previousRadiance),aerialPerspective.rgb,aerialPerspectiveSaturation);return aerialPerspective;}\nvec4 applyAerialPerspectiveIntensity(vec4 aerialPerspective) {\n#if APPLY_AERIAL_PERSPECTIVE_INTENSITY\nif (aerialPerspectiveIntensity==0.) {aerialPerspective=vec4(0.);} else {float previousAlpha=aerialPerspective.a;aerialPerspective/=max(0.00001,previousAlpha);aerialPerspective*=pow(previousAlpha,1./aerialPerspectiveIntensity);}\n#endif\nreturn aerialPerspective;}\nvec4 applyAerialPerspectiveRadianceBias(vec4 aerialPerspective) {\n#if APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS\nfloat originalRadiance=dot(aerialPerspective.rgb,LuminanceEncodeApprox);float targetRadiance=originalRadiance+aerialPerspectiveRadianceBias;if (originalRadiance>0.) {aerialPerspective*=max(0.,targetRadiance/originalRadiance);} else {aerialPerspective=max(vec4(0.),vec4(aerialPerspectiveRadianceBias));}\naerialPerspective.a=min(aerialPerspective.a,1.);\n#endif\nreturn aerialPerspective;}\nbool sampleAerialPerspectiveLut(\nvec2 screenUV,\nbool clampToLutRange,\nfloat distanceFromCamera,\nfloat numAerialPerspectiveLutLayers,\nfloat aerialPerspectiveLutKMPerSlice,\nfloat aerialPerspectiveLutRangeKM,\nout vec4 aerialPerspective) {aerialPerspective=vec4(0.);\n#if USE_AERIAL_PERSPECTIVE_LUT\nif (distanceFromCamera>0. &&\n(clampToLutRange || distanceFromCamera<aerialPerspectiveLutRangeKM) &&\nclampedCameraRadius<=atmosphereRadius) {float layer=toAerialPerspectiveLayer(distanceFromCamera,aerialPerspectiveLutKMPerSlice);float normalizedLayer=sqrt(layer/numAerialPerspectiveLutLayers); \nlayer=min(normalizedLayer*numAerialPerspectiveLutLayers,numAerialPerspectiveLutLayers);float weight=min(layer,1.);float layerIdx=max(0.,layer-1.);float floorLayerIdx=floor(layerIdx);vec4 aerialPerspectiveLayer0=textureLod(aerialPerspectiveLut,vec3(screenUV,floorLayerIdx),0.);vec4 aerialPerspectiveLayer1=textureLod(aerialPerspectiveLut,vec3(screenUV,floorLayerIdx+1.),0.);aerialPerspective=mix(aerialPerspectiveLayer0,aerialPerspectiveLayer1,layerIdx-floorLayerIdx);aerialPerspective.rgb*=atmosphereExposure;aerialPerspective=applyAerialPerspectiveSaturation(aerialPerspective);aerialPerspective=weight*applyAerialPerspectiveIntensity(aerialPerspective);aerialPerspective=applyAerialPerspectiveRadianceBias(aerialPerspective);return true;}\n#endif\nreturn false;}\n#if RENDER_TRANSMITTANCE\nvoid getTransmittanceParameters(vec2 uv,out float radius,out float cosAngleLightToZenith,out float distanceToAtmosphereEdge) {vec2 unit=uvToUnit(uv,TransmittanceLutDomainInUVSpace,TransmittanceLutHalfTexelSize);float distanceToHorizon=unit.y*horizonDistanceToAtmosphereEdge;float distanceToHorizonSquared=distanceToHorizon*distanceToHorizon;radius=sqrtClamped(distanceToHorizonSquared+planetRadiusSquared);float minDistanceToAtmosphereEdge=atmosphereRadius-radius;float maxDistanceToAtmosphereEdge=distanceToHorizon+horizonDistanceToAtmosphereEdge;distanceToAtmosphereEdge=minDistanceToAtmosphereEdge+unit.x*(maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);float distanceToAtmosphereEdgeSquared=distanceToAtmosphereEdge*distanceToAtmosphereEdge;cosAngleLightToZenith =\ndistanceToAtmosphereEdge<=0. ?\n1. :\n(horizonDistanceToAtmosphereEdgeSquared-distanceToAtmosphereEdgeSquared-distanceToHorizonSquared)/(2.*radius*distanceToAtmosphereEdge);cosAngleLightToZenith=clamp(cosAngleLightToZenith,-1.,1.);}\nvec4 renderTransmittance(vec2 uv) {float radius,cosAngleLightToZenith,distanceToAtmosphereEdgeAlongAngle;getTransmittanceParameters(uv,radius,cosAngleLightToZenith,distanceToAtmosphereEdgeAlongAngle);float sinAngleLightToZenith=sqrtClamped(1.-cosAngleLightToZenith*cosAngleLightToZenith);vec3 directionToLight=normalize(vec3(0.,cosAngleLightToZenith,sinAngleLightToZenith));vec3 transmittance=computeTransmittance(vec3(0.,radius,0.),directionToLight,distanceToAtmosphereEdgeAlongAngle,TransmittanceSampleCount);return vec4(transmittance,avg(transmittance));}\n#endif\n#if RENDER_MULTI_SCATTERING\nvec3 getSphereSample(float azimuth,float inclination,out float sinInclination) {sinInclination=sin(inclination);return vec3(sinInclination*sin(azimuth),cos(inclination),sinInclination*cos(azimuth));}\nconst float MultiScatteringInclinationSampleCount=8.;const float MultiScatteringAzimuthSampleCount=2.*MultiScatteringInclinationSampleCount;const float MultiScatteringLutSampleCount=64.;const float MultiScatteringAzimuthIterationAngle=TWO_PI/MultiScatteringAzimuthSampleCount;const float MultiScatteringInclinationIterationAngle=PI/MultiScatteringInclinationSampleCount;const float MultiScatteringAngleStepProduct=MultiScatteringAzimuthIterationAngle*MultiScatteringInclinationIterationAngle;\n#define inline\nvec4 renderMultiScattering(vec2 uv,sampler2D transmittanceLut) {vec2 unit=uvToUnit(uv,MultiScatteringLutDomainInUVSpace,MultiScatteringLutHalfTexelSize);float cosAngleLightToZenith=2.*unit.x-1.;float sinAngleLightToZenith=sqrtClamped(1.-cosAngleLightToZenith*cosAngleLightToZenith);vec3 directionToLight=normalize(vec3(0.,cosAngleLightToZenith,sinAngleLightToZenith));float rayOriginRadius=planetRadius+max(unit.y,0.001)*atmosphereThickness;vec3 rayOrigin=vec3(0.,rayOriginRadius,0.);vec3 inscattered=vec3(0.);vec3 multiScatteringTotal=vec3(0.);for (float i=0.5; i<MultiScatteringAzimuthSampleCount; ++i) {float azimuth=MultiScatteringAzimuthIterationAngle*i;for (float j=0.5; j<MultiScatteringInclinationSampleCount; ++j) {float inclination=MultiScatteringInclinationIterationAngle*j;float sinInclination;vec3 rayDirection=getSphereSample(azimuth,inclination,sinInclination);vec3 transmittance;vec3 multiScattering;vec3 radiance=integrateScatteredRadiance(\nfalse,\n1.,\ntransmittanceLut,\nrayOrigin,\nrayDirection,\ndirectionToLight,\n100000000.,\nMultiScatteringLutSampleCount,\n-1.,\ntransmittance,\nmultiScattering);float weight=RECIPROCAL_PI4*abs(sinInclination)*MultiScatteringAngleStepProduct;multiScatteringTotal+=multiScattering*weight;inscattered+=radiance*weight;}}\nvec3 multiScattering=inscattered/max(vec3(0.000001),vec3(1.)-multiScatteringTotal);return vec4(multiScattering,1.);}\n#endif\nfloat computeCosHorizonAngleFromZenith(float radius) {float sinAngleBetweenHorizonAndNadir=min(1.,planetRadius/radius);float cosHorizonAngleFromNadir=sqrt(1.-sinAngleBetweenHorizonAndNadir*sinAngleBetweenHorizonAndNadir);float cosHorizonAngleFromZenith=-cosHorizonAngleFromNadir;return cosHorizonAngleFromZenith;}\n#if RENDER_SKY_VIEW\nvoid getSkyViewParametersFromUV(\nfloat radius,\nvec2 uv,\nout float cosAngleBetweenViewAndZenith,\nout float cosAngleBetweenViewAndLightOnPlane) {vec2 unit=uvToUnit(uv,SkyViewLutDomainInUVSpace,SkyViewLutHalfTexelSize);float cosHorizonAngleFromZenith=computeCosHorizonAngleFromZenith(radius);if (unit.y<0.5) {float coord=2.*unit.y; \ncoord*=coord; \ncosAngleBetweenViewAndZenith=mix(-1.,cosHorizonAngleFromZenith,coord); } else {float coord=2.*unit.y-1.; \ncoord*=coord; \ncosAngleBetweenViewAndZenith=mix(cosHorizonAngleFromZenith,1.,coord); }\n{float coord=unit.x;cosAngleBetweenViewAndLightOnPlane=1.-2.*coord;}}\n#define inline\nvec4 renderSkyView(vec2 uv,sampler2D transmittanceLut,sampler2D multiScatteringLut) {float cosAngleBetweenViewAndZenith;float cosAngleBetweenViewAndLightOnPlane;getSkyViewParametersFromUV(clampedCameraRadius,uv,cosAngleBetweenViewAndZenith,cosAngleBetweenViewAndLightOnPlane);float sinAngleBetweenViewAndZenith=sqrtClamped(1.-cosAngleBetweenViewAndZenith*cosAngleBetweenViewAndZenith);float sinAngleBetweenViewAndLightOnPlane=sqrtClamped(1.-cosAngleBetweenViewAndLightOnPlane*cosAngleBetweenViewAndLightOnPlane);vec3 rayDirection =\nvec3(\nsinAngleBetweenViewAndZenith*cosAngleBetweenViewAndLightOnPlane,\ncosAngleBetweenViewAndZenith,\nsinAngleBetweenViewAndZenith*sinAngleBetweenViewAndLightOnPlane);bool intersectsAtmosphere=false;vec3 cameraPositionGlobalClampedToTopOfAtmosphere=vec3(0.);moveToTopAtmosphere(\nvec3(0.,clampedCameraRadius,0.),\nclampedCameraRadius,\nvec3(0.,1.,0.),\nrayDirection,\nintersectsAtmosphere,\ncameraPositionGlobalClampedToTopOfAtmosphere);if (!intersectsAtmosphere) {return vec4(0.);}\nvec3 transmittance;vec3 radiance=integrateScatteredRadiance(\nfalse,\natmosphereExposure*lightIntensity,\ntransmittanceLut,\nmultiScatteringLut,\nmultiScatteringIntensity,\ncameraPositionGlobalClampedToTopOfAtmosphere,\nrayDirection,\ndirectionToLightRelativeToCameraGeocentricNormal,\n100000000.,\nSkyViewLutSampleCount,\n-1.,\ntransmittance);float transparency=1.-avg(transmittance);return vec4(radiance,transparency);}\n#endif\n#if RENDER_CAMERA_VOLUME\n#define inline\nvec4 renderCameraVolume(\nvec3 positionOnNearPlane,\nfloat layerIdx,\nsampler2D transmittanceLut,\nsampler2D multiScatteringLut) {vec4 result=vec4(0.);vec3 rayDirection=normalize(positionOnNearPlane);float layer=layerIdxToAerialPerspectiveLayer(layerIdx);float tMax=toAerialPerspectiveDepth(layer);float tMaxMax=tMax;vec3 cameraPositionGlobalClampedToTopOfAtmosphere=clampedCameraPositionGlobal;if (clampedCameraRadius>=atmosphereRadius) {bool intersectsAtmosphere=false;moveToTopAtmosphere(\nclampedCameraPositionGlobal,\nclampedCameraRadius,\ncameraGeocentricNormal,\nrayDirection,\nintersectsAtmosphere,\ncameraPositionGlobalClampedToTopOfAtmosphere);if (!intersectsAtmosphere) {return result;}\nfloat distanceToAtmosphere=distance(clampedCameraPositionGlobal,cameraPositionGlobalClampedToTopOfAtmosphere);if (tMaxMax<distanceToAtmosphere) {return result;}\ntMaxMax=max(0.,tMaxMax-distanceToAtmosphere);}\nfloat sampleCount=min(SkyViewLutSampleCount,2.*layer+2.);vec3 transmittance;vec3 radiance=integrateScatteredRadiance(\ntrue,\nlightIntensity,\ntransmittanceLut,\nmultiScatteringLut,\nmultiScatteringIntensity,\ncameraPositionGlobalClampedToTopOfAtmosphere,\nrayDirection,\ndirectionToLight,\ntMaxMax,\nsampleCount,\n-1.,\ntransmittance);float transparency=1.-avg(transmittance);result=vec4(radiance,transparency);return result;}\n#endif\n`;\n// Sideeffect\nif (!ShaderStore.IncludesShadersStore[name]) {\n ShaderStore.IncludesShadersStore[name] = shader;\n}\n/** @internal */\nexport const atmosphereFunctions = { name, shader };\n"]}
1
+ {"version":3,"file":"atmosphereFunctions.js","sourceRoot":"","sources":["../../../../../../dev/addons/src/atmosphere/Shaders/ShadersInclude/atmosphereFunctions.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,OAAO,EAAE,WAAW,EAAE,4CAAiC;AACvD,sEAA2D;AAE3D,MAAM,IAAI,GAAG,qBAAqB,CAAC;AACnC,MAAM,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2Od,CAAC;AACF,aAAa;AACb,IAAI,CAAC,WAAW,CAAC,oBAAoB,CAAC,IAAI,CAAC,EAAE,CAAC;IAC1C,WAAW,CAAC,oBAAoB,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC;AACpD,CAAC;AACD,gBAAgB;AAChB,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC","sourcesContent":["// Do not edit.\nimport { ShaderStore } from \"core/Engines/shaderStore\";\nimport \"core/Shaders/ShadersInclude/intersectionFunctions\";\n\nconst name = \"atmosphereFunctions\";\nconst shader = `#include<intersectionFunctions>\nconst vec2 MultiScatteringLutSize=vec2(32.0,32.0);const vec2 MultiScatteringLutDomainInUVSpace=(MultiScatteringLutSize-vec2(1.0))/MultiScatteringLutSize;const vec2 MultiScatteringLutHalfTexelSize=vec2(0.5)/MultiScatteringLutSize;const float NumAerialPerspectiveLutLayers=32.0;const vec3 AerialPerspectiveLutSize=vec3(16.0,64.0,NumAerialPerspectiveLutLayers);const vec2 DiffuseSkyIrradianceLutSize=vec2(64.0,16.0);const vec2 DiffuseSkyIrradianceLutDomainInUVSpace=(DiffuseSkyIrradianceLutSize-vec2(1.0))/DiffuseSkyIrradianceLutSize;const vec2 DiffuseSkyIrradianceLutHalfTexelSize=vec2(0.5)/DiffuseSkyIrradianceLutSize;const vec2 SkyViewLutSize=vec2(128.0,128.0);const vec2 SkyViewLutDomainInUVSpace=(SkyViewLutSize-vec2(1.0))/SkyViewLutSize;const vec2 SkyViewLutHalfTexelSize=vec2(0.5)/SkyViewLutSize;const float AerialPerspectiveLutKMPerSlice=4.0;const float AerialPerspectiveLutRangeKM=AerialPerspectiveLutKMPerSlice*NumAerialPerspectiveLutLayers;const float TransmittanceSampleCount=128.0;const float SkyViewLutSampleCount=30.0;const vec2 TransmittanceLutSize=vec2(256.,64.);const vec2 TransmittanceLutDomainInUVSpace=(TransmittanceLutSize-vec2(1.))/TransmittanceLutSize;const vec2 TransmittanceLutHalfTexelSize=vec2(0.5)/TransmittanceLutSize;const float TransmittanceHorizonRange=2.*TransmittanceLutHalfTexelSize.x;const float TransmittanceMaxUnoccludedU=1.-0.5*TransmittanceHorizonRange;const float TransmittanceMinOccludedU=1.+0.5*TransmittanceHorizonRange;vec2 uvToUnit(vec2 uv,vec2 domainInUVSpace,vec2 halfTexelSize) {return (uv-halfTexelSize)/domainInUVSpace;}\nvec2 unitToUV(vec2 unit,vec2 domainInUVSpace,vec2 halfTexelSize) {return unit*domainInUVSpace+halfTexelSize;}\nfloat sphereIntersectNearest(vec3 rayOrigin,vec3 rayDirection,float sphereRadius) {vec2 result=sphereIntersectFromOrigin(rayOrigin,rayDirection,sphereRadius);float c=dot(rayOrigin,rayOrigin)-sphereRadius*sphereRadius;return c>=0.0 ?\nresult.y :\nresult.x;}\nvoid moveToTopAtmosphere(\nvec3 cameraPosition,\nfloat positionRadius,\nvec3 positionGeocentricNormal,\nvec3 rayDirection,\nout bool intersectsAtmosphere,\nout vec3 cameraPositionClampedToTopOfAtmosphere) {intersectsAtmosphere=true;cameraPositionClampedToTopOfAtmosphere=cameraPosition;if (positionRadius>atmosphereRadius) {float tTop=sphereIntersectNearest(cameraPosition,rayDirection,atmosphereRadius);if (tTop>=0.0) {vec3 upOffset=-planetRadiusOffset*positionGeocentricNormal;cameraPositionClampedToTopOfAtmosphere=cameraPosition+rayDirection*tTop+upOffset;} else {intersectsAtmosphere=false;}}}\nvoid getSkyViewUVFromParameters(\nbool intersectsGround,\nfloat cosHorizonAngleFromZenith,\nfloat cosAngleBetweenViewAndZenith,\nfloat cosAngleBetweenViewAndLightOnPlane,\nout vec2 uv)\n{vec2 unit=vec2(0.0);if (intersectsGround) {float coord=(cosAngleBetweenViewAndZenith+1.0)/(cosHorizonAngleFromZenith+1.0);coord=sqrtClamped(coord); \nunit.y=0.5*coord; } else {float coord=(cosAngleBetweenViewAndZenith-cosHorizonAngleFromZenith)/(1.0-cosHorizonAngleFromZenith);coord=sqrtClamped(coord); \nunit.y=0.5*coord+0.5; }\n{float coord=0.5-0.5*cosAngleBetweenViewAndLightOnPlane;unit.x=coord;}\nuv=unitToUV(unit,SkyViewLutDomainInUVSpace,SkyViewLutHalfTexelSize);}\n#if USE_SKY_VIEW_LUT && SAMPLE_SKY_VIEW_LUT\n#define inline\nvec4 sampleSkyViewLut(\nsampler2D skyViewLut,\nfloat positionRadius,\nvec3 geocentricNormal,\nvec3 rayDirection,\nvec3 directionToLight,\nfloat cosHorizonAngleFromZenith,\nout float cosAngleBetweenViewAndZenith,\nout bool isRayIntersectingGround) {cosAngleBetweenViewAndZenith=dot(rayDirection,geocentricNormal);if (positionRadius>atmosphereRadius) {float sinAngleBetweenViewAndNadir=sqrtClamped(1.-cosAngleBetweenViewAndZenith*cosAngleBetweenViewAndZenith);if (sinAngleBetweenViewAndNadir>sinCameraAtmosphereHorizonAngleFromNadir) {isRayIntersectingGround=false;return vec4(0.);}}\nvec3 sideVector=normalize(cross(geocentricNormal,rayDirection));vec3 forwardVector=normalize(cross(sideVector,geocentricNormal));vec2 lightOnPlane=normalize(vec2(dot(directionToLight,forwardVector),dot(directionToLight,sideVector)));float cosAngleBetweenViewAndLightOnPlane=lightOnPlane.x;float rayIntersectionScale=mix(0.95,1.,saturate((positionRadius-planetRadius)/atmosphereThickness));isRayIntersectingGround =\npositionRadius>planetRadius &&\n(rayIntersectionScale*cosAngleBetweenViewAndZenith)<=cosHorizonAngleFromZenith;vec2 uv;getSkyViewUVFromParameters(\nisRayIntersectingGround,\ncosHorizonAngleFromZenith,\ncosAngleBetweenViewAndZenith,\ncosAngleBetweenViewAndLightOnPlane,\nuv);return textureLod(skyViewLut,uv,0.);}\n#endif\nfloat computeRayleighPhase(float onePlusCosThetaSq) {return 0.0596831037*onePlusCosThetaSq;}\nfloat computeMiePhaseCornetteShanks(float cosTheta,float onePlusCosThetaSq) {const float g=0.8;const float gSquared=g*g;const float oneMinusGSquared=1.-gSquared;const float onePlusGSquared=1.+gSquared;const float twoPlusGSquared=2.+gSquared;const float twoG=2.*g;const float threeOverEightPi=3./(8.*PI);return threeOverEightPi*oneMinusGSquared*onePlusCosThetaSq/(twoPlusGSquared*pow(onePlusGSquared-twoG*cosTheta,1.5));}\nfloat computeOzoneDensity(float normalizedViewHeight) {const float MinOzoneDensity=0.135;const float OneMinusMinOzoneDensity=1.-MinOzoneDensity;const float OzoneStartHeight=.15; \nconst float PeakOzoneHeight=.25;const float MaxOzoneHeight=0.6;const float InverseRampupDistance=1./(PeakOzoneHeight-OzoneStartHeight);const float InverseRampdownDistance=1./(MaxOzoneHeight-PeakOzoneHeight);float lowerAtmosphereDensity=MinOzoneDensity+OneMinusMinOzoneDensity*max(0.,normalizedViewHeight-OzoneStartHeight)*InverseRampupDistance;float sqrtUpperAtmosphereDensity=max(0.,1.-(normalizedViewHeight-PeakOzoneHeight)*InverseRampdownDistance);float upperAtmosphereDensity=sqrtUpperAtmosphereDensity*sqrtUpperAtmosphereDensity;float densityOzone=normalizedViewHeight<PeakOzoneHeight ? lowerAtmosphereDensity : upperAtmosphereDensity;return densityOzone;}\nvoid sampleMediumRGB(\nfloat viewHeight,\nout vec3 scatteringRayleigh,\nout vec3 scatteringMie,\nout vec3 extinction,\nout vec3 scattering) {float normalizedViewHeight=saturate(viewHeight*inverseAtmosphereThickness);float densityMie=exp(-83.333*normalizedViewHeight);float densityRayleigh=exp(-12.5*normalizedViewHeight);float densityOzone=computeOzoneDensity(normalizedViewHeight);scatteringRayleigh=densityRayleigh*peakRayleighScattering;scatteringMie=densityMie*peakMieScattering;scattering=scatteringMie+scatteringRayleigh;vec3 extinctionRayleigh=scatteringRayleigh;vec3 extinctionMie=densityMie*peakMieExtinction;vec3 extinctionOzone=densityOzone*peakOzoneAbsorption;extinction=extinctionRayleigh+extinctionMie+extinctionOzone;}\nvec3 computeTransmittance(vec3 rayOriginGlobal,vec3 rayDirection,float tMax,float sampleCount) {vec3 opticalDepth=vec3(0.);float t=0.;float sampleSegmentWeight=tMax/sampleCount;const float sampleSegmentT=0.3;for (float s=0.; s<sampleCount; s+=1.) {float newT=sampleSegmentWeight*(s+sampleSegmentT);float dt=newT-t;t=newT;vec3 scatteringRayleigh,scatteringMie,extinction,scattering;vec3 samplePositionGlobal=rayOriginGlobal+t*rayDirection;sampleMediumRGB(length(samplePositionGlobal)-planetRadius,scatteringRayleigh,scatteringMie,extinction,scattering);opticalDepth+=extinction*dt;}\nreturn exp(-opticalDepth);}\n#if defined(SAMPLE_TRANSMITTANCE_LUT) || !defined(EXCLUDE_RAY_MARCHING_FUNCTIONS)\nvec2 getTransmittanceUV(float radius,float cosAngleLightToZenith,out float distanceToHorizon) {float radiusSquared=radius*radius;distanceToHorizon=sqrtClamped(radiusSquared-planetRadiusSquared);float cosAngleLightToZenithSquared=cosAngleLightToZenith*cosAngleLightToZenith;float discriminant=radiusSquared*(cosAngleLightToZenithSquared-1.)+atmosphereRadiusSquared;float distanceToAtmosphereEdge=max(0.,-radius*cosAngleLightToZenith+sqrtClamped(discriminant));float minDistanceToAtmosphereEdge=max(0.,atmosphereRadius-radius);float maxDistanceToAtmosphereEdge=distanceToHorizon+horizonDistanceToAtmosphereEdge;float cosAngleLightToZenithCoordinate=(distanceToAtmosphereEdge-minDistanceToAtmosphereEdge)/max(0.000001,maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);float distanceToHorizonCoordinate=distanceToHorizon/max(0.000001,horizonDistanceToAtmosphereEdge);vec2 unit=vec2(cosAngleLightToZenithCoordinate,distanceToHorizonCoordinate);return unit*TransmittanceLutDomainInUVSpace+TransmittanceLutHalfTexelSize; }\n#define inline\nvec4 sampleTransmittanceLut(sampler2D transmittanceLut,float positionRadius,float cosAngleLightToZenith) {float distanceToHorizon;vec2 uv=getTransmittanceUV(positionRadius,cosAngleLightToZenith,distanceToHorizon);float weight=smoothstep(TransmittanceMinOccludedU,TransmittanceMaxUnoccludedU,uv.x);return weight*textureLod(transmittanceLut,uv,0.);}\n#endif\n#ifndef EXCLUDE_RAY_MARCHING_FUNCTIONS\n#ifndef COMPUTE_MULTI_SCATTERING\n#define inline\nvec3 sampleMultiScatteringLut(sampler2D multiScatteringLut,float radius,float cosAngleLightToZenith) {vec2 unit=vec2(0.5+0.5*cosAngleLightToZenith,(radius-planetRadius)/atmosphereThickness);vec2 uv=unitToUV(unit,MultiScatteringLutDomainInUVSpace,MultiScatteringLutHalfTexelSize);vec3 multiScattering=textureLod(multiScatteringLut,uv,0.).rgb;return max(minMultiScattering,multiScattering);}\n#endif\nconst float uniformPhase=RECIPROCAL_PI4;\n#define inline\nvec3 integrateScatteredRadiance(\nbool isAerialPerspectiveLut,\nfloat lightIntensity,\nsampler2D transmittanceLut,\n#ifndef COMPUTE_MULTI_SCATTERING\nsampler2D multiScatteringLut,\nfloat multiScatteringIntensity,\n#endif\nvec3 rayOriginGlobal,\nvec3 rayDirection,\nvec3 directionToLight,\nfloat tMaxMax,\nfloat sampleCount,\nfloat distanceToSurface,\nout vec3 transmittance\n#if COMPUTE_MULTI_SCATTERING\n,out vec3 multiScattering\n#endif\n) {vec3 radiance=vec3(0.);transmittance=vec3(1.);\n#if COMPUTE_MULTI_SCATTERING\nmultiScattering=vec3(0.);\n#endif\nfloat tBottom=sphereIntersectNearest(rayOriginGlobal,rayDirection,planetRadius);float tTop=sphereIntersectNearest(rayOriginGlobal,rayDirection,atmosphereRadius);float tMax=0.;if (tBottom<0.) {if (tTop<0.) {return radiance;} else {tMax=tTop;}} else {if (tTop>0.) {if (isAerialPerspectiveLut) {tMax=tTop;} else {tMax=min(tBottom,tTop);}}}\nif (distanceToSurface>0. && distanceToSurface<tMax) {tMax=distanceToSurface;}\ntMax=min(tMax,tMaxMax);\n#ifndef COMPUTE_MULTI_SCATTERING\nfloat cosTheta=dot(rayDirection,directionToLight);float onePlusCosThetaSq=1.+cosTheta*cosTheta;float rayleighPhase=computeRayleighPhase(onePlusCosThetaSq);float miePhase=computeMiePhaseCornetteShanks(cosTheta,onePlusCosThetaSq);\n#endif\nfloat transmittanceScale=isAerialPerspectiveLut ? aerialPerspectiveTransmittanceScale : 1.;float t=0.;float sampleSegmentWeight=tMax/sampleCount;const float sampleSegmentT=0.3;for (float s=0.; s<sampleCount; s+=1.) {float newT=sampleSegmentWeight*(s+sampleSegmentT);float dt=newT-t;t=newT;vec3 samplePositionGlobal=rayOriginGlobal+t*rayDirection;float sampleRadiusGlobal=length(samplePositionGlobal);vec3 sampleGeocentricNormal=samplePositionGlobal/sampleRadiusGlobal;float sampleCosAngleLightToZenith=dot(directionToLight,sampleGeocentricNormal);vec3 scatteringRayleigh,scatteringMie,extinction,scattering;sampleMediumRGB(sampleRadiusGlobal-planetRadius,scatteringRayleigh,scatteringMie,extinction,scattering);vec3 transmittanceToLight=sampleTransmittanceLut(transmittanceLut,sampleRadiusGlobal,sampleCosAngleLightToZenith).rgb;\n#if COMPUTE_MULTI_SCATTERING\nvec3 phaseTimesScattering=uniformPhase*scattering;vec3 S=transmittanceToLight*phaseTimesScattering;\n#else\nvec3 phaseTimesScattering=scatteringMie*miePhase+scatteringRayleigh*rayleighPhase;vec3 multiScatteredRadiance=sampleMultiScatteringLut(multiScatteringLut,sampleRadiusGlobal,sampleCosAngleLightToZenith);vec3 S=transmittanceScale*transmittanceToLight*phaseTimesScattering+multiScatteringIntensity*multiScatteredRadiance*scattering;\n#endif\nvec3 sampleOpticalDepth=extinction*dt;vec3 sampleTransmittance=exp(-sampleOpticalDepth);vec3 clampedExtinction=max(vec3(0.0000001),extinction);vec3 SInt=(S-S*sampleTransmittance)/clampedExtinction;radiance+=transmittance*SInt;\n#if COMPUTE_MULTI_SCATTERING\nvec3 MSInt=(scattering-scattering*sampleTransmittance)/clampedExtinction;multiScattering+=transmittance*MSInt;\n#endif\ntransmittance*=sampleTransmittance;}\n#if USE_GROUND_ALBEDO\nif (tMax==tBottom && tBottom>0.) {vec3 planetPos=rayOriginGlobal+tBottom*rayDirection;float planetPosRadius=length(planetPos);vec3 planetPosGeocentricNormal=planetPos/planetPosRadius;float nDotL=dot(directionToLight,planetPosGeocentricNormal);vec3 lightTransmittance=sampleTransmittanceLut(transmittanceLut,planetPosRadius,nDotL).rgb;const float diffuseBrdf=RECIPROCAL_PI;radiance+=lightTransmittance*transmittance*groundAlbedo*(nDotL*diffuseBrdf);}\n#endif\nradiance*=lightIntensity;return radiance;}\n#endif\nfloat layerIdxToAerialPerspectiveLayer(float layerIdx) {float layer=(layerIdx+1.)/NumAerialPerspectiveLutLayers;layer*=layer; \nlayer*=NumAerialPerspectiveLutLayers;return layer;}\nfloat toAerialPerspectiveDepth(float layer) {return layer*AerialPerspectiveLutKMPerSlice;}\nfloat toAerialPerspectiveLayer(float distance,float aerialPerspectiveLutDistancePerSlice) {return distance/aerialPerspectiveLutDistancePerSlice;}\nvec4 applyAerialPerspectiveSaturation(vec4 aerialPerspective) {float previousRadiance=getLuminance(aerialPerspective.rgb);aerialPerspective.rgb=mix(vec3(previousRadiance),aerialPerspective.rgb,aerialPerspectiveSaturation);return aerialPerspective;}\nvec4 applyAerialPerspectiveIntensity(vec4 aerialPerspective) {\n#if APPLY_AERIAL_PERSPECTIVE_INTENSITY\nif (aerialPerspectiveIntensity==0.) {aerialPerspective=vec4(0.);} else {float previousAlpha=aerialPerspective.a;aerialPerspective/=max(0.00001,previousAlpha);aerialPerspective*=pow(previousAlpha,1./aerialPerspectiveIntensity);}\n#endif\nreturn aerialPerspective;}\nvec4 applyAerialPerspectiveRadianceBias(vec4 aerialPerspective) {\n#if APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS\nfloat originalRadiance=dot(aerialPerspective.rgb,LuminanceEncodeApprox);float targetRadiance=originalRadiance+aerialPerspectiveRadianceBias;if (originalRadiance>0.) {aerialPerspective*=max(0.,targetRadiance/originalRadiance);} else {aerialPerspective=max(vec4(0.),vec4(aerialPerspectiveRadianceBias));}\naerialPerspective.a=min(aerialPerspective.a,1.);\n#endif\nreturn aerialPerspective;}\nbool sampleAerialPerspectiveLut(\nvec2 screenUV,\nbool clampToLutRange,\nfloat distanceFromCamera,\nfloat numAerialPerspectiveLutLayers,\nfloat aerialPerspectiveLutKMPerSlice,\nfloat aerialPerspectiveLutRangeKM,\nout vec4 aerialPerspective) {aerialPerspective=vec4(0.);\n#if USE_AERIAL_PERSPECTIVE_LUT\nif (distanceFromCamera>0. &&\n(clampToLutRange || distanceFromCamera<aerialPerspectiveLutRangeKM) &&\nclampedCameraRadius<=atmosphereRadius) {float layer=toAerialPerspectiveLayer(distanceFromCamera,aerialPerspectiveLutKMPerSlice);float normalizedLayer=sqrt(layer/numAerialPerspectiveLutLayers); \nlayer=min(normalizedLayer*numAerialPerspectiveLutLayers,numAerialPerspectiveLutLayers);float weight=min(layer,1.);float layerIdx=max(0.,layer-1.);float floorLayerIdx=floor(layerIdx);vec4 aerialPerspectiveLayer0=textureLod(aerialPerspectiveLut,vec3(screenUV,floorLayerIdx),0.);vec4 aerialPerspectiveLayer1=textureLod(aerialPerspectiveLut,vec3(screenUV,floorLayerIdx+1.),0.);aerialPerspective=mix(aerialPerspectiveLayer0,aerialPerspectiveLayer1,layerIdx-floorLayerIdx);aerialPerspective.rgb*=atmosphereExposure;aerialPerspective=applyAerialPerspectiveSaturation(aerialPerspective);aerialPerspective=weight*applyAerialPerspectiveIntensity(aerialPerspective);aerialPerspective=applyAerialPerspectiveRadianceBias(aerialPerspective);return true;}\n#endif\nreturn false;}\n#if RENDER_TRANSMITTANCE\nvoid getTransmittanceParameters(vec2 uv,out float radius,out float cosAngleLightToZenith,out float distanceToAtmosphereEdge) {vec2 unit=uvToUnit(uv,TransmittanceLutDomainInUVSpace,TransmittanceLutHalfTexelSize);float distanceToHorizon=unit.y*horizonDistanceToAtmosphereEdge;float distanceToHorizonSquared=distanceToHorizon*distanceToHorizon;radius=sqrtClamped(distanceToHorizonSquared+planetRadiusSquared);float minDistanceToAtmosphereEdge=atmosphereRadius-radius;float maxDistanceToAtmosphereEdge=distanceToHorizon+horizonDistanceToAtmosphereEdge;distanceToAtmosphereEdge=minDistanceToAtmosphereEdge+unit.x*(maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);float distanceToAtmosphereEdgeSquared=distanceToAtmosphereEdge*distanceToAtmosphereEdge;cosAngleLightToZenith =\ndistanceToAtmosphereEdge<=0. ?\n1. :\n(horizonDistanceToAtmosphereEdgeSquared-distanceToAtmosphereEdgeSquared-distanceToHorizonSquared)/(2.*radius*distanceToAtmosphereEdge);cosAngleLightToZenith=clamp(cosAngleLightToZenith,-1.,1.);}\nvec4 renderTransmittance(vec2 uv) {float radius,cosAngleLightToZenith,distanceToAtmosphereEdgeAlongAngle;getTransmittanceParameters(uv,radius,cosAngleLightToZenith,distanceToAtmosphereEdgeAlongAngle);float sinAngleLightToZenith=sqrtClamped(1.-cosAngleLightToZenith*cosAngleLightToZenith);vec3 directionToLight=normalize(vec3(0.,cosAngleLightToZenith,sinAngleLightToZenith));vec3 transmittance=computeTransmittance(vec3(0.,radius,0.),directionToLight,distanceToAtmosphereEdgeAlongAngle,TransmittanceSampleCount);return vec4(transmittance,avg(transmittance));}\n#endif\n#if RENDER_MULTI_SCATTERING\nvec3 getSphereSample(float azimuth,float inclination,out float sinInclination) {sinInclination=sin(inclination);return vec3(sinInclination*sin(azimuth),cos(inclination),sinInclination*cos(azimuth));}\nconst float MultiScatteringInclinationSampleCount=8.;const float MultiScatteringAzimuthSampleCount=2.*MultiScatteringInclinationSampleCount;const float MultiScatteringLutSampleCount=64.;const float MultiScatteringAzimuthIterationAngle=TWO_PI/MultiScatteringAzimuthSampleCount;const float MultiScatteringInclinationIterationAngle=PI/MultiScatteringInclinationSampleCount;const float MultiScatteringAngleStepProduct=MultiScatteringAzimuthIterationAngle*MultiScatteringInclinationIterationAngle;\n#define inline\nvec4 renderMultiScattering(vec2 uv,sampler2D transmittanceLut) {vec2 unit=uvToUnit(uv,MultiScatteringLutDomainInUVSpace,MultiScatteringLutHalfTexelSize);float cosAngleLightToZenith=2.*unit.x-1.;float sinAngleLightToZenith=sqrtClamped(1.-cosAngleLightToZenith*cosAngleLightToZenith);vec3 directionToLight=normalize(vec3(0.,cosAngleLightToZenith,sinAngleLightToZenith));float rayOriginRadius=planetRadius+max(unit.y,0.001)*atmosphereThickness;vec3 rayOrigin=vec3(0.,rayOriginRadius,0.);vec3 inscattered=vec3(0.);vec3 multiScatteringTotal=vec3(0.);for (float i=0.5; i<MultiScatteringAzimuthSampleCount; ++i) {float azimuth=MultiScatteringAzimuthIterationAngle*i;for (float j=0.5; j<MultiScatteringInclinationSampleCount; ++j) {float inclination=MultiScatteringInclinationIterationAngle*j;float sinInclination;vec3 rayDirection=getSphereSample(azimuth,inclination,sinInclination);vec3 transmittance;vec3 multiScattering;vec3 radiance=integrateScatteredRadiance(\nfalse,\n1.,\ntransmittanceLut,\nrayOrigin,\nrayDirection,\ndirectionToLight,\n100000000.,\nMultiScatteringLutSampleCount,\n-1.,\ntransmittance,\nmultiScattering);float weight=RECIPROCAL_PI4*abs(sinInclination)*MultiScatteringAngleStepProduct;multiScatteringTotal+=multiScattering*weight;inscattered+=radiance*weight;}}\nvec3 multiScattering=inscattered/max(vec3(0.000001),vec3(1.)-multiScatteringTotal);return vec4(multiScattering,1.);}\n#endif\nfloat computeCosHorizonAngleFromZenith(float radius) {float sinAngleBetweenHorizonAndNadir=min(1.,planetRadius/radius);float cosHorizonAngleFromNadir=sqrt(1.-sinAngleBetweenHorizonAndNadir*sinAngleBetweenHorizonAndNadir);float cosHorizonAngleFromZenith=-cosHorizonAngleFromNadir;return cosHorizonAngleFromZenith;}\n#if RENDER_SKY_VIEW\nvoid getSkyViewParametersFromUV(\nfloat radius,\nvec2 uv,\nout float cosAngleBetweenViewAndZenith,\nout float cosAngleBetweenViewAndLightOnPlane) {vec2 unit=uvToUnit(uv,SkyViewLutDomainInUVSpace,SkyViewLutHalfTexelSize);float cosHorizonAngleFromZenith=computeCosHorizonAngleFromZenith(radius);if (unit.y<0.5) {float coord=2.*unit.y; \ncoord*=coord; \ncosAngleBetweenViewAndZenith=mix(-1.,cosHorizonAngleFromZenith,coord); } else {float coord=2.*unit.y-1.; \ncoord*=coord; \ncosAngleBetweenViewAndZenith=mix(cosHorizonAngleFromZenith,1.,coord); }\n{float coord=unit.x;cosAngleBetweenViewAndLightOnPlane=1.-2.*coord;}}\n#define inline\nvec4 renderSkyView(vec2 uv,sampler2D transmittanceLut,sampler2D multiScatteringLut) {float cosAngleBetweenViewAndZenith;float cosAngleBetweenViewAndLightOnPlane;getSkyViewParametersFromUV(clampedCameraRadius,uv,cosAngleBetweenViewAndZenith,cosAngleBetweenViewAndLightOnPlane);float sinAngleBetweenViewAndZenith=sqrtClamped(1.-cosAngleBetweenViewAndZenith*cosAngleBetweenViewAndZenith);float sinAngleBetweenViewAndLightOnPlane=sqrtClamped(1.-cosAngleBetweenViewAndLightOnPlane*cosAngleBetweenViewAndLightOnPlane);vec3 rayDirection =\nvec3(\nsinAngleBetweenViewAndZenith*cosAngleBetweenViewAndLightOnPlane,\ncosAngleBetweenViewAndZenith,\nsinAngleBetweenViewAndZenith*sinAngleBetweenViewAndLightOnPlane);bool intersectsAtmosphere=false;vec3 cameraPositionGlobalClampedToTopOfAtmosphere=vec3(0.);moveToTopAtmosphere(\nvec3(0.,clampedCameraRadius,0.),\nclampedCameraRadius,\nvec3(0.,1.,0.),\nrayDirection,\nintersectsAtmosphere,\ncameraPositionGlobalClampedToTopOfAtmosphere);if (!intersectsAtmosphere) {return vec4(0.);}\nvec3 transmittance;vec3 radiance=integrateScatteredRadiance(\nfalse,\natmosphereExposure*lightIntensity,\ntransmittanceLut,\nmultiScatteringLut,\nmultiScatteringIntensity,\ncameraPositionGlobalClampedToTopOfAtmosphere,\nrayDirection,\ndirectionToLightRelativeToCameraGeocentricNormal,\n100000000.,\nSkyViewLutSampleCount,\n-1.,\ntransmittance);float transparency=1.-avg(transmittance);return vec4(radiance,transparency);}\n#endif\n#if RENDER_CAMERA_VOLUME\n#define inline\nvec4 renderCameraVolume(\nvec3 positionOnNearPlane,\nfloat layerIdx,\nsampler2D transmittanceLut,\nsampler2D multiScatteringLut) {vec4 result=vec4(0.);vec3 rayDirection=normalize(positionOnNearPlane);float layer=layerIdxToAerialPerspectiveLayer(layerIdx);float tMax=toAerialPerspectiveDepth(layer);float tMaxMax=tMax;vec3 cameraPositionGlobalClampedToTopOfAtmosphere=clampedCameraPositionGlobal;if (clampedCameraRadius>=atmosphereRadius) {bool intersectsAtmosphere=false;moveToTopAtmosphere(\nclampedCameraPositionGlobal,\nclampedCameraRadius,\ncameraGeocentricNormal,\nrayDirection,\nintersectsAtmosphere,\ncameraPositionGlobalClampedToTopOfAtmosphere);if (!intersectsAtmosphere) {return result;}\nfloat distanceToAtmosphere=distance(clampedCameraPositionGlobal,cameraPositionGlobalClampedToTopOfAtmosphere);if (tMaxMax<distanceToAtmosphere) {return result;}\ntMaxMax=max(0.,tMaxMax-distanceToAtmosphere);}\nfloat sampleCount=min(SkyViewLutSampleCount,2.*layer+2.);vec3 transmittance;vec3 radiance=integrateScatteredRadiance(\ntrue,\nlightIntensity,\ntransmittanceLut,\nmultiScatteringLut,\nmultiScatteringIntensity,\ncameraPositionGlobalClampedToTopOfAtmosphere,\nrayDirection,\ndirectionToLight,\ntMaxMax,\nsampleCount,\n-1.,\ntransmittance);float transparency=1.-avg(transmittance);result=vec4(radiance,transparency);return result;}\n#endif\n`;\n// Sideeffect\nif (!ShaderStore.IncludesShadersStore[name]) {\n ShaderStore.IncludesShadersStore[name] = shader;\n}\n/** @internal */\nexport const atmosphereFunctions = { name, shader };\n"]}
@@ -1,3 +1,4 @@
1
+ import "@onerjs/core/ShadersWGSL/ShadersInclude/intersectionFunctions.js";
1
2
  /** @internal */
2
3
  export declare const atmosphereFunctionsWGSL: {
3
4
  name: string;
@@ -1,9 +1,15 @@
1
1
  // Do not edit.
2
2
  import { ShaderStore } from "@onerjs/core/Engines/shaderStore.js";
3
+ import "@onerjs/core/ShadersWGSL/ShadersInclude/intersectionFunctions.js";
3
4
  const name = "atmosphereFunctions";
4
- const shader = `const NumAerialPerspectiveLutLayers: f32=32.0;const AerialPerspectiveLutSize: vec3f=vec3f(16.0,64.0,NumAerialPerspectiveLutLayers);const AerialPerspectiveLutKMPerSlice: f32=4.0;const AerialPerspectiveLutRangeKM: f32=AerialPerspectiveLutKMPerSlice*NumAerialPerspectiveLutLayers;const TransmittanceSampleCount: i32=128;const TransmittanceLutSize: vec2f=vec2f(256.0,64.0);const TransmittanceLutDomainInUVSpace: vec2f=(TransmittanceLutSize-vec2f(1.0,1.0))/TransmittanceLutSize;const TransmittanceLutHalfTexelSize: vec2f=vec2f(0.5,0.5)/TransmittanceLutSize;const TransmittanceHorizonRange: f32=2.0*TransmittanceLutHalfTexelSize.x;const TransmittanceMaxUnoccludedU: f32=1.0-0.5*TransmittanceHorizonRange;const TransmittanceMinOccludedU: f32=1.0+0.5*TransmittanceHorizonRange;fn uvToUnit(uv: vec2f,domainInUVSpace: vec2f,halfTexelSize: vec2f)->vec2f {return (uv-halfTexelSize)/domainInUVSpace;}
5
- fn computeOzoneDensity(normalizedViewHeight: f32)->f32 {let MinOzoneDensity: f32=0.135;let OneMinusMinOzoneDensity: f32=1.0-MinOzoneDensity;let OzoneStartHeight: f32=0.15;
6
- let PeakOzoneHeight: f32=0.25;let MaxOzoneHeight: f32=0.6;let InverseRampupDistance: f32=1.0/(PeakOzoneHeight-OzoneStartHeight);let InverseRampdownDistance: f32=1.0/(MaxOzoneHeight-PeakOzoneHeight);let lowerAtmosphereDensity=MinOzoneDensity+OneMinusMinOzoneDensity*max(0.0,normalizedViewHeight-OzoneStartHeight)*InverseRampupDistance;let sqrtUpperAtmosphereDensity=max(0.0,1.0-(normalizedViewHeight-PeakOzoneHeight)*InverseRampdownDistance);let upperAtmosphereDensity=sqrtUpperAtmosphereDensity*sqrtUpperAtmosphereDensity;return select(upperAtmosphereDensity,lowerAtmosphereDensity,normalizedViewHeight<PeakOzoneHeight);}
5
+ const shader = `#include<intersectionFunctions>
6
+ const MultiScatteringLutSize=vec2f(32.,32.);const MultiScatteringLutDomainInUVSpace=(MultiScatteringLutSize-vec2f(1.))/MultiScatteringLutSize;const MultiScatteringLutHalfTexelSize=vec2f(.5)/MultiScatteringLutSize;const NumAerialPerspectiveLutLayers=32.;const AerialPerspectiveLutSize=vec3f(16.,64.,NumAerialPerspectiveLutLayers);const AerialPerspectiveLutKMPerSlice=4.;const AerialPerspectiveLutRangeKM=AerialPerspectiveLutKMPerSlice*NumAerialPerspectiveLutLayers;const TransmittanceSampleCount=128;const TransmittanceLutSize=vec2f(256.,64.);const TransmittanceLutDomainInUVSpace=(TransmittanceLutSize-vec2f(1.))/TransmittanceLutSize;const TransmittanceLutHalfTexelSize=vec2f(.5)/TransmittanceLutSize;const TransmittanceHorizonRange=2.*TransmittanceLutHalfTexelSize.x;const TransmittanceMaxUnoccludedU=1.-.5*TransmittanceHorizonRange;const TransmittanceMinOccludedU=1.+.5*TransmittanceHorizonRange;fn uvToUnit(uv: vec2f,domainInUVSpace: vec2f,halfTexelSize: vec2f)->vec2f {return (uv-halfTexelSize)/domainInUVSpace;}
7
+ fn unitToUV(unit: vec2f,domainInUVSpace: vec2f,halfTexelSize: vec2f)->vec2f {return unit*domainInUVSpace+halfTexelSize;}
8
+ fn sphereIntersectNearest(rayOrigin: vec3f,rayDirection: vec3f,sphereRadius: f32)->f32 {let result=sphereIntersectFromOrigin(rayOrigin,rayDirection,sphereRadius);let c=dot(rayOrigin,rayOrigin)-sphereRadius*sphereRadius;return select(result.x,result.y,c>=0.);}
9
+ fn computeRayleighPhase(onePlusCosThetaSq: f32)->f32 {return 0.0596831037*onePlusCosThetaSq;}
10
+ fn computeMiePhaseCornetteShanks(cosTheta: f32,onePlusCosThetaSq: f32)->f32 {const g=.8;const gSquared=g*g;const oneMinusGSquared=1.-gSquared;const onePlusGSquared=1.+gSquared;const twoPlusGSquared=2.+gSquared;const twoG=2.*g;const threeOverEightPi=3./(8.*PI);return threeOverEightPi*oneMinusGSquared*onePlusCosThetaSq/(twoPlusGSquared*pow(onePlusGSquared-twoG*cosTheta,1.5));}
11
+ fn computeOzoneDensity(normalizedViewHeight: f32)->f32 {const MinOzoneDensity=.135;const OneMinusMinOzoneDensity=1.-MinOzoneDensity;const OzoneStartHeight=.15;
12
+ const PeakOzoneHeight=.25;const MaxOzoneHeight=.6;const InverseRampupDistance=1./(PeakOzoneHeight-OzoneStartHeight);const InverseRampdownDistance=1./(MaxOzoneHeight-PeakOzoneHeight);let lowerAtmosphereDensity=MinOzoneDensity+OneMinusMinOzoneDensity*max(0.,normalizedViewHeight-OzoneStartHeight)*InverseRampupDistance;let sqrtUpperAtmosphereDensity=max(0.,1.-(normalizedViewHeight-PeakOzoneHeight)*InverseRampdownDistance);let upperAtmosphereDensity=sqrtUpperAtmosphereDensity*sqrtUpperAtmosphereDensity;let densityOzone=select(upperAtmosphereDensity,lowerAtmosphereDensity,normalizedViewHeight<PeakOzoneHeight);return densityOzone;}
7
13
  fn sampleMediumRGB(
8
14
  viewHeight: f32,
9
15
  scatteringRayleigh: ptr<function,vec3f>,
@@ -11,26 +17,74 @@ scatteringMie: ptr<function,vec3f>,
11
17
  extinction: ptr<function,vec3f>,
12
18
  scattering: ptr<function,vec3f>
13
19
  ) {let normalizedViewHeight=saturate(viewHeight*atmosphere.inverseAtmosphereThickness);let densityMie=exp(-83.333*normalizedViewHeight);let densityRayleigh=exp(-12.5*normalizedViewHeight);let densityOzone=computeOzoneDensity(normalizedViewHeight);*scatteringRayleigh=densityRayleigh*atmosphere.peakRayleighScattering;*scatteringMie=densityMie*atmosphere.peakMieScattering;*scattering=*scatteringMie+*scatteringRayleigh;let extinctionRayleigh=*scatteringRayleigh;let extinctionMie=densityMie*atmosphere.peakMieExtinction;let extinctionOzone=densityOzone*atmosphere.peakOzoneAbsorption;*extinction=extinctionRayleigh+extinctionMie+extinctionOzone;}
14
- fn computeTransmittance(rayOriginGlobal: vec3f,rayDirection: vec3f,tMax: f32,sampleCount: i32)->vec3f {var opticalDepth: vec3f=vec3f(0.0);var t: f32=0.0;let sampleSegmentWeight=tMax/f32(sampleCount);let sampleSegmentT: f32=0.3;for (var s: i32=0; s<sampleCount; s+=1) {let newT=sampleSegmentWeight*(f32(s)+sampleSegmentT);let dt=newT-t;t=newT;var scatteringRayleigh: vec3f;var scatteringMie: vec3f;var extinction: vec3f;var scattering: vec3f;let samplePositionGlobal=rayOriginGlobal+t*rayDirection;sampleMediumRGB(length(samplePositionGlobal)-atmosphere.planetRadius,&scatteringRayleigh,&scatteringMie,&extinction,&scattering);opticalDepth+=extinction*dt;}
20
+ fn computeTransmittance(rayOriginGlobal: vec3f,rayDirection: vec3f,tMax: f32,sampleCount: i32)->vec3f {var opticalDepth=vec3f(0.);var t=0.;let sampleSegmentWeight=tMax/f32(sampleCount);const sampleSegmentT=.3;for (var s=0; s<sampleCount; s+=1) {let newT=sampleSegmentWeight*(f32(s)+sampleSegmentT);let dt=newT-t;t=newT;var scatteringRayleigh: vec3f;var scatteringMie: vec3f;var extinction: vec3f;var scattering: vec3f;let samplePositionGlobal=rayOriginGlobal+t*rayDirection;sampleMediumRGB(length(samplePositionGlobal)-atmosphere.planetRadius,&scatteringRayleigh,&scatteringMie,&extinction,&scattering);opticalDepth+=extinction*dt;}
15
21
  return exp(-opticalDepth);}
16
22
  #if defined(SAMPLE_TRANSMITTANCE_LUT) || !defined(EXCLUDE_RAY_MARCHING_FUNCTIONS)
17
- fn getTransmittanceUV(radius: f32,cosAngleLightToZenith: f32,distanceToHorizon: ptr<function,f32>)->vec2f {let radiusSquared=radius*radius;let horizonDistance=sqrtClamped(radiusSquared-atmosphere.planetRadiusSquared);*distanceToHorizon=horizonDistance;let cosAngleLightToZenithSquared=cosAngleLightToZenith*cosAngleLightToZenith;let discriminant=radiusSquared*(cosAngleLightToZenithSquared-1.0)+atmosphere.atmosphereRadiusSquared;let distanceToAtmosphereEdge=max(0.0,-radius*cosAngleLightToZenith+sqrtClamped(discriminant));let minDistanceToAtmosphereEdge=max(0.0,atmosphere.atmosphereRadius-radius);let maxDistanceToAtmosphereEdge=horizonDistance+atmosphere.horizonDistanceToAtmosphereEdge;let cosAngleLightToZenithCoordinate=(distanceToAtmosphereEdge-minDistanceToAtmosphereEdge)/max(0.000001,maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);let distanceToHorizonCoordinate=horizonDistance/max(0.000001,atmosphere.horizonDistanceToAtmosphereEdge);let unit=vec2f(cosAngleLightToZenithCoordinate,distanceToHorizonCoordinate);return unit*TransmittanceLutDomainInUVSpace+TransmittanceLutHalfTexelSize;}
18
- fn sampleTransmittanceLut(transmittanceLut: texture_2d<f32>,positionRadius: f32,cosAngleLightToZenith: f32)->vec4f {var distanceToHorizon=0.0;let uv=getTransmittanceUV(positionRadius,cosAngleLightToZenith,&distanceToHorizon);let weight=smoothstep(TransmittanceMinOccludedU,TransmittanceMaxUnoccludedU,uv.x);return weight*textureSampleLevel(transmittanceLut,transmittanceLutSampler,uv,0.0);}
23
+ fn getTransmittanceUV(radius: f32,cosAngleLightToZenith: f32,distanceToHorizon: ptr<function,f32>)->vec2f {let radiusSquared=radius*radius;let horizonDistance=sqrtClamped(radiusSquared-atmosphere.planetRadiusSquared);*distanceToHorizon=horizonDistance;let cosAngleLightToZenithSquared=cosAngleLightToZenith*cosAngleLightToZenith;let discriminant=radiusSquared*(cosAngleLightToZenithSquared-1.)+atmosphere.atmosphereRadiusSquared;let distanceToAtmosphereEdge=max(0.,-radius*cosAngleLightToZenith+sqrtClamped(discriminant));let minDistanceToAtmosphereEdge=max(0.,atmosphere.atmosphereRadius-radius);let maxDistanceToAtmosphereEdge=horizonDistance+atmosphere.horizonDistanceToAtmosphereEdge;let cosAngleLightToZenithCoordinate=(distanceToAtmosphereEdge-minDistanceToAtmosphereEdge)/max(.000001,maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);let distanceToHorizonCoordinate=horizonDistance/max(.000001,atmosphere.horizonDistanceToAtmosphereEdge);let unit=vec2f(cosAngleLightToZenithCoordinate,distanceToHorizonCoordinate);return unit*TransmittanceLutDomainInUVSpace+TransmittanceLutHalfTexelSize; }
24
+ fn sampleTransmittanceLut(transmittanceLut: texture_2d<f32>,positionRadius: f32,cosAngleLightToZenith: f32)->vec4f {var distanceToHorizon=0.;let uv=getTransmittanceUV(positionRadius,cosAngleLightToZenith,&distanceToHorizon);let weight=smoothstep(TransmittanceMinOccludedU,TransmittanceMaxUnoccludedU,uv.x);return weight*textureSampleLevel(transmittanceLut,transmittanceLutSampler,uv,0.);}
19
25
  #endif
20
- fn layerIdxToAerialPerspectiveLayer(layerIdx: f32)->f32 {var layer=(layerIdx+1.0)/NumAerialPerspectiveLutLayers;layer*=layer;
26
+ #ifndef EXCLUDE_RAY_MARCHING_FUNCTIONS
27
+ #ifndef COMPUTE_MULTI_SCATTERING
28
+ fn sampleMultiScatteringLut(multiScatteringLut: texture_2d<f32>,radius: f32,cosAngleLightToZenith: f32)->vec3f {let unit=vec2f(.5+.5*cosAngleLightToZenith,(radius-atmosphere.planetRadius)/atmosphere.atmosphereThickness);let uv=unitToUV(unit,MultiScatteringLutDomainInUVSpace,MultiScatteringLutHalfTexelSize);let multiScattering=textureSampleLevel(multiScatteringLut,multiScatteringLutSampler,uv,0.).rgb;return max(atmosphere.minMultiScattering,multiScattering);}
29
+ #endif
30
+ const uniformPhase=RECIPROCAL_PI4;fn integrateScatteredRadiance(
31
+ isAerialPerspectiveLut: bool,
32
+ lightIntensityParam: f32,
33
+ transmittanceLut: texture_2d<f32>,
34
+ #ifndef COMPUTE_MULTI_SCATTERING
35
+ multiScatteringLut: texture_2d<f32>,
36
+ multiScatteringIntensityParam: f32,
37
+ #endif
38
+ rayOriginGlobal: vec3f,
39
+ rayDirection: vec3f,
40
+ directionToLightParam: vec3f,
41
+ tMaxMax: f32,
42
+ sampleCount: i32,
43
+ distanceToSurface: f32,
44
+ transmittance: ptr<function,vec3f>
45
+ #if COMPUTE_MULTI_SCATTERING
46
+ ,multiScattering: ptr<function,vec3f>
47
+ #endif
48
+ )->vec3f {var radiance=vec3f(0.);*transmittance=vec3f(1.);
49
+ #if COMPUTE_MULTI_SCATTERING
50
+ *multiScattering=vec3f(0.);
51
+ #endif
52
+ let tBottom=sphereIntersectNearest(rayOriginGlobal,rayDirection,atmosphere.planetRadius);let tTop=sphereIntersectNearest(rayOriginGlobal,rayDirection,atmosphere.atmosphereRadius);var tMax=0.;if (tBottom<0.) {if (tTop<0.) {return radiance;} else {tMax=tTop;}} else {if (tTop>0.) {if (isAerialPerspectiveLut) {tMax=tTop;} else {tMax=min(tBottom,tTop);}}}
53
+ if (distanceToSurface>0. && distanceToSurface<tMax) {tMax=distanceToSurface;}
54
+ tMax=min(tMax,tMaxMax);
55
+ #ifndef COMPUTE_MULTI_SCATTERING
56
+ let cosTheta=dot(rayDirection,directionToLightParam);let onePlusCosThetaSq=1.+cosTheta*cosTheta;let rayleighPhase=computeRayleighPhase(onePlusCosThetaSq);let miePhase=computeMiePhaseCornetteShanks(cosTheta,onePlusCosThetaSq);
57
+ #endif
58
+ let transmittanceScale=select(1.,atmosphere.aerialPerspectiveTransmittanceScale,isAerialPerspectiveLut);var t=0.;let sampleSegmentWeight=tMax/f32(sampleCount);const sampleSegmentT=.3;for (var s=0; s<sampleCount; s+=1) {let newT=sampleSegmentWeight*(f32(s)+sampleSegmentT);let dt=newT-t;t=newT;let samplePositionGlobal=rayOriginGlobal+t*rayDirection;let sampleRadiusGlobal=length(samplePositionGlobal);let sampleGeocentricNormal=samplePositionGlobal/sampleRadiusGlobal;let sampleCosAngleLightToZenith=dot(directionToLightParam,sampleGeocentricNormal);var scatteringRayleigh: vec3f;var scatteringMie: vec3f;var extinction: vec3f;var scattering: vec3f;sampleMediumRGB(sampleRadiusGlobal-atmosphere.planetRadius,&scatteringRayleigh,&scatteringMie,&extinction,&scattering);let transmittanceToLight=sampleTransmittanceLut(transmittanceLut,sampleRadiusGlobal,sampleCosAngleLightToZenith).rgb;
59
+ #if COMPUTE_MULTI_SCATTERING
60
+ let phaseTimesScattering=uniformPhase*scattering;let S=transmittanceToLight*phaseTimesScattering;
61
+ #else
62
+ let phaseTimesScattering=scatteringMie*miePhase+scatteringRayleigh*rayleighPhase;let multiScatteredRadiance=sampleMultiScatteringLut(multiScatteringLut,sampleRadiusGlobal,sampleCosAngleLightToZenith);let S=transmittanceScale*transmittanceToLight*phaseTimesScattering+multiScatteringIntensityParam*multiScatteredRadiance*scattering;
63
+ #endif
64
+ let sampleOpticalDepth=extinction*dt;let sampleTransmittanceVal=exp(-sampleOpticalDepth);let clampedExtinction=max(vec3f(.0000001),extinction);let SInt=(S-S*sampleTransmittanceVal)/clampedExtinction;radiance+=*transmittance*SInt;
65
+ #if COMPUTE_MULTI_SCATTERING
66
+ let MSInt=(scattering-scattering*sampleTransmittanceVal)/clampedExtinction;*multiScattering+=*transmittance*MSInt;
67
+ #endif
68
+ *transmittance*=sampleTransmittanceVal;}
69
+ #if USE_GROUND_ALBEDO
70
+ if (tMax==tBottom && tBottom>0.) {let planetPos=rayOriginGlobal+tBottom*rayDirection;let planetPosRadius=length(planetPos);let planetPosGeocentricNormal=planetPos/planetPosRadius;let nDotL=dot(directionToLightParam,planetPosGeocentricNormal);let lightTransmittance=sampleTransmittanceLut(transmittanceLut,planetPosRadius,nDotL).rgb;const diffuseBrdf=RECIPROCAL_PI;radiance+=lightTransmittance**transmittance*atmosphere.groundAlbedo*(nDotL*diffuseBrdf);}
71
+ #endif
72
+ radiance*=lightIntensityParam;return radiance;}
73
+ #endif
74
+ fn layerIdxToAerialPerspectiveLayer(layerIdx: f32)->f32 {var layer=(layerIdx+1.)/NumAerialPerspectiveLutLayers;layer*=layer;
21
75
  layer*=NumAerialPerspectiveLutLayers;return layer;}
22
76
  fn toAerialPerspectiveDepth(layer: f32)->f32 {return layer*AerialPerspectiveLutKMPerSlice;}
23
77
  fn toAerialPerspectiveLayer(distance: f32,aerialPerspectiveLutDistancePerSlice: f32)->f32 {return distance/aerialPerspectiveLutDistancePerSlice;}
24
78
  fn applyAerialPerspectiveSaturation(aerialPerspective: vec4f)->vec4f {let previousRadiance=getLuminance(aerialPerspective.rgb);let mixed=mix(vec3f(previousRadiance),aerialPerspective.rgb,atmosphere.aerialPerspectiveSaturation);return vec4f(mixed,aerialPerspective.a);}
25
79
  fn applyAerialPerspectiveIntensity(aerialPerspective: vec4f)->vec4f {var result=aerialPerspective;
26
80
  #if APPLY_AERIAL_PERSPECTIVE_INTENSITY
27
- if (atmosphere.aerialPerspectiveIntensity==0.0) {result=vec4f(0.0);} else {let previousAlpha=result.a;result=result/max(0.00001,previousAlpha);result=result*pow(previousAlpha,1.0/atmosphere.aerialPerspectiveIntensity);}
81
+ if (atmosphere.aerialPerspectiveIntensity==0.) {result=vec4f(0.);} else {let previousAlpha=result.a;result=result/max(.00001,previousAlpha);result=result*pow(previousAlpha,1./atmosphere.aerialPerspectiveIntensity);}
28
82
  #endif
29
83
  return result;}
30
84
  fn applyAerialPerspectiveRadianceBias(aerialPerspective: vec4f)->vec4f {var result=aerialPerspective;
31
85
  #if APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS
32
- let originalRadiance=dot(result.rgb,LuminanceEncodeApprox);let targetRadiance=originalRadiance+atmosphere.aerialPerspectiveRadianceBias;if (originalRadiance>0.0) {result=result*max(0.0,targetRadiance/originalRadiance);} else {result=max(vec4f(0.0),vec4f(atmosphere.aerialPerspectiveRadianceBias));}
33
- result.a=min(result.a,1.0);
86
+ let originalRadiance=dot(result.rgb,LuminanceEncodeApprox);let targetRadiance=originalRadiance+atmosphere.aerialPerspectiveRadianceBias;if (originalRadiance>0.) {result=result*max(0.,targetRadiance/originalRadiance);} else {result=max(vec4f(0.),vec4f(atmosphere.aerialPerspectiveRadianceBias));}
87
+ result.a=min(result.a,1.);
34
88
  #endif
35
89
  return result;}
36
90
  fn sampleAerialPerspectiveLut(
@@ -41,21 +95,37 @@ numAerialPerspectiveLutLayers: f32,
41
95
  aerialPerspectiveLutKMPerSlice: f32,
42
96
  aerialPerspectiveLutRangeKM: f32,
43
97
  aerialPerspective: ptr<function,vec4f>
44
- )->bool {*aerialPerspective=vec4f(0.0);
98
+ )->bool {*aerialPerspective=vec4f(0.);
45
99
  #if USE_AERIAL_PERSPECTIVE_LUT
46
- if (distanceFromCamera>0.0 &&
100
+ if (distanceFromCamera>0. &&
47
101
  (clampToLutRange || distanceFromCamera<aerialPerspectiveLutRangeKM) &&
48
102
  atmosphere.clampedCameraRadius<=atmosphere.atmosphereRadius) {var layer=toAerialPerspectiveLayer(distanceFromCamera,aerialPerspectiveLutKMPerSlice);let normalizedLayer=sqrt(layer/numAerialPerspectiveLutLayers);
49
- layer=min(normalizedLayer*numAerialPerspectiveLutLayers,numAerialPerspectiveLutLayers);let weight=min(layer,1.0);let layerIdx=max(0.0,layer-1.0);let floorLayerIdx=floor(layerIdx);let aerialPerspectiveLayer0=textureSampleLevel(aerialPerspectiveLut,aerialPerspectiveLutSampler,screenUV,i32(floorLayerIdx),0.0);let aerialPerspectiveLayer1=textureSampleLevel(aerialPerspectiveLut,aerialPerspectiveLutSampler,screenUV,i32(floorLayerIdx+1.0),0.0);var interpolated=mix(aerialPerspectiveLayer0,aerialPerspectiveLayer1,layerIdx-floorLayerIdx);interpolated=vec4f(interpolated.rgb*atmosphere.atmosphereExposure,interpolated.a);interpolated=applyAerialPerspectiveSaturation(interpolated);interpolated=weight*applyAerialPerspectiveIntensity(interpolated);interpolated=applyAerialPerspectiveRadianceBias(interpolated);*aerialPerspective=interpolated;return true;}
103
+ layer=min(normalizedLayer*numAerialPerspectiveLutLayers,numAerialPerspectiveLutLayers);let weight=min(layer,1.);let layerIdx=max(0.,layer-1.);let floorLayerIdx=floor(layerIdx);let aerialPerspectiveLayer0=textureSampleLevel(aerialPerspectiveLut,aerialPerspectiveLutSampler,screenUV,i32(floorLayerIdx),0.);let aerialPerspectiveLayer1=textureSampleLevel(aerialPerspectiveLut,aerialPerspectiveLutSampler,screenUV,i32(floorLayerIdx+1.),0.);var interpolated=mix(aerialPerspectiveLayer0,aerialPerspectiveLayer1,layerIdx-floorLayerIdx);interpolated=vec4f(interpolated.rgb*atmosphere.atmosphereExposure,interpolated.a);interpolated=applyAerialPerspectiveSaturation(interpolated);interpolated=weight*applyAerialPerspectiveIntensity(interpolated);interpolated=applyAerialPerspectiveRadianceBias(interpolated);*aerialPerspective=interpolated;return true;}
50
104
  #endif
51
105
  return false;}
52
106
  #if RENDER_TRANSMITTANCE
53
107
  fn getTransmittanceParameters(uv: vec2f,radius: ptr<function,f32>,cosAngleLightToZenith: ptr<function,f32>,distanceToAtmosphereEdge: ptr<function,f32>) {let unit=uvToUnit(uv,TransmittanceLutDomainInUVSpace,TransmittanceLutHalfTexelSize);let distanceToHorizon=unit.y*atmosphere.horizonDistanceToAtmosphereEdge;let distanceToHorizonSquared=distanceToHorizon*distanceToHorizon;*radius=sqrtClamped(distanceToHorizonSquared+atmosphere.planetRadiusSquared);let minDistanceToAtmosphereEdge=atmosphere.atmosphereRadius-*radius;let maxDistanceToAtmosphereEdge=distanceToHorizon+atmosphere.horizonDistanceToAtmosphereEdge;*distanceToAtmosphereEdge=minDistanceToAtmosphereEdge+unit.x*(maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);let distanceToAtmosphereEdgeSquared=*distanceToAtmosphereEdge**distanceToAtmosphereEdge;*cosAngleLightToZenith=select(
54
- (atmosphere.horizonDistanceToAtmosphereEdgeSquared-distanceToAtmosphereEdgeSquared-distanceToHorizonSquared)/(2.0**radius**distanceToAtmosphereEdge),
55
- 1.0,
56
- *distanceToAtmosphereEdge<=0.0
57
- );*cosAngleLightToZenith=clamp(*cosAngleLightToZenith,-1.0,1.0);}
58
- fn renderTransmittance(uv: vec2f)->vec4f {var radius: f32;var cosAngleLightToZenith: f32;var distanceToAtmosphereEdgeAlongAngle: f32;getTransmittanceParameters(uv,&radius,&cosAngleLightToZenith,&distanceToAtmosphereEdgeAlongAngle);let sinAngleLightToZenith=sqrtClamped(1.0-cosAngleLightToZenith*cosAngleLightToZenith);let directionToLight=normalize(vec3f(0.0,cosAngleLightToZenith,sinAngleLightToZenith));let transmittance=computeTransmittance(vec3f(0.0,radius,0.0),directionToLight,distanceToAtmosphereEdgeAlongAngle,TransmittanceSampleCount);return vec4f(transmittance,avg(transmittance));}
108
+ (atmosphere.horizonDistanceToAtmosphereEdgeSquared-distanceToAtmosphereEdgeSquared-distanceToHorizonSquared)/(2.**radius**distanceToAtmosphereEdge),
109
+ 1.,
110
+ *distanceToAtmosphereEdge<=0.
111
+ );*cosAngleLightToZenith=clamp(*cosAngleLightToZenith,-1.,1.);}
112
+ fn renderTransmittance(uv: vec2f)->vec4f {var radius: f32;var cosAngleLightToZenith: f32;var distanceToAtmosphereEdgeAlongAngle: f32;getTransmittanceParameters(uv,&radius,&cosAngleLightToZenith,&distanceToAtmosphereEdgeAlongAngle);let sinAngleLightToZenith=sqrtClamped(1.-cosAngleLightToZenith*cosAngleLightToZenith);let directionToLight=normalize(vec3f(0.,cosAngleLightToZenith,sinAngleLightToZenith));let transmittance=computeTransmittance(vec3f(0.,radius,0.),directionToLight,distanceToAtmosphereEdgeAlongAngle,TransmittanceSampleCount);return vec4f(transmittance,avg(transmittance));}
113
+ #endif
114
+ #if RENDER_MULTI_SCATTERING
115
+ fn getSphereSample(azimuth: f32,inclination: f32,sinInclination: ptr<function,f32>)->vec3f {*sinInclination=sin(inclination);return vec3f(*sinInclination*sin(azimuth),cos(inclination),*sinInclination*cos(azimuth));}
116
+ const MultiScatteringInclinationSampleCount=8.;const MultiScatteringAzimuthSampleCount=2.*MultiScatteringInclinationSampleCount;const MultiScatteringSampleCount=64;const MultiScatteringAzimuthIterationAngle=TWO_PI/MultiScatteringAzimuthSampleCount;const MultiScatteringInclinationIterationAngle=PI/MultiScatteringInclinationSampleCount;const MultiScatteringAngleStepProduct=MultiScatteringAzimuthIterationAngle*MultiScatteringInclinationIterationAngle;fn renderMultiScattering(uv: vec2f,transmittanceLut: texture_2d<f32>)->vec4f {let unit=uvToUnit(uv,MultiScatteringLutDomainInUVSpace,MultiScatteringLutHalfTexelSize);let cosAngleLightToZenith=2.*unit.x-1.;let sinAngleLightToZenith=sqrtClamped(1.-cosAngleLightToZenith*cosAngleLightToZenith);let directionToLightLocal=normalize(vec3f(0.,cosAngleLightToZenith,sinAngleLightToZenith));let rayOriginRadius=atmosphere.planetRadius+max(unit.y,.001)*atmosphere.atmosphereThickness;let rayOrigin=vec3f(0.,rayOriginRadius,0.);var inscattered=vec3f(0.);var multiScatteringTotal=vec3f(0.);for (var i=.5; i<MultiScatteringAzimuthSampleCount; i+=1.) {let azimuth=MultiScatteringAzimuthIterationAngle*i;for (var j=.5; j<MultiScatteringInclinationSampleCount; j+=1.) {let inclination=MultiScatteringInclinationIterationAngle*j;var sinInclination: f32;let rayDirection=getSphereSample(azimuth,inclination,&sinInclination);var transmittanceVal: vec3f;var multiScatteringVal: vec3f;let radianceVal=integrateScatteredRadiance(
117
+ false,
118
+ 1.,
119
+ transmittanceLut,
120
+ rayOrigin,
121
+ rayDirection,
122
+ directionToLightLocal,
123
+ 100000000.,
124
+ MultiScatteringSampleCount,
125
+ -1.,
126
+ &transmittanceVal,
127
+ &multiScatteringVal);let weight=RECIPROCAL_PI4*abs(sinInclination)*MultiScatteringAngleStepProduct;multiScatteringTotal+=multiScatteringVal*weight;inscattered+=radianceVal*weight;}}
128
+ let multiScatteringResult=inscattered/max(vec3f(.000001),vec3f(1.)-multiScatteringTotal);return vec4f(multiScatteringResult,1.);}
59
129
  #endif
60
130
  `;
61
131
  // Sideeffect
@@ -1 +1 @@
1
- {"version":3,"file":"atmosphereFunctions.js","sourceRoot":"","sources":["../../../../../../dev/addons/src/atmosphere/ShadersWGSL/ShadersInclude/atmosphereFunctions.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,OAAO,EAAE,WAAW,EAAE,4CAAiC;AAEvD,MAAM,IAAI,GAAG,qBAAqB,CAAC;AACnC,MAAM,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwDd,CAAC;AACF,aAAa;AACb,IAAI,CAAC,WAAW,CAAC,wBAAwB,CAAC,IAAI,CAAC,EAAE,CAAC;IAC9C,WAAW,CAAC,wBAAwB,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC;AACxD,CAAC;AACD,gBAAgB;AAChB,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC","sourcesContent":["// Do not edit.\nimport { ShaderStore } from \"core/Engines/shaderStore\";\n\nconst name = \"atmosphereFunctions\";\nconst shader = `const NumAerialPerspectiveLutLayers: f32=32.0;const AerialPerspectiveLutSize: vec3f=vec3f(16.0,64.0,NumAerialPerspectiveLutLayers);const AerialPerspectiveLutKMPerSlice: f32=4.0;const AerialPerspectiveLutRangeKM: f32=AerialPerspectiveLutKMPerSlice*NumAerialPerspectiveLutLayers;const TransmittanceSampleCount: i32=128;const TransmittanceLutSize: vec2f=vec2f(256.0,64.0);const TransmittanceLutDomainInUVSpace: vec2f=(TransmittanceLutSize-vec2f(1.0,1.0))/TransmittanceLutSize;const TransmittanceLutHalfTexelSize: vec2f=vec2f(0.5,0.5)/TransmittanceLutSize;const TransmittanceHorizonRange: f32=2.0*TransmittanceLutHalfTexelSize.x;const TransmittanceMaxUnoccludedU: f32=1.0-0.5*TransmittanceHorizonRange;const TransmittanceMinOccludedU: f32=1.0+0.5*TransmittanceHorizonRange;fn uvToUnit(uv: vec2f,domainInUVSpace: vec2f,halfTexelSize: vec2f)->vec2f {return (uv-halfTexelSize)/domainInUVSpace;}\nfn computeOzoneDensity(normalizedViewHeight: f32)->f32 {let MinOzoneDensity: f32=0.135;let OneMinusMinOzoneDensity: f32=1.0-MinOzoneDensity;let OzoneStartHeight: f32=0.15; \nlet PeakOzoneHeight: f32=0.25;let MaxOzoneHeight: f32=0.6;let InverseRampupDistance: f32=1.0/(PeakOzoneHeight-OzoneStartHeight);let InverseRampdownDistance: f32=1.0/(MaxOzoneHeight-PeakOzoneHeight);let lowerAtmosphereDensity=MinOzoneDensity+OneMinusMinOzoneDensity*max(0.0,normalizedViewHeight-OzoneStartHeight)*InverseRampupDistance;let sqrtUpperAtmosphereDensity=max(0.0,1.0-(normalizedViewHeight-PeakOzoneHeight)*InverseRampdownDistance);let upperAtmosphereDensity=sqrtUpperAtmosphereDensity*sqrtUpperAtmosphereDensity;return select(upperAtmosphereDensity,lowerAtmosphereDensity,normalizedViewHeight<PeakOzoneHeight);}\nfn sampleMediumRGB(\nviewHeight: f32,\nscatteringRayleigh: ptr<function,vec3f>,\nscatteringMie: ptr<function,vec3f>,\nextinction: ptr<function,vec3f>,\nscattering: ptr<function,vec3f>\n) {let normalizedViewHeight=saturate(viewHeight*atmosphere.inverseAtmosphereThickness);let densityMie=exp(-83.333*normalizedViewHeight);let densityRayleigh=exp(-12.5*normalizedViewHeight);let densityOzone=computeOzoneDensity(normalizedViewHeight);*scatteringRayleigh=densityRayleigh*atmosphere.peakRayleighScattering;*scatteringMie=densityMie*atmosphere.peakMieScattering;*scattering=*scatteringMie+*scatteringRayleigh;let extinctionRayleigh=*scatteringRayleigh;let extinctionMie=densityMie*atmosphere.peakMieExtinction;let extinctionOzone=densityOzone*atmosphere.peakOzoneAbsorption;*extinction=extinctionRayleigh+extinctionMie+extinctionOzone;}\nfn computeTransmittance(rayOriginGlobal: vec3f,rayDirection: vec3f,tMax: f32,sampleCount: i32)->vec3f {var opticalDepth: vec3f=vec3f(0.0);var t: f32=0.0;let sampleSegmentWeight=tMax/f32(sampleCount);let sampleSegmentT: f32=0.3;for (var s: i32=0; s<sampleCount; s+=1) {let newT=sampleSegmentWeight*(f32(s)+sampleSegmentT);let dt=newT-t;t=newT;var scatteringRayleigh: vec3f;var scatteringMie: vec3f;var extinction: vec3f;var scattering: vec3f;let samplePositionGlobal=rayOriginGlobal+t*rayDirection;sampleMediumRGB(length(samplePositionGlobal)-atmosphere.planetRadius,&scatteringRayleigh,&scatteringMie,&extinction,&scattering);opticalDepth+=extinction*dt;}\nreturn exp(-opticalDepth);}\n#if defined(SAMPLE_TRANSMITTANCE_LUT) || !defined(EXCLUDE_RAY_MARCHING_FUNCTIONS)\nfn getTransmittanceUV(radius: f32,cosAngleLightToZenith: f32,distanceToHorizon: ptr<function,f32>)->vec2f {let radiusSquared=radius*radius;let horizonDistance=sqrtClamped(radiusSquared-atmosphere.planetRadiusSquared);*distanceToHorizon=horizonDistance;let cosAngleLightToZenithSquared=cosAngleLightToZenith*cosAngleLightToZenith;let discriminant=radiusSquared*(cosAngleLightToZenithSquared-1.0)+atmosphere.atmosphereRadiusSquared;let distanceToAtmosphereEdge=max(0.0,-radius*cosAngleLightToZenith+sqrtClamped(discriminant));let minDistanceToAtmosphereEdge=max(0.0,atmosphere.atmosphereRadius-radius);let maxDistanceToAtmosphereEdge=horizonDistance+atmosphere.horizonDistanceToAtmosphereEdge;let cosAngleLightToZenithCoordinate=(distanceToAtmosphereEdge-minDistanceToAtmosphereEdge)/max(0.000001,maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);let distanceToHorizonCoordinate=horizonDistance/max(0.000001,atmosphere.horizonDistanceToAtmosphereEdge);let unit=vec2f(cosAngleLightToZenithCoordinate,distanceToHorizonCoordinate);return unit*TransmittanceLutDomainInUVSpace+TransmittanceLutHalfTexelSize;}\nfn sampleTransmittanceLut(transmittanceLut: texture_2d<f32>,positionRadius: f32,cosAngleLightToZenith: f32)->vec4f {var distanceToHorizon=0.0;let uv=getTransmittanceUV(positionRadius,cosAngleLightToZenith,&distanceToHorizon);let weight=smoothstep(TransmittanceMinOccludedU,TransmittanceMaxUnoccludedU,uv.x);return weight*textureSampleLevel(transmittanceLut,transmittanceLutSampler,uv,0.0);}\n#endif\nfn layerIdxToAerialPerspectiveLayer(layerIdx: f32)->f32 {var layer=(layerIdx+1.0)/NumAerialPerspectiveLutLayers;layer*=layer; \nlayer*=NumAerialPerspectiveLutLayers;return layer;}\nfn toAerialPerspectiveDepth(layer: f32)->f32 {return layer*AerialPerspectiveLutKMPerSlice;}\nfn toAerialPerspectiveLayer(distance: f32,aerialPerspectiveLutDistancePerSlice: f32)->f32 {return distance/aerialPerspectiveLutDistancePerSlice;}\nfn applyAerialPerspectiveSaturation(aerialPerspective: vec4f)->vec4f {let previousRadiance=getLuminance(aerialPerspective.rgb);let mixed=mix(vec3f(previousRadiance),aerialPerspective.rgb,atmosphere.aerialPerspectiveSaturation);return vec4f(mixed,aerialPerspective.a);}\nfn applyAerialPerspectiveIntensity(aerialPerspective: vec4f)->vec4f {var result=aerialPerspective;\n#if APPLY_AERIAL_PERSPECTIVE_INTENSITY\nif (atmosphere.aerialPerspectiveIntensity==0.0) {result=vec4f(0.0);} else {let previousAlpha=result.a;result=result/max(0.00001,previousAlpha);result=result*pow(previousAlpha,1.0/atmosphere.aerialPerspectiveIntensity);}\n#endif\nreturn result;}\nfn applyAerialPerspectiveRadianceBias(aerialPerspective: vec4f)->vec4f {var result=aerialPerspective;\n#if APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS\nlet originalRadiance=dot(result.rgb,LuminanceEncodeApprox);let targetRadiance=originalRadiance+atmosphere.aerialPerspectiveRadianceBias;if (originalRadiance>0.0) {result=result*max(0.0,targetRadiance/originalRadiance);} else {result=max(vec4f(0.0),vec4f(atmosphere.aerialPerspectiveRadianceBias));}\nresult.a=min(result.a,1.0);\n#endif\nreturn result;}\nfn sampleAerialPerspectiveLut(\nscreenUV: vec2f,\nclampToLutRange: bool,\ndistanceFromCamera: f32,\nnumAerialPerspectiveLutLayers: f32,\naerialPerspectiveLutKMPerSlice: f32,\naerialPerspectiveLutRangeKM: f32,\naerialPerspective: ptr<function,vec4f>\n)->bool {*aerialPerspective=vec4f(0.0);\n#if USE_AERIAL_PERSPECTIVE_LUT\nif (distanceFromCamera>0.0 &&\n(clampToLutRange || distanceFromCamera<aerialPerspectiveLutRangeKM) &&\natmosphere.clampedCameraRadius<=atmosphere.atmosphereRadius) {var layer=toAerialPerspectiveLayer(distanceFromCamera,aerialPerspectiveLutKMPerSlice);let normalizedLayer=sqrt(layer/numAerialPerspectiveLutLayers); \nlayer=min(normalizedLayer*numAerialPerspectiveLutLayers,numAerialPerspectiveLutLayers);let weight=min(layer,1.0);let layerIdx=max(0.0,layer-1.0);let floorLayerIdx=floor(layerIdx);let aerialPerspectiveLayer0=textureSampleLevel(aerialPerspectiveLut,aerialPerspectiveLutSampler,screenUV,i32(floorLayerIdx),0.0);let aerialPerspectiveLayer1=textureSampleLevel(aerialPerspectiveLut,aerialPerspectiveLutSampler,screenUV,i32(floorLayerIdx+1.0),0.0);var interpolated=mix(aerialPerspectiveLayer0,aerialPerspectiveLayer1,layerIdx-floorLayerIdx);interpolated=vec4f(interpolated.rgb*atmosphere.atmosphereExposure,interpolated.a);interpolated=applyAerialPerspectiveSaturation(interpolated);interpolated=weight*applyAerialPerspectiveIntensity(interpolated);interpolated=applyAerialPerspectiveRadianceBias(interpolated);*aerialPerspective=interpolated;return true;}\n#endif\nreturn false;}\n#if RENDER_TRANSMITTANCE\nfn getTransmittanceParameters(uv: vec2f,radius: ptr<function,f32>,cosAngleLightToZenith: ptr<function,f32>,distanceToAtmosphereEdge: ptr<function,f32>) {let unit=uvToUnit(uv,TransmittanceLutDomainInUVSpace,TransmittanceLutHalfTexelSize);let distanceToHorizon=unit.y*atmosphere.horizonDistanceToAtmosphereEdge;let distanceToHorizonSquared=distanceToHorizon*distanceToHorizon;*radius=sqrtClamped(distanceToHorizonSquared+atmosphere.planetRadiusSquared);let minDistanceToAtmosphereEdge=atmosphere.atmosphereRadius-*radius;let maxDistanceToAtmosphereEdge=distanceToHorizon+atmosphere.horizonDistanceToAtmosphereEdge;*distanceToAtmosphereEdge=minDistanceToAtmosphereEdge+unit.x*(maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);let distanceToAtmosphereEdgeSquared=*distanceToAtmosphereEdge**distanceToAtmosphereEdge;*cosAngleLightToZenith=select(\n(atmosphere.horizonDistanceToAtmosphereEdgeSquared-distanceToAtmosphereEdgeSquared-distanceToHorizonSquared)/(2.0**radius**distanceToAtmosphereEdge),\n1.0,\n*distanceToAtmosphereEdge<=0.0\n);*cosAngleLightToZenith=clamp(*cosAngleLightToZenith,-1.0,1.0);}\nfn renderTransmittance(uv: vec2f)->vec4f {var radius: f32;var cosAngleLightToZenith: f32;var distanceToAtmosphereEdgeAlongAngle: f32;getTransmittanceParameters(uv,&radius,&cosAngleLightToZenith,&distanceToAtmosphereEdgeAlongAngle);let sinAngleLightToZenith=sqrtClamped(1.0-cosAngleLightToZenith*cosAngleLightToZenith);let directionToLight=normalize(vec3f(0.0,cosAngleLightToZenith,sinAngleLightToZenith));let transmittance=computeTransmittance(vec3f(0.0,radius,0.0),directionToLight,distanceToAtmosphereEdgeAlongAngle,TransmittanceSampleCount);return vec4f(transmittance,avg(transmittance));}\n#endif\n`;\n// Sideeffect\nif (!ShaderStore.IncludesShadersStoreWGSL[name]) {\n ShaderStore.IncludesShadersStoreWGSL[name] = shader;\n}\n/** @internal */\nexport const atmosphereFunctionsWGSL = { name, shader };\n"]}
1
+ {"version":3,"file":"atmosphereFunctions.js","sourceRoot":"","sources":["../../../../../../dev/addons/src/atmosphere/ShadersWGSL/ShadersInclude/atmosphereFunctions.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,OAAO,EAAE,WAAW,EAAE,4CAAiC;AACvD,0EAA+D;AAE/D,MAAM,IAAI,GAAG,qBAAqB,CAAC;AACnC,MAAM,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6Hd,CAAC;AACF,aAAa;AACb,IAAI,CAAC,WAAW,CAAC,wBAAwB,CAAC,IAAI,CAAC,EAAE,CAAC;IAC9C,WAAW,CAAC,wBAAwB,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC;AACxD,CAAC;AACD,gBAAgB;AAChB,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC","sourcesContent":["// Do not edit.\nimport { ShaderStore } from \"core/Engines/shaderStore\";\nimport \"core/ShadersWGSL/ShadersInclude/intersectionFunctions\";\n\nconst name = \"atmosphereFunctions\";\nconst shader = `#include<intersectionFunctions>\nconst MultiScatteringLutSize=vec2f(32.,32.);const MultiScatteringLutDomainInUVSpace=(MultiScatteringLutSize-vec2f(1.))/MultiScatteringLutSize;const MultiScatteringLutHalfTexelSize=vec2f(.5)/MultiScatteringLutSize;const NumAerialPerspectiveLutLayers=32.;const AerialPerspectiveLutSize=vec3f(16.,64.,NumAerialPerspectiveLutLayers);const AerialPerspectiveLutKMPerSlice=4.;const AerialPerspectiveLutRangeKM=AerialPerspectiveLutKMPerSlice*NumAerialPerspectiveLutLayers;const TransmittanceSampleCount=128;const TransmittanceLutSize=vec2f(256.,64.);const TransmittanceLutDomainInUVSpace=(TransmittanceLutSize-vec2f(1.))/TransmittanceLutSize;const TransmittanceLutHalfTexelSize=vec2f(.5)/TransmittanceLutSize;const TransmittanceHorizonRange=2.*TransmittanceLutHalfTexelSize.x;const TransmittanceMaxUnoccludedU=1.-.5*TransmittanceHorizonRange;const TransmittanceMinOccludedU=1.+.5*TransmittanceHorizonRange;fn uvToUnit(uv: vec2f,domainInUVSpace: vec2f,halfTexelSize: vec2f)->vec2f {return (uv-halfTexelSize)/domainInUVSpace;}\nfn unitToUV(unit: vec2f,domainInUVSpace: vec2f,halfTexelSize: vec2f)->vec2f {return unit*domainInUVSpace+halfTexelSize;}\nfn sphereIntersectNearest(rayOrigin: vec3f,rayDirection: vec3f,sphereRadius: f32)->f32 {let result=sphereIntersectFromOrigin(rayOrigin,rayDirection,sphereRadius);let c=dot(rayOrigin,rayOrigin)-sphereRadius*sphereRadius;return select(result.x,result.y,c>=0.);}\nfn computeRayleighPhase(onePlusCosThetaSq: f32)->f32 {return 0.0596831037*onePlusCosThetaSq;}\nfn computeMiePhaseCornetteShanks(cosTheta: f32,onePlusCosThetaSq: f32)->f32 {const g=.8;const gSquared=g*g;const oneMinusGSquared=1.-gSquared;const onePlusGSquared=1.+gSquared;const twoPlusGSquared=2.+gSquared;const twoG=2.*g;const threeOverEightPi=3./(8.*PI);return threeOverEightPi*oneMinusGSquared*onePlusCosThetaSq/(twoPlusGSquared*pow(onePlusGSquared-twoG*cosTheta,1.5));}\nfn computeOzoneDensity(normalizedViewHeight: f32)->f32 {const MinOzoneDensity=.135;const OneMinusMinOzoneDensity=1.-MinOzoneDensity;const OzoneStartHeight=.15; \nconst PeakOzoneHeight=.25;const MaxOzoneHeight=.6;const InverseRampupDistance=1./(PeakOzoneHeight-OzoneStartHeight);const InverseRampdownDistance=1./(MaxOzoneHeight-PeakOzoneHeight);let lowerAtmosphereDensity=MinOzoneDensity+OneMinusMinOzoneDensity*max(0.,normalizedViewHeight-OzoneStartHeight)*InverseRampupDistance;let sqrtUpperAtmosphereDensity=max(0.,1.-(normalizedViewHeight-PeakOzoneHeight)*InverseRampdownDistance);let upperAtmosphereDensity=sqrtUpperAtmosphereDensity*sqrtUpperAtmosphereDensity;let densityOzone=select(upperAtmosphereDensity,lowerAtmosphereDensity,normalizedViewHeight<PeakOzoneHeight);return densityOzone;}\nfn sampleMediumRGB(\nviewHeight: f32,\nscatteringRayleigh: ptr<function,vec3f>,\nscatteringMie: ptr<function,vec3f>,\nextinction: ptr<function,vec3f>,\nscattering: ptr<function,vec3f>\n) {let normalizedViewHeight=saturate(viewHeight*atmosphere.inverseAtmosphereThickness);let densityMie=exp(-83.333*normalizedViewHeight);let densityRayleigh=exp(-12.5*normalizedViewHeight);let densityOzone=computeOzoneDensity(normalizedViewHeight);*scatteringRayleigh=densityRayleigh*atmosphere.peakRayleighScattering;*scatteringMie=densityMie*atmosphere.peakMieScattering;*scattering=*scatteringMie+*scatteringRayleigh;let extinctionRayleigh=*scatteringRayleigh;let extinctionMie=densityMie*atmosphere.peakMieExtinction;let extinctionOzone=densityOzone*atmosphere.peakOzoneAbsorption;*extinction=extinctionRayleigh+extinctionMie+extinctionOzone;}\nfn computeTransmittance(rayOriginGlobal: vec3f,rayDirection: vec3f,tMax: f32,sampleCount: i32)->vec3f {var opticalDepth=vec3f(0.);var t=0.;let sampleSegmentWeight=tMax/f32(sampleCount);const sampleSegmentT=.3;for (var s=0; s<sampleCount; s+=1) {let newT=sampleSegmentWeight*(f32(s)+sampleSegmentT);let dt=newT-t;t=newT;var scatteringRayleigh: vec3f;var scatteringMie: vec3f;var extinction: vec3f;var scattering: vec3f;let samplePositionGlobal=rayOriginGlobal+t*rayDirection;sampleMediumRGB(length(samplePositionGlobal)-atmosphere.planetRadius,&scatteringRayleigh,&scatteringMie,&extinction,&scattering);opticalDepth+=extinction*dt;}\nreturn exp(-opticalDepth);}\n#if defined(SAMPLE_TRANSMITTANCE_LUT) || !defined(EXCLUDE_RAY_MARCHING_FUNCTIONS)\nfn getTransmittanceUV(radius: f32,cosAngleLightToZenith: f32,distanceToHorizon: ptr<function,f32>)->vec2f {let radiusSquared=radius*radius;let horizonDistance=sqrtClamped(radiusSquared-atmosphere.planetRadiusSquared);*distanceToHorizon=horizonDistance;let cosAngleLightToZenithSquared=cosAngleLightToZenith*cosAngleLightToZenith;let discriminant=radiusSquared*(cosAngleLightToZenithSquared-1.)+atmosphere.atmosphereRadiusSquared;let distanceToAtmosphereEdge=max(0.,-radius*cosAngleLightToZenith+sqrtClamped(discriminant));let minDistanceToAtmosphereEdge=max(0.,atmosphere.atmosphereRadius-radius);let maxDistanceToAtmosphereEdge=horizonDistance+atmosphere.horizonDistanceToAtmosphereEdge;let cosAngleLightToZenithCoordinate=(distanceToAtmosphereEdge-minDistanceToAtmosphereEdge)/max(.000001,maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);let distanceToHorizonCoordinate=horizonDistance/max(.000001,atmosphere.horizonDistanceToAtmosphereEdge);let unit=vec2f(cosAngleLightToZenithCoordinate,distanceToHorizonCoordinate);return unit*TransmittanceLutDomainInUVSpace+TransmittanceLutHalfTexelSize; }\nfn sampleTransmittanceLut(transmittanceLut: texture_2d<f32>,positionRadius: f32,cosAngleLightToZenith: f32)->vec4f {var distanceToHorizon=0.;let uv=getTransmittanceUV(positionRadius,cosAngleLightToZenith,&distanceToHorizon);let weight=smoothstep(TransmittanceMinOccludedU,TransmittanceMaxUnoccludedU,uv.x);return weight*textureSampleLevel(transmittanceLut,transmittanceLutSampler,uv,0.);}\n#endif\n#ifndef EXCLUDE_RAY_MARCHING_FUNCTIONS\n#ifndef COMPUTE_MULTI_SCATTERING\nfn sampleMultiScatteringLut(multiScatteringLut: texture_2d<f32>,radius: f32,cosAngleLightToZenith: f32)->vec3f {let unit=vec2f(.5+.5*cosAngleLightToZenith,(radius-atmosphere.planetRadius)/atmosphere.atmosphereThickness);let uv=unitToUV(unit,MultiScatteringLutDomainInUVSpace,MultiScatteringLutHalfTexelSize);let multiScattering=textureSampleLevel(multiScatteringLut,multiScatteringLutSampler,uv,0.).rgb;return max(atmosphere.minMultiScattering,multiScattering);}\n#endif\nconst uniformPhase=RECIPROCAL_PI4;fn integrateScatteredRadiance(\nisAerialPerspectiveLut: bool,\nlightIntensityParam: f32,\ntransmittanceLut: texture_2d<f32>,\n#ifndef COMPUTE_MULTI_SCATTERING\nmultiScatteringLut: texture_2d<f32>,\nmultiScatteringIntensityParam: f32,\n#endif\nrayOriginGlobal: vec3f,\nrayDirection: vec3f,\ndirectionToLightParam: vec3f,\ntMaxMax: f32,\nsampleCount: i32,\ndistanceToSurface: f32,\ntransmittance: ptr<function,vec3f>\n#if COMPUTE_MULTI_SCATTERING\n,multiScattering: ptr<function,vec3f>\n#endif\n)->vec3f {var radiance=vec3f(0.);*transmittance=vec3f(1.);\n#if COMPUTE_MULTI_SCATTERING\n*multiScattering=vec3f(0.);\n#endif\nlet tBottom=sphereIntersectNearest(rayOriginGlobal,rayDirection,atmosphere.planetRadius);let tTop=sphereIntersectNearest(rayOriginGlobal,rayDirection,atmosphere.atmosphereRadius);var tMax=0.;if (tBottom<0.) {if (tTop<0.) {return radiance;} else {tMax=tTop;}} else {if (tTop>0.) {if (isAerialPerspectiveLut) {tMax=tTop;} else {tMax=min(tBottom,tTop);}}}\nif (distanceToSurface>0. && distanceToSurface<tMax) {tMax=distanceToSurface;}\ntMax=min(tMax,tMaxMax);\n#ifndef COMPUTE_MULTI_SCATTERING\nlet cosTheta=dot(rayDirection,directionToLightParam);let onePlusCosThetaSq=1.+cosTheta*cosTheta;let rayleighPhase=computeRayleighPhase(onePlusCosThetaSq);let miePhase=computeMiePhaseCornetteShanks(cosTheta,onePlusCosThetaSq);\n#endif\nlet transmittanceScale=select(1.,atmosphere.aerialPerspectiveTransmittanceScale,isAerialPerspectiveLut);var t=0.;let sampleSegmentWeight=tMax/f32(sampleCount);const sampleSegmentT=.3;for (var s=0; s<sampleCount; s+=1) {let newT=sampleSegmentWeight*(f32(s)+sampleSegmentT);let dt=newT-t;t=newT;let samplePositionGlobal=rayOriginGlobal+t*rayDirection;let sampleRadiusGlobal=length(samplePositionGlobal);let sampleGeocentricNormal=samplePositionGlobal/sampleRadiusGlobal;let sampleCosAngleLightToZenith=dot(directionToLightParam,sampleGeocentricNormal);var scatteringRayleigh: vec3f;var scatteringMie: vec3f;var extinction: vec3f;var scattering: vec3f;sampleMediumRGB(sampleRadiusGlobal-atmosphere.planetRadius,&scatteringRayleigh,&scatteringMie,&extinction,&scattering);let transmittanceToLight=sampleTransmittanceLut(transmittanceLut,sampleRadiusGlobal,sampleCosAngleLightToZenith).rgb;\n#if COMPUTE_MULTI_SCATTERING\nlet phaseTimesScattering=uniformPhase*scattering;let S=transmittanceToLight*phaseTimesScattering;\n#else\nlet phaseTimesScattering=scatteringMie*miePhase+scatteringRayleigh*rayleighPhase;let multiScatteredRadiance=sampleMultiScatteringLut(multiScatteringLut,sampleRadiusGlobal,sampleCosAngleLightToZenith);let S=transmittanceScale*transmittanceToLight*phaseTimesScattering+multiScatteringIntensityParam*multiScatteredRadiance*scattering;\n#endif\nlet sampleOpticalDepth=extinction*dt;let sampleTransmittanceVal=exp(-sampleOpticalDepth);let clampedExtinction=max(vec3f(.0000001),extinction);let SInt=(S-S*sampleTransmittanceVal)/clampedExtinction;radiance+=*transmittance*SInt;\n#if COMPUTE_MULTI_SCATTERING\nlet MSInt=(scattering-scattering*sampleTransmittanceVal)/clampedExtinction;*multiScattering+=*transmittance*MSInt;\n#endif\n*transmittance*=sampleTransmittanceVal;}\n#if USE_GROUND_ALBEDO\nif (tMax==tBottom && tBottom>0.) {let planetPos=rayOriginGlobal+tBottom*rayDirection;let planetPosRadius=length(planetPos);let planetPosGeocentricNormal=planetPos/planetPosRadius;let nDotL=dot(directionToLightParam,planetPosGeocentricNormal);let lightTransmittance=sampleTransmittanceLut(transmittanceLut,planetPosRadius,nDotL).rgb;const diffuseBrdf=RECIPROCAL_PI;radiance+=lightTransmittance**transmittance*atmosphere.groundAlbedo*(nDotL*diffuseBrdf);}\n#endif\nradiance*=lightIntensityParam;return radiance;}\n#endif\nfn layerIdxToAerialPerspectiveLayer(layerIdx: f32)->f32 {var layer=(layerIdx+1.)/NumAerialPerspectiveLutLayers;layer*=layer; \nlayer*=NumAerialPerspectiveLutLayers;return layer;}\nfn toAerialPerspectiveDepth(layer: f32)->f32 {return layer*AerialPerspectiveLutKMPerSlice;}\nfn toAerialPerspectiveLayer(distance: f32,aerialPerspectiveLutDistancePerSlice: f32)->f32 {return distance/aerialPerspectiveLutDistancePerSlice;}\nfn applyAerialPerspectiveSaturation(aerialPerspective: vec4f)->vec4f {let previousRadiance=getLuminance(aerialPerspective.rgb);let mixed=mix(vec3f(previousRadiance),aerialPerspective.rgb,atmosphere.aerialPerspectiveSaturation);return vec4f(mixed,aerialPerspective.a);}\nfn applyAerialPerspectiveIntensity(aerialPerspective: vec4f)->vec4f {var result=aerialPerspective;\n#if APPLY_AERIAL_PERSPECTIVE_INTENSITY\nif (atmosphere.aerialPerspectiveIntensity==0.) {result=vec4f(0.);} else {let previousAlpha=result.a;result=result/max(.00001,previousAlpha);result=result*pow(previousAlpha,1./atmosphere.aerialPerspectiveIntensity);}\n#endif\nreturn result;}\nfn applyAerialPerspectiveRadianceBias(aerialPerspective: vec4f)->vec4f {var result=aerialPerspective;\n#if APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS\nlet originalRadiance=dot(result.rgb,LuminanceEncodeApprox);let targetRadiance=originalRadiance+atmosphere.aerialPerspectiveRadianceBias;if (originalRadiance>0.) {result=result*max(0.,targetRadiance/originalRadiance);} else {result=max(vec4f(0.),vec4f(atmosphere.aerialPerspectiveRadianceBias));}\nresult.a=min(result.a,1.);\n#endif\nreturn result;}\nfn sampleAerialPerspectiveLut(\nscreenUV: vec2f,\nclampToLutRange: bool,\ndistanceFromCamera: f32,\nnumAerialPerspectiveLutLayers: f32,\naerialPerspectiveLutKMPerSlice: f32,\naerialPerspectiveLutRangeKM: f32,\naerialPerspective: ptr<function,vec4f>\n)->bool {*aerialPerspective=vec4f(0.);\n#if USE_AERIAL_PERSPECTIVE_LUT\nif (distanceFromCamera>0. &&\n(clampToLutRange || distanceFromCamera<aerialPerspectiveLutRangeKM) &&\natmosphere.clampedCameraRadius<=atmosphere.atmosphereRadius) {var layer=toAerialPerspectiveLayer(distanceFromCamera,aerialPerspectiveLutKMPerSlice);let normalizedLayer=sqrt(layer/numAerialPerspectiveLutLayers); \nlayer=min(normalizedLayer*numAerialPerspectiveLutLayers,numAerialPerspectiveLutLayers);let weight=min(layer,1.);let layerIdx=max(0.,layer-1.);let floorLayerIdx=floor(layerIdx);let aerialPerspectiveLayer0=textureSampleLevel(aerialPerspectiveLut,aerialPerspectiveLutSampler,screenUV,i32(floorLayerIdx),0.);let aerialPerspectiveLayer1=textureSampleLevel(aerialPerspectiveLut,aerialPerspectiveLutSampler,screenUV,i32(floorLayerIdx+1.),0.);var interpolated=mix(aerialPerspectiveLayer0,aerialPerspectiveLayer1,layerIdx-floorLayerIdx);interpolated=vec4f(interpolated.rgb*atmosphere.atmosphereExposure,interpolated.a);interpolated=applyAerialPerspectiveSaturation(interpolated);interpolated=weight*applyAerialPerspectiveIntensity(interpolated);interpolated=applyAerialPerspectiveRadianceBias(interpolated);*aerialPerspective=interpolated;return true;}\n#endif\nreturn false;}\n#if RENDER_TRANSMITTANCE\nfn getTransmittanceParameters(uv: vec2f,radius: ptr<function,f32>,cosAngleLightToZenith: ptr<function,f32>,distanceToAtmosphereEdge: ptr<function,f32>) {let unit=uvToUnit(uv,TransmittanceLutDomainInUVSpace,TransmittanceLutHalfTexelSize);let distanceToHorizon=unit.y*atmosphere.horizonDistanceToAtmosphereEdge;let distanceToHorizonSquared=distanceToHorizon*distanceToHorizon;*radius=sqrtClamped(distanceToHorizonSquared+atmosphere.planetRadiusSquared);let minDistanceToAtmosphereEdge=atmosphere.atmosphereRadius-*radius;let maxDistanceToAtmosphereEdge=distanceToHorizon+atmosphere.horizonDistanceToAtmosphereEdge;*distanceToAtmosphereEdge=minDistanceToAtmosphereEdge+unit.x*(maxDistanceToAtmosphereEdge-minDistanceToAtmosphereEdge);let distanceToAtmosphereEdgeSquared=*distanceToAtmosphereEdge**distanceToAtmosphereEdge;*cosAngleLightToZenith=select(\n(atmosphere.horizonDistanceToAtmosphereEdgeSquared-distanceToAtmosphereEdgeSquared-distanceToHorizonSquared)/(2.**radius**distanceToAtmosphereEdge),\n1.,\n*distanceToAtmosphereEdge<=0.\n);*cosAngleLightToZenith=clamp(*cosAngleLightToZenith,-1.,1.);}\nfn renderTransmittance(uv: vec2f)->vec4f {var radius: f32;var cosAngleLightToZenith: f32;var distanceToAtmosphereEdgeAlongAngle: f32;getTransmittanceParameters(uv,&radius,&cosAngleLightToZenith,&distanceToAtmosphereEdgeAlongAngle);let sinAngleLightToZenith=sqrtClamped(1.-cosAngleLightToZenith*cosAngleLightToZenith);let directionToLight=normalize(vec3f(0.,cosAngleLightToZenith,sinAngleLightToZenith));let transmittance=computeTransmittance(vec3f(0.,radius,0.),directionToLight,distanceToAtmosphereEdgeAlongAngle,TransmittanceSampleCount);return vec4f(transmittance,avg(transmittance));}\n#endif\n#if RENDER_MULTI_SCATTERING\nfn getSphereSample(azimuth: f32,inclination: f32,sinInclination: ptr<function,f32>)->vec3f {*sinInclination=sin(inclination);return vec3f(*sinInclination*sin(azimuth),cos(inclination),*sinInclination*cos(azimuth));}\nconst MultiScatteringInclinationSampleCount=8.;const MultiScatteringAzimuthSampleCount=2.*MultiScatteringInclinationSampleCount;const MultiScatteringSampleCount=64;const MultiScatteringAzimuthIterationAngle=TWO_PI/MultiScatteringAzimuthSampleCount;const MultiScatteringInclinationIterationAngle=PI/MultiScatteringInclinationSampleCount;const MultiScatteringAngleStepProduct=MultiScatteringAzimuthIterationAngle*MultiScatteringInclinationIterationAngle;fn renderMultiScattering(uv: vec2f,transmittanceLut: texture_2d<f32>)->vec4f {let unit=uvToUnit(uv,MultiScatteringLutDomainInUVSpace,MultiScatteringLutHalfTexelSize);let cosAngleLightToZenith=2.*unit.x-1.;let sinAngleLightToZenith=sqrtClamped(1.-cosAngleLightToZenith*cosAngleLightToZenith);let directionToLightLocal=normalize(vec3f(0.,cosAngleLightToZenith,sinAngleLightToZenith));let rayOriginRadius=atmosphere.planetRadius+max(unit.y,.001)*atmosphere.atmosphereThickness;let rayOrigin=vec3f(0.,rayOriginRadius,0.);var inscattered=vec3f(0.);var multiScatteringTotal=vec3f(0.);for (var i=.5; i<MultiScatteringAzimuthSampleCount; i+=1.) {let azimuth=MultiScatteringAzimuthIterationAngle*i;for (var j=.5; j<MultiScatteringInclinationSampleCount; j+=1.) {let inclination=MultiScatteringInclinationIterationAngle*j;var sinInclination: f32;let rayDirection=getSphereSample(azimuth,inclination,&sinInclination);var transmittanceVal: vec3f;var multiScatteringVal: vec3f;let radianceVal=integrateScatteredRadiance(\nfalse,\n1.,\ntransmittanceLut,\nrayOrigin,\nrayDirection,\ndirectionToLightLocal,\n100000000.,\nMultiScatteringSampleCount,\n-1.,\n&transmittanceVal,\n&multiScatteringVal);let weight=RECIPROCAL_PI4*abs(sinInclination)*MultiScatteringAngleStepProduct;multiScatteringTotal+=multiScatteringVal*weight;inscattered+=radianceVal*weight;}}\nlet multiScatteringResult=inscattered/max(vec3f(.000001),vec3f(1.)-multiScatteringTotal);return vec4f(multiScatteringResult,1.);}\n#endif\n`;\n// Sideeffect\nif (!ShaderStore.IncludesShadersStoreWGSL[name]) {\n ShaderStore.IncludesShadersStoreWGSL[name] = shader;\n}\n/** @internal */\nexport const atmosphereFunctionsWGSL = { name, shader };\n"]}
@@ -0,0 +1,8 @@
1
+ import "../ShadersWGSL/ShadersInclude/atmosphereUboDeclaration.js";
2
+ import "@onerjs/core/ShadersWGSL/ShadersInclude/helperFunctions.js";
3
+ import "../ShadersWGSL/ShadersInclude/atmosphereFunctions.js";
4
+ /** @internal */
5
+ export declare const multiScatteringPixelShaderWGSL: {
6
+ name: string;
7
+ shader: string;
8
+ };
@@ -0,0 +1,22 @@
1
+ // Do not edit.
2
+ import { ShaderStore } from "@onerjs/core/Engines/shaderStore.js";
3
+ import "../ShadersWGSL/ShadersInclude/atmosphereUboDeclaration.js";
4
+ import "@onerjs/core/ShadersWGSL/ShadersInclude/helperFunctions.js";
5
+ import "../ShadersWGSL/ShadersInclude/atmosphereFunctions.js";
6
+ const name = "multiScatteringPixelShader";
7
+ const shader = `#define RENDER_MULTI_SCATTERING
8
+ #define COMPUTE_MULTI_SCATTERING
9
+ #include<atmosphereUboDeclaration>
10
+ var transmittanceLutSampler: sampler;var transmittanceLut: texture_2d<f32>;
11
+ #include<helperFunctions>
12
+ #include<atmosphereFunctions>
13
+ varying vUV: vec2f;@fragment
14
+ fn main(input: FragmentInputs)->FragmentOutputs {fragmentOutputs.color=renderMultiScattering(input.vUV,transmittanceLut);}
15
+ `;
16
+ // Sideeffect
17
+ if (!ShaderStore.ShadersStoreWGSL[name]) {
18
+ ShaderStore.ShadersStoreWGSL[name] = shader;
19
+ }
20
+ /** @internal */
21
+ export const multiScatteringPixelShaderWGSL = { name, shader };
22
+ //# sourceMappingURL=multiScattering.fragment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"multiScattering.fragment.js","sourceRoot":"","sources":["../../../../../dev/addons/src/atmosphere/ShadersWGSL/multiScattering.fragment.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,OAAO,EAAE,WAAW,EAAE,4CAAiC;AACvD,OAAO,wDAAwD,CAAC;AAChE,oEAAyD;AACzD,OAAO,mDAAmD,CAAC;AAE3D,MAAM,IAAI,GAAG,4BAA4B,CAAC;AAC1C,MAAM,MAAM,GAAG;;;;;;;;CAQd,CAAC;AACF,aAAa;AACb,IAAI,CAAC,WAAW,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;IACtC,WAAW,CAAC,gBAAgB,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC;AAChD,CAAC;AACD,gBAAgB;AAChB,MAAM,CAAC,MAAM,8BAA8B,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC","sourcesContent":["// Do not edit.\nimport { ShaderStore } from \"core/Engines/shaderStore\";\nimport \"../ShadersWGSL/ShadersInclude/atmosphereUboDeclaration\";\nimport \"core/ShadersWGSL/ShadersInclude/helperFunctions\";\nimport \"../ShadersWGSL/ShadersInclude/atmosphereFunctions\";\n\nconst name = \"multiScatteringPixelShader\";\nconst shader = `#define RENDER_MULTI_SCATTERING\n#define COMPUTE_MULTI_SCATTERING\n#include<atmosphereUboDeclaration>\nvar transmittanceLutSampler: sampler;var transmittanceLut: texture_2d<f32>;\n#include<helperFunctions>\n#include<atmosphereFunctions>\nvarying vUV: vec2f;@fragment\nfn main(input: FragmentInputs)->FragmentOutputs {fragmentOutputs.color=renderMultiScattering(input.vUV,transmittanceLut);}\n`;\n// Sideeffect\nif (!ShaderStore.ShadersStoreWGSL[name]) {\n ShaderStore.ShadersStoreWGSL[name] = shader;\n}\n/** @internal */\nexport const multiScatteringPixelShaderWGSL = { name, shader };\n"]}
@@ -18,7 +18,6 @@ import "./Shaders/compositeAerialPerspective.fragment.js";
18
18
  import "./Shaders/compositeSky.fragment.js";
19
19
  import "./Shaders/compositeGlobeAtmosphere.fragment.js";
20
20
  import "./Shaders/fullscreenTriangle.vertex.js";
21
- import "./Shaders/multiScattering.fragment.js";
22
21
  import "./Shaders/skyView.fragment.js";
23
22
  import "./Shaders/aerialPerspective.fragment.js";
24
23
  import "./Shaders/ShadersInclude/atmosphereFragmentDeclaration.js";
@@ -37,6 +36,7 @@ export declare class Atmosphere implements IDisposable {
37
36
  private readonly _directionToLight;
38
37
  private readonly _tempSceneAmbient;
39
38
  private readonly _engine;
39
+ private readonly _isDiffuseSkyIrradianceLutEnabled;
40
40
  private _physicalProperties;
41
41
  private _transmittanceLut;
42
42
  private _diffuseSkyIrradianceLut;
@@ -74,6 +74,7 @@ export declare class Atmosphere implements IDisposable {
74
74
  private _isEnabled;
75
75
  private _aerialPerspectiveLutHasBeenRendered;
76
76
  private _hasRenderedMultiScatteringLut;
77
+ private _hasEverRenderedMultiScatteringLut;
77
78
  private _multiScatteringEffectWrapper;
78
79
  private _multiScatteringLutRenderTarget;
79
80
  private _aerialPerspectiveLutEffectWrapper;
@@ -86,6 +87,7 @@ export declare class Atmosphere implements IDisposable {
86
87
  private _skyCompositorEffectWrapper;
87
88
  private _globeAtmosphereCompositorEffectWrapper;
88
89
  private _onBeforeCameraRenderObserver;
90
+ private _onBeforeRenderObserver;
89
91
  private _onBeforeDrawPhaseObserver;
90
92
  private _onAfterRenderingGroupObserver;
91
93
  /**
@@ -354,6 +356,7 @@ export declare class Atmosphere implements IDisposable {
354
356
  private _disposeSkyCompositor;
355
357
  private _disposeAerialPerspectiveCompositor;
356
358
  private _disposeGlobeAtmosphereCompositor;
359
+ private get _isGlobalLutsReady();
357
360
  /**
358
361
  * Updates the camera variables that are specific to the atmosphere.
359
362
  * @param camera - The camera to update the variables for.