Highlighting terrain

I’m trying to display a translucent square over some terrain the mouse is pointing at. I tried several approaches, and ray casting appears to be the fastest approach in terms of performance, but I’m not very adequate at creating mesh shapes. I’ve read through this document on how to go about doing so, but I still think I’m missing something - either that or more likely I don’t fully understand what I’m doing.

The end result should be the pink square imitating the terrain the mouse is pointing at.

Here’s the code and result:

[java]
private Geometry drawFloorCursor()
{
Vector2f click2d = spaceContext.getInputManager().getCursorPosition();
Vector3f click3d = spaceContext.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
Vector3f dir = spaceContext.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();

    Ray ray = new Ray(click3d, dir);

    CollisionResults results = new CollisionResults();
    spaceContext.getWorld().getTerrainNode().collideWith(ray, results);

    if (results.size() < 1)
        return null;

    Vector3f centerPoint = results.getClosestCollision().getContactPoint();

    // get points around the center. left to right, top to bottom.

    Ray ltRay = new Ray(click3d.clone().addLocal(-1, centerPoint.getY() + 150, -1), Vector3f.UNIT_Y.negate());
    Ray mtRay = new Ray(click3d.clone().addLocal(0, centerPoint.getY() + 150, -1), Vector3f.UNIT_Y.negate());
    Ray rtRay = new Ray(click3d.clone().addLocal(1, centerPoint.getY() + 150, -1), Vector3f.UNIT_Y.negate());

    Ray lmRay = new Ray(click3d.clone().addLocal(-1, centerPoint.getY() + 150, 0), Vector3f.UNIT_Y.negate());
    // Ray mmRay = new Ray(click3d.clone().addLocal(0, centerPoint.getY() + 50, -1), Vector3f.UNIT_Y.negate());
    Ray rmRay = new Ray(click3d.clone().addLocal(1, centerPoint.getY() + 150, 0), Vector3f.UNIT_Y.negate());

    Ray lbRay = new Ray(click3d.clone().addLocal(-1, centerPoint.getY() + 150, 1), Vector3f.UNIT_Y.negate());
    Ray mbRay = new Ray(click3d.clone().addLocal(0, centerPoint.getY() + 150, 1), Vector3f.UNIT_Y.negate());
    Ray rbRay = new Ray(click3d.clone().addLocal(1, centerPoint.getY() + 150, 1), Vector3f.UNIT_Y.negate());

    CollisionResults ltResults = new CollisionResults();
    CollisionResults mtResults = new CollisionResults();
    CollisionResults rtResults = new CollisionResults();

    CollisionResults lmResults = new CollisionResults();
    CollisionResults rmResults = new CollisionResults();

    CollisionResults lbResults = new CollisionResults();
    CollisionResults mbResults = new CollisionResults();
    CollisionResults rbResults = new CollisionResults();

    spaceContext.getWorld().getTerrainNode().collideWith(ltRay, ltResults);
    spaceContext.getWorld().getTerrainNode().collideWith(mtRay, mtResults);
    spaceContext.getWorld().getTerrainNode().collideWith(rtRay, rtResults);

    spaceContext.getWorld().getTerrainNode().collideWith(lmRay, lmResults);
    spaceContext.getWorld().getTerrainNode().collideWith(rmRay, rmResults);

    spaceContext.getWorld().getTerrainNode().collideWith(lbRay, lbResults);
    spaceContext.getWorld().getTerrainNode().collideWith(mbRay, mbResults);
    spaceContext.getWorld().getTerrainNode().collideWith(rbRay, rbResults);

    Vector3f lt = ltResults.getClosestCollision().getContactPoint();
    Vector3f mt = mtResults.getClosestCollision().getContactPoint();
    Vector3f rt = rtResults.getClosestCollision().getContactPoint();

    Vector3f lm = lmResults.getClosestCollision().getContactPoint();
    Vector3f rm = rmResults.getClosestCollision().getContactPoint();

    Vector3f lb = lbResults.getClosestCollision().getContactPoint();
    Vector3f mb = mbResults.getClosestCollision().getContactPoint();
    Vector3f rb = rbResults.getClosestCollision().getContactPoint();


    // create mesh
    Vector3f[] vertices =
    {
        new Vector3f(-1, centerPoint.getY() - lb.getY(), 1),
        new Vector3f(0, centerPoint.getY() - mb.getY(), 1),
        new Vector3f(1, centerPoint.getY() - rb.getY(), 1),

        new Vector3f(-1, centerPoint.getY() - lm.getY(), 0),
        new Vector3f(0, 0, 0),
        new Vector3f(1, centerPoint.getY() - rm.getY(), 0),

        new Vector3f(-1, centerPoint.getY() - lt.getY(), -1),
        new Vector3f(0, centerPoint.getY() - mt.getY(), -1),
        new Vector3f(1, centerPoint.getY() - rt.getY(), -1),
    };

    Vector2f[] texCoord =
    {
        new Vector2f(-1, -1),
        new Vector2f(1, -1),
        new Vector2f(-1, 1),
        new Vector2f(1, 1),
    };

    int[] indexes =
    {
        3,0,1,
        1,4,3,

        4,1,2,
        2,5,4,

        6,3,4,
        4,7,6,

        7,4,5,
        5,8,7,
    };

    Mesh mesh = new Mesh();
    mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
    mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
    mesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));
    mesh.updateBound();

    Geometry geom = new Geometry("Floor Cursor", mesh);
    geom.setMaterial(null);

    Material mat = new Material(spaceContext.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setTexture("ColorMap", spaceContext.getAssetManager().loadTexture("Textures/gui/groundcursor.png"));
    mat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
    // mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);

    geom.setMaterial(mat);
    // geom.rotate(-FastMath.HALF_PI, 0, 0);
    // geom.setQueueBucket(RenderQueue.Bucket.Transparent);

    geom.setLocalTranslation(centerPoint);
    return geom;
}

[/java]

I couple of things to look into for the custom mesh:

[java]mesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));[/java]

The number of components you want is probably 3.

Did you intend your tex coords to be negative?