Skip to content

wip: signals #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/components/Env/LightComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useRef } from "react";
import { Light, useStore } from "../../hooks/useStore";
import { useSignals } from "./useSignals";
import { Lightformer } from "@react-three/drei";
import * as THREE from "three";
import { LayerMaterial } from "lamina";
import { LightformerLayers } from "./LightformerLayers";

export function LightComponent({ light }: { light: Light }) {
const {
id,
distance,
phi,
theta,
intensity,
shape,
scale,
scaleX,
scaleY,
visible,
rotation,
opacity,
} = light;

const ref = useRef<THREE.Mesh>(null);

const signals = useStore((state) => state.getSignalsForTarget(id));

useSignals(ref, signals);

return (
<Lightformer
ref={ref}
visible={visible}
form={shape}
intensity={intensity}
position={new THREE.Vector3().setFromSphericalCoords(
distance,
phi,
theta
)}
rotation={[rotation, 0, 0]}
scale={[scale * scaleX, scale * scaleY, scale]}
target={[0, 0, 0]}
castShadow={false}
receiveShadow={false}
>
<LayerMaterial
alpha={opacity}
transparent
side={THREE.DoubleSide}
toneMapped={false}
>
<LightformerLayers light={light} />
</LayerMaterial>
</Lightformer>
);
}
64 changes: 6 additions & 58 deletions src/components/Env/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { LayerMaterial } from "lamina";
import { useControls, folder, LevaInputs } from "leva";
import { useStore } from "../../hooks/useStore";
import { LightformerLayers } from "./LightformerLayers";
import { useState } from "react";
import React, { useRef, useState } from "react";
import { useSignals } from "./useSignals";
import { LightComponent } from "./LightComponent";

export function Env() {
const mode = useStore((state) => state.mode);
Expand Down Expand Up @@ -72,63 +74,9 @@ export function Env() {
near={0.01}
>
<color attach="background" args={[backgroundColor]} />
{lights.map((light) => {
const {
id,
distance,
phi,
theta,
intensity,
shape,
scale,
scaleX,
scaleY,
visible,
rotation,
opacity,
animate,
animationSpeed,
animationFloatIntensity,
animationRotationIntensity,
animationFloatingRange,
} = light;

return (
<Float
key={id}
enabled={animate}
speed={animationSpeed} // Animation speed, defaults to 1
rotationIntensity={animationRotationIntensity} // XYZ rotation intensity, defaults to 1
floatIntensity={animationFloatIntensity} // Up/down float intensity, works like a multiplier with floatingRange,defaults to 1
floatingRange={animationFloatingRange}
>
<Lightformer
visible={visible}
form={shape}
intensity={intensity}
position={new THREE.Vector3().setFromSphericalCoords(
distance,
phi,
theta
)}
rotation={[rotation, 0, 0]}
scale={[scale * scaleX, scale * scaleY, scale]}
target={[0, 0, 0]}
castShadow={false}
receiveShadow={false}
>
<LayerMaterial
alpha={opacity}
transparent
side={THREE.DoubleSide}
toneMapped={false}
>
<LightformerLayers light={light} />
</LayerMaterial>
</Lightformer>
</Float>
);
})}
{lights.map((light) => (
<LightComponent key={light.id} light={light} />
))}
</Environment>
);
}
98 changes: 98 additions & 0 deletions src/components/Env/useSignals.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { useFrame } from "@react-three/fiber";
import React, { useRef } from "react";
import { Easings, type Easing, type Signal } from "../../hooks/useStore";

export function useSignals<R extends React.MutableRefObject<THREE.Mesh | null>>(
ref: R,
signals: Signal[] = []
) {
useFrame(({ clock }) => {
if (!ref.current) {
return;
}

const dt = clock.getElapsedTime();

for (const signal of signals) {
if (signal.animation === "pingpong") {
const t = dt % (signal.duration * 2);

if (t < signal.duration) {
const v = ease(signal.easing, t / signal.duration);
ref.current[signal.property][signal.axis] =
signal.start + (signal.end - signal.start) * v;
} else {
const v = ease(
signal.easing,
(t - signal.duration) / signal.duration
);
ref.current[signal.property][signal.axis] =
signal.end + (signal.start - signal.end) * v;
}
} else if (signal.animation === "loop") {
const t = dt % signal.duration;
const v = ease(signal.easing, t / signal.duration);

ref.current[signal.property][signal.axis] =
signal.start + (signal.end - signal.start) * v;
} else if (signal.animation === "once") {
const t = Math.min(dt, signal.duration);
const v = ease(signal.easing, t / signal.duration);

ref.current[signal.property][signal.axis] =
signal.start + (signal.end - signal.start) * v;
}
}

// Manually update the transform matrix
ref.current.updateMatrix();
});

return ref;
}

