Change all material types of object from Standard to Basic

Hey, I want to know how to change all materials of a model from MeshStandardMaterial to MeshBasicMaterial.

After loading the object I call the following function:

function changeMaterial( object ) {

    object.traverse(

        ( element ) => {

            if( element?.material?.type != undefined ) {

                let targetMaterial = new THREE.MeshBasicMaterial();
                targetMaterial = element.material.clone();
                element.material = targetMaterial;

            }

        }

    );

}

The problem is that this function doesn’t change the type of the material.

you can use these lines after the condition
the goal here is to create a new material using the map (texture) of your loader’s Standard material.
I don’t think cloning is working in this case. But I’m interested if a more straightforward answer exist. :thinking:

	const targetMaterial = new THREE.MeshBasicMaterial({ map: object.material.map });
	targetMaterial.map.needsUpdate = true;
	object.material = targetMaterial;

	//optional but useful
	object.material.map.anisotropy = renderer.capabilities.getMaxAnisotropy();

Thank you for your answer. If I do it like you wrote, I get the following error code:

Uncaught (in promise) TypeError: Cannot set properties of null (setting ‘needsUpdate’)

The code I’ve used is:

function changeMaterial( object ) {

    object.traverse(

        ( element ) => {

            if( element?.material?.type != undefined ) {
                let targetMaterial = new THREE.MeshBasicMaterial( {map: element.material.map} );
                targetMaterial.map.needsUpdate = true;
                element.material = targetMaterial;
                targetMaterial.map.needsUpdate = false;
            }
        }
    );
}

To avoid this error I deleted the needsUpdate. The problem about this is that the material gets white color. So you can’t see the model anymore.

Does your loaded object return/contain any material.map?
maybe you have to retrieve different properties from your Standard material.

No. The map properties are null.