Merge AssetContainer into another

/**
 * Merge src AssetContainer into target, and dispose src
 * @param {AssetContainer} src
 * @param {AssetContainer} target
 */
function mergeInto( src, target ) {

	src.removeAllFromScene();
	src.rootNodes.forEach( e => target.rootNodes.push( e ) );
	src.cameras.forEach( e => target.cameras.push( e ) );
	src.lights.forEach( e => target.lights.push( e ) );
	src.meshes.forEach( e => target.meshes.push( e ) );
	src.skeletons.forEach( e => target.skeletons.push( e ) );
	src.particleSystems.forEach( e => target.particleSystems.push( e ) );
	src.animations.forEach( e => target.animations.push( e ) );
	src.animationGroups.forEach( e => target.animationGroups.push( e ) );
	src.multiMaterials.forEach( e => target.multiMaterials.push( e ) );
	src.materials.forEach( e => target.materials.push( e ) );
	src.morphTargetManagers.forEach( e => target.morphTargetManagers.push( e ) );
	src.geometries.forEach( e => target.geometries.push( e ) );
	src.transformNodes.forEach( e => target.transformNodes.push( e ) );
	src.actionManagers.forEach( e => target.actionManagers.push( e ) );
	src.textures.forEach( e => target.textures.push( e ) );
	src.postProcesses.forEach( e => target.postProcesses.push( e ) );

	if ( src.environmentTexture && ! target.environmentTexture ) {

		target.environmentTexture = src.environmentTexture;
		src.environmentTexture = null;

	}

	src.rootNodes.length = 0;
	src.cameras.length = 0;
	src.lights.length = 0;
	src.meshes.length = 0;
	src.skeletons.length = 0;
	src.particleSystems.length = 0;
	src.animations.length = 0;
	src.animationGroups.length = 0;
	src.multiMaterials.length = 0;
	src.materials.length = 0;
	src.morphTargetManagers.length = 0;
	src.geometries.length = 0;
	src.transformNodes.length = 0;
	src.actionManagers.length = 0;
	src.textures.length = 0;
	src.postProcesses.length = 0;
	src.dispose();
	return target;

}
2 Likes

Thanks for sharing! :slight_smile: