ZhangXianQiang
2024-04-18 ad36d7964d99f69ce299a33130920bdf3a5a1197
feat:添加射线检测
2个文件已修改
69 ■■■■■ 已修改文件
src/views/screen/components/screen-map-three/experience/world/map.js 66 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
vue.config.js 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/screen/components/screen-map-three/experience/world/map.js
@@ -4,14 +4,21 @@
import textureMapImage from '@/assets/map/texture/gz-map.jpg';
import textureMapFxImage from '@/assets/map/texture/gz-map-fx.jpg';
import { BufferGeometryUtils } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
import gsap from 'gsap';
// 地图深度
const MAP_DEPTH = 0.2;
const projection = d3.geoMercator().center([104.779307, 29.33924]).translate([0, 0, 0]);
const raycaster = new THREE.Raycaster();
export default class Map {
    constructor(experience) {
        this.experience = experience;
        this.scene = this.experience.scene;
        this.camera = this.experience.camera;
        this.material = null;
        this.textureLoader = new THREE.TextureLoader();
        this.setTexture();
@@ -59,7 +66,6 @@
            // 多个情况
            // console.log(feature.geometry.type);
            if (feature.geometry.type === "MultiPolygon") {
                console.log(feature.geometry.coordinates);
                feature.geometry.coordinates.forEach((coordinate) => {
                    coordinate.forEach((rows) => {
                        const line = this.drawBoundary(rows);
@@ -79,14 +85,19 @@
                    province.add(mesh);
                });
            }
            // 合并成一个mesh
            // const mergedGeometry = BufferGeometryUtils.mergeGeometries(province.children);
            this.map.add(province);
        });
        this.map.position.set(1, 1, -1.5);
        this.map.scale.set(10, 10, 10);
        this.map.rotation.set(THREE.MathUtils.degToRad(-90), 0, THREE.MathUtils.degToRad(20));
        this.container = new THREE.Object3D();
        this.container.add(this.map);
        this.scene.add(this.container);
        // this.container = new THREE.Object3D();
        // this.container.add(this.map);
        this.scene.add(this.map);
        console.log(this.map);
        this.setMouseEvent();
    }
    /**
@@ -136,4 +147,51 @@
            this.sideMaterial,
        ]);
    }
    setMouseEvent() {
        this.mouseEvent = this.handleEvent.bind(this);
        // this.experience.canvas.addEventListener("mousemove", this.throttle(this.mouseEvent,100));
        this.experience.canvas.addEventListener("mousemove", this.mouseEvent);
    }
    removeMouseEvent() {
        this.experience.canvas.removeEventListener("mousemove", this.mouseEvent);
    }
    handleEvent(e) {
        // console.log(this);
        // return;
        if (this.map) {
            const mouse = new THREE.Vector2();
            mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
            mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;
            raycaster.setFromCamera(mouse, this.camera.instance);
            const intersects = raycaster.intersectObjects(this.map.children, true);
            if (intersects.length) {
                intersects.forEach((item) => {
                    if (item.object)
                        if (item.object.meshName === 'province') {
                            console.log(item.object);
                            this.animation(item.object);
                        }
                });
            }
        }
    }
    throttle(callback, delay) {
        let lastCall = 0;
        return function () {
            let now = new Date().getTime();
            if (now - lastCall >= delay) {
                lastCall = now;
                callback.apply(null, arguments);
            }
        };
    }
    animation(mesh) {
        console.log(mesh);
    }
}
vue.config.js
@@ -35,7 +35,8 @@
    proxy: {
      // detail: https://cli.vuejs.org/config/#devserver-proxy
      [process.env.VUE_APP_BASE_API]: {
        target: `http://localhost:8080`,
        // target: `http://localhost:8080`,
        target: `http://192.168.3.87:8080`,
        changeOrigin: true,
        pathRewrite: {
          ['^' + process.env.VUE_APP_BASE_API]: ''