Pick with support for custom types?

Hi,

is it possible to do something like
class MyMeshType extends Mesh { .. }
and when you do const pickResult = Scene.pick.. and then do something like
if (pickResult.pickedMesh instanceof MyMeshType) {...}?

My current attempt is very much like the above, just with a testMessage: string in MyMeshType.

Currently I’m putting all the ‘extra data’ in the metadata-property, but on some types I want to be able to add custom code when the position changes, something like

class MyTestMesh extends Mesh {
        get position(): Vector3
    {
        console.log('get position');
        return super.position;
    }
    set position(newPosition: Vector3)
    {
        console.log('set position');
        super.position = newPosition;
    }
}

https://doc.babylonjs.com/babylon101/raycasts#predicate

I know about the predicate, currently using it. Feeding it with a instanceof doesn’t seem to work :confused:

Guessing this is something with how Babylon adds the nodes to its ‘tree’?

Playground testing

Wait, here you are using a TS cast so the property customName is not available. You are just telling the type system that you know it is a TestMesh, which is not the case cause in the function createSphere we are creating a Mesh not a TestMesh.

In your case you might be able to rely on Behaviors which will fit nicely with your intent: Behaviors - Babylon.js Documentation

This seems more like exactly what I need.

But my main problem is to detect position changes on a mesh; soo onAfterWorldMatrixUpdateObservable seems like a good candidate?

Sounds perfect :slight_smile:

Got a somewhat OK prototype working with the position update now, thanks for the input.

Any tips on where to look for creating custom meshes? I’m mostly drawing lines, boxes and spheres in a “2D view” using orthographic camera. Had some troubles with sprites as they looked really bad when zoomed in or out - maybe I should have used some kind of LOD?

This might help a lot Create Parametric Shapes - Babylon.js Documentation

About the sprite this is rather weird, could you repro in the playground ???