UNPKG

@takram/three-clouds

Version:
3 lines (2 loc) 4.03 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("react/jsx-runtime"),C=require("@react-three/fiber"),M=require("@react-three/postprocessing"),r=require("react"),l=require("three"),m=require("@takram/three-atmosphere/r3f"),T=require("@takram/three-geospatial"),u=require("./shared.cjs"),w=r.createContext(null),O=({layers:e,disableDefault:t=!1,children:n})=>{const[a,d]=r.useState();return r.useLayoutEffect(()=>{e.set(t?Array(4).fill(u.CloudLayer.DEFAULT):u.CloudLayers.DEFAULT),d({layers:e,indexPool:[0,1,2,3],disableDefault:t})},[e,t]),a!=null&&h.jsx(w.Provider,{value:a,children:n})};function g(e,t){const n=r.useMemo(()=>typeof e=="string"?new l.TextureLoader().load(e,a=>{a.minFilter=l.LinearMipMapLinearFilter,a.magFilter=l.LinearFilter,a.wrapS=l.RepeatWrapping,a.wrapT=l.RepeatWrapping,a.colorSpace=l.NoColorSpace,a.needsUpdate=!0}):void 0,[e]);return r.useEffect(()=>{if(n!=null)return()=>{n.dispose()}},[n]),(typeof e=="string"?n:e)??null}function S(e,t){const n=r.useMemo(()=>typeof e=="string"?new T.DataTextureLoader(l.Data3DTexture,T.parseUint8Array,{width:t,height:t,depth:t,format:l.RedFormat,minFilter:l.LinearFilter,magFilter:l.LinearFilter,wrapS:l.RepeatWrapping,wrapT:l.RepeatWrapping,wrapR:l.RepeatWrapping,colorSpace:l.NoColorSpace}).load(e):void 0,[e,t]);return r.useEffect(()=>{if(n!=null)return()=>{n.dispose()}},[n]),(typeof e=="string"?n:e)??null}function q(e){const t=r.useMemo(()=>typeof e=="string"?new T.STBNLoader().load(e):void 0,[e]);return r.useEffect(()=>{if(t!=null)return()=>{t.dispose()}},[t]),(typeof e=="string"?t:e)??null}const P=({ref:e,disableDefaultLayers:t=!1,localWeatherTexture:n=u.DEFAULT_LOCAL_WEATHER_URL,shapeTexture:a=u.DEFAULT_SHAPE_URL,shapeDetailTexture:d=u.DEFAULT_SHAPE_DETAIL_URL,turbulenceTexture:p=u.DEFAULT_TURBULENCE_URL,stbnTexture:L=T.DEFAULT_STBN_URL,children:c,...f})=>{const{textures:i,transientStates:o,...E}=r.useContext(m.AtmosphereContext),[D,F]=m.separateProps({...u.cloudsPassOptionsDefaults,...E,...i,...f}),s=r.useMemo(()=>new u.CloudsEffect,[]);r.useEffect(()=>()=>{s.dispose()},[s]),C.useFrame(()=>{o!=null&&(s.sunDirection.copy(o.sunDirection),s.worldToECEFMatrix.copy(o.worldToECEFMatrix))}),r.useEffect(()=>{if(o!=null)return o.overlay=s.atmosphereOverlay,o.shadow=s.atmosphereShadow,o.shadowLength=s.atmosphereShadowLength,()=>{o.overlay=null,o.shadow=null,o.shadowLength=null}},[s,o]);const y=r.useCallback(x=>{if(o!=null)switch(x.property){case"atmosphereOverlay":o.overlay=s.atmosphereOverlay;break;case"atmosphereShadow":o.shadow=s.atmosphereShadow;break;case"atmosphereShadowLength":o.shadowLength=s.atmosphereShadowLength;break}},[s,o]);r.useEffect(()=>(s.events.addEventListener("change",y),()=>{s.events.removeEventListener("change",y)}),[s,y]),C.useThree(({gl:x})=>x);const U=g(n),A=S(a,u.CLOUD_SHAPE_TEXTURE_SIZE),v=S(d,u.CLOUD_SHAPE_DETAIL_TEXTURE_SIZE),_=g(p),b=q(L),{camera:R}=r.useContext(M.EffectComposerContext);return h.jsxs(h.Fragment,{children:[h.jsx("primitive",{ref:e,object:s,mainCamera:R,...D,localWeatherTexture:U,shapeTexture:A,shapeDetailTexture:v,turbulenceTexture:_,stbnTexture:b,...F}),h.jsx(O,{layers:s.cloudLayers,disableDefault:t,children:c})]})},W=({ref:e,index:t,...n})=>{const a=r.useContext(w);if(a==null)throw new Error("CloudLayer can only be used within the Clouds component!");const{layers:d,indexPool:p,disableDefault:L}=a,[c,f]=r.useState();if(r.useLayoutEffect(()=>{if(t!=null){const i=p.indexOf(t);if(i!==-1)return p.splice(i,1),f(t),()=>{p.push(t),f(void 0)}}else{const i=p.sort((o,E)=>o-E).shift();if(i!=null)return f(i),()=>{p.push(i),f(void 0)}}},[t,d,p]),r.useLayoutEffect(()=>{if(c==null)return;const i=d[c];return()=>{i.copy(L?u.CloudLayer.DEFAULT:u.CloudLayers.DEFAULT[c])}},[d,c,L]),r.useEffect(()=>{c!=null&&(typeof e=="function"?e(d[c]):e!=null&&(e.current=d[c]))},[e,d,c]),c!=null){const i=d[c];i.copy(L?u.CloudLayer.DEFAULT:u.CloudLayers.DEFAULT[c]),i.set(n)}return null};exports.CloudLayer=W;exports.Clouds=P; //# sourceMappingURL=r3f.cjs.map