Javascript Three.js在两个模型的最近顶点之间绘制动态线

Javascript Three.js在两个模型的最近顶点之间绘制动态线,javascript,three.js,Javascript,Three.js,我使用OBJloader和Three.JS库在场景中加载了两个模型。一些模型通过使用model.setRotationFromQuaternion(camera.quaternion)在广告牌上显示 我的目标是从广告牌上的一个顶点到相应模型上的一个顶点绘制线——当场景第一次加载时,应该在两个模型上最近的点之间绘制线。模型可以自由旋转,因此线在旋转时需要改变,保持连接到相同的初始垂直 想象一下,就像广告牌是一个标签,线条连接在标签和旋转模型上的某个地方 我怎样才能做到这一点 下面是我的代码片段-问

我使用OBJloader和Three.JS库在场景中加载了两个模型。一些模型通过使用
model.setRotationFromQuaternion(camera.quaternion)在广告牌上显示

我的目标是从广告牌上的一个顶点到相应模型上的一个顶点绘制线——当场景第一次加载时,应该在两个模型上最近的点之间绘制线。模型可以自由旋转,因此线在旋转时需要改变,保持连接到相同的初始垂直

想象一下,就像广告牌是一个标签,线条连接在标签和旋转模型上的某个地方

我怎样才能做到这一点

下面是我的代码片段-问题是所有模型的位置都是0,0,0,所以我需要知道如何获得标签和模型上顶点的位置,并将两者连接起来

 addLabelLines(){
    var geometry = new THREE.Geometry();    

    for ( var i = 0; i < this.labels.length; i ++ ) { 
        var currentLabel = this.labels[i];
        var modelMatchingLabel;
        //find matching model
        for(var j = 0; j < this.models.length; j++){
          if(currentLabel.name.toLowerCase().indexOf(this.models[j].name) >= 0){
            modelMatchingLabel = this.models[j];
          }
        }

        if(!modelMatchingLabel){
          console.warn("no model matching label "+currentLabel.name);
          return;
        }
        else{
          console.log('found model '+modelMatchingLabel.name +" matches label "+currentLabel.name);

          geometry.vertices.push( currentLabel.position );
          geometry.vertices.push( modelMatchingLabel.position );
        }        
    }


    var material = new THREE.LineBasicMaterial( { color: 0x800080 } );

    line = new THREE.Line( geometry, material, THREE.LineSegments );

    scene.add( line );
  }
addLabelLines(){
var geometry=new THREE.geometry();
对于(var i=0;i=0){
modelMatchingLabel=this.models[j];
}
}
如果(!modelMatchingLabel){
console.warn(“无型号匹配标签”+currentLabel.name);
返回;
}
否则{
log('found model'+modelMatchingLabel.name+“matches label”+currentlab.name);
几何体.顶点.推(currentLabel.position);
geometry.Vertexs.push(modelMatchingLabel.position);
}        
}
var material=新的三线基本材质({color:0x800080});
直线=新的三条直线(几何体、材质、三条线段);
场景。添加(行);
}

要获得能够画线的最近点,需要典型的“最近邻”算法。看看这个例子,因为它将为您提供一种非常有效的方式来实现您所寻找的目标


我可以知道你最近的顶点是什么意思吗?线路的一些交点连接两个模型的中心?或者,网格的顶点?网格的垂直度。