javascript - 如何在 Three.js 中制作平行曲线用于道路标记?

标签 javascript three.js bezier curve

我正在尝试绘制一些平行曲线,一条紧挨着一条,但是如果我通过修改/平移它们在一个轴上的位置来将它们分开,结果不是我想要的.

代码很简单,用贝塞尔曲线制作中心路径,并用其他 Material 克隆侧面。

var bezier = new THREE.CubicBezierCurve3(
                new THREE.Vector3(initx, inity, 0),
                new THREE.Vector3(cp1x, cp1y, 0),
                new THREE.Vector3( cp2x, cp2y, 0),
                new THREE.Vector3(finalx, finaly, 0)
                );

var curvePath = new THREE.CurvePath();
curvePath.add(bezier);

var path = curvePath.createPointsGeometry( 5 );
path.computeLineDistances();

var lineMat = new THREE.LineDashedMaterial({ color: 0xFFFFFF,
                                             dashSize: 3,
                                             gapSize: 2, 
                                             linewidth: 10});

var curveLine = new THREE.Line(path, lineMat);

curveLine.rotation.set(-Math.PI/2,0,0);
curveLine.position.y = 0.1;

var leftLine = curveLine.clone();
leftLine.material = matLine;

var rightLine = curveLine.clone();
rightLine.material = matLine;

leftLine.translateX(-3.5);
rightLine.position.set(3.5,0,0);

scene.add(curveLine);
scene.add(leftLine);
curveLine.add(rightLine);

这是结果: enter image description here

这是我正在寻找的一些示例:

d source: https://pomax.github.io/bezierjs/ source: https://pomax.github.io/bezierjs/

最后两张图片来自Bezier.js图书馆。

我认为对曲线应用偏移使用第一条线的法线绘制另一条线可能是解决方案,但我在文档中找不到任何有用的内容。 我认为也可以从切轴或类似的距离画一条线,但这可能是一种更简单的方法,有什么想法吗?

我查看了 this但它适用于直线。

最佳答案

最后,我自己完成了。

我使用getTangent()计算了主曲线在其点上的切线法线,并将其旋转90度以获得普通的。我为该法线创建了更多顶点,并将其值乘以标量。然后我从世界坐标的法线中得到了我想要的点,并将它们连接起来,创建了一个 CatmullRomCurve3 几何图形:

for(var i = 0.0; i <= 1.0; i += 0.2){

    var point = curvePath.getPointAt(i);

    var tangent = curvePath.getTangent(i);
    var extension1 = new THREE.Vector3(tangent.x*2, tangent.y*2, 0);
    var extension2 = new THREE.Vector3(extension1.x*2, extension1.y*2, 0);

    var tangentGeometry = new THREE.Geometry();
    tangentGeometry.vertices.push(tangent, extension1, extension2);

    var tangentLine = new THREE.Line(tangentGeometry,greenMaterial);
    tangentLine.position.set(point.x, point.y, point.z);

    normal.updateMatrixWorld(); // VERY IMPORTANT

    var normal = tangentLine.clone();
    normal.rotateZ(Math.PI/2);

    var normalLastVertex = normal.geometry.vertices[2].clone();
    normalLastVertex.applyMatrix4(normal.matrixWorld); //convert to world coords.

    pointsArray.push(normalLastVertex); //for using them out of the loop

    curveLine.add(tangentLine);
    curveLine.add(normal);
}

这里我在点数组中有最后一个法线顶点,所以我必须创建将它们连接到循环之外的样条线。

    var splineCurveOffsetLeft = new THREE.CatmullRomCurve3([

       new THREE.Vector3((pointsArray[0].x), pointsArray[0].y, 0),
       new THREE.Vector3((pointsArray[1].x), pointsArray[1].y, 0),
       new THREE.Vector3((pointsArray[2].x), pointsArray[2].y, 0),
       new THREE.Vector3((pointsArray[3].x), pointsArray[3].y, 0),
       new THREE.Vector3((pointsArray[4].x), pointsArray[4].y, 0),
       new THREE.Vector3((pointsArray[5].x), pointsArray[5].y, 0)           
    ]);
splineCurveOffsetLeft.type = 'catmullrom';
splineCurveOffsetLeft.closed = false;

var offsetGeometry = new THREE.Geometry();
offsetGeometry.vertices = splineCurveOffsetLeft.getPoints(6);

var offsetLine = new THREE.Line(offsetGeometry, cyanMaterial);

offsetLine.rotation.set(-Math.PI/2, 0, 0);
offsetLine.position.y=0.1;

scene.add(offsetLine);

我对另一条偏移线做了同样的事情,只是反转其旋转,这就是结果:

Offset curve

绿色 = 切线,红色 = 法线,青色 = 平行曲线

关于javascript - 如何在 Three.js 中制作平行曲线用于道路标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45816041/

相关文章:

javascript - 如何整合Golang后端和Javascript(three.js)前端?

3d - 如何在视觉上将贝塞尔曲线的透视投影与原始曲线匹配,仅投影其控制点?

c++ - 计算多边形 union 时在 CGAL 中触发的断言

ios - 如何以与 Siri 显示的波浪状效果相同的方式为线条设置动画?

javascript - "Clipping"背景在堆叠上下文中看到自身下方

javascript - 保存和恢复 jQuery 对象上的 "onclick"操作

javascript - OpenLayers 3/Javascript : Why is there a "delay" when my array is filled?

javascript - 没有内容的异步等待获取API不会继续执行代码。

javascript - THREE.js - 如何实现这种效果?

javascript - Three.js TextGeometry 绘图问题