Skip to content
cjcliffe edited this page Oct 15, 2011 · 6 revisions

CubicVR.Landscape

Note: This class is currently experimental and subject to change.

The Landscape class is a SceneObject that generates and manages a heightfield. It can be bound to a scene using [[Scene]].bindSceneObject(..).

Currently it only accepts a single material and appears centered at [0,0,0] when added to the scene but will eventually have proper placement and texture spatting support.

Constructor:

Landscape( size, divisions_x, divisions_z, mat )

Parameters:

  • size : The total length of the axis with the largest number of divisions.
  • divisions_x : Total divisions on the X-axis.
  • divisions_z : Total divisions on the Z-axis.
  • mat : Material to apply.

Note that faces must have square dimensions on the X/Z axis and therefore only one size is supplied. If divisions_x and divisions_z do not match size will represent the axis with the highest number of divisions.

i.e. size of 1000.0 with divisions_x of 128 and divisions_z of 64 will yield a heightfield sized 1000.0 on the X-axis and 500.0 on the Z-axis.

To complete the Landscape you should apply a UVMapper (if required for textured materials) and call myLandscape.getMesh().calcNormals().compile(); to prepare it for rendering.

Methods:

getMesh()

Return the Mesh generated for the heightfield. This is required for applying UV, calculating normals and compiling for use in a Scene.

Returns:

The Mesh representing the heightfield object.

setIndexedHeight( ipos, jpos, val )

Set the height of the given point index (in divisions) on the X and Z axis.

Parameters:

  • x : Index from 0 to < divisions_x along the X-axis.
  • z : Index from 0 to < divisions_z along the Z-axis.
  • val : Y value to set at the given X/Z location.

Returns:

Height (Y-axis) value of the heightfield at the given X/Z coordinates.

mapGen( w_func, ipos, jpos, ilen, jlen )

Step through each point on the heightfield and use the value returned from the supplied function w_func. If index parameters are not supplied it will assume to process the entire field.

Parameters:

  • w_func : The function called for each point on the heightfield to be evaluated. Format is function(x, z) {} and must return the desired Y-axis value at the given X/Z.
  • ipos (optional) : Index from 0 to < divisions_x along the X-axis to start.
  • jpos (optional) : Index from 0 to < divisions_z along the Z-axis to start.
  • ilen (optional) : Number of X-axis divisions to walk.
  • jlen (optional) : Number of Z-axis divisions to walk.

Returns:

none

getHeightValue( x, z ) or getHeightValue( pt )

Get the Y height value of a given position on the X/Z plane.

Parameters:

  • x : Position on the X-axis.
  • z : Position on the Z-axis.
  • pt : A point in the format [ x, y, z ] (y-value will be ignored).

Returns:

Height (Y-axis) value of the heightfield at the given X/Z coordinates.

orient( x, z, width, length, heading )

Get the resulting position and rotation for an object to rest on the heightfield given it's position, base dimensions and heading on the X/Z plane. Additional work is done to ensure the base of the object does not intersect the heightfield.

Parameters:

  • x : Position on the X-axis.
  • z : Position on the Z-axis.
  • width : The width of the base of the object to orient (local X-axis).
  • length : The length of the base of the object to orient (local Z-axis).
  • heading : The heading (in degrees) the object is facing.

Returns:

Resulting position and rotation values in the format [ [pos_x, pos_y, pos_z], [rot_x, rot_y, rot_z] ].

Example usage:

From samples/basic/landscape.html

// Generate a grass material for the landscape
var landscapeMaterial = new CubicVR.Material({
   textures: {
      color: new CubicVR.Texture("../images/grass.jpg")
   }
});
                
// Generate a planar UVMapper for the landscape material.
var landscapeUV = new CubicVR.UVMapper({
   projectionMode: CubicVR.enums.uv.projection.PLANAR,
   projectionAxis: CubicVR.enums.uv.axis.Y,
   scale: [1, 1, 1]
});

// Generate a size 400 landscape with 92 divisions in the X and Z axis, apply landscapeMaterial to faces.
var landscape = new CubicVR.Landscape(200,92,92,landscapeMaterial);
                
landscape.mapGen(function(x,z) {
   return 2.0*Math.sin(x/4.0)+2.0*Math.cos(z/5.0)-4.0;
});

// Apply the UVMapper coordinates to the landscape
landscapeUV.apply(landscape.getMesh(),landscapeMaterial);
                
// Compile the heightfield mesh and prepare it for rendering
landscape.getMesh().calcNormals().compile();

// New scene with our canvas dimensions and default camera with FOV 80
var scene = new CubicVR.Scene(canvas.width, canvas.height, 80);

// Bind the landscape to the scene
scene.bindSceneObject(landscape);
Clone this wiki locally