Class Terrain

java.lang.Object
toxi.geom.mesh.Terrain

public class Terrain extends Object
Implementation of a 2D grid based heightfield with basic intersection features and conversion to TriangleMesh. The terrain is always located in the XZ plane with the positive Y axis as up vector.
  • Constructor Details

    • Terrain

      public Terrain(int width, int depth, float scale)
      Constructs a new and initially flat terrain of the given size in the XZ plane, centred around the world origin.
      Parameters:
      width -
      depth -
      scale -
    • Terrain

      public Terrain(int width, int depth, Vec2D scale)
  • Method Details

    • clear

      public Terrain clear()
    • getDepth

      public int getDepth()
      Returns:
      number of grid cells along the Z axis.
    • getElevation

      public float[] getElevation()
    • getHeightAtCell

      public float getHeightAtCell(int x, int z)
      Parameters:
      x -
      z -
      Returns:
      the elevation at grid point
    • getHeightAtPoint

      public float getHeightAtPoint(float x, float z)
      Computes the elevation of the terrain at the given 2D world coordinate (based on current terrain scale).
      Parameters:
      x - scaled world coord x
      z - scaled world coord z
      Returns:
      interpolated elevation
    • getScale

      public Vec2D getScale()
      Returns:
      the scale
    • getWidth

      public int getWidth()
      Returns:
      number of grid cells along the X axis.
    • intersectAtPoint

      public IsectData3D intersectAtPoint(float x, float z)
      Computes the 3D position (with elevation) and normal vector at the given 2D location in the terrain. The position is in scaled world coordinates based on the given terrain scale. The returned data is encapsulated in a IsectData3D instance.
      Parameters:
      x -
      z -
      Returns:
      intersection data parcel
    • setElevation

      public Terrain setElevation(float[] elevation)
      Sets the elevation of all cells to those of the given array values.
      Parameters:
      elevation - array of height values
      Returns:
      itself
    • setHeightAtCell

      public Terrain setHeightAtCell(int x, int z, float h)
      Sets the elevation for a single given grid cell.
      Parameters:
      x -
      z -
      h - new elevation value
      Returns:
      itself
    • setScale

      public void setScale(float scale)
    • setScale

      public void setScale(Vec2D scale)
      Parameters:
      scale - the scale to set
    • toMesh

      public Mesh3D toMesh()
    • toMesh

      public Mesh3D toMesh(float groundLevel)
    • toMesh

      public Mesh3D toMesh(Mesh3D mesh)
      Creates a TriangleMesh instance of the terrain surface or adds its geometry to an existing mesh.
      Parameters:
      mesh -
      Returns:
      mesh instance
    • toMesh

      public Mesh3D toMesh(Mesh3D mesh, float groundLevel)
      Creates a TriangleMesh instance of the terrain and constructs side panels and a bottom plane to form a fully enclosed mesh volume, e.g. suitable for CNC fabrication or 3D printing. The bottom plane will be created at the given ground level (can also be negative) and the sides are extended downward to that level too.
      Parameters:
      mesh - existing mesh or null
      groundLevel -
      Returns:
      mesh
    • toMesh

      public Mesh3D toMesh(Mesh3D mesh, int minX, int minZ, int maxX, int maxZ)
    • toMesh

      public Mesh3D toMesh(Mesh3D mesh, int mix, int miz, int mxx, int mxz, float groundLevel)
    • updateElevation

      public Terrain updateElevation()