Alex10 Posted February 8, 2016 Share Posted February 8, 2016 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; }; Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted February 8, 2016 Share Posted February 8, 2016 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; }; Quote Link to comment Share on other sites More sharing options...
Alex10 Posted February 8, 2016 Author Share Posted February 8, 2016 Ok I understand, but what if I clone the mesh in different parts of code? For example, in the multiplayer game when there is a new player I need to clone already loaded mesh. And it is only the first time cloned. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted February 8, 2016 Share Posted February 8, 2016 Not sure to understand your question, but if you need to clone it elsewhere, just save the source in your code. Quote Link to comment Share on other sites More sharing options...
Alex10 Posted February 8, 2016 Author Share Posted February 8, 2016 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 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted February 8, 2016 Share Posted February 8, 2016 This is more about javascript and less about babylon.js BUT please provide a sample on the PG and I'll help you setting this up Quote Link to comment Share on other sites More sharing options...
Alex10 Posted February 8, 2016 Author Share Posted February 8, 2016 Ok. Spawn function needs the interval to clone the mesh.http://www.babylonjs-playground.com/#8XUNI#2 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted February 9, 2016 Share Posted February 9, 2016 http://www.babylonjs-playground.com/#8XUNI#3 Quote Link to comment Share on other sites More sharing options...
Alex10 Posted February 9, 2016 Author Share Posted February 9, 2016 But there's only one skull loaded. I assumed there should be four skulls? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted February 9, 2016 Share Posted February 9, 2016 Sorry I wanted to do things too quickly http://www.babylonjs-playground.com/#8XUNI#4 Alex10 1 Quote Link to comment Share on other sites More sharing options...
Alex10 Posted February 9, 2016 Author Share Posted February 9, 2016 Thank you, I understand everything I can load only through these callback. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted February 9, 2016 Share Posted February 9, 2016 This is because loading is asynchronous. But once loaded you can start using your code from the callback Alex10 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.