How to create a Three.js 3D line series with width and thickness?

前端 未结 2 520
庸人自扰
庸人自扰 2020-12-09 05:48

Is there a way to create a Three.js 3D line series with width and thickness?

Even though the Three.js line object supports linewidth, this attribute is not yet suppo

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 06:15

    As a potential solution. You could take your 3d points, then use THREE.Vector3.project method to figure out screen-space coordinates. Then simply use canvas and it's lineTo and moveTo operations. Canvas 2d context does support variable line thickness.

    var w = renderer.domElement.innerWidth;
    var h = renderer.domElement.innerHeight;
    vector.project(camera);
    context2d.lineWidth = 3;
    var x = (vector.x+1)*(w/2);
    var y = h - (vector.y+1)*(h/2);
    context2d.lineTo(x,y);
    

    Also, i don't think you can use the same canvas for that, so it would have to be a layer (another canvas) above your gl rendering context canvas.

    If you have infrequent camera changes - it is also possible to construct line out of polygons and update it's vertex positions based on camera transform. For orthographic camera this would work best as only rotations would require vertex position manipulation.

    Lastly, you could disable canvas clearing and draw your lines several times with offset inside a circle or a box. After that you can re-enable clearing. This would require several extra draw operations, but it's probably the most scalable approach.

    The reason lines don't work as you'd expect out of the box is due to how ANGLE works, it's used in Chrome and in Firefox to my knowledge, it emulates OpenGL via DirectX. Guys from ANGLE state that WebGL spec only requires support of line thickness up-to 1, so they do not see it as a bug and don't intend to "fix" it. Line thickness should work on non-windows OSs though, where ANGLE is not used.

提交回复
热议问题