From 38154da5693b4eb75226f02370e198b6e16c8211 Mon Sep 17 00:00:00 2001 From: Nick Matantsev Date: Wed, 29 Dec 2021 12:24:53 -0500 Subject: [PATCH] Add ability to delay lightmapper start --- src/core/Lightmap.tsx | 16 +++++++- src/stories/delay.stories.tsx | 76 +++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 src/stories/delay.stories.tsx diff --git a/src/core/Lightmap.tsx b/src/core/Lightmap.tsx index 424ca03..d0a559f 100644 --- a/src/core/Lightmap.tsx +++ b/src/core/Lightmap.tsx @@ -94,6 +94,7 @@ const LegacySuspenseFallbackIntercept: React.FC<{ const LightmapMain: React.FC< WorkbenchSettings & { + disabled?: boolean; legacySuspense?: boolean; children: React.ReactElement; } @@ -104,6 +105,11 @@ const LightmapMain: React.FC< const requestWork = useWorkRequest(); + // disabled prop can start out true and become false, but afterwards we ignore it + const enabledRef = useRef(!props.disabled); + enabledRef.current = enabledRef.current || !props.disabled; + const allowStart = enabledRef.current; + // debug reference to workbench for intermediate display const [workbench, setWorkbench] = useState(null); @@ -115,6 +121,11 @@ const LightmapMain: React.FC< const legacySuspenseWaitPromiseRef = useRef | null>(null); const sceneRef = useRef(); useLayoutEffect(() => { + // ignore if nothing to do yet + if (!allowStart) { + return; + } + // await until wrapped scene is loaded, if suspense was triggered const sceneReadyPromise = legacySuspenseWaitPromiseRef.current || Promise.resolve(); @@ -143,7 +154,7 @@ const LightmapMain: React.FC< }); setProgress({ promise, isComplete: false }); - }, [requestWork]); + }, [allowStart, requestWork]); const debugInfo = useMemo( () => @@ -193,8 +204,9 @@ const LightmapMain: React.FC< // set "legacySuspense" to correctly wait for content load in legacy Suspense mode export type LightmapProps = WorkbenchSettings & { - workPerFrame?: number; + disabled?: boolean; legacySuspense?: boolean; + workPerFrame?: number; }; const Lightmap = React.forwardRef< diff --git a/src/stories/delay.stories.tsx b/src/stories/delay.stories.tsx new file mode 100644 index 0000000..872b697 --- /dev/null +++ b/src/stories/delay.stories.tsx @@ -0,0 +1,76 @@ +import React from 'react'; +import { Story, Meta } from '@storybook/react'; +import { Canvas } from '@react-three/fiber'; +import { OrbitControls } from '@react-three/drei'; +import * as THREE from 'three'; + +import Lightmap from '../core/Lightmap'; +import Spinner from './Spinner'; +import { DebugOverlayRenderer, DebugOverlayWidgets } from './DebugOverlayScene'; + +export default { + title: 'Delay via disabled flag', + parameters: { + layout: 'fullscreen', + docs: { + source: { + type: 'code' + } + } + }, + argTypes: { + disabled: { + control: { + type: 'boolean', + displayName: 'disabled' + } + } + }, + decorators: [(story) =>
{story()}
] +} as Meta; + +type StoryWithArgs = Story<{ disabled: boolean }>; + +export const Main: StoryWithArgs = ({ disabled }) => ( + { + gl.toneMapping = THREE.ACESFilmicToneMapping; + gl.toneMappingExposure = 0.9; + + gl.outputEncoding = THREE.sRGBEncoding; + }} + > + + }> + + + + + + + + + + + + + + + + + + + + + +); +Main.args = { + disabled: true +};