Jump to content

how to update mesh positions correctly ?


Hartha
 Share

Recommended Posts

Hello,

After I update the mesh positions I faced some problems,
1- All actions I registered to the mesh no longer work

2- I can't click on the mesh


scene.onPointerDown = (event, pick) =>{
  console.log(pick.pickedMesh)
}

always returns null.


my code

var canvas = document.querySelector("#renderCanvas");
var engine = new BABYLON.Engine(canvas, true);
var createScene = function () {
   var scene = new BABYLON.Scene(engine);
   scene.clearColor = BABYLON.Color3.Gray();
   var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -20), scene);
   camera.setTarget(BABYLON.Vector3.Zero());
   camera.attachControl(canvas, false);
   var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
   light.intensity = .5;
   return scene;
};
var scene = createScene();

engine.runRenderLoop(function () {
   scene.render();
});


var mesh = new BABYLON.Mesh('mesh', scene);
var vrtxData = new BABYLON.VertexData();
var pos = [0, 0, 0, 5, 0, 0, 5, 0, 5];
var indices = [0, 1, 2];
vrtxData.positions = pos;
vrtxData.indices = indices;
vrtxData.applyToMesh(mesh, true);


var material = new BABYLON.StandardMaterial('m', scene);
mesh.material = material;
material.diffuseColor = BABYLON.Color3.Red();
mesh.actionManager = new BABYLON.ActionManager(scene);
mesh.actionManager.registerAction(
    new BABYLON.SetValueAction(BABYLON.ActionManager.OnPointerOverTrigger, mesh.material, "diffuseColor", BABYLON.Color3.Green())
)

mesh.actionManager.registerAction(
    new BABYLON.SetValueAction(BABYLON.ActionManager.OnPointerOutTrigger, mesh.material, "diffuseColor", BABYLON.Color3.Red())
)

// move mesh to the right
for (var i = 0; i < pos.length; i += 3) {
    pos[i] += 5;
}

mesh.updateVerticesData(BABYLON.VertexBuffer.PositionKind, pos);


scene.onPointerDown = (event, pick) =>{
  console.log(pick.pickedMesh.name)
}

 

Link to comment
Share on other sites

here is how i pick a mesh. maybe try that instead? feel free to change the "change position" bit of course.

scene.onPointerDown = function (evt, pickResult) {
            if (pickResult.hit) {
                console.log(pickResult.pickedPoint.x);
                console.log(pickResult.pickedPoint.y);
                console.log(pickResult.pickedPoint.z);            
                
                var pickInfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh; });
                
                var star = pickInfo.pickedMesh;
                
                //change position
                star.position.x = pickResult.pickedPoint.x +1;    
            }
        };

 

Link to comment
Share on other sites

I think the solution to the picking problem should be to move the mesh by modifying mesh.position instead of changing the vertex data.

(shown in the code georage posted)

I am pretty sure that will fix your picking issues. (Because that's the way I move my meshes and picking works fine.)

mesh.position.x += 1;

 

Link to comment
Share on other sites

Sorry for the late reply, I was sick for a week, and thank you all for helping.

I know that 

mesh.position.x += 1

move the mesh without problems but it doesn't update or change the value of the vertices, later i need to retrieve the vertices and do some calculations on it so i need it to be accurate, I can use mesh.positoin with some workaround but it seems better to use updateVertices.

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