Outils pour utilisateurs

Outils du site


terrain

Ceci est une ancienne révision du document !


Création d'un terrain 3D

import peasy.PeasyCam;
 
PeasyCam cam;
 
int largeurGrille, longueurGrille;
int longueurTerrain, largeurTerrain;
int intervalle = 20;
float[][] terrain;
Maille[][] vecteurs;
 
public void settings() {
  size(800, 800, P3D);
}
 
void setup() {
  cam = new PeasyCam(this, 400);
  longueurTerrain = 1200;
  largeurTerrain = 1200;
  longueurGrille = longueurTerrain / intervalle;
  largeurGrille = largeurTerrain / intervalle;
  terrain = new float[largeurGrille][longueurGrille];
  for (int y = 0; y < longueurGrille; y++) {
    for (int x = 0; x < largeurGrille; x++) {
      terrain[x][y] = map(noise(x / 10.0, y / 10.0), 0, 1, -100, 100);
    }
  }
  vecteurs = new Maille[2 * largeurGrille - 2][longueurGrille - 1];
  println(vecteurs.length);
  for (int y = 0; y < longueurGrille - 1; y++) {
    for (int x = 0; x < largeurGrille - 1; x++) {
      vecteurs[2 * x][y] = new Maille(intervalle, x, y, terrain[x][y], terrain[x][y + 1], terrain[x + 1][y], false);
      vecteurs[2 * x + 1][y] = new Maille(intervalle, x + 1, y + 1, terrain[x + 1][y + 1], terrain[x][y + 1], terrain[x + 1][y], true);
    }
  }
}
 
void draw() {
  background(0);  
  rotateX(PI / 3);
  translate(-largeurTerrain / 2, -longueurTerrain / 2);
  stroke(255, 0, 0);
  noFill();
 
  for (int y = 0; y < longueurGrille - 1; y++) {
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < largeurGrille; x++) {
      vertex(x * intervalle, y * intervalle, terrain[x][y]);
      vertex(x * intervalle, (y + 1) * intervalle, terrain[x][y + 1]);
    }
    endShape();
  }
  for (int y = 0; y < longueurGrille - 1; y++) {
    for (int x = 0; x < 2 * largeurGrille - 2; x++) {
      vecteurs[x][y].afficher();
    }
  }
}
 
class Maille {
  PVector position, direction;
  PVector i, j;
  PVector origine, point1, point2;
  int inter;
 
  Maille(int _inter, int x, int y, float u, float v, float w, boolean paire) {
    inter = _inter;
    origine = new PVector(x * inter, y * inter, u);
    if (paire) {
      i = new PVector(0, -inter, w - u);
      j = new PVector(-inter, 0, v - u);
    } else {
      i = new PVector(0, inter, v - u);
      j = new PVector(inter, 0, w - u);
    }
    direction = new PVector();
    PVector.cross(j, i, direction);
    direction.setMag(inter);
 
    PVector milieu = PVector.sub(j, i);
    milieu = milieu.div(2);
    PVector g = PVector.add(i, milieu);
    g = g.mult(2.0 / 3.0);
    position = new PVector(x * inter, y *inter, u);
    position.add(g);
  }
 
 
  void afficher() {
    pushMatrix();
    pushStyle();
    stroke(255);
    translate(position.x, position.y, position.z);
    line(0, 0, 0, direction.x, direction.y, direction.z);
    popStyle();
    popMatrix();
    /*
    pushMatrix();
    pushStyle();
    stroke(255, 0, 0);
    translate(origine.x, origine.y, origine.z);
    line(0, 0, 0, i.x, i.y, i.z);
    stroke(0, 255, 0);
    line(0, 0, 0, j.x, j.y, j.z);
    popStyle();
    popMatrix();*/
  }
 
  void eroder() {
 
  }
}

Inspiration

terrain.1574873907.txt.gz · Dernière modification : 2019/11/27 16:58 de Mushussu