Shapes3d library terrain

Has anyone used the terrain class in V3.0 of shapes3d? It is substantially different from what was done in V2 and I haven’t seen any V3.0 examples.

Mark

1 Like

Here it is explained how to find examples

See Help with Shapes3D

I’ve seen the examples previously but there are no examples as far as I can see related to my question about terrain.

Mark

1 Like

Shapes3D V3 was a huge task and towards the end I needed a break from it so some aspects still need some work they include

  • Camera and TerrainCam needs finishing (implement slerping velocity towards heading)
  • Examples using Terrain, SkyBox etc.

Now it might be a while before I get round to doing this properly but creating terrain and skyboxes are perfectly possible with Shapes3D V3 as shown in this video. It was created from the sketch code below and will need the G4P library installed as well. The texture images are already included in the library so nothing to do there :wink:

import g4p_controls.*;

import shapes3d.*;
import shapes3d.contour.*;
import shapes3d.org.apache.commons.math.*;
import shapes3d.org.apache.commons.math.geometry.*;
import shapes3d.path.*;
import shapes3d.utils.*;

GView view;

Shape3D sky_scene;
Terrain terrain;
TerrainCamera cam;
Box[] boxes;

float worldSize = 1000;
int mapSize = 400;
float view_dist = 100;
float ang = 0, deltaAng = 0;
float speed = 80;
float mouse_sensitivity = 0.5;
IntervalTimer sw;

public void setup() {
  size(340, 340, P2D);
  cursor(CROSS);
  view = new GView(this, 20, 20, 300, 300, P3D);
  view.addListener(new Listen());
  // (1) Create a height map
  PImage grass = loadImage("grass.jpg");
  Heightmap height_map = new HeightmapOSN(mapSize, 400, 0.002);
  height_map.adjustHeights(0, 50);
  // Create a Terrain from the heightmap
  terrain = new Terrain(height_map, worldSize);
  terrain.texture(grass).uv(0, 20, 0, 20).drawMode(S3D.TEXTURE);
  terrain.worldHorizon(200);
  // Create the default sky box
  sky_scene = new SkyBox(400, -8f);
  sky_scene.texture(this, "skybox_04.jpg"); // Also try 00 01 02 03 04 05
  // Create a camera perspective so we can adjust near zone
  Perspective p = new Perspective(view.width(), view.height());
  p.nearZ(10);
  System.out.println(p);
  // Create the camera 
  cam = new TerrainCamera(terrain, sky_scene);
  cam.velocity(new PVector(50, 0, 50))
    .heading(new PVector(-0.1f, 0, -0.3f))
    .up(new PVector(0, -1, 0))
    .perspective(p)
    .hoverHeight(20);

  // Make boxes
  int n = 10;
  boxes = new Box[n*n];
  float delta = terrain.worldSize() / (n + 0);
  for (int i = 0; i < n; i++) {
    float pz = (i + 0.5f) * delta;
    for (int j = 0; j < n; j++) {
      float px = (j + 0.5f) * delta;
      float py = terrain.height(px, pz);
      int idx = i * n + j;
      boxes[idx] = new Box(15, 10, 15);
      boxes[idx].moveTo(px, py - 2, pz).drawMode(S3D.SOLID);
      if (i == 0 || i == n-1 || j == 0 || j == n-1) {
        boxes[idx].fill(0xFFCC3333);
      } else {
        boxes[idx].fill(0xFF33CCCC);
      }
      boxes[idx].tagNo = 1000 + i * n + j;
      terrain.addShape(boxes[idx]);
    }
  }
  // Timer
  sw = new IntervalTimer(this);
}

public void draw() {
  background(96, 0, 0);
}

public class Listen extends GViewListener {

  public void mouseClicked() {
    Picked p = Shape3D.pick(getPApplet(), getGraphics(), mouseX(), mouseY());
    if (p != null) {
      System.out.println("Shape number " + p.shape.tagNo + "   part picked " + p.part + "   part flag " + p.partFlag);
    } else {
      System.out.println("Missed");
    }
  }

  public void mouseMoved() {
    deltaAng = map(mouseX(), 0, width(), mouse_sensitivity, -mouse_sensitivity);
  }

  public void update() {
    // ############################################################
    // Get the graphics context and camera
    PGraphics pg = getGraphics();
    // ############################################################
    // Initialise the canvas
    pg.beginDraw();
    pg.background(200, 200, 255);
    pg.resetMatrix();
    // ############################################################
    // World view lighting here (optional)
    pg.ambientLight(180, 180, 180);
    //pg.directionalLight(255, 255, 255, -1, 1, -1);
    pg.directionalLight(255, 255, 255, 0, 1, 0); // lit from below

    // ############################################################
    // set model view - using camera state
    ang += deltaAng * sw.elapsedTime();
    cam.velocity(new PVector(speed * cos(ang), 0, speed * sin(ang)));
    cam.update(sw.elapsedTime());
    cam.apply(pg);
    // ############################################################
    // Model view lighting here (optional)

    // ############################################################
    // Code to draw canvas
    pg.background(0, 190);

    terrain.draw(pg, cam);
    sky_scene.draw(pg);

    // We are done!!!
    pg.endDraw();
  }
}
2 Likes

Thanks for that, just needed some help on getting the Heightmap going.

Mark