2024-07-12 16:42:23 -04:00
|
|
|
import { useLayoutEffect, useEffect, useRef, useState } 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
|
|
|
|
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-07-12 16:42:23 -04:00
|
|
|
const startEngineInstance = (restart: boolean = false) => {
|
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(
|
2024-07-12 16:42:23 -04:00
|
|
|
streamRef?.current?.offsetWidth ?? 0,
|
|
|
|
streamRef?.current?.offsetHeight ?? 0
|
2023-09-25 19:49:53 -07:00
|
|
|
)
|
2024-07-12 16:42:23 -04:00
|
|
|
if (restart) {
|
|
|
|
kclManager.isFirstRender = false
|
2023-09-25 19:49:53 -07:00
|
|
|
}
|
2024-07-12 16:42:23 -04:00
|
|
|
engineCommandManager.start({
|
|
|
|
restart,
|
|
|
|
setMediaStream: (mediaStream) => setMediaStream(mediaStream),
|
|
|
|
setIsStreamReady: (isStreamReady) => setAppState({ isStreamReady }),
|
|
|
|
width: quadWidth,
|
|
|
|
height: quadHeight,
|
|
|
|
executeCode: () => {
|
|
|
|
// We only want to execute the code here that we already have set.
|
|
|
|
// Nothing else.
|
|
|
|
kclManager.isFirstRender = true
|
|
|
|
return kclManager.executeCode(true, true).then(() => {
|
|
|
|
kclManager.isFirstRender = false
|
|
|
|
})
|
|
|
|
},
|
|
|
|
token,
|
|
|
|
settings,
|
|
|
|
makeDefaultPlanes: () => {
|
|
|
|
return makeDefaultPlanes(kclManager.engineCommandManager)
|
|
|
|
},
|
|
|
|
modifyGrid: (hidden: boolean) => {
|
|
|
|
return modifyGrid(kclManager.engineCommandManager, hidden)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
settings.modelingSend({
|
|
|
|
type: 'Set context',
|
|
|
|
data: {
|
|
|
|
streamDimensions: {
|
|
|
|
streamWidth: quadWidth,
|
|
|
|
streamHeight: quadHeight,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
hasSetNonZeroDimensions.current = true
|
2024-06-04 08:32:24 -04:00
|
|
|
}
|
|
|
|
|
2024-07-12 16:42:23 -04:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
const { width: quadWidth, height: quadHeight } = getDimensions(
|
|
|
|
streamRef?.current?.offsetWidth ?? 0,
|
|
|
|
streamRef?.current?.offsetHeight ?? 0
|
|
|
|
)
|
|
|
|
if (!hasSetNonZeroDimensions.current && quadHeight && quadWidth) {
|
|
|
|
startEngineInstance()
|
|
|
|
}
|
|
|
|
}, [
|
2024-06-04 08:32:24 -04:00
|
|
|
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(
|
2024-07-12 16:42:23 -04:00
|
|
|
streamRef?.current?.offsetWidth ?? 0,
|
|
|
|
streamRef?.current?.offsetHeight ?? 0
|
2023-09-25 19:49:53 -07:00
|
|
|
)
|
|
|
|
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 = () => {
|
2024-07-12 16:42:23 -04:00
|
|
|
startEngineInstance(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
const onVisibilityChange = () => {
|
|
|
|
if (window.document.visibilityState === 'visible') {
|
|
|
|
if (
|
|
|
|
!engineCommandManager.engineConnection?.isReady() &&
|
|
|
|
!engineCommandManager.engineConnection?.isConnecting()
|
|
|
|
) {
|
|
|
|
startEngineInstance()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.document.addEventListener('visibilitychange', onVisibilityChange)
|
|
|
|
|
|
|
|
const onAnyInput = () => {
|
|
|
|
if (
|
|
|
|
!engineCommandManager.engineConnection?.isReady() &&
|
|
|
|
!engineCommandManager.engineConnection?.isConnecting()
|
|
|
|
) {
|
|
|
|
startEngineInstance()
|
|
|
|
}
|
2024-06-04 08:32:24 -04:00
|
|
|
}
|
2024-07-12 16:42:23 -04:00
|
|
|
window.document.addEventListener('keydown', onAnyInput)
|
|
|
|
window.document.addEventListener('mousemove', onAnyInput)
|
|
|
|
window.document.addEventListener('mousedown', onAnyInput)
|
|
|
|
window.document.addEventListener('scroll', onAnyInput)
|
|
|
|
window.document.addEventListener('touchstart', onAnyInput)
|
2024-06-04 08:32:24 -04:00
|
|
|
|
|
|
|
const onOffline = () => {
|
2024-07-12 16:42:23 -04:00
|
|
|
kclManager.isFirstRender = true
|
2024-06-04 08:32:24 -04:00
|
|
|
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-07-12 16:42:23 -04:00
|
|
|
window.document.removeEventListener(
|
|
|
|
'visibilitychange',
|
|
|
|
onVisibilityChange
|
|
|
|
)
|
|
|
|
window.document.removeEventListener('keydown', onAnyInput)
|
|
|
|
window.document.removeEventListener('mousemove', onAnyInput)
|
|
|
|
window.document.removeEventListener('mousedown', onAnyInput)
|
|
|
|
window.document.removeEventListener('scroll', onAnyInput)
|
|
|
|
window.document.removeEventListener('touchstart', onAnyInput)
|
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
|
|
|
}
|
2024-07-12 16:42:23 -04:00
|
|
|
|
|
|
|
// Engine relies on many settings so we should rebind events when it changes
|
|
|
|
// We have to list out the ones we care about because the settings object holds
|
|
|
|
// non-settings too...
|
|
|
|
}, [
|
|
|
|
settings.enableSSAO,
|
|
|
|
settings.highlightEdges,
|
|
|
|
settings.showScaleGrid,
|
|
|
|
settings.theme,
|
|
|
|
settings.pool,
|
|
|
|
])
|
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
|
|
|
}
|