| | |
| | | |
| | | 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(); |
| | |
| | | // 多个情况 |
| | | // 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); |
| | |
| | | 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(); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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); |
| | | } |
| | | } |