@takram/three-atmosphere
Version:
A Three.js and R3F implementation of Precomputed Atmospheric Scattering
3 lines (2 loc) • 6.7 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("react/jsx-runtime"),h=require("@react-three/fiber"),v=require("@react-three/postprocessing"),A=require("postprocessing"),e=require("react"),R=require("three"),L=require("@takram/three-geospatial"),i=require("./shared.cjs"),b=require("@react-three/drei"),M=e.createContext({}),j=e.forwardRef(function({textures:r=i.DEFAULT_PRECOMPUTED_TEXTURES_URL,ellipsoid:n=L.Ellipsoid.WGS84,correctAltitude:a=!0,photometric:s=!0,date:o,children:m},u){const d=e.useRef({sunDirection:new R.Vector3,moonDirection:new R.Vector3,rotationMatrix:new R.Matrix4,ellipsoidCenter:new R.Vector3,ellipsoidMatrix:new R.Matrix4,overlay:null,shadow:null,shadowLength:null}),f=h.useThree(({gl:p})=>p),[l,g]=e.useState(typeof r!="string"?r:void 0);e.useEffect(()=>{if(typeof r=="string"){const p=new i.PrecomputedTexturesLoader;p.setTypeFromRenderer(f),(async()=>{g(await p.loadAsync(r))})().catch(T=>{console.error(T)})}else g(r)},[r,f]);const S=e.useMemo(()=>({textures:l,ellipsoid:n,correctAltitude:a,photometric:s,transientStates:d.current}),[l,n,a,s]),x=e.useMemo(()=>{const{sunDirection:p,moonDirection:T,rotationMatrix:E}=d.current;return D=>{i.getECIToECEFRotationMatrix(D,E),i.getSunDirectionECI(D,p).applyMatrix4(E),i.getMoonDirectionECI(D,T).applyMatrix4(E)}},[]),t=o!=null&&!isNaN(+o)?+o:void 0;return e.useEffect(()=>{t!=null&&x(t)},[t,x]),e.useImperativeHandle(u,()=>({...d.current,textures:l,updateByDate:x}),[l,x]),y.jsx(M.Provider,{value:S,children:m})});function w(c){const{irradianceTexture:r,scatteringTexture:n,transmittanceTexture:a,ellipsoid:s,correctAltitude:o,photometric:m,sunDirection:u,sunAngularRadius:d,renderTargetCount:f,...l}=c;return[{irradianceTexture:r,scatteringTexture:n,transmittanceTexture:a,ellipsoid:s,correctAltitude:o,photometric:m,sunDirection:u,sunAngularRadius:d,renderTargetCount:f},l]}function F(c){const[r,n]=e.useState(typeof c!="string"?c??null:null);return e.useEffect(()=>{if(typeof c=="string"){const a=new L.STBNLoader;(async()=>{n(await a.loadAsync(c))})().catch(s=>{console.error(s)})}else n(c??null)},[c]),r}const _=e.forwardRef(function({stbnTexture:r=L.DEFAULT_STBN_URL,...n},a){const{textures:s,transientStates:o,...m}=e.useContext(M),[u,{blendFunction:d,...f}]=w({...i.aerialPerspectiveEffectOptionsDefaults,...m,...s,...n}),l=e.useContext(v.EffectComposerContext),{normalPass:g,camera:S}=l,x="geometryPass"in l&&l.geometryPass instanceof A.RenderPass&&"geometryTexture"in l.geometryPass&&l.geometryPass.geometryTexture instanceof R.Texture?l.geometryPass.geometryTexture:void 0,t=e.useMemo(()=>new i.AerialPerspectiveEffect(void 0,{blendFunction:d}),[d]);e.useEffect(()=>()=>{t.dispose()},[t]);const[p,T]=e.useState(!1);h.useFrame(()=>{o!=null&&(t.sunDirection.copy(o.sunDirection),t.moonDirection.copy(o.moonDirection),t.ellipsoidCenter.copy(o.ellipsoidCenter),t.ellipsoidMatrix.copy(o.ellipsoidMatrix),t.overlay=o.overlay,t.shadow=o.shadow,t.shadowLength=o.shadowLength,!p&&t.shadow!=null&&T(!0))});const E=F(p?r:void 0);return y.jsx("primitive",{ref:a,object:t,mainCamera:S,normalBuffer:x??(g==null?void 0:g.texture)??null,...u,...f,stbnTexture:E,octEncodedNormal:x!=null})}),k=e.forwardRef(function(r,n){const{textures:a,transientStates:s,...o}=e.useContext(M),[m,{sun:u,moon:d,moonDirection:f,moonAngularRadius:l,lunarRadianceScale:g,groundAlbedo:S,...x}]=w({...i.skyMaterialParametersDefaults,...o,...a,...r}),t=e.useMemo(()=>new i.SkyMaterial,[]);return e.useEffect(()=>()=>{t.dispose()},[t]),h.useFrame(()=>{s!=null&&(t.sunDirection.copy(s.sunDirection),t.moonDirection.copy(s.moonDirection),t.ellipsoidCenter.copy(s.ellipsoidCenter),t.ellipsoidMatrix.copy(s.ellipsoidMatrix),t.shadowLength=s.shadowLength)}),y.jsx(b.ScreenQuad,{renderOrder:i.SKY_RENDER_ORDER,...x,ref:n,children:y.jsx("primitive",{object:t,...m,sun:u,moon:d,moonDirection:f,moonAngularRadius:l,lunarRadianceScale:g,groundAlbedo:S})})});function P(c){return r=>{c.forEach(n=>{typeof n=="function"?n(r):n!=null&&(n.current=r)})}}const U=e.forwardRef(function(r,n){const{textures:a,transientStates:s,...o}=e.useContext(M),m=e.useRef(null);return h.useFrame(()=>{const u=m.current;u!=null&&s!=null&&(u.sunDirection.copy(s.sunDirection),u.ellipsoidCenter.copy(s.ellipsoidCenter),u.ellipsoidMatrix.copy(s.ellipsoidMatrix),u.update())}),h.extend({SkyLightProbe:i.SkyLightProbe}),y.jsx("skyLightProbe",{ref:P([m,n]),...i.skyLightProbeParametersDefaults,...o,...a,...r})}),N=e.forwardRef(function({data:r=i.DEFAULT_STARS_DATA_URL,...n},a){const{textures:s,transientStates:o,...m}=e.useContext(M),[u,{pointSize:d,radianceScale:f,background:l,...g}]=w({...i.starsMaterialParametersDefaults,...m,...s,...n}),[S,x]=e.useState(typeof r!="string"?r:void 0);e.useEffect(()=>{if(typeof r=="string"){const D=new L.ArrayBufferLoader;(async()=>{x(await D.loadAsync(r))})().catch(C=>{console.error(C)})}else x(r)},[r]);const t=e.useMemo(()=>S!=null?new i.StarsGeometry(S):void 0,[S]);e.useEffect(()=>()=>{t==null||t.dispose()},[t]);const p=e.useMemo(()=>new i.StarsMaterial,[]);e.useEffect(()=>()=>{p.dispose()},[p]);const T=e.useRef(null);h.useFrame(({camera:D})=>{var C;o!=null&&D.isPerspectiveCamera===!0&&(p.sunDirection.copy(o.sunDirection),(C=T.current)==null||C.setRotationFromMatrix(o.rotationMatrix),p.ellipsoidCenter.copy(o.ellipsoidCenter),p.ellipsoidMatrix.copy(o.ellipsoidMatrix))});const E=h.useThree(({camera:D})=>D);return t==null||E.isPerspectiveCamera!==!0?null:y.jsxs("points",{ref:P([T,a]),frustumCulled:!1,renderOrder:i.SKY_RENDER_ORDER+1,...g,children:[y.jsx("primitive",{object:t}),y.jsx("primitive",{object:p,...u,pointSize:d,radianceScale:f,background:l,depthTest:!0,depthWrite:!1})]})}),q=e.forwardRef(function({position:r,...n},a){const{textures:s,transientStates:o,...m}=e.useContext(M),u=e.useRef(null);h.useFrame(()=>{const f=u.current;f!=null&&o!=null&&(f.sunDirection.copy(o.sunDirection),f.ellipsoidCenter.copy(o.ellipsoidCenter),f.ellipsoidMatrix.copy(o.ellipsoidMatrix),f.update())});const d=e.useMemo(()=>new R.Object3D,[]);return h.extend({SunDirectionalLight:i.SunDirectionalLight}),y.jsxs(y.Fragment,{children:[y.jsx("sunDirectionalLight",{ref:P([u,a]),...i.sunDirectionalLightParametersDefaults,...m,...s,...n,target:d}),y.jsx("primitive",{object:d,position:r})]})});function O(c=i.DEFAULT_PRECOMPUTED_TEXTURES_URL){const r=h.useThree(({gl:a})=>a);return{textures:h.useLoader(i.PrecomputedTexturesLoader,c,a=>{a.setTypeFromRenderer(r)})}}exports.AerialPerspective=_;exports.Atmosphere=j;exports.AtmosphereContext=M;exports.Sky=k;exports.SkyLight=U;exports.Stars=N;exports.SunLight=q;exports.separateProps=w;exports.useAtmosphereTextureProps=O;
//# sourceMappingURL=r3f.cjs.map