Thickness of lines using THREE.LineBasicMaterial

后端 未结 8 1681
小鲜肉
小鲜肉 2020-11-27 06:53

I am using the code below to create hundreds of lines in my three.js scene

edgeGeometry[i] = new THREE.Geometry();
edgeGeometry[i].vertices[0] = v(x1,y1,z1);         


        
8条回答
  •  甜味超标
    2020-11-27 07:03

    Thanks to Wilt's answer for pointing me in the right direction with THREE.MeshLine.

    It can be slightly trickier than they make it out to be, however... So here's my solution following their docs and their demo code very carefully... (assuming you've already included Three and MeshLine):

        renderer = new THREE.WebGLRenderer({ canvas });
    
        //...
    
        function createCircle(resolution) {
            let circleGeometry = new THREE.Geometry();
            for (let rotation = 0; rotation <= Math.PI * 2.0; rotation += Math.PI * 0.1) {
                circleGeometry.vertices.push(
                    new THREE.Vector3(Math.cos(rotation), Math.sin(rotation), 0));
            }
            let circleLine = new MeshLine();
            circleLine.setGeometry(circleGeometry);
            //Bonus: parabolic width! (See Z rotation below.)
            //circleLine.setGeometry(circleGeometry, function(point) {
                //return Math.pow(4 * point * (1 - point), 1);
            //});
    
            //Note: resolution is *required*!
            return new THREE.Mesh(circleLine.geometry,
                new MeshLineMaterial({
                    color: 'blue',
                    resolution,
                    sizeAttenuation: 0,
                    lineWidth: 5.0,
                    side: THREE.DoubleSide
                }));
        }
    
        let circle = createCircle(new THREE.Vector2(canvas.width, canvas.height));
        circle.rotation.x = Math.PI * 0.5;
        circle.position.y = 20.0;
        scene.add(circle);
    
        //In update, to rotate the circle (e.g. if using parabola above):
        world.circle.rotation.z += 0.05;
    

    With size attenuation off and using THREE.DoubleSide, like I did above, the circle will look like a nice, consistent circle no matter where you're looking at it from (not "true 3D").

    For just a line, you can obviously easily adapt.

提交回复
热议问题