-
Notifications
You must be signed in to change notification settings - Fork 12
/
App.tsx
366 lines (306 loc) · 16.6 KB
/
App.tsx
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import React, { useRef, useCallback, useEffect, useState, useMemo } from 'react'
import { EMState, createEM } from "./em"
import { makeDrawSquareInfo, makeDrawEllipseInfo, DrawShape } from "./em/drawing"
import { signalSourceToDescriptor, descriptorToSignalSource, makeMaterialMap } from './em/serialization'
import {
SideBarType, BrushType, CollapsibleContainer, SettingsComponent, ExamplesComponent,
MaterialBrushMenu, SignalBrushMenu, MultiMenu, MultiMenuChild, InfoBox, LoadingIndicator,
ShareBox, ResetButtons, BrushSelectionButtons, MenuSelectionButtons, BrushCursor, MiscButtons,
InteractiveCanvas, FullscreenView
} from './components'
import { clamp, qualityPresets } from './util'
import { getSharedSimulatorMap, shareSimulatorMap } from './share'
const defaultPreset = qualityPresets["Medium"]
const defaultSignalBrushValue = 10
const defaultSignalBrushSize = 1
const defaultSignalFrequency = 3
const defaultPermittivityBrushValue = 5
const defaultPermeabilityBrushValue = 1
const defaultConductivityBrushValue = 0
const defaultMaterialBrushSize = 5
const defaultBrushDrawShape = DrawShape.Square
const initialDt = defaultPreset.dt
const initialCellSize = defaultPreset.cellSize
const initialSimulationSpeed = 1
const initialGridSizeLongest = defaultPreset.gridSizeLongest
const initialResolutionScale = defaultPreset.resolutionScale
const initialWindowSize: [number, number] = [window.innerWidth, window.innerHeight]
const initialCanvasSize: [number, number] = calculateCanvasSize(initialWindowSize, initialResolutionScale)
const initialGridSize: [number, number] = calculateGridSize(initialGridSizeLongest, initialCanvasSize)
const initialReflectiveBoundary = false
function calculateCanvasSize(windowSize: [number, number], resolutionScale: number): [number, number] {
return [Math.round(windowSize[0] * resolutionScale), Math.round(windowSize[1] * resolutionScale)]
}
function calculateGridSize(gridSizeLongest: number, canvasSize: [number, number]): [number, number] {
const canvasAspect = canvasSize[0] / canvasSize[1]
return canvasSize[0] >= canvasSize[1] ?
[gridSizeLongest, Math.ceil(gridSizeLongest / canvasAspect)] :
[Math.ceil(gridSizeLongest * canvasAspect), gridSizeLongest]
}
export default function () {
const urlShareId = window.location.hash ? window.location.hash.substr(1) : null
const [shareId, setShareId] = useState<string | null>(urlShareId)
const drawCanvasRef = useRef<HTMLCanvasElement>(null)
const [canvasSize, setCanvasSize] = useState<[number, number]>(initialCanvasSize)
const [windowSize, setWindowSize] = useState<[number, number]>(initialWindowSize)
const [gridSizeLongest, setGridSizeLongest] = useState(initialGridSizeLongest)
const [dt, setDt] = useState(initialDt)
const [cellSize, setCellSize] = useState(initialCellSize)
const [resolutionScale, setResolutionScale] = useState(initialResolutionScale)
const [simulationSpeed, setSimulationSpeed] = useState(initialSimulationSpeed)
const [reflectiveBoundary, setReflectiveBoundary] = useState(initialReflectiveBoundary)
const gridSize = useMemo<[number, number]>(() => calculateGridSize(gridSizeLongest, canvasSize), [canvasSize, gridSizeLongest])
const [em, setEm] = useState<EMState | null>(null)
// Would use useMemo for gpu here, but useMemo does not seem to work with ref dependencies.
useEffect(() => {
if (drawCanvasRef.current) {
setEm(createEM(drawCanvasRef.current, initialGridSize,
initialCellSize, initialReflectiveBoundary, initialDt))
}
}, [drawCanvasRef])
// Window resize canvas
useEffect(() => {
const adjustCanvasSize = () => {
const wndSize: [number, number] = [window.innerWidth, window.innerHeight]
setCanvasSize(calculateCanvasSize(wndSize, resolutionScale))
setWindowSize(wndSize)
}
adjustCanvasSize()
window.addEventListener("resize", adjustCanvasSize)
return () => window.removeEventListener("resize", adjustCanvasSize)
}, [resolutionScale])
// Load share id
useEffect(() => {
if (em && urlShareId) {
setShowLoading(true)
console.log(`Loading ${urlShareId}`)
getSharedSimulatorMap(urlShareId).then(simulatorMap => {
// Load material
em.loadMaterialFromComponents(
simulatorMap.materialMap.permittivity,
simulatorMap.materialMap.permeability,
simulatorMap.materialMap.conductivity
)
// Load settings
setDt(simulatorMap.simulationSettings.dt)
setGridSizeLongest(Math.max(simulatorMap.simulationSettings.gridSize[0], simulatorMap.simulationSettings.gridSize[1]))
setCellSize(simulatorMap.simulationSettings.cellSize)
// Load sources
em.setSources(simulatorMap.sourceDescriptors.map(desc => descriptorToSignalSource(desc)))
console.log(`Loaded ${urlShareId}`)
}).catch(err => console.error(`Error getting share ${urlShareId}: ${JSON.stringify(err)}`)).finally(() => setShowLoading(false))
}
}, [em, urlShareId])
// Update render sim output size
useEffect(() => {
if (em) {
em.adjustCanvasSize(canvasSize)
em.resetFields()
em.resetMaterials()
}
}, [em, canvasSize])
// Update simulator grid size
useEffect(() => {
if (em) {
em.setGridSize(gridSize)
em.resetFields()
em.resetMaterials()
}
}, [em, gridSize])
// Update simulator cell size
useEffect(() => {
if (em) {
em.setCellSize(cellSize)
}
}, [em, cellSize])
// Update reflective boundary
useEffect(() => {
if (em) {
em.setReflectiveBoundary(reflectiveBoundary)
}
}, [em, reflectiveBoundary])
const [signalBrushSize, setSignalBrushSize] = useState(defaultSignalBrushSize)
const [signalBrushValue, setSignalBrushValue] = useState(defaultSignalBrushValue)
const [activeBrushShape, setActiveBrushDrawShape] = useState<DrawShape>(defaultBrushDrawShape)
const [materialBrushSize, setMaterialBrushSize] = useState(defaultMaterialBrushSize)
const [permittivityBrushValue, setPermittivityBrushValue] = useState(defaultPermittivityBrushValue)
const [permeabilityBrushValue, setPermeabilityBrushValue] = useState(defaultPermeabilityBrushValue)
const [conductivityBrushValue, setConductivityBrushValue] = useState(defaultConductivityBrushValue)
const [signalFrequency, setSignalFrequency] = useState(defaultSignalFrequency)
const [activeBrush, setActiveBrush] = useState(BrushType.Signal)
const [mousePosition, setMousePosition] = useState<[number, number] | null>(null)
const signalStrength = useRef(0)
const mouseDownPos = useRef<[number, number] | null>(null)
const [shareInProgress, setShareInProgress] = useState(false)
const [sideMenuCollapsed, setSideMenuCollapsed] = useState(false)
const [infoVisible, setInfoVisible] = useState(false)
const [showLoading, setShowLoading] = useState(false)
// Snap input across a line
const [snapInput, setSnapInput] = useState(false)
// Convert from window location to the coordinates used
// by the simulator's draw function.
const windowToSimulationPoint = useMemo(() => {
return (windowPoint: [number, number]) => {
const simulationPoint: [number, number] = [
clamp(0, 1, windowPoint[0] / windowSize[0]),
clamp(0, 1, 1 - windowPoint[1] / windowSize[1])
]
return simulationPoint
}
}, [windowSize])
// Simulate one step
const simStep = useCallback(() => {
if (em) {
if (mouseDownPos.current !== null) {
const center: [number, number] = windowToSimulationPoint(mouseDownPos.current)
const brushHalfSize: [number, number] = [
signalBrushSize / gridSize[0] / 2,
signalBrushSize / gridSize[1] / 2
]
const value = -signalBrushValue * 2000 * Math.cos(2 * Math.PI * signalFrequency * em.getTime())
const drawInfo = activeBrushShape === DrawShape.Square ?
makeDrawSquareInfo(center, brushHalfSize, value) :
makeDrawEllipseInfo(center, brushHalfSize, value)
em.injectSignal(drawInfo, dt)
}
em.stepSim(dt)
}
}, [em, dt, signalFrequency, signalBrushValue, signalBrushSize, windowToSimulationPoint, activeBrushShape, gridSize])
// Change simulation speed
useEffect(() => {
if (simulationSpeed > 0) {
const timer = setInterval(simStep, 1000 / simulationSpeed * dt)
return () => clearInterval(timer)
}
return undefined
}, [simStep, dt, simulationSpeed])
// Draw one frame
const drawStep = useCallback(() => {
if (em) {
if (drawCanvasRef.current) {
const cnvSize = calculateCanvasSize([window.innerWidth, window.innerHeight], resolutionScale)
drawCanvasRef.current.width = cnvSize[0]
drawCanvasRef.current.height = cnvSize[1]
}
em?.renderToCanvas(true, true)
}
}, [em, resolutionScale, drawCanvasRef])
// Draw loop
useEffect(() => {
let stop = false
const drawIfNotStopped = () => {
if (!stop) {
drawStep()
requestAnimationFrame(drawIfNotStopped)
}
}
requestAnimationFrame(drawIfNotStopped)
return () => { stop = true }
}, [drawStep])
// Reset materials in the simulator
const resetMaterials = useCallback(() => {
if (em) {
em.setSources([])
em.resetMaterials()
}
}, [em])
// Reset fields in the simulator
const resetFields = useCallback(() => {
if (em) {
em.resetFields()
signalStrength.current = 0
}
}, [em])
const [isInputDown, setIsInputDown] = useState(false)
const activeBrushSize = useMemo(() => (activeBrush === BrushType.Signal ? signalBrushSize : materialBrushSize) * (canvasSize[0] / gridSize[0]), [activeBrush, signalBrushSize, materialBrushSize, canvasSize, gridSize])
const [sideBar, setSideBar] = useState(SideBarType.SignalBrush)
const [shareVisible, setShareVisible] = useState(false)
const hideWhenInputDownStyle = useMemo<React.CSSProperties>(() => isInputDown ? { pointerEvents: "none", opacity: 0.2 } : {}, [isInputDown])
const generateShareUrl = useCallback(() => {
if (em) {
setShareInProgress(true)
const material = em.getMaterial()
if (material) {
shareSimulatorMap({
materialMap: makeMaterialMap(material),
simulationSettings: {
cellSize: cellSize,
dt: dt,
gridSize: gridSize,
simulationSpeed: 1
},
sourceDescriptors: em.getSources().map(source => signalSourceToDescriptor(source))
})
.then(shareId => setShareId(shareId))
.catch(err => console.log("Error uploading share: " + JSON.stringify(err)))
.finally(() => setShareInProgress(false))
}
}
}, [dt, cellSize, gridSize, em])
const shareUrl = useMemo(() => {
return shareId ? `${window.location.origin}${window.location.pathname}#${shareId}` : null
}, [shareId])
// Open side menu when switching the side bar
useEffect(() => {
setSideMenuCollapsed(false)
}, [sideBar])
return <>
<FullscreenView>
<InteractiveCanvas activeBrush={activeBrush} activeBrushShape={activeBrushShape} canvasSize={canvasSize}
conductivityBrushValue={conductivityBrushValue} drawCanvasRef={drawCanvasRef} em={em}
gridSize={gridSize} materialBrushSize={materialBrushSize} mouseDownPos={mouseDownPos}
permeabilityBrushValue={permeabilityBrushValue} permittivityBrushValue={permittivityBrushValue}
setIsInputDown={setIsInputDown} setMousePosition={setMousePosition} snapInput={snapInput}
windowSize={windowSize} windowToSimulationPoint={windowToSimulationPoint} />
<BrushCursor mousePosition={mousePosition} activeBrushSize={activeBrushSize} brushShape={activeBrushShape} />
<MiscButtons extraStyle={hideWhenInputDownStyle} generateShareUrl={generateShareUrl}
infoVisible={infoVisible} setInfoVisible={setInfoVisible}
shareVisible={shareVisible} setShareVisible={setShareVisible} />
<BrushSelectionButtons activeSideBar={sideBar} setActiveSideBar={setSideBar} setActiveBrush={setActiveBrush} extraStyle={hideWhenInputDownStyle} />
<MenuSelectionButtons activeSideBar={sideBar} setActiveSideBar={setSideBar} extraStyle={hideWhenInputDownStyle} />
<ResetButtons resetFields={resetFields} resetMaterials={resetMaterials} extraStyle={hideWhenInputDownStyle} />
<CollapsibleContainer collapsed={sideMenuCollapsed} setCollapsed={setSideMenuCollapsed} title={sideBar.toString()}
style={{ position: "absolute", top: "50%", height: "400px", marginTop: "-200px", right: 0, opacity: 0.9, ...hideWhenInputDownStyle }}>
<MultiMenu activeState={sideBar}>
<MultiMenuChild activateForState={SideBarType.SignalBrush}>
<SignalBrushMenu
signalBrushSize={signalBrushSize} setSignalBrushSize={setSignalBrushSize}
signalBrushValue={signalBrushValue} setSignalBrushValue={setSignalBrushValue}
signalFrequency={signalFrequency} setSignalFrequency={setSignalFrequency}
activeBrushShape={activeBrushShape} setActiveBrushDrawShape={setActiveBrushDrawShape}
snapInput={snapInput} setSnapInput={setSnapInput} />
</MultiMenuChild>
<MultiMenuChild activateForState={SideBarType.MaterialBrush}>
<MaterialBrushMenu
materialBrushSize={materialBrushSize} setMaterialBrushSize={setMaterialBrushSize}
permittivityBrushValue={permittivityBrushValue} setPermittivityBrushValue={setPermittivityBrushValue}
permeabilityBrushValue={permeabilityBrushValue} setPermeabilityBrushValue={setPermeabilityBrushValue}
conductivityBrushValue={conductivityBrushValue} setConductivityBrushValue={setConductivityBrushValue}
activeBrushShape={activeBrushShape} setActiveBrushDrawShape={setActiveBrushDrawShape}
snapInput={snapInput} setSnapInput={setSnapInput} />
</MultiMenuChild>
<MultiMenuChild activateForState={SideBarType.Settings}>
<SettingsComponent
gridSizeLongest={gridSizeLongest} setGridSizeLongest={setGridSizeLongest}
simulationSpeed={simulationSpeed} setSimulationSpeed={setSimulationSpeed}
resolutionScale={resolutionScale} setResolutionScale={setResolutionScale}
cellSize={cellSize} setCellSize={setCellSize}
reflectiveBoundary={reflectiveBoundary} setReflectiveBoundary={setReflectiveBoundary}
dt={dt} setDt={setDt}
qualityPresets={qualityPresets} />
</MultiMenuChild>
<MultiMenuChild activateForState={SideBarType.Examples}>
<ExamplesComponent
em={em} setCellSize={setCellSize} setDt={setDt}
setGridSizeLongest={setGridSizeLongest} setSimulationSpeed={setSimulationSpeed}
gridSize={gridSize} dt={dt}
cellSize={cellSize} simulationSpeed={simulationSpeed} />
</MultiMenuChild>
</MultiMenu>
</CollapsibleContainer>
</FullscreenView>
<ShareBox visible={shareVisible} setVisible={setShareVisible} shareUrl={shareUrl} shareInProgress={shareInProgress} />
<LoadingIndicator visible={(shareVisible && (shareInProgress || !shareUrl)) || showLoading} />
<InfoBox visible={infoVisible} setVisible={setInfoVisible} />
</>
}