-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds boxes visual * Updates * Updates for wip anims
- Loading branch information
Showing
21 changed files
with
327 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,35 @@ | ||
import { useState } from "react"; | ||
import { useVisualContext, useVisualContextSetters } from "@/context/visual"; | ||
import { useEnergyInfo } from "@/lib/appState"; | ||
import { useVisualContext } from "@/context/visual"; | ||
import { ScalarMovingAvgEventDetector } from "@/lib/analyzers/eventDetector"; | ||
import { useAppStateActions, useEnergyInfo } from "@/lib/appState"; | ||
import { type IScalarTracker } from "@/lib/mappers/valueTracker/common"; | ||
import { EnergyTracker } from "@/lib/mappers/valueTracker/energyTracker"; | ||
import { AVAILABLE_COLOR_PALETTES } from "@/lib/palettes"; | ||
import { useFrame } from "@react-three/fiber"; | ||
|
||
const PaletteUpdater = ({ | ||
scalarTracker, | ||
}: { | ||
scalarTracker: IScalarTracker; | ||
}) => { | ||
const threshold = 0.5; | ||
const frameSpan = 10; | ||
const { setPalette } = useVisualContextSetters(); | ||
const [movingAvg, setMovingAvg] = useState(0); | ||
const { paletteTrackEnergy: enabled } = useVisualContext(); | ||
const detector = new ScalarMovingAvgEventDetector(0.5, 50, 500); | ||
const { nextPalette } = useAppStateActions(); | ||
|
||
useFrame(() => { | ||
const curr = scalarTracker.getNormalizedValue(); | ||
if (movingAvg < threshold && curr > threshold) { | ||
setPalette((prev) => { | ||
const currIdx = AVAILABLE_COLOR_PALETTES.indexOf(prev) ?? 0; | ||
const nextIdx = (currIdx + 1) % AVAILABLE_COLOR_PALETTES.length; | ||
return AVAILABLE_COLOR_PALETTES[nextIdx]; | ||
}); | ||
setMovingAvg(1); | ||
} else { | ||
setMovingAvg((prev) => { | ||
return (prev * (frameSpan - 1) + curr) / frameSpan; | ||
}); | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
if (detector.step(scalarTracker.getNormalizedValue())) { | ||
nextPalette(); | ||
} | ||
}); | ||
|
||
return <></>; | ||
}; | ||
|
||
const PaletteTracker = () => { | ||
export const PaletteTracker = () => { | ||
const energyInfo = useEnergyInfo(); | ||
const scalarTracker = new EnergyTracker(energyInfo); | ||
|
||
return <PaletteUpdater scalarTracker={scalarTracker} />; | ||
}; | ||
|
||
export const MaybePaletteTracker = () => { | ||
const { paletteTrackEnergy } = useVisualContext(); | ||
if (!paletteTrackEnergy) { | ||
return null; | ||
} | ||
return <PaletteTracker />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { useEffect, useMemo, useRef } from "react"; | ||
import { ScalarMovingAvgEventDetector } from "@/lib/analyzers/eventDetector"; | ||
import { usePalette } from "@/lib/appState"; | ||
import { type IScalarTracker } from "@/lib/mappers/valueTracker/common"; | ||
import { ColorPalette } from "@/lib/palettes"; | ||
import { useFrame } from "@react-three/fiber"; | ||
import { | ||
BoxGeometry, | ||
Matrix4, | ||
MeshBasicMaterial, | ||
type InstancedMesh, | ||
} from "three"; | ||
|
||
const BaseBoxes = ({ | ||
scalarTracker, | ||
nBoxes = 5, | ||
gridSize = 10, | ||
cellSize = 0.25, | ||
}: { | ||
scalarTracker: IScalarTracker; | ||
nBoxes?: number; | ||
gridSize?: number; | ||
cellSize?: number; | ||
}) => { | ||
const rotateDurationMs = 250; | ||
const nRows = gridSize; | ||
const nCols = gridSize; | ||
const detector = useMemo( | ||
() => new ScalarMovingAvgEventDetector(0.65, 150, 2 * rotateDurationMs), | ||
[rotateDurationMs], | ||
); | ||
const meshRef = useRef<InstancedMesh>(null!); | ||
const tmpMatrix = useMemo(() => new Matrix4(), []); | ||
const palette = usePalette(); | ||
const lut = ColorPalette.getPalette(palette).buildLut(); | ||
|
||
const cellAssignments = useMemo( | ||
() => | ||
Array.from({ length: nBoxes }, (_) => { | ||
const row = Math.floor(nRows * Math.random()); | ||
const col = Math.floor(nCols * Math.random()); | ||
return { | ||
fromRow: row, | ||
fromCol: col, | ||
toRow: row, | ||
toCol: col, | ||
}; | ||
}), | ||
[nBoxes, nRows, nCols], | ||
); | ||
|
||
// Recolor; | ||
useEffect(() => { | ||
for (let instanceIdx = 0; instanceIdx < nBoxes; instanceIdx++) { | ||
meshRef.current.setColorAt( | ||
instanceIdx, | ||
lut.getColor(instanceIdx / (nBoxes - 1)), | ||
); | ||
} | ||
meshRef.current.instanceColor!.needsUpdate = true; | ||
}, [lut, nBoxes]); | ||
|
||
useFrame(() => { | ||
if (detector.step(scalarTracker?.getNormalizedValue() ?? 0)) { | ||
// random jitter | ||
const rowJitter = Math.floor(Math.random() * 3) - 1; | ||
const colJitter = Math.floor(Math.random() * 3) - 1; | ||
for (let i = 0; i < nBoxes; i++) { | ||
cellAssignments[i].fromRow = cellAssignments[i].toRow; | ||
cellAssignments[i].fromCol = cellAssignments[i].toCol; | ||
cellAssignments[i].toRow += (Math.random() > 0.5 ? 1 : -1) * colJitter; | ||
cellAssignments[i].toCol += (Math.random() > 0.5 ? 1 : -1) * rowJitter; | ||
} | ||
} | ||
|
||
const alpha = Math.min( | ||
1, | ||
Math.max(0, detector.timeSinceLastEventMs / rotateDurationMs), | ||
); | ||
|
||
let normCubeX, normCubeY, x, y, z; | ||
cellAssignments.forEach( | ||
({ fromRow, fromCol, toRow, toCol }, instanceIdx) => { | ||
const row = fromRow + alpha * (toRow - fromRow); | ||
const col = fromCol + alpha * (toCol - fromCol); | ||
|
||
// Find a random cell | ||
normCubeX = row / (nRows - 1); | ||
normCubeY = col / (nCols - 1); | ||
|
||
x = nRows * cellSize * (normCubeX - 0.5); | ||
y = nCols * cellSize * (normCubeY - 0.5); | ||
z = 0; | ||
// Position | ||
tmpMatrix.setPosition(x, y, z); | ||
|
||
meshRef.current.setMatrixAt(instanceIdx, tmpMatrix); | ||
}, | ||
); | ||
|
||
// Update the instance | ||
meshRef.current.instanceMatrix.needsUpdate = true; | ||
}); | ||
|
||
return ( | ||
<instancedMesh | ||
ref={meshRef} | ||
castShadow={true} | ||
receiveShadow={true} | ||
args={[new BoxGeometry(), new MeshBasicMaterial(), nBoxes]} | ||
> | ||
<boxGeometry attach="geometry" args={[cellSize, cellSize, cellSize, 1]} /> | ||
<meshBasicMaterial attach="material" color={"white"} toneMapped={false} /> | ||
</instancedMesh> | ||
); | ||
}; | ||
|
||
export default BaseBoxes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { type VisualProps } from "@/components/visualizers/common"; | ||
import Ground from "@/components/visualizers/ground"; | ||
import { Vector3 } from "three"; | ||
|
||
import BaseBoxes from "./base"; | ||
|
||
const BoxesVisual = ({ scalarTracker }: VisualProps) => { | ||
const nBoxes = 100; | ||
const gridSize = 100; | ||
const cellSize = 0.25; | ||
|
||
return ( | ||
<> | ||
<BaseBoxes | ||
scalarTracker={ | ||
scalarTracker ?? { | ||
getNormalizedValue: () => Math.sin(0.0025 * Date.now()) + 1, | ||
} | ||
} | ||
nBoxes={nBoxes} | ||
gridSize={gridSize} | ||
cellSize={cellSize} | ||
/> | ||
<Ground position={new Vector3(0, 0, -cellSize / 2)} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default BoxesVisual; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.