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