Skip to content
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

opt: only animate tiles in frustum #7

Open
wants to merge 1 commit into
base: manual-frustum-culling
Choose a base branch
from
Open
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
49 changes: 36 additions & 13 deletions src/components/canvas/InstancedTiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const TileInstances = ({
const mainInstancedMeshRef = useRef<THREE.InstancedMesh>(null)
const topInstancedMeshRef = useRef<THREE.InstancedMesh>(null)
const bottomInstancedMeshRef = useRef<THREE.InstancedMesh>(null)
const frustum = useRef<THREE.Frustum>(new THREE.Frustum())
const boundingBox = useMemo(() => {
const bb = new THREE.Box3()
bb.min.set(position[0] - 1, position[1], position[2] - 1)
Expand Down Expand Up @@ -95,26 +96,48 @@ const TileInstances = ({

useEffect(() => {
setTileStates((tileStates) =>
tileStates.map((tileState, index) =>
tileState.flipped !== (tiles[index].address !== '0x0') && tileState.animationState === ANIMATION_STATES.IDLE
? {
...tileState,
flipped: tiles[index].address !== '0x0',
animationState: ANIMATION_STATES.JUMPING,
animationProgress: 0,
}
: tileState,
),
tileStates.map((tileState, index) => {
const newState = { ...tileState }
if (
tileState.flipped !== (tiles[index].address !== '0x0') &&
tileState.animationState === ANIMATION_STATES.IDLE
) {
newState.flipped = tiles[index].address !== '0x0'
newState.animationState = ANIMATION_STATES.JUMPING
newState.animationProgress = 0
}

const tileBB = new THREE.Box3()
tileBB.min.set(
position[0] + (tileState.position.x - 1) * TILE_SIZE * 1.1,
position[1],
position[2] + (tileState.position.z - 1) * TILE_SIZE * 1.1,
)
tileBB.max.set(
position[0] + (tileState.position.x + 1) * TILE_SIZE * 1.1,
position[1] + TILE_SIZE * 0.1,
position[2] + (tileState.position.z + 1) * TILE_SIZE * 1.1,
)

if (!frustum.current!.intersectsBox(tileBB)) {
newState.animationState = ANIMATION_STATES.IDLE
newState.animationProgress = 0
newState.position.y = 0
newState.rotation.x = newState.flipped ? Math.PI : 0
newState.color = newState.flipped ? TILE_SMILEY_SIDE_COLOR : TILE_ROBOT_SIDE_COLOR
}

return newState
}),
)
}, [tiles])

useFrame((state, delta) => {
const frustum = new THREE.Frustum()
frustum.setFromProjectionMatrix(
frustum.current!.setFromProjectionMatrix(
new THREE.Matrix4().multiplyMatrices(state.camera.projectionMatrix, state.camera.matrixWorldInverse),
)

tileGroupRef.current!.visible = frustum.intersectsBox(boundingBox)
tileGroupRef.current!.visible = frustum.current!.intersectsBox(boundingBox)

const jumpHeight = 0.5
const hoverHeight = 0.1
Expand Down