Jump to content

Cloning mesh removes the original mesh, can't hide the copy


snupas
 Share

Recommended Posts

I'm trying to test out morphtargets, so I load up a scene and try to create a copy of the original mesh to then scale it and morph between the two, but for some reason the original mesh isn't being rendered anymore, plus the copy isn't hidden with setEnabled().

If I remove all of the copy code, the original mesh starts being rendered fine again.

var scene;
var mesh;
var meshCopy;
    if (BABYLON.Engine.isSupported()) {
        var canvas = document.getElementById("renderCanvas");
        var engine = new BABYLON.Engine(canvas, true);

        BABYLON.SceneLoader.Load("assets/2/", "untitled.babylon", engine, function (newScene) {
 
            newScene.executeWhenReady(function () {
                // Attach camera to canvas inputs
                newScene.activeCamera.attachControl(canvas);
                scene = newScene;
                mesh = scene.meshes[0];
                mesh.convertToFlatShadedMesh();
                meshCopy = mesh.clone("1");
                meshCopy.x -=10;
                // meshCopy.parent = mesh;
                meshCopy.setEnabled(false);
                meshCopy.position.y =+ 1;
                meshCopy.scaling = new BABYLON.Vector3(1.1, 1.5, 1.0);
                meshCopy.bakeCurrentTransformIntoVertices();
                var manager = new BABYLON.MorphTargetManager();
                mesh.morphTargetManager = manager;
                //var target = BABYLON.MorphTarget.FromMesh(meshCopy, "instance1", 0.25);
               
                engine.runRenderLoop(function() {
                    newScene.render();
                });
            });
        }, function (progress) {

        });
    }

 

Link to comment
Share on other sites

Ahh firefox properly outputs the error:

Error: WebGL warning: Exceeded 16 live WebGL contexts for this principal, losing the least recently used one.

How am I exceeding 16 live webgl contexts with a single copy?

EDIT: Nevermind, that doesn't relate to this error, I don't get this error in Chrome or Edge.

Link to comment
Share on other sites

Your fireFox error is unrelated.  It just means you hit the refresh button too many times.  Query forum should you care more.

Your real problem is clone.  It does not do what you think like in Java & other languages.  It allows multiple meshes to share the same vertex data.  It is for saving space, yet still allows for different materials on each mesh.  When you scaled it you did it for both.  You could load it twice, if it that is possible.  FYI, doing 2 Loads is highly ill-advised.  Use Append for more than one.  Also do not use the callback option, or it gets really messy when you have 2.

var scene = new BABYLON.Scene(engine);

BABYLON.SceneLoader.Append("assets/2/", "untitled.babylon", scene);
BABYLON.SceneLoader.Append("assets/2/", "untitled.babylon", scene);

scene.executeWhenReady(function () {

    // do your stuff here
    // Once the scene is loaded, register a render loop
    engine.runRenderLoop(function() {
        scene.render();
     });
});

The trick is you will now have 2 meshes with the same name.  There is probably a better way to do this by just copying the positions / normals/ etc, and making a new mesh.  I never needed to look into that, as I use a source code exporter.  I just say 'var mesh1 = new moduleNm.MyMesh(scene);' twice. JSON is a pile.

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...