Jump to content

Instances and clone mesh


Recommended Posts




ive got a question, because i need to have 400 imported meshes in my scene, im using createInstance at the moment, but first problem came with changing color of clicked element, with instances when i click 1 mesh and change its color, it changes on all meshes i know that i cant change material for each created instance, so ive tried with clone, but its to slow for more elements, so i was trying to make a workaround. First ive created 400 instances, than when someone click on 1 mesh i was trying to dispose this mesh, make a clone of a template make some things that i needed and after someone click on another mesh i disposed the clone and created new instance from template, in theory it should work but in practice it froze the web browser ;]


Here is some of mine code

var rackTemplate;BABYLON.SceneLoader.ImportMesh("rack_hp", "./", "rack_hp.babylon", scene, function (newMeshes) {                newMeshes[0].scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);                rackTemplate = newMeshes[0].clone("rackTemplate");                newMeshes[0].dispose();                rackTemplate.id = "rackTemplate";                rackTemplate.isVisible = false;                rackTemplate.material = new BABYLON.StandardMaterial("rackMaterial", scene);                rackTemplate.material.diffuseColor = new BABYLON.Color3(0.30, 0.30, 0.30);                for (var index = 0; index < 600; index++) {                    var newInstance = rackTemplate.createInstance("rack" + (index));                    newInstance.id = "rack" + (index + 1);                    newInstance.position = new BABYLON.Vector3(index * 7, 9, 0);                }            });//EVENTSvar startingPoint;var currentMesh;var prevObjectMaterialDiffuseColor;var onPointerDown = function (evt) {                if (evt.button !== 0) {                    return;                }                // check if we are under a mesh                var pickInfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh !== ground; });                if (pickInfo.hit) {                    if (currentMesh != null && currentMesh != undefined) {                        var newInstance = rackTemplate.createInstance(currentMesh.id);                        newInstance.id = currentMesh.id;                        newInstance.position = currentMesh.position;                        newInstance.isVisible = true;                        currentMesh.dispose();                    }                    var objectId = pickInfo.pickedMesh.id;                    var objectPosition = pickInfo.pickedMesh.position;                    pickInfo.pickedMesh.dispose();                                        var rackClone = rackTemplate.clone(objectId);                    rackClone.id = objectId;                    rackClone.position = objectPosition;                    rackClone.isVisible = true;                    rackClone.material = new BABYLON.StandardMaterial("objectMovingMaterial", scene);                    rackClone.material.diffuseColor = new BABYLON.Color3(0.10, 0.80, 0.10);                    currentMesh = rackClone;                                        startingPoint = getGroundPosition(evt);                                        if (startingPoint) {                        camera.detachControl(canvas);                    }                }            }

P.S. ArcRotateCamera is not working for me with babylon 2.1 beta but its fine on 2.0

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.

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.


  • Recently Browsing   0 members

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