How to apply a given material to an imported mesh?

Hi guys!

Howdy?

I was tinkering with the following “Playground” example trying to improve it by applying a material to an imported mesh. (https://playground.babylonjs.com/#20OAV9#8621)

So… I simplified the original example to this one:

var createScene = function () {
    var scene = new BABYLON.Scene(engine);
    var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 2,  Math.PI / 4, 5, BABYLON.Vector3.Zero(), scene);
    camera.attachControl(canvas, true);
	
	//Light direction is up and left
	var light = new BABYLON.HemisphericLight("hemiLight", new BABYLON.Vector3(-1, 1, 0), scene);
	light.diffuse = new BABYLON.Color3(1, 1, 1);
	
	var mat1 = new BABYLON.StandardMaterial("mat1", scene);
	mat1.diffuseTexture = new BABYLON.Texture("https://i.imgur.com/Wk1cGEq.png", scene);
	mat1.bumpTexture = new BABYLON.Texture("https://i.imgur.com/wGyk6os.png", scene);	
	
	//original image and bump
	var sphere1 = BABYLON.MeshBuilder.CreateSphere("sphere1", {}, scene);
	sphere1.material = mat1;

    BABYLON.SceneLoader.ImportMesh("", "https://raw.githubusercontent.com/chicortiz/glb/main/", 'su.glb', scene, function(newMeshes){});
	
    //var suglb = BABYLON.SceneLoader.ImportMesh ('su.glb', {}, scene)
    //suglb.material			
	    
    return scene;

};

.
.
with just one material to simplify things. The idea is to apply the material of the sphere to the monkey head, but I still don’t know how to do it.
.

.
Do you guys have any ideas? Any hint is appreciated!

Thank you very much for your time,

Greetings,

Ortiz.

Hi !

Here, you were close, the “newMeshes” arg in ImportMesh is what you need

The trick is that the actual monkey to which you want to give the material is a child of a root material.

2 Likes

Hi SvenFrankson! That’s it! Thank you so much. :smiley:

Sharing the final “cleaned” version of the text (playground) bellow:

    var scene = new BABYLON.Scene(engine);
    var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 2,  Math.PI / 4, 5, BABYLON.Vector3.Zero(), scene);
    camera.attachControl(canvas, true);
	
	//Light direction is up and left
	var light = new BABYLON.HemisphericLight("hemiLight", new BABYLON.Vector3(-1, 1, 0), scene);
	light.diffuse = new BABYLON.Color3(1, 1, 1);
	
	var mat1 = new BABYLON.StandardMaterial("mat1", scene);
	mat1.diffuseTexture = new BABYLON.Texture("https://i.imgur.com/Wk1cGEq.png", scene);
	mat1.bumpTexture = new BABYLON.Texture("https://i.imgur.com/wGyk6os.png", scene);	
	
	//original image and bump
	//var sphere1 = BABYLON.MeshBuilder.CreateSphere("sphere1", {}, scene);
	//sphere1.material = mat1;

    BABYLON.SceneLoader.ImportMesh("", "https://raw.githubusercontent.com/chicortiz/glb/main/", 'su.glb', scene, function(newMeshes){
    newMeshes[0].getChildMeshes()[0].material = mat1;
    });
			
    return scene;

};