• three.js 绘制3d地图


    通过地图数据配合three可以做出非常酷炫的地图,在大数据展示中十分常见。

    这篇郭先生就来说说使用three.js几何体制作3D地图。在线案例点击原文地址

    地图的数据是各个地图块的点数组,通过THREE.ExtrudeGeometry方法挤压出地图的版块,然后通过THREE.Line方法画出地图的分割线。地图的数据参见DATAV.GeoAtlas,鼠标悬浮到地图版块高亮,效果如图

    1. 得到数据,遍历数据,处理数据

    rawMap() {
        this.worldGeometry = mapJson; //mapJson就是地图的json数据
        this.worldGeometry.features.forEach((worldItem, worldItemIndex) => {
            worldItem.geometry.coordinates.forEach(worldChildItem => {
                worldChildItem.forEach(countryItem => { //每个版块的点数组
                    this.drawExtrude(this.drawShape(countryItem)) //传递数据画出地图的shape,返回结果再传到drawExtrude方法得到ExtrudeGeometry网格
                    this.drawLine(countryItem); //传递数据画出地图边线
                });
            });
        });
            group.scale.y = 1.2; //group里面包含所有版块网格
        scene.add(group);
        lineGroup.scale.y = 1.2; //lineGruop里面包含所有线的网格
        scene.add(lineGroup);
        this.render();
    }

    2. 传递数据画出地图的shape,返回结果再传到drawExtrude方法得到ExtrudeGeometry网格。

    drawShape(posArr) {
        var shape = new THREE.Shape();
        shape.moveTo(posArr[0][0] - this.offsetX, posArr[0][1] - this.offsetY);
        posArr.forEach(item => {
            shape.lineTo(item[0] - this.offsetX, item[1] - this.offsetY);
        });
        return shape;
    }
    drawExtrude(shapeObj) {
        var geometry = new THREE.ExtrudeGeometry(shapeObj, this.options);
        var material1 = new THREE.MeshPhongMaterial({
            color: this.bgColor,
            specular: this.bgColor
        });
        var material2 = new THREE.MeshBasicMaterial({
            color: 0x008bfb
        });
        let shapeGeometryObj = new THREE.Mesh(geometry, [material1, material2]);
        shapeGeometryObj.name = 'board';
        group.add(shapeGeometryObj);
    }

    3. 传递数据画出地图边线

    drawLine(posArr) {
        let geometry1 = new THREE.Geometry();
        let geometry2 = new THREE.Geometry();
            posArr.forEach(item => {
            geometry1.vertices.push(
                new THREE.Vector3(
                    item[0] - this.offsetX,
                    item[1] - this.offsetY,
                    1.01
                )
            );
            geometry2.vertices.push(
                new THREE.Vector3(
                    item[0] - this.offsetX,
                    item[1] - this.offsetY,
                    -0.01
                )
            );
        });
        let lineMaterial = new THREE.LineBasicMaterial({ color: 0x008bfb });
        let line1 = new THREE.Line(geometry1, lineMaterial);
        let line2 = new THREE.Line(geometry2, lineMaterial);
        lineGroup.add(line1);
        lineGroup.add(line2);
    }

    4. 鼠标悬浮后高亮版块

    handleMousemove(event) {
        event.preventDefault();
        let mouse = new THREE.Vector2(0, 0);
        mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
        mouse.y = - (event.clientY / window.innerHeight) * 2 + 1;
        this.raycaster.setFromCamera(mouse, camera);
        let intersects = this.raycaster.intersectObjects(group.children);
        this.previousObj.material[0].color = new THREE.Color(this.bgColor);
        if(intersects[0] && intersects[0].object) {
            intersects[0].object.material[0].color = new THREE.Color(0xffaa00);
            this.previousObj = intersects[0].object; //previousObj保存悬浮的对象,鼠标移开后恢复颜色。
        }
    }

    主要代码部分就是这样,我们也可以在颜色改变时加入一些渐变动画,three.js可以写出各种各样的地图,这是入门级的版本,希望给萌新一些启发。

    转载请注明地址:郭先生的博客

  • 相关阅读:
    61组第二次团队作业
    饮水机电路图工作原理及电路图分析
    七种基础模拟电路的用法
    C语言判断一个数是奇数还是偶数
    F28335 UART串口配置及数据发送
    F28335 SPI配置及收发数据
    电容的九大功能
    查询接口---flask+python+mysql
    python参数传递
    java基础
  • 原文地址:https://www.cnblogs.com/vadim-web/p/13320838.html
Copyright © 2020-2023  润新知