Jump to content

[SOLVED] Update imported mesh positions for morphing


pichayuth
 Share

Recommended Posts

Hi,

I have imported mesh from Blender which is basic box.

I want to update mesh vertices positions each time I click the mesh, so I will get a random new shape from the imported mesh. (modify the existing mesh to get new one)

this is the import mesh code 

 var m
    BABYLON.SceneLoader.ImportMesh('', 'mesh/Box/', 'Box.babylon', scene, function (meshes, particleSystems) {
        m = meshes[0];
        m.isVisible = true;
        var temp = m.getVerticesData(BABYLON.VertexBuffer.PositionKind);
        var count = 0;
        for (var i = 0; i < temp.length / 3; i++) {
            var arr = BABYLON.Vector3.FromArray(temp, count);
            count += 3;
            console.log(arr);
        }
    })

Each time I click, I try to change its color first and it worked and call updateMeshPositions. after calling updateMeshPositions, I log out the new positions and all positions is already changed, but the mesh look the same.

var positionFunction = function (positions) {
        for (var i = 0; i < positions.length; i++) {
            if (i % 2 === 0)
                positions *= 2;
            if (i % 3 === 0)
                positions *= 4;
        }
    };

    scene.onPointerDown = function (evt, pick) {
        if (pick.pickedMesh) {
            var material = new BABYLON.StandardMaterial('mat1', scene);
            material.emissiveColor = new BABYLON.Color3(0, 1, 0);
            pick.pickedMesh.material = material;

            m.updateMeshPositions(positionFunction, true);
            temp = m.getVerticesData(BABYLON.VertexBuffer.PositionKind);
            console.log(temp);
        }
    };

I don't understand that although the positions is changed but the mesh look the same.

Thank you, every one in advance

Peace.

Link to comment
Share on other sites

yep, but problem may be that coming from a .babylon that is always set to non-updateable.  For my source code generator for Blender, I only generate update-able geometry source code when there are Blender shapekeys on the mesh.

Perhaps, get the geometry, then do another setVerticesData().  This time setting last arg to true.

Link to comment
Share on other sites

thank you jerome and JCPalmer

On 2/15/2017 at 11:03 PM, JCPalmer said:

Perhaps, get the geometry, then do another setVerticesData().  This time setting last arg to true.

did you mean after loading the mesh in, I crate new Geometry by using the mesh .getVerticesData(BABYLON.VertexBuffer.PositionKind) and set updatable to true, am i understand it right? 

Link to comment
Share on other sites

yep. Checking, .babylon files can set geometry to updateable (FYI, incremental hardcoded as un-updateable).  Not sure what exporters support this other than BJS serializer (Blender does not right now).  Check with your exporter source.

Assuming it does not, doing a new setVerticesData() from the results of a getVerticesData() would seem to be required.

Link to comment
Share on other sites

Thank you JCPalmer

I will take a look at .babylon and maybe exporter, but what I have done now is, I called updateMeshPositions to update the vertices and get those vertices and setVerticesData.
The mesh is changed, but I don't know if this is the best way to update it.


            m.updateMeshPositions(positionFunction, true);
            var temp = m.getVerticesData(BABYLON.VertexBuffer.PositionKind);
            m.setVerticesData(BABYLON.VertexBuffer.PositionKind, temp, true);

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