Hartha Posted February 1, 2017 Share Posted February 1, 2017 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) } Quote Link to comment Share on other sites More sharing options...
georage Posted February 1, 2017 Share Posted February 1, 2017 are you throwing an error? your console.log line does not end in a semi-colon. Not sure that will help but give it a shot. Quote Link to comment Share on other sites More sharing options...
Dad72 Posted February 1, 2017 Share Posted February 1, 2017 scene.onPointerDown = (event, pick) =>{ This should work better scene.onPointerDown = function(event, pick) { console.log(pick.pickedMesh.name) }; Dieterich 1 Quote Link to comment Share on other sites More sharing options...
georage Posted February 1, 2017 Share Posted February 1, 2017 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; } }; Dieterich 1 Quote Link to comment Share on other sites More sharing options...
Hartha Posted February 1, 2017 Author Share Posted February 1, 2017 @georage pickResult.hit always returns false when i click on the mesh, babylon raycast didn't hit the mesh! @Dad72 nothing changed Quote Link to comment Share on other sites More sharing options...
Dad72 Posted February 1, 2017 Share Posted February 1, 2017 You must also set isPickable to true so that it can be selectable mesh.isPickable = true; Exemple here : http://www.babylonjs-playground.com/#Q7PIC Quote Link to comment Share on other sites More sharing options...
Hartha Posted February 1, 2017 Author Share Posted February 1, 2017 without updating the positions i can pick the mesh and everything is normal, all problems appear after the update I tried to set mesh.isPickable to true after updating but nothing changed Quote Link to comment Share on other sites More sharing options...
jpdev Posted February 1, 2017 Share Posted February 1, 2017 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; georage 1 Quote Link to comment Share on other sites More sharing options...
Dad72 Posted February 1, 2017 Share Posted February 1, 2017 Indeed. JPdev is right. Otherwise, you must update the vertex data. updateMeshPositions(); updateVerticesData(); Or move your object with mesh.position = BABYLON.Vector3 jpdev 1 Quote Link to comment Share on other sites More sharing options...
Hartha Posted February 7, 2017 Author Share Posted February 7, 2017 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. Quote Link to comment Share on other sites More sharing options...
JohnK Posted February 7, 2017 Share Posted February 7, 2017 Just curious as to why you need the vertex positions. Anyway you can use mesh.getWorldMatrix() and then apply the Matrix to the vertices using v = BABYLON.Vector3.TransformCoordinates(vector, matrix) when mesh in correct position. waverider 1 Quote Link to comment Share on other sites More sharing options...
Hartha Posted February 8, 2017 Author Share Posted February 8, 2017 I am working on a simple 3D Editor using babylon. Thank you for helping. 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.