Navigation Menu

Skip to content

Commit

Permalink
added subdivisionsX and subdivisionsY options to groundMesh (#1339)
Browse files Browse the repository at this point in the history
* added subdivisionsX and subdivisionsY options to groundMesh

* removed redundant _subdivisions from groundMesh

* replaced references to groundMesh._subdivisions in MeshBuilder with _subdivisionsX, _subdivisionsY
  • Loading branch information
abow authored and RaananW committed Sep 5, 2016
1 parent 0d688f3 commit 1be2abf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
48 changes: 33 additions & 15 deletions src/Mesh/babylon.groundMesh.ts
Expand Up @@ -4,7 +4,9 @@

private _worldInverse = new Matrix();
private _heightQuads: { slope: Vector2; facet1: Vector4; facet2: Vector4 }[];
public _subdivisions: number;

public _subdivisionsX: number;
public _subdivisionsY: number;
public _width: number;
public _height: number;
public _minX: number;
Expand All @@ -17,12 +19,21 @@
}

public get subdivisions(): number {
return this._subdivisions;
return Math.min(this._subdivisionsX, this._subdivisionsY);
}

public get subdivisionsX(): number {
return this._subdivisionsX;
}

public get subdivisionsY(): number {
return this._subdivisionsY;
}

public optimize(chunksCount: number, octreeBlocksSize = 32): void {
this._subdivisions = chunksCount;
this.subdivide(this._subdivisions);
this._subdivisionsX = chunksCount;
this._subdivisionsY = chunksCount;
this.subdivide(chunksCount);
this.createOrUpdateSubmeshesOctree(octreeBlocksSize);
}

