楔子在很多应用中都有使用,尤其是一些公园应用。两者都需要展示公园的地面环境,路面是地面的一部分。通常的做法是在建模时构建所有相关元素,然后将它们导入到显示系统中进行显示。但是,在某些情况下,建模可能不是很方便,所以需要3D编辑器直接对简单的路面进行编辑。Roadsurfaceobjectextension简单的路面应该由路径生成。我们知道threejs中有对象通过路径生成管道。参考文章WebGL管网显示(及TubeGeometry优化)。管道横截面为圆形。道路横截面预计为矩形。因此,我们可以按照管道的思路创建一个类似的对象PathRectGeometry,只是在计算顶点的时候,横截面不再使用圆形,而是矩形。代码如下:letpoints=[newVec3(-width/2,-height/2,0),newVec3(-width/2,height/2,0),newVec3(width/2,height/2,0),newVec3(width/2,-height/2,0)]if(!scope.clockwise){points=[newVec3(-width/2,-height/2,0),newVec3(width/2,-height/2,0),newVec3(width/2,height/2,0),newVec3(-width/2,height/2,0)];}for(letj=0;j<=points.length;j++){letjj=j==points.length?0:j;让点=点[jj];让radius=Math.hypot(point.x,point.y);constsin=point.y/radius;constcos=point.x/radius;normal.x=(cos*N.x+sin*B.x);normal.y=(cos*N.y+sin*B.y);normal.z=(cos*N.z+sin*B.z);normal.normalize();normals.push(0,1,0);//顶点vertex.x=P.x+radius*normal.x;vertex.y=P.y+radius*normal.y;vertex.z=P.z+radius*normal.z;vertices.push(vertex.x,vertex.y,vertex.z);通过PathRectGeometry创建对象的效果如下图所示:路面编辑通过在平面上放置点来构建直线和贝塞尔曲线,然后通过构建的直线生成路径,通过生成路面效果即可路径,图形。getView().addEventListener("click",(event)=>{letnow=newDate().getTime();if(t!=0&&now-t<500){return;}t=now;if(路径){让pos=graph.getPositionOnPlaneByEvent(事件,平面);constraintsHorizo??ntalOrVertical(路径,pos);path.lineTo(pos.x,pos.y,pos.z);tempPath=path.clone(tempPath);tempRoad.geometry.path=tempPath;}})大致过程如下:生成的路径上会有很多控制点,拖动控制点可以修改路径两次:会有斑马线之类的,点击生成斑马线,斑马线可以通过算法自动计算,//找到road1到road2的连接函数createJointShape(road1,road2){letpath=road1.geometry.path;让path2=road2.geometry.path;让lastPoint=path.points.at(-1);让lastCurve=path.curves.at(-1);让曲线=path2.curves;控制台日志(曲线);让minCurve,minDist=Infinity,minPointfor(leti=0;i
