loadParts method

Future<void> loadParts(
  1. MD2LoadData config
)

Implementation

Future<void> loadParts(MD2LoadData config ) async{
	MorphAnimMesh createPart(MD2LoaderData data, Texture skinMap) {
		final materialWireframe = MeshLambertMaterial.fromMap( { 'color': 0xffaa00, 'wireframe': true } );
		final materialTexture = MeshLambertMaterial.fromMap( { 'color': 0xffffff, 'wireframe': false, 'map': skinMap } );

		final mesh = MorphAnimMesh( data.geometry, materialTexture );
		mesh.rotation.y = - math.pi / 2;
      mesh.animations = data.animations;

		mesh.castShadow = true;
		mesh.receiveShadow = true;

		mesh.materialTexture = materialTexture;
		mesh.materialWireframe = materialWireframe;

		return mesh;
	}

	void checkLoadingComplete() {
		loadCounter--;
		if ( loadCounter == 0 ){
        onLoadComplete?.call();
      }
	}

	Future<List<Texture>> loadTextures( baseUrl, textureUrls ) async{
		final textureLoader = TextureLoader();
      textureLoader.flipY = true;
		final List<Texture> textures = [];

		for (int i = 0; i < textureUrls.length; i ++ ) {
        final text = await textureLoader.unknown( baseUrl + textureUrls[ i ]);
        checkLoadingComplete();
        if(text != null){
          textures.add(text);
          textures[i].mapping = UVMapping;
          textures[i].name = textureUrls[ i ];
          textures[i].colorSpace = SRGBColorSpace;
        }
		}

		return textures;
	}

  loadCounter = config.weapons.length * 2 + config.skins.length + 1;

	final weaponsTextures = [];
	for (int i = 0; i < config.weapons.length; i ++ ){
      weaponsTextures.add(config.weapons[ i ][ 1 ]);
    }
	// SKINS

	skinsBody = await loadTextures('${config.path }skins/', config.skins );
	skinsWeapon = await loadTextures('${config.path}skins/', weaponsTextures );

	// BODY

	final loader = MD2Loader();

	await loader.unknown( config.path + config.body).then( ( data ) {
		final boundingBox = BoundingBox();
		boundingBox.setFromBuffer( data!.geometry.attributes['position'] );

		root.position.y = - scale * boundingBox.min.y;

		final mesh = createPart( data, skinsBody[ 0 ] );
		mesh.scale.setValues( scale, scale, scale );

		root.add( mesh );
		meshBody = mesh;

		meshBody?.clipOffset = 0;
		activeAnimationClip = mesh.animations.isNotEmpty?mesh.animations[0]:null;

		mixer = AnimationMixer( mesh );
		checkLoadingComplete();
	});

	// WEAPONS
	for (int i = 0; i < config.weapons.length; i ++ ) {
		await loader.unknown( config.path + config.weapons[i][0]).then((geo){
			final mesh = createPart( geo!, skinsWeapon[ i ] );
			mesh.scale.setValues( scale, scale, scale );
			mesh.visible = false;
			mesh.name = config.weapons[ i ][0];
			root.add( mesh );
			weapons.add(mesh);
			meshWeapon = mesh;
			checkLoadingComplete();
      });
	}
}