-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.js
94 lines (84 loc) · 3.3 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React, { useEffect, useState } from 'react';
import { Canvas, useThree } from '@react-three/fiber';
import useAppStore from './useAppStore'
import useSceneManager from './useSceneManager';
import SceneSelector from './SceneSelector';
import { EntityStoreDemo } from './animationComponents';
/**
* Handles camera adjustments on window resize for react-three-fiber.
*/
function CameraAdjuster({ isOrthographic }) {
const { camera, gl } = useThree();
useEffect(() => {
const handleResize = () => {
if (isOrthographic) {
camera.left = window.innerWidth / -2;
camera.right = window.innerWidth / 2;
camera.top = window.innerHeight / 2;
camera.bottom = window.innerHeight / -2;
} else {
camera.aspect = window.innerWidth / window.innerHeight;
}
camera.updateProjectionMatrix();
gl.setSize(window.innerWidth, window.innerHeight);
};
handleResize();
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, [camera, gl, isOrthographic]);
return null; // Component only handles side effects
}
export default function App() {
// This is a bit convoluted. One issue is that the button needs to be outside of the <Canvas>
const { sceneComponent, isOrthographic, key } = useSceneManager();
const pausePhysics = useAppStore((state) => state.pausePhysics);
const setPausePhysics = useAppStore((state) => state.setPausePhysics);
const setOption = useAppStore((state) => state.setOption);
const fixParticles = useAppStore((state) => state.getOption("fixParticles"));
const physicsDebug = useAppStore((state) => state.getOption("physicsDebug"));
const showParticles = useAppStore((state) => state.getOption("showParticles"));
const hideBlobs = useAppStore((state) => state.getOption("hideBlobs"));
const toggleAnimation = () => {
setPausePhysics(!pausePhysics); // Toggle animation state
};
const toggleOption = (option, value) => {
setOption(option, !value);
};
function handleSpeakClick(text) {
const utterance = new SpeechSynthesisUtterance("Enabled");
utterance.pitch = 1;
utterance.rate = 1;
window.speechSynthesis.speak(utterance);
}
return (
<>
{ false && (
//Added here as a hack for interactive testing
<EntityStoreDemo />
)}
<SceneSelector />
<button onClick={toggleAnimation}>
{pausePhysics ? 'Play physics' : 'Pause physics'}
</button>
<button onClick={() => {toggleOption("fixParticles", fixParticles)}}>
{fixParticles ? 'Dynamic particles' : 'Fix particles'}
</button>
<button onClick={() => {toggleOption("physicsDebug", physicsDebug)}}>
{physicsDebug ? 'Physics debug off' : 'Physics debug on'}
</button>
<button onClick={() => {toggleOption("showParticles", showParticles)}}>
{showParticles ? 'Hide Particles' : 'Show Particles'}
</button>
<button onClick={() => {toggleOption("hideBlobs", hideBlobs)}}>
{hideBlobs ? 'Show Blobs' : 'Hide Blobs'}
</button>
<button onClick={handleSpeakClick}>
Enable Narration
</button>
<Canvas key={key} orthographic={isOrthographic} >
{sceneComponent}
<CameraAdjuster isOrthographic={isOrthographic} />
</Canvas>
</>
);
}