Mapbox特效(3D飞线)

时间不早了。不说废话了。先上效果
视频和gif都比较难搞,我这边偷个懒放个图片上去
原理:
Mapbox的customLayer+ three.js的QuadraticBezierCurve3。把线进行分段,每次只显示一部分,动态更改显示的部分就可以做出移动的效果。不会的同学请看代码。欢迎私信骚扰。

	let positions = [] ;
   //传入参数
    let height  =1000
    let anypoint = [[114.58,28.61],[114.58,28.58]]
    var point1 = turf.point([114.58,28.61]);
    var point2 = turf.point([114.58,28.58]);
    var midpoint = turf.midpoint(point1, point2).geometry.coordinates;
    anypoint.splice(1,0,midpoint)
    for(let i = 0 ; i < anypoint.length ; i++){
        if(i == 1){
            positions.push(mapboxgl.MercatorCoordinate.fromLngLat(anypoint[i], height))
        }else{
            positions.push(mapboxgl.MercatorCoordinate.fromLngLat(anypoint[i], 0))
        }
    }

    let p1 = new THREE.Vector3(positions[0].x,positions[0].y,positions[0].z)
    let p2 = new THREE.Vector3(positions[1].x,positions[1].y,positions[1].z)
    let p3 = new THREE.Vector3(positions[2].x,positions[2].y,positions[2].z)
    var curve = new THREE.QuadraticBezierCurve3(p1, p2, p3);
    const linepoints = curve.getPoints(60)
    const highLightGeometry = new THREE.Geometry();
    let i = 0 ;
    let j = 20 ; 
    highLightGeometry.vertices = linepoints.slice(i,j); // 将分割后的前三个点赋值给顶点数据,后面只需改变这个顶点数组
    highLightGeometry.verticesNeedUpdate = true; // 如果顶点队列中的数据被修改,该值需要被设置为 true
    highLightGeometry.colors = [
    new THREE.Color('#ffff00'),
    new THREE.Color('#ffffff'),
    new THREE.Color('#ffff00'),
    ];
    // let geometry = new THREE.BufferGeometry()
    // geometry.addAttribute('position',new THREE.BufferAttribute(new Float32Array(positions), 3))
    let WallLayer = {
        id:'FlyLine01',
        type:'custom',
        renderingMode:'2d',
        onAdd:(map:any,gl:any)=>{

            let camera = new THREE.Camera();
            let scene = new THREE.Scene();
            let renderer = new THREE.WebGLRenderer({
                canvas: map.getCanvas(),
                context: gl, 
                antialias: true 
            })
            let light = new THREE.PointLight(0xfca4c5)
            light.position.set(0, 250, 0)
            scene.add(light)
            // 创建材质                
            // geometry.option
            const material = new THREE.LineBasicMaterial({
                vertexColors: true,
                linewidth: 20
            });
            var color1 = new THREE.Color( 0xFFFFFF ),color2 = new THREE.Color( 0x444444 ),color3 = new THREE.Color( 0xFF0000 ),color4 = new THREE.Color( 0x0000FF );
            highLightGeometry.colors.push(color2,color1,color3,color3,color4,color2,color1,color3,color2,color4,color3,color2,color1,color1,color2,color3,color4,color4,color3,color1)
            let mesh = new THREE.Line(highLightGeometry, material)
            scene.add(mesh)
            renderer.autoClear = false
            this.camera = camera ; 
            this.scene = scene
            this.renderer = renderer ; 
            this.matrtial = material;
            this.mesh = mesh ;
            this.map = map
        },
        render:(gl:any,matrix:any)=>{
                var m = new THREE.Matrix4().fromArray(matrix);
                this.camera.projectionMatrix = m
                this.renderer.state.reset()
                this.renderer.render(this.scene, this.camera)
                this.map.triggerRepaint();
            }   


}




viewer.addLayer(WallLayer);
let temp = setInterval(()=>{
    if(j >= linepoints.length){
        i = 0 ;
        j = 20 ; 
        this.mesh.geometry.vertices = linepoints.slice(i,j)
        this.mesh.geometry.verticesNeedUpdate = true
    }else{
        i+=1;
        j+=1;
        this.mesh.geometry.vertices = linepoints.slice(i,j)
        this.mesh.geometry.verticesNeedUpdate = true//需动态设置为true不然每次会被重置为false  
    }//动态修改顶点
},50)

小记:最近身体有点难受,就不详细写了,有时间我会慢慢把我实现的特效分享出来。有什么疑惑的地方大可以直接贴出

最后感谢几位博主的文章

  1. WebGL three.js学习笔记 自定义顶点建立几何体
  2. ArcGIS JS API+Three.js实现动态航线效果
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值