0

I'm making a procedural terrain generation. At the moment I got a terrain that has been generated by Perlin noise. Now I want to implement a procedural grass generation but with some constraints.

I only want to generate grass if is in a radius of the center of the world and on top of the ground. I can easily spawn grass in a radius of the center of the world. The problem is that I don't know how to know the height of the ground for that specified coordinate.

This is my attempt to implement this but doesn't work and I don't have an idea how to do it. I need some advice.

var group = new THREE.Group();

noise.seed(23);
var worldWidth = 256, worldDepth = 256;
var data = generateNoise( worldWidth, worldDepth );
var geometry = new THREE.PlaneBufferGeometry( 7500, 7500, worldWidth - 1, worldDepth - 1 );
geometry.rotateX( - Math.PI / 2 );
geometry.computeBoundingBox();

var vertices = geometry.attributes.position.array;

var positions   = []

for ( var i = 0, j = 0, l = vertices.length; i < l; i ++, j += 3 ) {
    vertices [ j + 1 ] =  data[ i ] * 10;

    //check if this coordinate is inside the radius we want to know if can spawn a grass
    // if is inside spawn a plant knowing the height of this position

}

var grass = new THREEx.createGrassTufts(positions)
group.add(grass);

var texture = assets.textures.grass.val;

var material = new THREE.MeshPhongMaterial( { map: texture, shading: THREE.SmoothShading } );

var ground = new THREE.Mesh( geometry, material );
ground.receiveShadow = true;
ground.castShadow = true;

group.add(ground);
Jota_sk
  • 169
  • 2
  • 14
  • _but doesn't work and I don't have an idea how to do it_. Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions *without a clear problem statement are not useful to other readers*. See: How to create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Marcs Nov 30 '16 at 22:44
  • Isn't the height of the ground the same value you're assigning from the Perlin noise? If you're looking for the average height, or height from the lowest point, you'll either need to get that information from your noise set, or calculate your average/lowest points during the manipulation pass of the vertices, then perform a second pass to calculate the true height. – TheJim01 Dec 01 '16 at 22:13

1 Answers1

0

Perlin noise is created for cell array:

  • find back the cells from the coords cx= x / cell_size_x and cy = ...
  • get height value from noise matrix cell: height = cell[cx][cy]

Note: this is pseudo code

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85