|
| 1 | +import { ThreeLayer, ThreeRender } from '@antv/l7-three'; |
| 2 | +import * as THREE from 'three'; |
| 3 | +import type { TestCase } from '../../types'; |
| 4 | +import { CaseScene } from '../../utils'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Three.js 动画效果演示 |
| 8 | + * 展示动态旋转、缩放和波动的 3D 对象 |
| 9 | + */ |
| 10 | +export const threeAnimation: TestCase = async (options) => { |
| 11 | + const scene = await CaseScene({ |
| 12 | + ...options, |
| 13 | + // Three.js r163+ 需要 WebGL2,必须使用 'device' 渲染器 |
| 14 | + renderer: 'device', |
| 15 | + mapConfig: { |
| 16 | + style: 'dark', |
| 17 | + center: [116.4074, 39.9042], |
| 18 | + zoom: 12, |
| 19 | + pitch: 45, |
| 20 | + rotation: 0, |
| 21 | + }, |
| 22 | + }); |
| 23 | + |
| 24 | + scene.registerRenderService(ThreeRender); |
| 25 | + |
| 26 | + const center = scene.getCenter(); |
| 27 | + const animatedObjects: Array<{ |
| 28 | + mesh: THREE.Mesh; |
| 29 | + type: 'rotate' | 'pulse' | 'wave'; |
| 30 | + speed: number; |
| 31 | + initialScale: number; |
| 32 | + }> = []; |
| 33 | + |
| 34 | + const threeJSLayer = new ThreeLayer({ |
| 35 | + enableMultiPassRenderer: false, |
| 36 | + onAddMeshes: (threeScene, layer) => { |
| 37 | + // 环境光照 |
| 38 | + threeScene.add(new THREE.AmbientLight(0xffffff, 0.4)); |
| 39 | + |
| 40 | + // 主光源 |
| 41 | + const mainLight = new THREE.DirectionalLight(0xffffff, 0.8); |
| 42 | + mainLight.position.set(50, 100, 50); |
| 43 | + threeScene.add(mainLight); |
| 44 | + |
| 45 | + // 彩色点光源 |
| 46 | + const colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff]; |
| 47 | + colors.forEach((color, i) => { |
| 48 | + const angle = (i / colors.length) * Math.PI * 2; |
| 49 | + const radius = 0.05; |
| 50 | + const light = new THREE.PointLight(color, 1, 20000); |
| 51 | + layer.setObjectLngLat( |
| 52 | + light, |
| 53 | + [center.lng + Math.cos(angle) * radius, center.lat + Math.sin(angle) * radius], |
| 54 | + 5000, |
| 55 | + ); |
| 56 | + threeScene.add(light); |
| 57 | + }); |
| 58 | + |
| 59 | + // 创建旋转的环 |
| 60 | + for (let i = 0; i < 3; i++) { |
| 61 | + const radius = 2000 + i * 1500; |
| 62 | + const tube = 200; |
| 63 | + const geometry = new THREE.TorusGeometry(radius, tube, 16, 100); |
| 64 | + const material = new THREE.MeshPhongMaterial({ |
| 65 | + color: [0x049ef4, 0x00ff88, 0xff6600][i], |
| 66 | + emissive: [0x049ef4, 0x00ff88, 0xff6600][i], |
| 67 | + emissiveIntensity: 0.3, |
| 68 | + transparent: true, |
| 69 | + opacity: 0.8, |
| 70 | + }); |
| 71 | + const torus = new THREE.Mesh(geometry, material); |
| 72 | + |
| 73 | + // 不同的初始旋转 |
| 74 | + torus.rotation.x = Math.PI / 2 + i * 0.3; |
| 75 | + torus.rotation.y = i * 0.5; |
| 76 | + |
| 77 | + layer.setObjectLngLat(torus, [center.lng, center.lat], 0); |
| 78 | + threeScene.add(torus); |
| 79 | + |
| 80 | + animatedObjects.push({ |
| 81 | + mesh: torus, |
| 82 | + type: 'rotate', |
| 83 | + speed: 0.01 * (i + 1) * (i % 2 === 0 ? 1 : -1), |
| 84 | + initialScale: 1, |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + // 创建脉冲球体 |
| 89 | + for (let i = 0; i < 5; i++) { |
| 90 | + const angle = (i / 5) * Math.PI * 2; |
| 91 | + const distance = 0.03; |
| 92 | + const geometry = new THREE.SphereGeometry(500, 32, 32); |
| 93 | + const material = new THREE.MeshPhongMaterial({ |
| 94 | + color: 0xff3366, |
| 95 | + emissive: 0xff3366, |
| 96 | + emissiveIntensity: 0.4, |
| 97 | + transparent: true, |
| 98 | + opacity: 0.7, |
| 99 | + }); |
| 100 | + const sphere = new THREE.Mesh(geometry, material); |
| 101 | + |
| 102 | + layer.setObjectLngLat( |
| 103 | + sphere, |
| 104 | + [center.lng + Math.cos(angle) * distance, center.lat + Math.sin(angle) * distance], |
| 105 | + 1000, |
| 106 | + ); |
| 107 | + threeScene.add(sphere); |
| 108 | + |
| 109 | + animatedObjects.push({ |
| 110 | + mesh: sphere, |
| 111 | + type: 'pulse', |
| 112 | + speed: 0.02 + i * 0.005, |
| 113 | + initialScale: 1, |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + // 创建波动效果的地表 |
| 118 | + const planeGeometry = new THREE.PlaneGeometry(20000, 20000, 32, 32); |
| 119 | + const planeMaterial = new THREE.MeshPhongMaterial({ |
| 120 | + color: 0x049ef4, |
| 121 | + emissive: 0x0044aa, |
| 122 | + emissiveIntensity: 0.2, |
| 123 | + transparent: true, |
| 124 | + opacity: 0.5, |
| 125 | + wireframe: true, |
| 126 | + side: THREE.DoubleSide, |
| 127 | + }); |
| 128 | + const plane = new THREE.Mesh(planeGeometry, planeMaterial); |
| 129 | + plane.rotation.x = -Math.PI / 2; |
| 130 | + |
| 131 | + // 保存原始顶点位置用于动画 |
| 132 | + const positions = planeGeometry.attributes.position.array as Float32Array; |
| 133 | + plane.userData.originalPositions = new Float32Array(positions); |
| 134 | + |
| 135 | + layer.setObjectLngLat(plane, [center.lng, center.lat], -500); |
| 136 | + threeScene.add(plane); |
| 137 | + |
| 138 | + animatedObjects.push({ |
| 139 | + mesh: plane, |
| 140 | + type: 'wave', |
| 141 | + speed: 0.02, |
| 142 | + initialScale: 1, |
| 143 | + }); |
| 144 | + |
| 145 | + // 创建中心发光核心 |
| 146 | + const coreGeometry = new THREE.IcosahedronGeometry(800, 2); |
| 147 | + const coreMaterial = new THREE.MeshPhongMaterial({ |
| 148 | + color: 0xffffff, |
| 149 | + emissive: 0x049ef4, |
| 150 | + emissiveIntensity: 0.8, |
| 151 | + flatShading: true, |
| 152 | + }); |
| 153 | + const core = new THREE.Mesh(coreGeometry, coreMaterial); |
| 154 | + layer.setObjectLngLat(core, [center.lng, center.lat], 3000); |
| 155 | + threeScene.add(core); |
| 156 | + |
| 157 | + animatedObjects.push({ |
| 158 | + mesh: core, |
| 159 | + type: 'rotate', |
| 160 | + speed: 0.03, |
| 161 | + initialScale: 1, |
| 162 | + }); |
| 163 | + |
| 164 | + // 动画更新 |
| 165 | + const clock = new THREE.Clock(); |
| 166 | + |
| 167 | + const animate = () => { |
| 168 | + const time = clock.getElapsedTime(); |
| 169 | + |
| 170 | + animatedObjects.forEach((obj) => { |
| 171 | + if (obj.type === 'rotate') { |
| 172 | + obj.mesh.rotation.z += obj.speed; |
| 173 | + obj.mesh.rotation.x += obj.speed * 0.5; |
| 174 | + } else if (obj.type === 'pulse') { |
| 175 | + const scale = obj.initialScale + Math.sin(time * 3 + obj.speed * 100) * 0.3; |
| 176 | + obj.mesh.scale.set(scale, scale, scale); |
| 177 | + } else if (obj.type === 'wave') { |
| 178 | + const positions = (obj.mesh.geometry as THREE.PlaneGeometry).attributes.position |
| 179 | + .array as Float32Array; |
| 180 | + const originals = obj.mesh.userData.originalPositions as Float32Array; |
| 181 | + |
| 182 | + for (let i = 0; i < positions.length; i += 3) { |
| 183 | + const x = originals[i]; |
| 184 | + const y = originals[i + 1]; |
| 185 | + positions[i + 2] = |
| 186 | + Math.sin(x * 0.001 + time) * 500 + Math.cos(y * 0.001 + time) * 500; |
| 187 | + } |
| 188 | + |
| 189 | + (obj.mesh.geometry as THREE.PlaneGeometry).attributes.position.needsUpdate = true; |
| 190 | + obj.mesh.geometry.computeVertexNormals(); |
| 191 | + } |
| 192 | + }); |
| 193 | + |
| 194 | + requestAnimationFrame(animate); |
| 195 | + }; |
| 196 | + |
| 197 | + animate(); |
| 198 | + }, |
| 199 | + }).animate(true); |
| 200 | + |
| 201 | + scene.addLayer(threeJSLayer); |
| 202 | + |
| 203 | + return scene; |
| 204 | +}; |
0 commit comments