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