three.js / physi.js heightfield wont accept geometry

593 views Asked by At

I'm attempting to create a large terrain in three.js, i'm using physi.js as the physics engine.

generating the geometry from the heightmap is no problem so far. however, when i try to add it as a THREE.Mesh it works beautifully, when i try adding it as a Physijs.HeightfieldMesh i get the following error:

TypeError: geometry.vertices[(a + (((this._physijs.ypts - b) - 1) * this._physijs.ypts))] is undefined

The geometry is generated as a plane, then the Z position of each vertex gets modified according to the heightmap.

var geometry = new THREE.PlaneGeometry( img.naturalWidth, img.naturalHeight,img.naturalWidth -1, img.naturalHeight -1 );
var material = new THREE.MeshLambertMaterial( { color : 0x0F0F0F } );

//set height of vertices
for ( var i = 0; i<plane.geometry.vertices.length; i++ ) {
    plane.geometry.vertices[i].z = data[i];//let's just assume the data is correct since it works as a THREE.Mesh
}

var terrain = new THREE.Mesh(geometry, material); // works

//does not work
var terrain = new Physijs.heightfieldMesh(
    geometry, 
    material,
    0
);
1

There are 1 answers

0
2pha On BEST ANSWER

I think your problem is you are using "plane.geometry" instead of just "geometry" in the loop to set the vertex height.
Maybe it should be:

//set height of vertices
for ( var i = 0; i < geometry.vertices.length; i++ ) {
    geometry.vertices[i].z = data[i];//let's just assume the data is correct since it works as a THREE.Mesh
}

This fiddle that I created seems to work ok.