Hi guys, I am building a webVR scene using A-Frame and three.js.
Right now I am trying to build a component that will generate a moving perlin noise landscape. I am using a processing script I built through this video as the basis for my three.js code. However I am having trouble getting it to look right. Instead of making a curved and hilly landscape, instead it ends up looking like this:
Here is the code I have for it. Tick is an a-frame component function that runs every frame. I’m using an imported obj file made from the mesh I made in the processing sketch:
tick: function(time, timeDelta) {
var el = this.el;
if(el.getObject3D('mesh')) {
var terrain = el.getObject3D('mesh').children[0].geometry;
// console.log(terrain.vertices);
// var terrain = el.getObject3D('mesh').geometry;
var data = this.data;
var simplex = new SimplexNoise();
//
//
terrain.verticesNeedUpdate = true;
//
// var xoff = 0;
// var yoff = data.flying;
data.flying -= 0.1;
var yoff = data.flying;
var xoff = 0;
//
for (var i = 0; i < terrain.vertices.length; i++)
{
var v = terrain.vertices[i];
v.z = simplex.noise2D(xoff, yoff) * 10;
// do stuff with v...
yoff += 0.2;
xoff += 0.2;
}
And here is the original processing sketch:
import nervoussystem.obj.*;
boolean record = false;
int cols, rows;
int scl = 20;
int w = 2000;
int h = 1700;
float flying = 0;
float[][] terrain;
void setup() {
size(600, 600, P3D);
cols = w / scl;
rows = h / scl;
terrain = new float[cols][rows];
}
void draw() {
flying -= 0.1;
float yoff = flying;
for(int y = 0; y < rows; y++) {
float xoff = 0;
for(int x = 0; x < cols; x++) {
terrain[x][y] = map(noise(xoff, yoff), 0, 1, -100, 100);
xoff += 0.2;
}
yoff += 0.2;
}
background(0);
stroke(255);
noFill();
translate(width/2, height/2+50);
rotateX(PI/3);
translate(-w/2, -h/2);
if (record) {
beginRecord("nervoussystem.obj.OBJExport", "terrain.obj");
}
for(int y = 0; y < rows - 1; y++) {
beginShape(TRIANGLE_STRIP);
for (int x = 0; x < cols; x++) {
vertex(x * scl, y * scl, terrain[x][y]);
vertex(x * scl, (y + 1) * scl, terrain[x][y+1]);
}
endShape();
}
if (record) {
endRecord();
record = false;
}
}
Any help would be greatly appreciated!