简体   繁体   English

在Three.js中为弯曲路径添加宽度

[英]Adding Width to a Curved Path in Three.js

I created a racetrack shaped curve in three.js with the following code: 我使用以下代码在three.js中创建了跑道形曲线:

var path = new THREE.Path();

    path.moveTo(150,50);
    path.lineTo(150,150);
    path.quadraticCurveTo(150,175,100,175);
    path.quadraticCurveTo(50,175,50,150);
    path.lineTo(50,50);
    path.quadraticCurveTo(50,25,100,25);
    path.quadraticCurveTo(150,25,150,50);

    var pathPoints = path.getPoints();

    var pathGeometry = new THREE.BufferGeometry().setFromPoints(pathPoints);
    var pathMaterial = new THREE.LineBasicMaterial({color:0xFF1493});

    var raceTrack = new THREE.Line(pathGeometry,pathMaterial);
    scene.add(raceTrack);

}

Right now, the track is just a single line, and I wanted to know if it was possible to make the track into a geometry with a width (still 2D) so that I can add a texture to it. 现在,轨道只是一条直线,我想知道是否有可能将轨道制成宽度(仍为2D)的几何图形,以便可以为其添加纹理。

Currently, the path looks something like this: 当前,路径如下所示:

1像素宽的路径

What I'd like is to be able to draw the same shape, but just increase the width of the line: 我想要的是能够绘制相同的形状,但是只需增加线条的宽度即可:

5px宽的路径

With R91 , three.js added support for triangulated lines. 使用R91three.js添加了对三角线的支持。 This approach enables line width greater than 1px in a reliable way. 这种方法可以可靠地使线宽大于1px。

Demo: https://threejs.org/examples/webgl_lines_fat.html 演示: https//threejs.org/examples/webgl_lines_fat.html

There are no textures applied on the example but uv-coordinates are generated for the underlying geometry. 没有在示例上应用纹理,但为基础几何生成了uv坐标。

With regular Line -objects, there is no way to achieve this. 对于常规的Line对象,无法实现此目的。

Here are a few options you can try: 您可以尝试以下几种方法:

  • create your complete "race-track" as a geometry, including both the inner and the outer line. 创建完整的“跑道”作为几何图形,包括内线和外线。 I have no idea what you need this for, but this is probably the most "correct" way to do something like this (using this approach, the race-track can have different width at different points, you have control over how bends and turns behave and so on). 我不知道您需要什么,但这可能是这样做的最“正确”方法(使用这种方法,跑道可以在不同点处具有不同的宽度,您可以控制弯曲和转弯的方式表现等等)。 A quick example: 一个简单的例子:

     const shape = new THREE.Shape(); shape.moveTo(150, 50); shape.lineTo(150, 150); shape.quadraticCurveTo(150, 175, 100, 175); shape.quadraticCurveTo(50, 175, 50, 150); shape.lineTo(50, 50); shape.quadraticCurveTo(50, 25, 100, 25); shape.quadraticCurveTo(150, 25, 150, 50); const innerShape = new THREE.Path(); innerShape.moveTo(140, 40); innerShape.lineTo(140, 140); innerShape.quadraticCurveTo(140, 165, 90, 165); innerShape.quadraticCurveTo(60, 165, 60, 140); innerShape.lineTo(60, 50); innerShape.quadraticCurveTo(60, 35, 110, 35); innerShape.quadraticCurveTo(140, 35, 140, 60); shape.holes.push(innerShape); var raceTrack = new THREE.Mesh( new THREE.ShapeGeometry(shape), new THREE.MeshBasicMaterial({ color: 0xff1493 }) ); 

    full code here: https://codepen.io/usefulthink/pen/eMBgmE 完整代码在这里: https : //codepen.io/usefulthink/pen/eMBgmE

  • alternatively, you can use the new fat-lines implementation shown here: https://threejs.org/examples/?q=line#webgl_lines_fat or the MeshLine implementation from here: https://github.com/spite/THREE.MeshLine , but I have the feeling those do not what you are looking for... 或者,您可以使用此处显示的新胖线实现: https ://threejs.org/examples/?q = line#webgl_lines_fat或来自此处的MeshLine实现: https : //github.com/spite/THREE.MeshLine ,但我觉得这些不是您想要的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM