@takram/three-atmosphere
Version:
A Three.js and R3F implementation of Precomputed Atmospheric Scattering
378 lines (377 loc) • 9.76 kB
JavaScript
import { jsx as x, jsxs as N, Fragment as U } from "react/jsx-runtime";
import { useThree as k, useFrame as v, extend as b, useLoader as I } from "@react-three/fiber";
import { EffectComposerContext as _ } from "@react-three/postprocessing";
import { RenderPass as G } from "postprocessing";
import { createContext as z, useRef as w, useMemo as g, useEffect as h, useImperativeHandle as H, useContext as T, useState as B, version as W, useCallback as J } from "react";
import { Matrix4 as j, Vector3 as R, Texture as K, Object3D as Q } from "three";
import { Ellipsoid as V, DEFAULT_STBN_URL as X, STBNLoader as Y, ArrayBufferLoader as q } from "@takram/three-geospatial";
import { x as F, P as Z, g as $, e as ee, f as te, a as re, A as ne, L as oe, B as se, C as ie, t as O, z as ae, y as ce, F as le, k as ue, E as pe, G as fe, J as de, H as me, j as ge } from "./shared.js";
import { ScreenQuad as xe } from "@react-three/drei";
const M = /* @__PURE__ */ z({}), be = ({
ref: r,
textures: e,
ellipsoid: s = V.WGS84,
correctAltitude: n = !0,
date: t,
children: l
}) => {
const i = w({
sunDirection: new R(),
moonDirection: new R(),
rotationMatrix: new j(),
ellipsoidCenter: new R(),
ellipsoidMatrix: new j(),
overlay: null,
shadow: null,
shadowLength: null,
lightingMask: null
}), p = k(({ gl: c }) => c), u = g(
() => typeof e == "string" ? new F().setType(p).load(e) : void 0,
[e, p]
);
h(() => {
if (u != null)
return () => {
for (const c of Object.values(u))
c == null || c.dispose();
};
}, [u]);
const a = g(
() => e == null ? new Z(p) : void 0,
[e, p]
);
h(() => {
if (a != null)
return a.update().catch((c) => {
console.error(c);
}), () => {
a.dispose();
};
}, [a]);
const f = (a == null ? void 0 : a.textures) ?? (typeof e == "string" ? u : e), y = g(
() => ({
textures: f,
ellipsoid: s,
correctAltitude: n,
transientStates: i.current
}),
[f, s, n]
), d = g(() => {
const { sunDirection: c, moonDirection: m, rotationMatrix: D } = i.current;
return (S) => {
$(S, D), ee(S, c).applyMatrix4(D), te(S, m).applyMatrix4(D);
};
}, []), o = t != null && !isNaN(+t) ? +t : void 0;
return h(() => {
o != null && d(o);
}, [o, d]), H(
r,
() => ({
...i.current,
textures: f,
updateByDate: d
}),
[f, d]
), /* @__PURE__ */ x(M.Provider, { value: y, children: l });
};
function A(r) {
const {
irradianceTexture: e,
scatteringTexture: s,
transmittanceTexture: n,
singleMieScatteringTexture: t,
higherOrderScatteringTexture: l,
ellipsoid: i,
correctAltitude: p,
sunDirection: u,
sunAngularRadius: a,
renderTargetCount: f,
...y
} = r;
return [
{
irradianceTexture: e,
scatteringTexture: s,
transmittanceTexture: n,
singleMieScatteringTexture: t,
higherOrderScatteringTexture: l,
ellipsoid: i,
correctAltitude: p,
sunDirection: u,
sunAngularRadius: a,
renderTargetCount: f
},
y
];
}
function ye(r) {
const e = g(
() => typeof r == "string" ? new Y().load(r) : void 0,
[r]
);
return h(() => {
if (e != null)
return () => {
e.dispose();
};
}, [e]), (typeof r == "string" ? e : r) ?? null;
}
const Ae = ({
ref: r,
stbnTexture: e = X,
...s
}) => {
const { textures: n, transientStates: t, ...l } = T(M), [i, { blendFunction: p, ...u }] = A({
...re,
...l,
...n,
...s
}), a = T(_), { normalPass: f, camera: y } = a, d = "geometryPass" in a && a.geometryPass instanceof G && "geometryTexture" in a.geometryPass && a.geometryPass.geometryTexture instanceof K ? a.geometryPass.geometryTexture : void 0, o = g(
() => new ne(void 0, { blendFunction: p }),
[p]
);
h(() => () => {
o.dispose();
}, [o]);
const [c, m] = B(!1);
v(() => {
t != null && (o.sunDirection.copy(t.sunDirection), o.moonDirection.copy(t.moonDirection), o.ellipsoidCenter.copy(t.ellipsoidCenter), o.ellipsoidMatrix.copy(t.ellipsoidMatrix), o.overlay = t.overlay, o.shadow = t.shadow, o.shadowLength = t.shadowLength, o.lightingMask = t.lightingMask, !c && o.shadow != null && m(!0));
});
const D = ye(
c ? e : void 0
);
return /* @__PURE__ */ x(
"primitive",
{
ref: r,
object: o,
mainCamera: y,
normalBuffer: d ?? (f == null ? void 0 : f.texture) ?? null,
...i,
...u,
stbnTexture: D,
octEncodedNormal: d != null
}
);
};
function he(r) {
return (e) => {
for (const s of r) E(s, e);
};
}
function De(r) {
return (e) => {
const s = [];
for (const n of r) {
const t = E(n, e), l = typeof t == "function";
s.push(l ? t : () => E(n, null));
}
return () => {
for (const n of s) n();
};
};
}
function E(r, e) {
if (typeof r == "function")
return r(e);
r && (r.current = e);
}
var P = parseInt(W.split(".")[0], 10) >= 19 ? De : he;
const Te = ({
ref: r,
...e
}) => {
const { transientStates: s } = T(M), n = J(
(i) => {
if (i != null && s != null)
return s.lightingMask = {
map: i.texture,
channel: "r"
}, () => {
s.lightingMask = null;
};
},
[s]
), { scene: t, camera: l } = T(_);
return b({ LightingMaskPass: oe }), /* @__PURE__ */ x(
"lightingMaskPass",
{
ref: P([n, r]),
...e,
args: [t, l]
}
);
}, je = Te, Ne = ({ ref: r, ...e }) => {
const { textures: s, transientStates: n, ...t } = T(M), [
l,
{
sun: i,
moon: p,
moonDirection: u,
moonAngularRadius: a,
lunarRadianceScale: f,
groundAlbedo: y,
...d
}
] = A({
...se,
...t,
...s,
...e
}), o = g(() => new ie(), []);
return h(() => () => {
o.dispose();
}, [o]), v(() => {
n != null && (o.sunDirection.copy(n.sunDirection), o.moonDirection.copy(n.moonDirection), o.ellipsoidCenter.copy(n.ellipsoidCenter), o.ellipsoidMatrix.copy(n.ellipsoidMatrix), o.shadowLength = n.shadowLength);
}), /* @__PURE__ */ x(
xe,
{
renderOrder: O,
...d,
ref: r,
children: /* @__PURE__ */ x(
"primitive",
{
object: o,
...l,
sun: i,
moon: p,
moonDirection: u,
moonAngularRadius: a,
lunarRadianceScale: f,
groundAlbedo: y
}
)
}
);
}, _e = ({
ref: r,
...e
}) => {
const { textures: s, transientStates: n, ...t } = T(M), l = w(null);
return v(() => {
const i = l.current;
i != null && n != null && (i.sunDirection.copy(n.sunDirection), i.ellipsoidCenter.copy(n.ellipsoidCenter), i.ellipsoidMatrix.copy(n.ellipsoidMatrix), i.update());
}), b({ SkyLightProbe: ae }), /* @__PURE__ */ x(
"skyLightProbe",
{
ref: P([l, r]),
...ce,
...t,
...s,
...e
}
);
}, Be = ({
ref: r,
data: e = ue,
...s
}) => {
const { textures: n, transientStates: t, ...l } = T(M), [
i,
{ pointSize: p, radianceScale: u, intensity: a, background: f, ...y }
] = A({
...le,
...l,
...n,
...s
}), [d, o] = B(
typeof e != "string" ? e : void 0
);
h(() => {
if (typeof e == "string") {
const C = new q();
(async () => {
o(await C.loadAsync(e));
})().catch((L) => {
console.error(L);
});
} else
o(e);
}, [e]);
const c = g(
() => d != null ? new pe(d) : void 0,
[d]
);
h(() => () => {
c == null || c.dispose();
}, [c]);
const m = g(() => new fe(), []);
h(() => () => {
m.dispose();
}, [m]);
const D = w(null);
v(({ camera: C }) => {
var L;
t != null && C.isPerspectiveCamera === !0 && (m.sunDirection.copy(t.sunDirection), (L = D.current) == null || L.setRotationFromMatrix(t.rotationMatrix), m.ellipsoidCenter.copy(t.ellipsoidCenter), m.ellipsoidMatrix.copy(t.ellipsoidMatrix));
});
const S = k(({ camera: C }) => C);
return c == null || S.isPerspectiveCamera !== !0 ? null : /* @__PURE__ */ N(
"points",
{
ref: P([D, r]),
frustumCulled: !1,
renderOrder: O + 1,
...y,
children: [
/* @__PURE__ */ x("primitive", { object: c }),
/* @__PURE__ */ x(
"primitive",
{
object: m,
...i,
pointSize: p,
radianceScale: u,
intensity: a,
background: f,
depthTest: !0,
depthWrite: !1
}
)
]
}
);
}, Fe = ({
ref: r,
position: e,
...s
}) => {
const { textures: n, transientStates: t, ...l } = T(M), i = w(null);
v(() => {
const u = i.current;
u != null && t != null && (u.sunDirection.copy(t.sunDirection), u.ellipsoidCenter.copy(t.ellipsoidCenter), u.ellipsoidMatrix.copy(t.ellipsoidMatrix), u.update());
});
const p = g(() => new Q(), []);
return b({ SunDirectionalLight: de }), /* @__PURE__ */ N(U, { children: [
/* @__PURE__ */ x(
"sunDirectionalLight",
{
ref: P([i, r]),
...me,
...l,
...n,
...s,
target: p
}
),
/* @__PURE__ */ x("primitive", { object: p, position: e })
] });
}, Me = new F();
function Oe(r = ge) {
const e = k(({ gl: n }) => n);
return { textures: I(Me.setType(e), r) };
}
export {
Ae as AerialPerspective,
be as Atmosphere,
M as AtmosphereContext,
je as IrradianceMask,
Te as LightingMask,
Ne as Sky,
_e as SkyLight,
Be as Stars,
Fe as SunLight,
A as separateProps,
Oe as useAtmosphereTextureProps
};
//# sourceMappingURL=r3f.js.map