我正在尝试在three.js中创建一个大型地形,我正在使用physi.js作为物理引擎。
到目前为止,从高度图生成几何图形是没有问题的。但是,当我尝试将其添加为THREE.Mesh
时,它可以很好地工作,当我尝试将其添加为Physijs.HeightfieldMesh
时,我会收到以下错误:
TypeError: geometry.vertices[(a + (((this._physijs.ypts - b) - 1) * this._physijs.ypts))] is undefined
将几何体生成为平面,然后根据高度图修改每个顶点的Z位置。
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
);
答案 0 :(得分:1)
我认为你的问题是你在循环中使用“plane.geometry”而不仅仅是“几何”来设置顶点高度。
也许它应该是:
//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似乎工作正常。