maxime1992

Select a mesh and change it's color?

Recommended Posts

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.