2023-09-25 19:49:53 -07:00
|
|
|
import { useLayoutEffect, useEffect, useRef } from 'react'
|
2024-03-22 16:55:30 +11:00
|
|
|
import { engineCommandManager, kclManager } from 'lib/singletons'
|
2023-09-25 19:49:53 -07:00
|
|
|
import { deferExecution } from 'lib/utils'
|
2024-03-22 09:35:07 -04:00
|
|
|
import { Themes } from 'lib/theme'
|
2024-06-30 19:21:24 -07:00
|
|
|
import { makeDefaultPlanes, modifyGrid } from 'lang/wasm'
|
2024-07-02 17:16:27 +10:00
|
|
|
import { useModelingContext } from './useModelingContext'
|
2024-07-12 10:57:27 +10:00
|
|
|
import { useAppState, useAppStream } from 'AppState'
|
2023-09-14 10:47:55 +10:00
|
|
|
|
|
|
|
export function useSetupEngineManager(
|
|
|
|
streamRef: React.RefObject<HTMLDivElement>,
|
2024-03-22 09:35:07 -04:00
|
|
|
token?: string,
|
2024-04-19 00:58:32 -04:00
|
|
|
settings = {
|
2024-04-25 15:51:33 -04:00
|
|
|
pool: null,
|
2024-04-19 00:58:32 -04:00
|
|
|
theme: Themes.System,
|
|
|
|
highlightEdges: true,
|
2024-04-24 13:59:25 -07:00
|
|
|
enableSSAO: true,
|
2024-07-02 17:16:27 +10:00
|
|
|
modelingSend: (() => {}) as any,
|
|
|
|
modelingContext: {} as any,
|
2024-07-01 15:31:42 -04:00
|
|
|
showScaleGrid: false,
|
2024-04-19 00:58:32 -04:00
|
|
|
} as {
|
2024-04-25 15:51:33 -04:00
|
|
|
pool: string | null
|
2024-04-19 00:58:32 -04:00
|
|
|
theme: Themes
|
|
|
|
highlightEdges: boolean
|
2024-04-24 13:59:25 -07:00
|
|
|
enableSSAO: boolean
|
2024-07-02 17:16:27 +10:00
|
|
|
modelingSend: ReturnType<typeof useModelingContext>['send']
|
|
|
|
modelingContext: ReturnType<typeof useModelingContext>['context']
|
2024-07-01 15:31:42 -04:00
|
|
|
showScaleGrid: boolean
|
2024-04-19 00:58:32 -04:00
|
|
|
}
|
2023-09-14 10:47:55 +10:00
|
|
|
) {
|
2024-07-03 09:22:46 +10:00
|
|
|
const { setAppState } = useAppState()
|
2024-07-12 10:57:27 +10:00
|
|
|
const { setMediaStream } = useAppStream()
|
2023-09-14 10:47:55 +10:00
|
|
|
|
|
|
|
const streamWidth = streamRef?.current?.offsetWidth
|
|
|
|
const streamHeight = streamRef?.current?.offsetHeight
|
|
|
|
|
2023-09-25 19:49:53 -07:00
|
|
|
const hasSetNonZeroDimensions = useRef<boolean>(false)
|
2023-09-14 10:47:55 +10:00
|
|
|
|
2024-04-25 15:51:33 -04:00
|
|
|
if (settings.pool) {
|
|
|
|
// override the pool param (?pool=) to request a specific engine instance
|
|
|
|
// from a particular pool.
|
|
|
|
engineCommandManager.pool = settings.pool
|
|
|
|
}
|
|
|
|
|
2024-06-04 08:32:24 -04:00
|
|
|
const startEngineInstance = () => {
|
2023-09-25 19:49:53 -07:00
|
|
|
// Load the engine command manager once with the initial width and height,
|
|
|
|
// then we do not want to reload it.
|
|
|
|
const { width: quadWidth, height: quadHeight } = getDimensions(
|
|
|
|
streamWidth,
|
|
|
|
streamHeight
|
|
|
|
)
|
2024-07-02 17:16:27 +10:00
|
|
|
if (
|
|
|
|
!hasSetNonZeroDimensions.current &&
|
|
|
|
quadHeight &&
|
|
|
|
quadWidth &&
|
|
|
|
settings.modelingSend
|
|
|
|
) {
|
2023-09-25 19:49:53 -07:00
|
|
|
engineCommandManager.start({
|
2024-07-12 10:57:27 +10:00
|
|
|
setMediaStream: setMediaStream,
|
2024-07-03 09:22:46 +10:00
|
|
|
setIsStreamReady: (isStreamReady) => setAppState({ isStreamReady }),
|
2023-09-25 19:49:53 -07:00
|
|
|
width: quadWidth,
|
|
|
|
height: quadHeight,
|
2024-04-17 20:18:07 -07:00
|
|
|
executeCode: () => {
|
|
|
|
// We only want to execute the code here that we already have set.
|
|
|
|
// Nothing else.
|
2024-07-04 01:40:45 -04:00
|
|
|
kclManager.isFirstRender = true
|
|
|
|
return kclManager.executeCode(true, true).then(() => {
|
|
|
|
kclManager.isFirstRender = false
|
|
|
|
})
|
2023-10-11 13:36:54 +11:00
|
|
|
},
|
2023-09-25 19:49:53 -07:00
|
|
|
token,
|
2024-04-19 00:58:32 -04:00
|
|
|
settings,
|
2024-04-15 17:18:32 -07:00
|
|
|
makeDefaultPlanes: () => {
|
|
|
|
return makeDefaultPlanes(kclManager.engineCommandManager)
|
|
|
|
},
|
2024-06-30 19:21:24 -07:00
|
|
|
modifyGrid: (hidden: boolean) => {
|
|
|
|
return modifyGrid(kclManager.engineCommandManager, hidden)
|
|
|
|
},
|
2023-09-25 19:49:53 -07:00
|
|
|
})
|
2024-07-02 17:16:27 +10:00
|
|
|
settings.modelingSend({
|
|
|
|
type: 'Set context',
|
|
|
|
data: {
|
|
|
|
streamDimensions: {
|
|
|
|
streamWidth: quadWidth,
|
|
|
|
streamHeight: quadHeight,
|
|
|
|
},
|
|
|
|
},
|
2023-09-25 19:49:53 -07:00
|
|
|
})
|
|
|
|
hasSetNonZeroDimensions.current = true
|
|
|
|
}
|
2024-06-04 08:32:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
useLayoutEffect(startEngineInstance, [
|
|
|
|
streamRef?.current?.offsetWidth,
|
|
|
|
streamRef?.current?.offsetHeight,
|
2024-07-02 17:16:27 +10:00
|
|
|
settings.modelingSend,
|
2024-06-04 08:32:24 -04:00
|
|
|
])
|
2023-09-25 19:49:53 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleResize = deferExecution(() => {
|
|
|
|
const { width, height } = getDimensions(
|
|
|
|
streamRef?.current?.offsetWidth,
|
|
|
|
streamRef?.current?.offsetHeight
|
|
|
|
)
|
|
|
|
if (
|
2024-07-02 17:16:27 +10:00
|
|
|
settings.modelingContext.store.streamDimensions.streamWidth !== width ||
|
|
|
|
settings.modelingContext.store.streamDimensions.streamHeight !== height
|
2023-09-25 19:49:53 -07:00
|
|
|
) {
|
|
|
|
engineCommandManager.handleResize({
|
|
|
|
streamWidth: width,
|
|
|
|
streamHeight: height,
|
|
|
|
})
|
2024-07-02 17:16:27 +10:00
|
|
|
settings.modelingSend({
|
|
|
|
type: 'Set context',
|
|
|
|
data: {
|
|
|
|
streamDimensions: {
|
|
|
|
streamWidth: width,
|
|
|
|
streamHeight: height,
|
|
|
|
},
|
|
|
|
},
|
2023-09-25 19:49:53 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}, 500)
|
|
|
|
|
2024-06-04 08:32:24 -04:00
|
|
|
const onOnline = () => {
|
|
|
|
startEngineInstance()
|
|
|
|
}
|
|
|
|
|
|
|
|
const onOffline = () => {
|
|
|
|
engineCommandManager.tearDown()
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('online', onOnline)
|
|
|
|
window.addEventListener('offline', onOffline)
|
2023-09-25 19:49:53 -07:00
|
|
|
window.addEventListener('resize', handleResize)
|
2023-09-14 10:47:55 +10:00
|
|
|
return () => {
|
2024-06-04 08:32:24 -04:00
|
|
|
window.removeEventListener('online', onOnline)
|
|
|
|
window.removeEventListener('offline', onOffline)
|
2023-09-25 19:49:53 -07:00
|
|
|
window.removeEventListener('resize', handleResize)
|
2023-09-14 10:47:55 +10:00
|
|
|
}
|
2023-09-25 19:49:53 -07:00
|
|
|
}, [])
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDimensions(streamWidth?: number, streamHeight?: number) {
|
2023-11-13 19:32:07 -05:00
|
|
|
const maxResolution = 2000
|
2023-09-25 19:49:53 -07:00
|
|
|
const width = streamWidth ? streamWidth : 0
|
|
|
|
const height = streamHeight ? streamHeight : 0
|
2023-11-13 19:32:07 -05:00
|
|
|
const ratio = Math.min(
|
|
|
|
Math.min(maxResolution / width, maxResolution / height),
|
|
|
|
1.0
|
|
|
|
)
|
|
|
|
const quadWidth = Math.round((width * ratio) / 4) * 4
|
|
|
|
const quadHeight = Math.round((height * ratio) / 4) * 4
|
2023-09-25 19:49:53 -07:00
|
|
|
return { width: quadWidth, height: quadHeight }
|
2023-09-14 10:47:55 +10:00
|
|
|
}
|