Expand Down Expand Up @@ -103,9 +114,11 @@
// Returns the element "facet" from the heightQuads array relative to (x, z) local coordinates
private _getFacetAt(x: number, z: number): Vector4 {
// retrieve col and row from x, z coordinates in the ground local system
var col = Math.floor((x + this._maxX) * this._subdivisions / this._width);
var row = Math.floor(-(z + this._maxZ) * this._subdivisions / this._height + this._subdivisions);
var quad = this._heightQuads[row * this._subdivisions + col];
var subdivisionsX = this._subdivisionsX;
var subdivisionsY = this._subdivisionsY;
var col = Math.floor((x + this._maxX) * this._subdivisionsX / this._width);
var row = Math.floor(-(z + this._maxZ) * this._subdivisionsY / this._height + this._subdivisionsY);
var quad = this._heightQuads[row * this._subdivisionsX + col];
var facet;
if (z < quad.slope.x * x + quad.slope.y) {
facet = quad.facet1;
Expand All @@ -121,11 +134,13 @@
// facet1 : Vector4(a, b, c, d) = first facet 3D plane equation : ax + by + cz + d = 0
// facet2 : Vector4(a, b, c, d) = second facet 3D plane equation : ax + by + cz + d = 0
private _initHeightQuads(): void {
var subdivisionsX = this._subdivisionsX;
var subdivisionsY = this._subdivisionsY;
this._heightQuads = new Array();
for (var row = 0; row < this._subdivisions; row++) {
for (var col = 0; col < this._subdivisions; col++) {
for (var row = 0; row < subdivisionsY; row++) {
for (var col = 0; col < subdivisionsX; col++) {
var quad = { slope: BABYLON.Vector2.Zero(), facet1: new BABYLON.Vector4(0, 0, 0, 0), facet2: new BABYLON.Vector4(0, 0, 0, 0) };
this._heightQuads[row * this._subdivisions + col] = quad;
this._heightQuads[row * subdivisionsX + col] = quad;
}
}
}
Expand Down Expand Up @@ -153,11 +168,14 @@
var d1 = 0; // facet plane equation : ax + by + cz + d = 0
var d2 = 0;

for (var row = 0; row < this._subdivisions; row++) {
for (var col = 0; col < this._subdivisions; col++) {
var subdivisionsX = this._subdivisionsX;
var subdivisionsY = this._subdivisionsY;

for (var row = 0; row < subdivisionsY; row++) {
for (var col = 0; col < subdivisionsX; col++) {
i = col * 3;
j = row * (this._subdivisions + 1) * 3;
k = (row + 1) * (this._subdivisions + 1) * 3;
j = row * (subdivisionsX + 1) * 3;
k = (row + 1) * (subdivisionsX + 1) * 3;
v1.x = positions[j + i];
v1.y = positions[j + i + 1];
v1.z = positions[j + i + 2];
Expand Down Expand Up @@ -190,7 +208,7 @@
d1 = -(norm1.x * v1.x + norm1.y * v1.y + norm1.z * v1.z);
d2 = -(norm2.x * v2.x + norm2.y * v2.y + norm2.z * v2.z);

var quad = this._heightQuads[row * this._subdivisions + col];
var quad = this._heightQuads[row * subdivisionsX + col];
quad.slope.copyFromFloats(cd, h);
quad.facet1.copyFromFloats(norm1.x, norm1.y, norm1.z, d1);
quad.facet2.copyFromFloats(norm2.x, norm2.y, norm2.z, d2);
Expand Down
29 changes: 15 additions & 14 deletions src/Mesh/babylon.mesh.vertexData.ts
Expand Up @@ -1180,7 +1180,7 @@
return vertexData;
}

public static CreateGround(options: { width?: number, height?: number, subdivisions?: number }): VertexData {
public static CreateGround(options: { width?: number, height?: number, subdivisions?: number, subdivisionsX?: number, subdivisionsY?: number }): VertexData {
var indices = [];
var positions = [];
var normals = [];
Expand All @@ -1189,28 +1189,29 @@

var width: number = options.width || 1;
var height: number = options.height || 1;
var subdivisions: number = options.subdivisions || 1;
var subdivisionsX: number = options.subdivisionsX || options.subdivisions || 1;
var subdivisionsY: number = options.subdivisionsY || options.subdivisions || 1;

for (row = 0; row <= subdivisions; row++) {
for (col = 0; col <= subdivisions; col++) {
var position = new Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
for (row = 0; row <= subdivisionsY; row++) {
for (col = 0; col <= subdivisionsX; col++) {
var position = new Vector3((col * width) / subdivisionsX - (width / 2.0), 0, ((subdivisionsY - row) * height) / subdivisionsY - (height / 2.0));
var normal = new Vector3(0, 1.0, 0);

positions.push(position.x, position.y, position.z);
normals.push(normal.x, normal.y, normal.z);
uvs.push(col / subdivisions, 1.0 - row / subdivisions);
uvs.push(col / subdivisionsX, 1.0 - row / subdivisionsX);
}
}

for (row = 0; row < subdivisions; row++) {
for (col = 0; col < subdivisions; col++) {
indices.push(col + 1 + (row + 1) * (subdivisions + 1));
indices.push(col + 1 + row * (subdivisions + 1));
indices.push(col + row * (subdivisions + 1));
for (row = 0; row < subdivisionsY; row++) {
for (col = 0; col < subdivisionsX; col++) {
indices.push(col + 1 + (row + 1) * (subdivisionsX + 1));
indices.push(col + 1 + row * (subdivisionsX + 1));
indices.push(col + row * (subdivisionsX + 1));

indices.push(col + (row + 1) * (subdivisions + 1));
indices.push(col + 1 + (row + 1) * (subdivisions + 1));
indices.push(col + row * (subdivisions + 1));
indices.push(col + (row + 1) * (subdivisionsX + 1));
indices.push(col + 1 + (row + 1) * (subdivisionsX + 1));
indices.push(col + row * (subdivisionsX + 1));
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/Mesh/babylon.meshBuilder.ts
Expand Up @@ -583,10 +583,11 @@
* The parameter `subdivisions` (positive integer) sets the number of subdivisions per side.
* The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.
*/
public static CreateGround(name: string, options: { width?: number, height?: number, subdivisions?: number, updatable?: boolean }, scene: any): Mesh {
public static CreateGround(name: string, options: { width?: number, height?: number, subdivisions?: number, subdivisionsX?: number, subdivisionsY?: number, updatable?: boolean }, scene: any): Mesh {
var ground = new GroundMesh(name, scene);
ground._setReady(false);
ground._subdivisions = options.subdivisions || 1;
ground._subdivisionsX = options.subdivisionsX || options.subdivisions || 1;
ground._subdivisionsY = options.subdivisionsY || options.subdivisions || 1;
ground._width = options.width || 1;
ground._height = options.height || 1;
ground._maxX = ground._width / 2;
Expand Down Expand Up @@ -651,7 +652,8 @@
var onReady = options.onReady;

var ground = new GroundMesh(name, scene);
ground._subdivisions = subdivisions;
ground._subdivisionsX = subdivisions;
ground._subdivisionsY = subdivisions;
ground._width = width;
ground._height = height;
ground._maxX = ground._width / 2.0;
Expand Down

0 comments on commit 1be2abf

Please sign in to comment.