function ease(easing: Easing, value: number) {
const [mX1, mY1, mX2, mY2] = Easings[easing];
const epsilon = 1e-6;
const curveX = (t: number) => {
const v = 1 - t;
return 3 * v * v * t * mX1 + 3 * v * t * t * mX2 + t * t * t;
};
const curveY = (t: number) => {
const v = 1 - t;
return 3 * v * v * t * mY1 + 3 * v * t * t * mY2 + t * t * t;
};
const solveCurveX = (x: number) => {
let t2 = x;
let derivative;
let x2;
for (let i = 0; i < 8; i++) {
x2 = curveX(t2) - x;
if (Math.abs(x2) < epsilon) {
return t2;
}
derivative = 3 * t2 * t2 - 6 * t2 * x + 3 * x;
if (Math.abs(derivative) < epsilon) {
break;
}
t2 -= x2 / derivative;
}
let t1 = 1;
let t0 = 0;
t2 = x;
while (t1 > t0) {
x2 = curveX(t2) - x;
if (Math.abs(x2) < epsilon) {
return t2;
}
if (x2 > 0) {
t1 = t2;
} else {
t0 = t2;
}
t2 = (t1 + t0) * 0.5;
}
return t2;
};
return curveY(solveCurveX(value));
}
131 changes: 93 additions & 38 deletions src/components/Outliner/LightListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
} from "@heroicons/react/24/solid";
import * as ContextMenu from "@radix-ui/react-context-menu";
import clsx from "clsx";
import { folder, useControls } from "leva";
import { useStore, Light } from "../../hooks/useStore";
import { LevaInputs, button, folder, useControls } from "leva";
import { useStore, Light, Easings } from "../../hooks/useStore";
import * as THREE from "three";

export function LightListItem({ light }: { light: Light }) {
const {
Expand Down Expand Up @@ -43,6 +44,10 @@ export function LightListItem({ light }: { light: Light }) {
const toggleSoloLightById = useStore((state) => state.toggleSoloLightById);
const isSolo = useStore((state) => state.isSolo);
const textureMaps = useStore((state) => state.textureMaps);
const signals = useStore((state) => state.getSignalsForTarget(id));
const addSignal = useStore((state) => state.addSignal);
const removeSignalById = useStore((state) => state.removeSignalById);
const updateSignal = useStore((state) => state.updateSignal);

useControls(() => {
if (selectedLightId !== id) {
Expand Down Expand Up @@ -224,47 +229,96 @@ export function LightListItem({ light }: { light: Light }) {
}
})(),

[`animate ~${id}`]: {
label: "Animate",
value: light.animate ?? false,
onChange: (v) => updateLight({ id, animate: v }),
},
[`Add Signal ~${id}`]: button(() =>
addSignal({
id: THREE.MathUtils.generateUUID(),
targetId: id,
property: "position",
axis: "x",
animation: "pingpong",
name: "New Signal",
start: 0,
end: 1,
duration: 1,
easing: "linear",
})
),

...(() => {
if (!light.animate) {
if (signals.length === 0) {
return {};
}

return {
[`animationSpeed ~${id}`]: {
label: "Animation Speed",
value: light.animationSpeed ?? 1,
min: 0,
onChange: (v) => updateLight({ id, animationSpeed: v }),
},
[`animationRotationIntensity ~${id}`]: {
label: "Rotation Intensity",
value: light.animationRotationIntensity ?? 1,
min: 0,
onChange: (v) =>
updateLight({ id, animationRotationIntensity: v }),
},
[`animationFloatIntensity ~${id}`]: {
label: "Float Intensity",
value: light.animationFloatIntensity ?? 1,
min: 0,
onChange: (v) =>
updateLight({ id, animationFloatIntensity: v }),
},
[`animationFloatingRange ~${id}`]: {
label: "Floating Range",
value: light.animationFloatingRange ?? [0, 2],
min: 0,
max: 2,
onChange: (v) =>
updateLight({ id, animationFloatingRange: v }),
},
};
const folders: Record<string, any> = {};

for (const signal of signals) {
const {
id: signalId,
property,
axis,
animation,
name,
start,
end,
duration,
easing,
} = signal;

folders[signal.id] = folder({
[`property ~${id} ~${signalId}`]: {
label: "Property",
value: property,
options: ["position", "rotation", "scale"],
onChange: (v) =>
updateSignal({ id: signalId, property: v }),
},

[`animation ~${id} ~${signalId}`]: {
label: "Animation",
value: animation,
options: ["pingpong", "loop", "once"],
onChange: (v) =>
updateSignal({ id: signalId, animation: v }),
},

[`axis ~${id} ~${signalId}`]: {
label: "Axis",
value: axis,
options: ["x", "y", "z"],
onChange: (v) => updateSignal({ id: signalId, axis: v }),
},
[`start ~${id} ~${signalId}`]: {
label: "Start",
value: start,
onChange: (v) => updateSignal({ id: signalId, start: v }),
},
[`end ~${id} ~${signalId}`]: {
label: "End",
value: end,
onChange: (v) => updateSignal({ id: signalId, end: v }),
},
[`duration ~${id} ~${signalId}`]: {
label: "Duration",
value: duration,
min: 0,
onChange: (v) =>
updateSignal({ id: signalId, duration: v }),
},
[`easing ~${id} ~${signalId}`]: {
label: "Easing",
value: easing,
options: Object.keys(Easings),
onChange: (v) => {
updateSignal({ id: signalId, easing: v });
},
},
[`Remove Signal ~${id} ~${signalId}`]: button(() =>
removeSignalById(signalId)
),
});
}

return folders;
})(),
},
{
Expand All @@ -275,6 +329,7 @@ export function LightListItem({ light }: { light: Light }) {
};
}
}, [
signals,
selectedLightId,
id,
name,
Expand Down
Loading