ThreeJS 里面的异步变同步

819 阅读1分钟

webgl_materials_cubemap_mipmaps

// load mipmaps
const pendings = []; //常见的做法
for ( let level = 0; level <= maxLevel; ++ level ) {
        const urls = [];
        for ( let face = 0; face < 6; ++ face ) {
                urls.push( path + 'cube_m0' + level + '_c0' + face + format );
        }
        const mipmapLevel = level;
        pendings.push( loadCubeTexture( urls ).then( function ( cubeTexture ) {
                mipmaps[ mipmapLevel ] = cubeTexture;
        } ) );
}
await Promise.all( pendings ); //异步变同步

webgl_tonemapping

const rgbeLoader = new RGBELoader()
        .setDataType( THREE.UnsignedByteType )
        .setPath( 'textures/equirectangular/' );
const gltfLoader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
const [ texture, gltf ] = await Promise.all( [
        rgbeLoader.loadAsync( 'venice_sunset_1k.hdr' ),
        gltfLoader.loadAsync( 'DamagedHelmet.gltf' ),
] );