Jump to content

Cloning multiple meshes


Alex10
 Share

Recommended Posts

I try to clone the same mesh, but for some reason, the mesh is cloned once.

How to clone the same mesh many?

var loader = new BABYLON.AssetsManager(scene);
dom1mesh = loader.addMeshTask('dom1', "", "/static/game/dom1/", "dom2.babylon");
mesh = new BABYLON.Mesh.CreateSphere(name, 8, 2, scene);
mesh.position = new BABYLON.Vector3( 5, 1, 5);
dom1mesh.onSuccess = function (task) {
    var nm = task.loadedMeshes[0];
    nm.position = new BABYLON.Vector3(10, 1, 10);
    nm.parent = mesh;
 };

mesh1 = BABYLON.Mesh.CreateSphere(name, 8, 2, scene);
mesh1.position = new BABYLON.Vector3( 15, 1, 15);
dom1mesh.onSuccess = function (task) {
    var nm = task.loadedMeshes[0].clone('2');
    nm.parent = mesh1;
};

mesh2 = BABYLON.Mesh.CreateSphere(name, 8, 2, scene);
mesh2.position = new BABYLON.Vector3( 25, 1, 25);
dom1mesh.onSuccess = function (task) {
    var nm = task.loadedMeshes[0].clone('3');
    nm.parent = mesh2;
};

mesh3 = BABYLON.Mesh.CreateSphere(name, 8, 2, scene);
mesh3.position = new BABYLON.Vector3( 35, 1, 35);
dom1mesh.onSuccess = function (task) {
    var nm = task.loadedMeshes[0].clone('4');
    nm.parent = mesh3;
};

 

cloning.png

Link to comment
Share on other sites

Hello and welcome!

this is because dom1mesh.onSucess is a callback. You override it every time you set it.

var loader = new BABYLON.AssetsManager(scene);
dom1mesh = loader.addMeshTask('dom1', "", "/static/game/dom1/", "dom2.babylon");
mesh = new BABYLON.Mesh.CreateSphere(name, 8, 2, scene);
mesh.position = new BABYLON.Vector3( 5, 1, 5);
dom1mesh.onSuccess = function (task) {
    var nm = task.loadedMeshes[0];
    nm.position = new BABYLON.Vector3(10, 1, 10);
    nm.parent = mesh;


    mesh1 = BABYLON.Mesh.CreateSphere(name, 8, 2, scene);
    mesh1.position = new BABYLON.Vector3( 15, 1, 15);

    nm = task.loadedMeshes[0].clone('2');
    nm.parent = mesh1;

    mesh2 = BABYLON.Mesh.CreateSphere(name, 8, 2, scene);
    mesh2.position = new BABYLON.Vector3( 25, 1, 25);
    nm = task.loadedMeshes[0].clone('3');
    nm.parent = mesh2;

    mesh3 = BABYLON.Mesh.CreateSphere(name, 8, 2, scene);
    mesh3.position = new BABYLON.Vector3( 35, 1, 35);
     nm = task.loadedMeshes[0].clone('4');
    nm.parent = mesh3;
};

 

Link to comment
Share on other sites

How can I clone without callback? Because I don't know how many go players. So I need dynamic cloning.
I have a game used to synchronize websockets.

scene = new BABYLON.Scene(engine);
loader =  new BABYLON.AssetsManager(scene);
var createScene = function () {
    .  .  .
    enemy = loader.addMeshTask('enemy1', "", "/static/game/", "name.babylon");
    .  .  .
    ws = new WebSocket(wsUri);
    ws.onmessage = function(event){
        var msg = JSON.parse(event.data);
        if(msg.e == 'new'){
            var newPlayer = new Player( msg.x, msg.y );
            newPlayer.init(msg.id);
        }else if(msg.e == 'move' ){
    .  .  .
    loader.load();
    return scene;
}

And every time the server receives a message via the web sockets that a new player:
 

function Player( startX, startY, is_local ) {
    .  .  .
    this.init = function (name, bot) {
        mesh = BABYLON.Mesh.CreateSphere(name, 16, 8, scene);
        mesh.material = new BABYLON.StandardMaterial('texture1', scene);
        mesh.material.diffuseColor = new BABYLON.Color3(0.7, 0.4, 0.5);
        mesh.material.alpha = 0.5;
        mesh.position = new BABYLON.Vector3(50, -3, -10);
        enemy.onSuccess = function (task) {
            var nm = task.loadedMeshes[0].clone(name);
            nm.parent = mesh;
        };
    };
    .  .  .
}

But cloning is triggered only the first time, in all other cases there is only a sphere, without a model.

Yj

cloned1.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...