three-earth-3d 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.en.md +303 -0
- package/README.md +303 -0
- package/bin/copy-assets.mjs +61 -0
- package/dist/three-earth-3d.es.js +8076 -0
- package/dist/three-earth-3d.umd.js +72 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/ts/Utils/Sizes.d.ts +33 -0
- package/dist/types/ts/Utils/arc.d.ts +11 -0
- package/dist/types/ts/Utils/common.d.ts +34 -0
- package/dist/types/ts/index.d.ts +1 -0
- package/dist/types/ts/interfaces/IEvents.d.ts +3 -0
- package/dist/types/ts/interfaces/IWord.d.ts +62 -0
- package/dist/types/ts/world/Assets.d.ts +16 -0
- package/dist/types/ts/world/Basic.d.ts +15 -0
- package/dist/types/ts/world/Data.d.ts +13 -0
- package/dist/types/ts/world/Earth.d.ts +100 -0
- package/dist/types/ts/world/Resources.d.ts +13 -0
- package/dist/types/ts/world/Word.d.ts +29 -0
- package/package.json +56 -0
- package/static/images/earth/aircraft.png +0 -0
- package/static/images/earth/aperture.png +0 -0
- package/static/images/earth/earth.jpg +0 -0
- package/static/images/earth/glow.png +0 -0
- package/static/images/earth/gradient.png +0 -0
- package/static/images/earth/label-old.png +0 -0
- package/static/images/earth/label.png +0 -0
- package/static/images/earth/light_column.png +0 -0
- package/static/images/earth/redCircle.png +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 GhostCat
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.en.md
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
# three-earth-3d
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/three-earth-3d)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
|
|
6
|
+
A 3D Earth visualization component built with [Three.js](https://threejs.org/), mountable to any DOM element.
|
|
7
|
+
|
|
8
|
+
> This project is a refactored version of [GhostCatcg/3d-earth](https://github.com/GhostCatcg/3d-earth). It migrates the build system to Vite and publishes the project as an npm package, enabling multiple independent 3D Earth instances to be created in any frontend project.
|
|
9
|
+
|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
**[中文 README](./README.md)**
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- 🌏 Earth texture with animated scan-light shader
|
|
19
|
+
- ✨ Glow and atmosphere effects
|
|
20
|
+
- 🛰 Orbiting satellite rings
|
|
21
|
+
- 🇨🇳 City markers + city labels (rendered via html2canvas)
|
|
22
|
+
- 🪐 Animated arc fly-lines between cities
|
|
23
|
+
- 🎛 Fully configurable options and custom data
|
|
24
|
+
- 🔧 `dispose()` method for clean instance teardown
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# pnpm
|
|
32
|
+
pnpm add three-earth-3d three
|
|
33
|
+
|
|
34
|
+
# npm
|
|
35
|
+
npm install three-earth-3d three
|
|
36
|
+
|
|
37
|
+
# yarn
|
|
38
|
+
yarn add three-earth-3d three
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
> `three` is a **peerDependency** and must be installed separately. Requires `three >= 0.130.0`.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Quick Start
|
|
46
|
+
|
|
47
|
+
### 1. Set up static assets
|
|
48
|
+
|
|
49
|
+
A built-in CLI tool copies all textures to your project's `public/` directory in **one command**:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Copy to ./public/images/earth/ (default)
|
|
53
|
+
npx three-earth-3d
|
|
54
|
+
|
|
55
|
+
# Or specify a custom destination (e.g. Nuxt / SvelteKit's static/)
|
|
56
|
+
npx three-earth-3d --dest static
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Resulting structure:
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
public/
|
|
63
|
+
└── images/
|
|
64
|
+
└── earth/
|
|
65
|
+
├── earth.jpg ← Earth texture map (~2.4 MB)
|
|
66
|
+
├── glow.png
|
|
67
|
+
├── gradient.png
|
|
68
|
+
├── aperture.png
|
|
69
|
+
├── label.png
|
|
70
|
+
├── light_column.png
|
|
71
|
+
├── redCircle.png
|
|
72
|
+
└── aircraft.png
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
> **Why not bundle images into the JS?**
|
|
76
|
+
> `earth.jpg` is ~2.4 MB. Inlining it as base64 would inflate the bundle to ~3 MB, which is unacceptable.
|
|
77
|
+
> Serving images separately allows browser caching and makes it easy to swap in a custom Earth texture.
|
|
78
|
+
|
|
79
|
+
### 2. HTML markup
|
|
80
|
+
|
|
81
|
+
The container must have an explicit width and height — the Earth will fill it:
|
|
82
|
+
|
|
83
|
+
```html
|
|
84
|
+
<div id="earth-canvas" style="width: 100%; height: 100vh; background: #010826;"></div>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 3. Initialize
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { World } from 'three-earth-3d'
|
|
91
|
+
|
|
92
|
+
const world = new World({
|
|
93
|
+
dom: document.getElementById('earth-canvas'),
|
|
94
|
+
// Base path where images are hosted (parent of /images/earth/)
|
|
95
|
+
// e.g. if images are at /public/images/earth/, set baseUrl to '' or '/'
|
|
96
|
+
baseUrl: '',
|
|
97
|
+
onReady() {
|
|
98
|
+
console.log('Earth is ready!')
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 4. Destroy
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
world.dispose()
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## Full Options
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { World } from 'three-earth-3d'
|
|
115
|
+
|
|
116
|
+
const world = new World({
|
|
117
|
+
/** Required: target DOM element */
|
|
118
|
+
dom: document.getElementById('earth-canvas'),
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Base URL for static image assets.
|
|
122
|
+
* Images are loaded from `${baseUrl}/images/earth/`.
|
|
123
|
+
* Default: '' (loads from /images/earth/ at site root)
|
|
124
|
+
*/
|
|
125
|
+
baseUrl: '',
|
|
126
|
+
|
|
127
|
+
/** Fired after the Earth is initialized and the entrance animation begins */
|
|
128
|
+
onReady: () => { console.log('ready') },
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Custom route data. Omit to use the built-in demo data.
|
|
132
|
+
* Each entry has one start point and multiple end points.
|
|
133
|
+
*/
|
|
134
|
+
data: [
|
|
135
|
+
{
|
|
136
|
+
startArray: { name: 'Beijing', E: 116.322056, N: 39.89491 },
|
|
137
|
+
endArray: [
|
|
138
|
+
{ name: 'Shanghai', E: 121.473701, N: 31.230416 },
|
|
139
|
+
{ name: 'Guangzhou', E: 113.264385, N: 23.129112 },
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
|
|
144
|
+
/** Earth settings */
|
|
145
|
+
earth: {
|
|
146
|
+
radius: 50, // Earth radius. Default: 50
|
|
147
|
+
rotateSpeed: 0.002, // Auto-rotation speed. Default: 0.002
|
|
148
|
+
isRotation: true, // Enable auto-rotation. Default: true
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
/** Satellite settings */
|
|
152
|
+
satellite: {
|
|
153
|
+
show: true, // Show satellites. Default: true
|
|
154
|
+
rotateSpeed: -0.01, // Orbit rotation speed. Default: -0.01
|
|
155
|
+
size: 1, // Satellite sphere size. Default: 1
|
|
156
|
+
number: 2, // Satellites per ring. Default: 2
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
/** Marker and light-pillar settings */
|
|
160
|
+
punctuation: {
|
|
161
|
+
circleColor: 0x3892ff, // Base ring color
|
|
162
|
+
lightColumn: {
|
|
163
|
+
startColor: 0xffffff, // Start-point pillar color
|
|
164
|
+
endColor: 0xff0000, // End-point pillar color
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
/** Fly-line settings */
|
|
169
|
+
flyLine: {
|
|
170
|
+
color: 0xd18547, // Arc track line color
|
|
171
|
+
speed: 0.004, // Fly-line trailing speed
|
|
172
|
+
flyLineColor: 0xff7714, // Fly-line dot color
|
|
173
|
+
},
|
|
174
|
+
})
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## API
|
|
180
|
+
|
|
181
|
+
### `new World(options: IWord)`
|
|
182
|
+
|
|
183
|
+
Creates a 3D Earth instance and mounts it to the specified DOM element.
|
|
184
|
+
|
|
185
|
+
### `world.dispose()`
|
|
186
|
+
|
|
187
|
+
Destroys the instance: releases the WebGL context, stops the animation loop, removes DOM elements, and cleans up event listeners. Always call this when navigating away in SPAs.
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## Framework Integration
|
|
192
|
+
|
|
193
|
+
### Vue 3
|
|
194
|
+
|
|
195
|
+
```vue
|
|
196
|
+
<template>
|
|
197
|
+
<div ref="earthEl" class="earth-container" />
|
|
198
|
+
</template>
|
|
199
|
+
|
|
200
|
+
<script setup lang="ts">
|
|
201
|
+
import { ref, onMounted, onBeforeUnmount } from 'vue'
|
|
202
|
+
import { World } from 'three-earth-3d'
|
|
203
|
+
|
|
204
|
+
const earthEl = ref<HTMLElement | null>(null)
|
|
205
|
+
let world: World | null = null
|
|
206
|
+
|
|
207
|
+
onMounted(() => {
|
|
208
|
+
world = new World({
|
|
209
|
+
dom: earthEl.value!,
|
|
210
|
+
baseUrl: '',
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
onBeforeUnmount(() => {
|
|
215
|
+
world?.dispose()
|
|
216
|
+
})
|
|
217
|
+
</script>
|
|
218
|
+
|
|
219
|
+
<style scoped>
|
|
220
|
+
.earth-container {
|
|
221
|
+
width: 100%;
|
|
222
|
+
height: 100vh;
|
|
223
|
+
background: #010826;
|
|
224
|
+
}
|
|
225
|
+
</style>
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### React
|
|
229
|
+
|
|
230
|
+
```tsx
|
|
231
|
+
import { useEffect, useRef } from 'react'
|
|
232
|
+
import { World } from 'three-earth-3d'
|
|
233
|
+
|
|
234
|
+
export default function EarthView() {
|
|
235
|
+
const containerRef = useRef<HTMLDivElement>(null)
|
|
236
|
+
|
|
237
|
+
useEffect(() => {
|
|
238
|
+
const world = new World({
|
|
239
|
+
dom: containerRef.current!,
|
|
240
|
+
baseUrl: '',
|
|
241
|
+
})
|
|
242
|
+
return () => world.dispose()
|
|
243
|
+
}, [])
|
|
244
|
+
|
|
245
|
+
return (
|
|
246
|
+
<div
|
|
247
|
+
ref={containerRef}
|
|
248
|
+
style={{ width: '100%', height: '100vh', background: '#010826' }}
|
|
249
|
+
/>
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## Local Development
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
# Clone
|
|
260
|
+
git clone https://github.com/adoin/three-earth-3d.git
|
|
261
|
+
cd three-earth-3d
|
|
262
|
+
|
|
263
|
+
# Install dependencies
|
|
264
|
+
pnpm install
|
|
265
|
+
|
|
266
|
+
# Start dev server (http://localhost:5173)
|
|
267
|
+
pnpm dev
|
|
268
|
+
|
|
269
|
+
# Build the library (outputs to dist/)
|
|
270
|
+
pnpm build
|
|
271
|
+
|
|
272
|
+
# Build demo and preview production output (http://localhost:4173)
|
|
273
|
+
pnpm preview
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
---
|
|
277
|
+
|
|
278
|
+
## Notes
|
|
279
|
+
|
|
280
|
+
1. **Static assets**: The package does not bundle images. Copy `static/images/earth/` to your `public` folder or CDN and point `baseUrl` to their parent path.
|
|
281
|
+
|
|
282
|
+
2. **Container dimensions**: The mount element must have a defined width and height, otherwise the render size is 0.
|
|
283
|
+
|
|
284
|
+
3. **Multiple instances**: Each `new World()` creates an independent WebGL context. Browsers typically allow 8–16 simultaneous WebGL contexts. Call `dispose()` on instances that are no longer needed.
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
## Credits
|
|
289
|
+
|
|
290
|
+
This project is based on [GhostCatcg/3d-earth](https://github.com/GhostCatcg/3d-earth). Many thanks to the original author **GhostCat** for the creative work and implementation.
|
|
291
|
+
|
|
292
|
+
Key changes in this fork:
|
|
293
|
+
- Build tool migrated from **Webpack** to **Vite** with library mode output
|
|
294
|
+
- Published as an **npm package** — instantiate via `new World({ dom })`
|
|
295
|
+
- Asset paths changed from hardcoded to configurable via `baseUrl`
|
|
296
|
+
- Renderer size now derived from `dom.offsetWidth/Height` instead of the global `window`
|
|
297
|
+
- Added `onReady` callback, `dispose()` method, and full TypeScript type exports
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
## License
|
|
302
|
+
|
|
303
|
+
[MIT](./LICENSE)
|
package/README.md
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
# three-earth-3d
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/three-earth-3d)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
|
|
6
|
+
基于 [Three.js](https://threejs.org/) 构建的 3D 地球可视化组件,可挂载到任意 DOM 元素上。
|
|
7
|
+
|
|
8
|
+
> 本项目由 [GhostCatcg/3d-earth](https://github.com/GhostCatcg/3d-earth) 改造而来,在其基础上迁移至 Vite 构建体系,并以 npm 包的形式发布,支持在任意前端项目中实例化多个独立的 3D 地球。
|
|
9
|
+
|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
**[English README](./README.en.md)**
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 功能特性
|
|
17
|
+
|
|
18
|
+
- 🌏 地球贴图 + 扫光 Shader 动画
|
|
19
|
+
- ✨ 辉光、大气层效果
|
|
20
|
+
- 🛰 卫星轨道环绕
|
|
21
|
+
- 🇨🇳 城市标点 + 城市标签(html2canvas 渲染)
|
|
22
|
+
- 🪐 城市间弧线飞线
|
|
23
|
+
- 🎛 全部参数可配置,支持自定义数据
|
|
24
|
+
- 🔧 提供 `dispose()` 方法,干净销毁实例
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## 安装
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# pnpm
|
|
32
|
+
pnpm add three-earth-3d three
|
|
33
|
+
|
|
34
|
+
# npm
|
|
35
|
+
npm install three-earth-3d three
|
|
36
|
+
|
|
37
|
+
# yarn
|
|
38
|
+
yarn add three-earth-3d three
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
> `three` 为 **peerDependency**,需要单独安装,支持 `three >= 0.130.0`。
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## 快速上手
|
|
46
|
+
|
|
47
|
+
### 1. 准备静态资源
|
|
48
|
+
|
|
49
|
+
包内提供了一个 CLI 工具,**一条命令**即可将所有贴图复制到你项目的 `public/` 目录:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# 复制到 ./public/images/earth/(默认)
|
|
53
|
+
npx three-earth-3d
|
|
54
|
+
|
|
55
|
+
# 或指定目标目录(例如 Nuxt / SvelteKit 的 static/)
|
|
56
|
+
npx three-earth-3d --dest static
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
执行后目录结构如下:
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
public/
|
|
63
|
+
└── images/
|
|
64
|
+
└── earth/
|
|
65
|
+
├── earth.jpg ← 地球贴图(主要素材,约 2.4 MB)
|
|
66
|
+
├── glow.png
|
|
67
|
+
├── gradient.png
|
|
68
|
+
├── aperture.png
|
|
69
|
+
├── label.png
|
|
70
|
+
├── light_column.png
|
|
71
|
+
├── redCircle.png
|
|
72
|
+
└── aircraft.png
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
> **为什么不内置到 JS 里?**
|
|
76
|
+
> `earth.jpg` 约 2.4 MB,base64 内联后会让 bundle 膨胀到 ~3 MB,得不偿失。
|
|
77
|
+
> 独立部署贴图可以被浏览器缓存,也更容易替换成自定义地球图。
|
|
78
|
+
|
|
79
|
+
### 2. HTML 结构
|
|
80
|
+
|
|
81
|
+
容器需要有明确的宽高,地球会撑满容器:
|
|
82
|
+
|
|
83
|
+
```html
|
|
84
|
+
<div id="earth-canvas" style="width: 100%; height: 100vh; background: #010826;"></div>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 3. 初始化
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { World } from 'three-earth-3d'
|
|
91
|
+
|
|
92
|
+
const world = new World({
|
|
93
|
+
dom: document.getElementById('earth-canvas'),
|
|
94
|
+
// 图片所在的基础路径(相对或绝对),对应 /images/earth/ 的上级
|
|
95
|
+
// 例如图片放在 /public/images/earth/,baseUrl 填 '' 或 '/'
|
|
96
|
+
baseUrl: '',
|
|
97
|
+
onReady() {
|
|
98
|
+
console.log('地球加载完成!')
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 4. 销毁
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
world.dispose()
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## 完整配置项
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { World } from 'three-earth-3d'
|
|
115
|
+
|
|
116
|
+
const world = new World({
|
|
117
|
+
/** 必填:挂载的 DOM 元素 */
|
|
118
|
+
dom: document.getElementById('earth-canvas'),
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 静态图片资源根路径
|
|
122
|
+
* 组件会在 `${baseUrl}/images/earth/` 下加载图片
|
|
123
|
+
* 默认值:''(即从网站根路径 /images/earth/ 加载)
|
|
124
|
+
*/
|
|
125
|
+
baseUrl: '',
|
|
126
|
+
|
|
127
|
+
/** 地球初始化并开始入场动画后触发 */
|
|
128
|
+
onReady: () => { console.log('ready') },
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 自定义航线数据,不传则使用内置示例数据(北京/杭州为起点的航线)
|
|
132
|
+
* 每一条记录包含一个起点和多个终点
|
|
133
|
+
*/
|
|
134
|
+
data: [
|
|
135
|
+
{
|
|
136
|
+
startArray: { name: '北京', E: 116.322056, N: 39.89491 },
|
|
137
|
+
endArray: [
|
|
138
|
+
{ name: '上海', E: 121.473701, N: 31.230416 },
|
|
139
|
+
{ name: '广州', E: 113.264385, N: 23.129112 },
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
|
|
144
|
+
/** 地球参数 */
|
|
145
|
+
earth: {
|
|
146
|
+
radius: 50, // 地球半径,默认 50
|
|
147
|
+
rotateSpeed: 0.002, // 自转速度,默认 0.002
|
|
148
|
+
isRotation: true, // 是否自转,默认 true
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
/** 卫星参数 */
|
|
152
|
+
satellite: {
|
|
153
|
+
show: true, // 是否显示卫星,默认 true
|
|
154
|
+
rotateSpeed: -0.01, // 轨道旋转速度,默认 -0.01
|
|
155
|
+
size: 1, // 卫星球体大小,默认 1
|
|
156
|
+
number: 2, // 每条轨道的卫星数量,默认 2
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
/** 标点和光柱参数 */
|
|
160
|
+
punctuation: {
|
|
161
|
+
circleColor: 0x3892ff, // 底座圆圈颜色
|
|
162
|
+
lightColumn: {
|
|
163
|
+
startColor: 0xffffff, // 起点光柱颜色
|
|
164
|
+
endColor: 0xff0000, // 终点光柱颜色
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
/** 飞线参数 */
|
|
169
|
+
flyLine: {
|
|
170
|
+
color: 0xd18547, // 轨迹线颜色
|
|
171
|
+
speed: 0.004, // 飞线拖尾运动速度
|
|
172
|
+
flyLineColor: 0xff7714, // 飞线自身颜色
|
|
173
|
+
},
|
|
174
|
+
})
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## API
|
|
180
|
+
|
|
181
|
+
### `new World(options: IWord)`
|
|
182
|
+
|
|
183
|
+
创建一个 3D 地球实例并挂载到指定 DOM。
|
|
184
|
+
|
|
185
|
+
### `world.dispose()`
|
|
186
|
+
|
|
187
|
+
销毁实例,释放 WebGL 上下文、停止动画循环、移除 DOM 元素及监听器。在 SPA 框架中切换路由时务必调用。
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## 在框架中使用
|
|
192
|
+
|
|
193
|
+
### Vue 3
|
|
194
|
+
|
|
195
|
+
```vue
|
|
196
|
+
<template>
|
|
197
|
+
<div ref="earthEl" class="earth-container" />
|
|
198
|
+
</template>
|
|
199
|
+
|
|
200
|
+
<script setup lang="ts">
|
|
201
|
+
import { ref, onMounted, onBeforeUnmount } from 'vue'
|
|
202
|
+
import { World } from 'three-earth-3d'
|
|
203
|
+
|
|
204
|
+
const earthEl = ref<HTMLElement | null>(null)
|
|
205
|
+
let world: World | null = null
|
|
206
|
+
|
|
207
|
+
onMounted(() => {
|
|
208
|
+
world = new World({
|
|
209
|
+
dom: earthEl.value!,
|
|
210
|
+
baseUrl: '',
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
onBeforeUnmount(() => {
|
|
215
|
+
world?.dispose()
|
|
216
|
+
})
|
|
217
|
+
</script>
|
|
218
|
+
|
|
219
|
+
<style scoped>
|
|
220
|
+
.earth-container {
|
|
221
|
+
width: 100%;
|
|
222
|
+
height: 100vh;
|
|
223
|
+
background: #010826;
|
|
224
|
+
}
|
|
225
|
+
</style>
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### React
|
|
229
|
+
|
|
230
|
+
```tsx
|
|
231
|
+
import { useEffect, useRef } from 'react'
|
|
232
|
+
import { World } from 'three-earth-3d'
|
|
233
|
+
|
|
234
|
+
export default function EarthView() {
|
|
235
|
+
const containerRef = useRef<HTMLDivElement>(null)
|
|
236
|
+
|
|
237
|
+
useEffect(() => {
|
|
238
|
+
const world = new World({
|
|
239
|
+
dom: containerRef.current!,
|
|
240
|
+
baseUrl: '',
|
|
241
|
+
})
|
|
242
|
+
return () => world.dispose()
|
|
243
|
+
}, [])
|
|
244
|
+
|
|
245
|
+
return (
|
|
246
|
+
<div
|
|
247
|
+
ref={containerRef}
|
|
248
|
+
style={{ width: '100%', height: '100vh', background: '#010826' }}
|
|
249
|
+
/>
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## 本地开发
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
# 克隆
|
|
260
|
+
git clone https://github.com/adoin/three-earth-3d.git
|
|
261
|
+
cd three-earth-3d
|
|
262
|
+
|
|
263
|
+
# 安装依赖
|
|
264
|
+
pnpm install
|
|
265
|
+
|
|
266
|
+
# 启动开发服务器(访问 http://localhost:5173)
|
|
267
|
+
pnpm dev
|
|
268
|
+
|
|
269
|
+
# 构建库产物(输出到 dist/)
|
|
270
|
+
pnpm build
|
|
271
|
+
|
|
272
|
+
# 构建 demo 并预览生产效果(访问 http://localhost:4173)
|
|
273
|
+
pnpm preview
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
---
|
|
277
|
+
|
|
278
|
+
## 注意事项
|
|
279
|
+
|
|
280
|
+
1. **图片资源**:组件本身不内置图片,需要将 `static/images/earth/` 下的素材部署到你的 public 或 CDN,并通过 `baseUrl` 指向其上级路径。
|
|
281
|
+
|
|
282
|
+
2. **容器尺寸**:挂载的 DOM 必须有确定的宽高,否则渲染尺寸为 0。
|
|
283
|
+
|
|
284
|
+
3. **多实例**:每个 `new World()` 会创建独立的 WebGL 上下文,注意浏览器对同时存在的 WebGL 上下文数量有限制(通常 8~16 个),请在不需要时及时调用 `dispose()`。
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
## 致谢
|
|
289
|
+
|
|
290
|
+
本项目基于 [GhostCatcg/3d-earth](https://github.com/GhostCatcg/3d-earth) 改造,感谢原作者 **GhostCat** 的创意与实现。
|
|
291
|
+
|
|
292
|
+
主要改动:
|
|
293
|
+
- 构建工具从 Webpack 迁移至 **Vite**,支持库模式打包
|
|
294
|
+
- 以 **npm 包**形式发布,可在任意项目中通过 `new World({ dom })` 实例化
|
|
295
|
+
- 资源路径由硬编码改为可配置的 `baseUrl`
|
|
296
|
+
- 挂载容器改用 `dom.offsetWidth/Height` 而非全局 `window` 尺寸
|
|
297
|
+
- 新增 `onReady` 回调、`dispose()` 方法及完整 TypeScript 类型导出
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
## License
|
|
302
|
+
|
|
303
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* three-earth-3d copy-assets
|
|
4
|
+
*
|
|
5
|
+
* 将包内的静态贴图资源复制到消费方项目的 public 目录。
|
|
6
|
+
*
|
|
7
|
+
* 用法:
|
|
8
|
+
* npx three-earth-3d → 复制到 ./public/images/earth/
|
|
9
|
+
* npx three-earth-3d --dest static → 复制到 ./static/images/earth/
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import fs from 'fs'
|
|
13
|
+
import path from 'path'
|
|
14
|
+
import { fileURLToPath } from 'url'
|
|
15
|
+
|
|
16
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
17
|
+
|
|
18
|
+
// 解析 --dest 参数,默认 public
|
|
19
|
+
const destArg = (() => {
|
|
20
|
+
const idx = process.argv.indexOf('--dest')
|
|
21
|
+
return idx !== -1 ? process.argv[idx + 1] : 'public'
|
|
22
|
+
})()
|
|
23
|
+
|
|
24
|
+
const SRC_DIR = path.resolve(__dirname, '../static/images/earth')
|
|
25
|
+
const DEST_DIR = path.resolve(process.cwd(), destArg, 'images/earth')
|
|
26
|
+
|
|
27
|
+
function copyDir(src, dest) {
|
|
28
|
+
fs.mkdirSync(dest, { recursive: true })
|
|
29
|
+
const entries = fs.readdirSync(src, { withFileTypes: true })
|
|
30
|
+
let count = 0
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
const srcPath = path.join(src, entry.name)
|
|
33
|
+
const destPath = path.join(dest, entry.name)
|
|
34
|
+
if (entry.isDirectory()) {
|
|
35
|
+
copyDir(srcPath, destPath)
|
|
36
|
+
} else {
|
|
37
|
+
fs.copyFileSync(srcPath, destPath)
|
|
38
|
+
console.log(` ✔ ${entry.name}`)
|
|
39
|
+
count++
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return count
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
console.log(`\nthree-earth-3d: copying assets`)
|
|
46
|
+
console.log(` from: ${SRC_DIR}`)
|
|
47
|
+
console.log(` to: ${DEST_DIR}\n`)
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
if (!fs.existsSync(SRC_DIR)) {
|
|
51
|
+
console.error('❌ Source assets not found. Please reinstall three-earth-3d.')
|
|
52
|
+
process.exit(1)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const count = copyDir(SRC_DIR, DEST_DIR)
|
|
56
|
+
console.log(`\n✅ Done! ${count} files copied to ${path.relative(process.cwd(), DEST_DIR)}`)
|
|
57
|
+
console.log(`\n Now set baseUrl: '' (or your custom path) when calling new World().\n`)
|
|
58
|
+
} catch (err) {
|
|
59
|
+
console.error('❌ Copy failed:', err.message)
|
|
60
|
+
process.exit(1)
|
|
61
|
+
}
|