Jump to content

Ground object. Which submesh was clicked?

Hugo M

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.

Link to comment
Share on other sites

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

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!

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

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.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...