@@ -9,13 +9,12 @@ import * as THREE from 'three';
9
9
import { materialIsSupported } from './lightScene' ;
10
10
import {
11
11
traverseSceneItems ,
12
- Workbench ,
13
12
WorkbenchSettings ,
14
13
LIGHTMAP_READONLY_FLAG ,
15
14
LIGHTMAP_IGNORE_FLAG
16
15
} from './workbench' ;
17
16
import { computeAutoUV2Layout } from './AutoUV2' ;
18
- import { runOffscreenWorkflow } from './offscreenWorkflow' ;
17
+ import { useOffscreenWorkflow , Debug } from './offscreenWorkflow' ;
19
18
20
19
// prevent lightmap and UV2 generation for content
21
20
// (but still allow contribution to lightmap, for e.g. emissive objects, large occluders, etc)
@@ -103,93 +102,64 @@ export type LightmapProps = WorkbenchSettings & {
103
102
const Lightmap : React . FC < React . PropsWithChildren < LightmapProps > > = ( {
104
103
disabled,
105
104
onComplete,
106
- ...props
105
+ children,
106
+ ...settings
107
107
} ) => {
108
- const initialPropsRef = useRef ( props ) ;
109
-
110
108
// track latest reference to onComplete callback
111
109
const onCompleteRef = useRef ( onComplete ) ;
112
110
onCompleteRef . current = onComplete ;
113
111
114
- // track one-time flip from disabled to non-disabled
115
- // (i.e. once allowStart is true, keep it true)
116
- const disabledStartRef = useRef ( true ) ;
117
- disabledStartRef . current = disabledStartRef . current && ! ! disabled ;
118
- const allowStart = ! disabledStartRef . current ;
119
-
120
- const [ result , setResult ] = useState < Workbench | null > ( null ) ;
112
+ // debug helper
121
113
const [ debugInfo , setDebugInfo ] = useState < DebugInfo | null > ( null ) ;
114
+ const debug : Debug = {
115
+ onAtlasMap ( atlasMap ) {
116
+ // initialize debug display of atlas texture as well as blank placeholder for output
117
+ const atlasTexture = new THREE . DataTexture (
118
+ atlasMap . data ,
119
+ atlasMap . width ,
120
+ atlasMap . height ,
121
+ THREE . RGBAFormat ,
122
+ THREE . FloatType
123
+ ) ;
122
124
123
- useLayoutEffect ( ( ) => {
124
- // @todo check if this runs multiple times on some React versions???
125
- const { children, ...settings } = initialPropsRef . current ;
126
-
127
- // set up abort signal promise
128
- let abortResolver = ( ) => undefined as void ;
129
- const abortPromise = new Promise < void > ( ( resolve ) => {
130
- abortResolver = resolve ;
131
- } ) ;
132
-
133
- // run main logic with the abort signal promise
134
- if ( allowStart ) {
135
- const workflowResult = runOffscreenWorkflow (
136
- children ,
137
- settings ,
138
- abortPromise ,
139
- {
140
- onAtlasMap ( atlasMap ) {
141
- // initialize debug display of atlas texture as well as blank placeholder for output
142
- const atlasTexture = new THREE . DataTexture (
143
- atlasMap . data ,
144
- atlasMap . width ,
145
- atlasMap . height ,
146
- THREE . RGBAFormat ,
147
- THREE . FloatType
148
- ) ;
125
+ const outputTexture = new THREE . DataTexture (
126
+ new Float32Array ( atlasMap . width * atlasMap . height * 4 ) ,
127
+ atlasMap . width ,
128
+ atlasMap . height ,
129
+ THREE . RGBAFormat ,
130
+ THREE . FloatType
131
+ ) ;
149
132
150
- const outputTexture = new THREE . DataTexture (
151
- new Float32Array ( atlasMap . width * atlasMap . height * 4 ) ,
152
- atlasMap . width ,
153
- atlasMap . height ,
133
+ setDebugInfo ( {
134
+ atlasTexture,
135
+ outputTexture
136
+ } ) ;
137
+ } ,
138
+ onPassComplete ( data , width , height ) {
139
+ setDebugInfo (
140
+ ( prev ) =>
141
+ prev && {
142
+ ...prev ,
143
+
144
+ // replace with a new texture with copied source buffer data
145
+ outputTexture : new THREE . DataTexture (
146
+ new Float32Array ( data ) ,
147
+ width ,
148
+ height ,
154
149
THREE . RGBAFormat ,
155
150
THREE . FloatType
156
- ) ;
157
-
158
- setDebugInfo ( {
159
- atlasTexture,
160
- outputTexture
161
- } ) ;
162
- } ,
163
- onPassComplete ( data , width , height ) {
164
- setDebugInfo (
165
- ( prev ) =>
166
- prev && {
167
- ...prev ,
168
-
169
- // replace with a new texture with copied source buffer data
170
- outputTexture : new THREE . DataTexture (
171
- new Float32Array ( data ) ,
172
- width ,
173
- height ,
174
- THREE . RGBAFormat ,
175
- THREE . FloatType
176
- )
177
- }
178
- ) ;
151
+ )
179
152
}
180
- }
181
153
) ;
182
-
183
- workflowResult . then ( ( result ) => {
184
- setResult ( result ) ;
185
- } ) ;
186
154
}
155
+ } ;
187
156
188
- // on early unmount, resolve the abort signal promise
189
- return ( ) => {
190
- abortResolver ( ) ;
191
- } ;
192
- } , [ allowStart ] ) ;
157
+ // main offscreen workflow state
158
+ const result = useOffscreenWorkflow (
159
+ disabled ? null : children ,
160
+ settings ,
161
+ debug
162
+ ) ;
193
163
194
164
const sceneRef = useRef < THREE . Scene > ( null ) ;
195
165
@@ -201,7 +171,7 @@ const Lightmap: React.FC<React.PropsWithChildren<LightmapProps>> = ({
201
171
// create UV2 coordinates for the final scene meshes
202
172
// @todo somehow reuse ones from the baker?
203
173
computeAutoUV2Layout (
204
- initialPropsRef . current . lightMapSize ,
174
+ [ result . atlasMap . width , result . atlasMap . height ] ,
205
175
traverseSceneItems ( sceneRef . current , true ) ,
206
176
{
207
177
texelsPerUnit : result . texelsPerUnit
@@ -211,11 +181,7 @@ const Lightmap: React.FC<React.PropsWithChildren<LightmapProps>> = ({
211
181
// copy texture data since this is coming from a foreign canvas
212
182
const texture = result . createOutputTexture ( ) ;
213
183
214
- updateFinalSceneMaterials (
215
- sceneRef . current ,
216
- texture ,
217
- ! ! initialPropsRef . current . ao
218
- ) ;
184
+ updateFinalSceneMaterials ( sceneRef . current , texture , result . aoMode ) ;
219
185
220
186
// notify listener and pass the texture instance intended for parent GL context
221
187
if ( onCompleteRef . current ) {
@@ -229,7 +195,7 @@ const Lightmap: React.FC<React.PropsWithChildren<LightmapProps>> = ({
229
195
< DebugContext . Provider value = { debugInfo } >
230
196
{ result ? (
231
197
< scene name = "Lightmap Result Scene" ref = { sceneRef } >
232
- { props . children }
198
+ { children }
233
199
</ scene >
234
200
) : null }
235
201
</ DebugContext . Provider >
0 commit comments