maxime1992 Posted March 15, 2018 Share Posted March 15, 2018 Hi! I'm completely a newbie in 3D world and BabylonJs. What I'd like to do is to be able to select a given mesh by clicking on it and change it's color to show that it has been selected. I've created a really basic scene with 2 meshes and googled my problem but couldn't find anything about that. Maybe it's super trivial (?) Here's my example, thanks for any help or any useful resource to get started on this task https://playground.babylonjs.com/#TC2K69 Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted March 15, 2018 Share Posted March 15, 2018 Hi @maxime1992 Welcome to the forums. Pretty simple task; https://playground.babylonjs.com/#TC2K69#1 You can read more about picking here; http://doc.babylonjs.com/babylon101/picking_collisions Quote Link to comment Share on other sites More sharing options...
maxime1992 Posted March 15, 2018 Author Share Posted March 15, 2018 Hey, thanks for the quick answer (and the demo!) @aWeirdo I'm facing 1 issue though with your demo: It's selecting or loosing the focus on a mesh if I drag and drop in/out of one of them. For example the biggest part is selected, you just want to rotate the camera and drag from/to an empty point, you'll lose the focus. I thought of using scene.onPointerPick But I'm getting scene.onPointerPick is not a function I tried to search into the event but there does not seem to be a starting and ending position so I don't know how to figure out whether it's a drag or not Quote Link to comment Share on other sites More sharing options...
babbleon Posted March 15, 2018 Share Posted March 15, 2018 try this with line #36 commented out.: https://playground.babylonjs.com/#TC2K69#3 Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted March 15, 2018 Share Posted March 15, 2018 Hi again @maxime1992 I added the following code to allow de-selecting if(selected) { selected.material.diffuseColor = BABYLON.Color3.White(); selected = null; } You can move it inside the hit-test to only allow de-selecting when selecting a new mesh, if(evt.pickInfo.hit && evt.pickInfo.pickedMesh && evt.event.button === 0){ if(selected) { selected.material.diffuseColor = BABYLON.Color3.White(); } selected = evt.pickInfo.pickedMesh; evt.pickInfo.pickedMesh.material.diffuseColor = BABYLON.Color3.Green(); } Or you can check if the pointer moves between pointerdown & pointerup (this will however make it difficult/impossible to pick if the pointer moves even slightly) https://playground.babylonjs.com/#TC2K69#4 You can also create pointerCaches and check how much a pointer has moved between pointerDown and pointerUp, Avoids a million pointerMove fires, and lets you set a limit for how much a pointer is allowed to move and still fire the de-/select code https://playground.babylonjs.com/#TC2K69#5 maxime1992 1 Quote Link to comment Share on other sites More sharing options...
maxime1992 Posted March 15, 2018 Author Share Posted March 15, 2018 @babbleon tried but it's not working as expected @aWeirdo thx it's getting closer BUT, if you make the scene rotating with your mouse and while it's rotating/in motion, click on a part it'll not be selected/unselected. Quote Link to comment Share on other sites More sharing options...
maxime1992 Posted March 15, 2018 Author Share Posted March 15, 2018 =x my bad @aWeirdo !!! It's perfect, thanks for your help 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.