Skip to content

Instantly share code, notes, and snippets.

@kostar111
Created March 4, 2014 10:01
Show Gist options
  • Save kostar111/9343571 to your computer and use it in GitHub Desktop.
Save kostar111/9343571 to your computer and use it in GitHub Desktop.
A new createGround function for Babylon.js
/**
* createGround()
* - name : Name of the new Ground
* - width : Width of the new Ground
* - height : Height of the new Ground
* - w_subdivisions : Number of Width's subdivisions in the new Ground
* - h_subdivisions : Number of Height's subdivisions in the new Ground
* - scene : Scene which contain the new Ground
* - updatable :
*
****************************************************************/
function createGround (name, width, height, w_subdivisions, h_subdivisions, scene, updatable) {
var ground = new BABYLON.Mesh(name, scene);
var indices = [];
var positions = [];
var normals = [];
var uvs = [];
var row, col;
for (row = 0; row <= h_subdivisions; row++) {
for (col = 0; col <= w_subdivisions; col++) {
var position = new BABYLON.Vector3((col * width) / w_subdivisions - (width / 2.0), 0, ((h_subdivisions - row) * height) / h_subdivisions - (height / 2.0));
var normal = new BABYLON.Vector3(0, 1.0, 0);
positions.push(position.x, position.y, position.z);
normals.push(normal.x, normal.y, normal.z);
uvs.push(col / w_subdivisions, 1.0 - row / h_subdivisions);
}
}
for (row = 0; row < h_subdivisions; row++) {
for (col = 0; col < w_subdivisions; col++) {
indices.push(col + 1 + (row + 1) * (w_subdivisions + 1));
indices.push(col + 1 + row * (w_subdivisions + 1));
indices.push(col + row * (w_subdivisions + 1));
indices.push(col + (row + 1) * (w_subdivisions + 1));
indices.push(col + 1 + (row + 1) * (w_subdivisions + 1));
indices.push(col + row * (w_subdivisions + 1));
}
}
ground.setVerticesData(positions, BABYLON.VertexBuffer.PositionKind, updatable);
ground.setVerticesData(normals, BABYLON.VertexBuffer.NormalKind, updatable);
ground.setVerticesData(uvs, BABYLON.VertexBuffer.UVKind, updatable);
ground.setIndices(indices);
return ground;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment