Skip to content

Commit c9af5f3

Browse files
committed
Nodes: Ensure background update waits for loaded images.
1 parent 95f1142 commit c9af5f3

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/renderers/common/nodes/Nodes.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import DataMap from '../DataMap.js';
22
import ChainMap from '../ChainMap.js';
33
import NodeBuilderState from './NodeBuilderState.js';
4-
import { NodeFrame, objectGroup, renderGroup, frameGroup, cubeTexture, texture, rangeFog, densityFog, reference, viewportBottomLeft, normalWorld, pmremTexture, viewportTopLeft } from '../../../nodes/Nodes.js';
4+
import { NodeFrame, objectGroup, renderGroup, frameGroup, cubeTexture, texture, rangeFog, densityFog, reference, viewportBottomLeft, normalWorld, pmremTexture, viewportTopLeft, vec4 } from '../../../nodes/Nodes.js';
55

66
import { EquirectangularReflectionMapping, EquirectangularRefractionMapping } from '../../../constants.js';
77

@@ -20,6 +20,8 @@ class Nodes extends DataMap {
2020
this.callHashCache = new ChainMap();
2121
this.groupsData = new ChainMap();
2222

23+
this._envMapFallback = vec4( 0, 0, 0, 1 );
24+
2325
}
2426

2527
updateGroup( nodeUniformsGroup ) {
@@ -264,13 +266,30 @@ class Nodes extends DataMap {
264266

265267
if ( background ) {
266268

267-
if ( sceneData.background !== background ) {
269+
if ( sceneData.background !== background || sceneData.backgroundReady === false ) {
268270

269271
let backgroundNode = null;
270272

271273
if ( background.isCubeTexture === true || ( background.mapping === EquirectangularReflectionMapping || background.mapping === EquirectangularRefractionMapping ) ) {
272274

273-
backgroundNode = pmremTexture( background, normalWorld );
275+
const image = background.image;
276+
277+
const isCubeMap = background.isCubeTexture === true;
278+
const isEquirectMap = ( background.mapping === EquirectangularReflectionMapping || background.mapping === EquirectangularRefractionMapping );
279+
280+
if ( ( isCubeMap && isCubeMapReady( image ) ) || ( isEquirectMap && isEquirectangularMapReady( image ) ) ) {
281+
282+
backgroundNode = pmremTexture( background, normalWorld );
283+
284+
sceneData.backgroundReady = true;
285+
286+
} else {
287+
288+
backgroundNode = this._envMapFallback; // fallback when texture data are not ready
289+
290+
sceneData.backgroundReady = false;
291+
292+
}
274293

275294
} else if ( background.isTexture === true ) {
276295

@@ -485,4 +504,30 @@ class Nodes extends DataMap {
485504

486505
}
487506

507+
function isCubeMapReady( image ) {
508+
509+
if ( image === null || image === undefined ) return false;
510+
511+
let count = 0;
512+
const length = 6;
513+
514+
for ( let i = 0; i < length; i ++ ) {
515+
516+
if ( image[ i ] !== undefined ) count ++;
517+
518+
}
519+
520+
return count === length;
521+
522+
523+
}
524+
525+
function isEquirectangularMapReady( image ) {
526+
527+
if ( image === null || image === undefined ) return false;
528+
529+
return image.height > 0;
530+
531+
}
532+
488533
export default Nodes;

0 commit comments

Comments
 (0)