createInstance make cloned mesh without color

Hi, I’m trying to use createInstance to clone some characters (they are from mixamo.com)
I use this codes for clone, but all of cloned meshes and original mesh are white, without any colors. also I upload an image from result, right is without clone and left is cloned result.
what is my problem? Thank you.

    for (var count = 0; count < 50; count++) {
        var offsetX = 200 * Math.random() - 100;
        var offsetZ = 200 * Math.random() - 100;
        for (index = 0; index < meshes.length; index++) {
            var instance = meshes[index].createInstance("instance" + count);
            instance.parent = meshes[index].parent;
            instance.position = meshes[index].position.clone();
         //   if (!instance.parent.subMeshes) {
                instance.position.x += offsetX;
                instance.position.z -= offsetZ;
        //    }
        }
    } 

Can you repro in the playground?

Here is how to link to your data: Using External Assets - Babylon.js Documentation

I find new thing about this problem, when I change position of original mesh, its color become true, but when I rotate camera and see other instance too, original mesh lost its color, if i see mesh side by side, they are white, but if i see them lonely in view, all of them have true color.
sorry for my bad English:rofl:.

how I can clone mesh’s color too?

which color :slight_smile: ?
Meshes use material to get their color.

Please do a repro in the PG it will be far easier

Dear Deltakosh, I can’t share all code, it is very big, but i will insert the method i use for cloning. my mesh is a character from Mixamo, how I can clone the color material?

	 BABYLON.SceneLoader.ImportMesh("",root_path,data,this.scene,(meshes, particleSystems, skeletons)=>{
		 
	    for(let i=0; i< meshes.length; i++){
	 	    meshes[i].rotation.x = -Math.PI/2;
		    meshes[i].rotation.y = Math.PI;
		    meshes[i].material.needDepthPrePass = true;//is ture????
		    meshes[i].convertToUnIndexedMesh();        //is ture????
		    meshes[i].actionManager = new BABYLON.ActionManager(scene);
		    meshes[i].actionManager.registerAction(
                new BABYLON.ExecuteCodeAction({
                        trigger: BABYLON.ActionManager.OnPickTrigger,
                    },
                    function () { showAvatarPanel(AVATARS[mesh_name]); }
                )
            );
		 
		    if( (x&&z) || x ==0 || z == 0)
		    	 moveOBJToPositionXZ(meshes,x,z);
		     
	 	    if( babylon_name == 'paladin' || babylon_name == 'vampire' )    
		        meshes[i].scaling = new BABYLON.Vector3(0.2,0.2,0.2);
		    else if(babylon_name == 'eva' || babylon_name == 'maw')	
		    	meshes[i].scaling = new BABYLON.Vector3(0.22,0.22,0.22);
		    else if( babylon_name == 'demon')
		    	meshes[i].scaling = new BABYLON.Vector3(0.18,0.18,0.18);
		    else if(babylon_name == 'knight' || babylon_name == 'akai'  || babylon_name == 'ganfaul' || babylon_name == 'nightshade' )    
		        meshes[i].scaling = new BABYLON.Vector3(0.15,0.15,0.15);
		    else
		        meshes[i].scaling = new BABYLON.Vector3(0.11,0.11,0.11);
		     //["akai","castle_guard","demon","eva","ganfaul","goblin","knight","maw","nightshade","paladin","vampire"];
		    
	 	}
		 
            for (var count = 0; count < 50; count++) {
        		
                var offsetX = 200 * Math.random() - 100;
                var offsetZ = 200 * Math.random() - 100;
                for (index = 0; index < meshes.length; index++) {
                    var instance = meshes[index].createInstance("instance" + count);
                    instance.parent = meshes[index].parent;
                    instance.position = meshes[index].position.clone();
                 //   if (!instance.parent.subMeshes) {
                        instance.position.x += offsetX;
                        instance.position.z -= offsetZ;
                //    }
                }
            } 
		 
		
		 if(setCameraForThis)
			 camera.setTarget(meshes[0]);
		 
		 stopAvatar(mesh_name,AVATARS,scene);
		
		// playAnimation(scene,skeletons[0],3,94,true,1.0);
		// playAnimation(scene,skeletons[0],101,133,true,1.0);
		// playAnimation(scene,skeletons[0],142,156,true,1.0);
		 
		 scene.setRenderingAutoClearDepthStencil(meshes.index, false, false, false);
		 	 
     });

if I understand correctly you want to jsut say clone.material = mesh.material

yes, but not working.

is any problem with my code?

We need a repro. Your code is out of context. At first glance it seems right but I’m not a JS compiler :slight_smile:

2 Likes

my problem become solved, I change my method, I write my answer, hope to be useful for my friends. I use below method: (I use ‘clone’ instead of ‘createInstance’ and I use ‘assetsManager’ too)

    var assetsManager = new BABYLON.AssetsManager(scene);
    var meshTask = assetsManager.addMeshTask(babylon_name, "", root_path, data);
    
    meshTask.onSuccess = function (task) {//onSuccess
        task.loadedMeshes[0].position = BABYLON.Vector3.Zero();
	    let meshes = task.loadedMeshes;//[0];
	    for(let i=0; i< meshes.length; i++){//for f8
		//	console.log('ya ali');
	 	    meshes[i].rotation.x = -Math.PI/2;
		    meshes[i].rotation.y = Math.PI;
		    meshes[i].material.needDepthPrePass = true;//is ture????
		    meshes[i].convertToUnIndexedMesh();        //is ture????
		    meshes[i].actionManager = new BABYLON.ActionManager(scene);
	
		    if( (x&&z) || x ==0 || z == 0)
		    	 moveOBJToPositionXZ(meshes,x,z);
		     
	 	    if( babylon_name == 'paladin' || babylon_name == 'vampire' )    
		        meshes[i].scaling = new BABYLON.Vector3(0.2,0.2,0.2);
		    else if(babylon_name == 'eva' || babylon_name == 'maw')	
		    	meshes[i].scaling = new BABYLON.Vector3(0.22,0.22,0.22);
		    else if( babylon_name == 'demon')
		    	meshes[i].scaling = new BABYLON.Vector3(0.18,0.18,0.18);
		    else if(babylon_name == 'knight' || babylon_name == 'akai'  || babylon_name == 'ganfaul' || babylon_name == 'nightshade' )    
		        meshes[i].scaling = new BABYLON.Vector3(0.15,0.15,0.15);
		    else
		        meshes[i].scaling = new BABYLON.Vector3(0.11,0.11,0.11);
		     //["akai","castle_guard","demon","eva","ganfaul","goblin","knight","maw","nightshade","paladin","vampire"];

		 if(setCameraForThis)
			 camera.setTarget(meshes[0]);
	
	
		 
		 scene.setRenderingAutoClearDepthStencil(meshes.index, false, false, false);
	
	for(j=9; j<10; j++){
	  let t = task.loadedMeshes[0].clone(''+j);
	      t.skeleton = task.loadedMeshes[0].skeleton.clone('skeleton'+j);
	   t.position.z = 10*j;
	   t.position.x = 10*j;
	    playAnimation(scene,t,3,94,true,1.0);
	}
	
		}//end of for f8
	
    }//end of onSuccess	
    
    assetsManager.onFinish = function (tasks) {
    	ENGINE.runRenderLoop(function () {
    		scene.render();
    	});
    };
    
    assetsManager.load();
1 Like

Thanks for the follow up :slight_smile: