Hugo M

Ground object. Which submesh was clicked?

Recommended Posts

I have this code:


      var tiledGround = new BABYLON.Mesh.CreateTiledGround("Tiled Ground", xmin, zmin, xmax, zmax, subdivisions, precision, scene);

      tiledGround.actionManager = new BABYLON.ActionManager(scene);
      tiledGround.actionManager.registerAction( new BABYLON.ExecuteCodeAction(
        e => {
          // TODO: I need to know which submesh was clicked
      var whiteMaterial = new BABYLON.StandardMaterial("White", scene);
      whiteMaterial.diffuseColor = new BABYLON.Color3(1, 1, 1);
      var blackMaterial = new BABYLON.StandardMaterial("Black", scene);
      blackMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);

      var multimat = new BABYLON.MultiMaterial("multi", scene);

      tiledGround.material = multimat;

      var verticesCount = tiledGround.getTotalVertices();
      var tileIndicesLength = tiledGround.getIndices().length / (subdivisions.w * subdivisions.h);

      tiledGround.subMeshes = [];
      var base = 0;
      for (var row = 0; row < subdivisions.h; row++) {
        for (var col = 0; col < subdivisions.w; col++) {
          tiledGround.subMeshes.push(new BABYLON.SubMesh(row%2 ^ col%2, 0, verticesCount, base, tileIndicesLength, tiledGround));
          base += tileIndicesLength;

So basically I'm not sure how to know which submesh was clicked.

The result for the event gives me some X and Y coordinates but they don't seem related at all with my mesh size.

Ej the tiledGround size is 10x10 (I'm not sure which units are used by Babylon) but the pointer gives me something like x: 800, y: 200. Obviously they are not the same magnitudes.

My point is that I'd be able to calculate to which tile correspond the click if the pointers were related to the actual position on the plane, but they don't seem to.

Share this post

Link to post
Share on other sites

Hi Hugo, welcome to the (old) forum.  Feel free to join us at the new forum:

Post about your issue... over there, if you would.  You don't need to paste your code again.  Here's a testing playground with your code included...

Apparently, no faceID values are available with tiledGround actionManager events.  (like you stated)

I believe you MIGHT need to "calculate" which face/cell was clicked... using some math.

Anyway, c'mon over to the new forum... re-post your issue in the QUESTIONS sub-forum, if you like.  (and include that playground URL listed earlier)

PERHAPS others will comment, here.  Not sure. 

There's a party happening at the new forum and I think almost everyone is over there, having fun with the new forum software.

Come on-over to the party... we'll work-on your issue over there.  Thx!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.