[SOLVED] How to assign material to multiple meshes?

I don’t know how to pass the material from signs array to signMesh.material

function getMat(s){
        mat = s.resource;
        var matName = s.name;
        matName = matName.substring(4,s.name.length);
        signsNames.push(matName);
        return mat;
    }

    signs.forEach(getMat);
    var m = getMat();

    console.log(getMat);

    signsMeshes.forEach(function(signMesh){

        var nodeName = signMesh.node.name;

        nodeName = nodeName.substring(4,nodeName.length);

        console.log(nodeName);

        if(signsNames.includes(nodeName)){
            signMesh.material = m;

            console.log('applied mat');
            console.log('--------------------');

        }
        else{
            console.warn('?');
            console.log('--------------------');
        }



    });

From what I understand, signsMeshes contains strings not objects. Is that correct?

If that’s the case you can’t do this: signMesh.material

Is your code running right now? Better provide a sample project where the code runs and include what you are trying to do with this code.

signsMeshes contains objects
this is array of MeshInstances

Can you share the project please? This will be much easier to understand within the context of the data itself.

1 Like

I can make a sample project with what I want

here is the sample project

https://playcanvas.com/editor/scene/918716

so the footbal should be blue,
bunny red,
crate green

but no idea how to make chest for example blue so will be the example of one material to 2 meshes (the blue material to football and chest)

and apply material from colors array here:
meshInstance.material = mat;

the same goes with the signs in the model
I want to sign mesh has the material according to name, example
mesh name is ‘oomiya’ so it should have material ‘oomiya’
but there are 3 meshes with ‘oomiya’ so they should have only one material
and next mesh name ‘higashi’ so should have material with name ‘higashi’

solved!

key part of code:

signsMeshes.forEach(function(signMesh){
        var nodeName = signMesh.node.name;

        var signName = signsNames.filter(function(signName){
            return nodeName.includes(signName);
        });

        console.log(signName);

        var matchedMat = signs.find(function(sign){

            if(sign.name.includes(signName[0])){
                return sign;
            }

        });

        console.log(matchedMat);

        if(matchedMat){
            signMesh.material = matchedMat.resource;

            console.log('node name', signMesh.node.name);
            console.log('mat name', signMesh.material.name);

            console.log('applied mat');
            console.log('--------------------');
        }

    });
2 Likes