我有XY坐标数组,必须从中获取各自的Z位置。我创建了以下代码来实现相同的目的。
此函数遍历数组并调用进一步的函数以获取Z值。
function generate_section(){
for(var i=0;i<points.length;i++){
//temporary try to get for the same.
var pts = points[i];
var z = sectioncall(pts.x,pts.y);
console.log(pts,z);
}
}
下面的功能是射线投射器,它以提供的x&y值投射光线并投射向下的光线。
function sectioncall(x,y){ //grabs the Z value for the provided XY
var top = new THREE.Vector3(x, y , 30 );
var bottom = new THREE.Vector3(x , y , -30 );
var direction = new THREE.Vector3();
direction = direction.subVectors( bottom, top ).normalize();
//start raycaster
var raycaster = new THREE.Raycaster();
raycaster.set( top, direction );
// calculate objects intersecting the picking ray
var intersects = rayCaster.intersectObjects(scene.getObjectByName('MyObj_s').children);
var rpt = intersects[0].point;
//draw a line the way ray caster casting the ray
var geometry = new THREE.Geometry();
geometry.vertices.push( top );
geometry.vertices.push( rpt );
var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
var line = new THREE.Line( geometry, material );
scene.add( line );
return rpt;
}
这样,返回值将对应于各个XY,而不是控制台上的最后一个XY。
答案 0 :(得分:0)
您在代码中犯了一个小错误。 应该是
var intersects = raycaster.intersectObjects(scene.getObjectByName('MyObj_s').children);
您将它拼写为rayCaster。