1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 | import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader'
import Stats from 'three/examples/jsm/libs/stats.module'
import JEASINGS from 'jeasings'
const elem = document.getElementById('locationData') as HTMLDivElement
const coordinate = { lat: -41.5, lon: 146.5 }
const scene = new THREE.Scene()
new RGBELoader().load('img/kloppenheim_06_puresky_1k.hdr', function (texture) {
texture.mapping = THREE.EquirectangularReflectionMapping
scene.background = texture
scene.environment = texture
})
const camera = new THREE.PerspectiveCamera(55, window.innerWidth / window.innerHeight, 0.1, 1000)
camera.position.set(0, 60, 100)
const renderer = new THREE.WebGLRenderer({
antialias: true,
logarithmicDepthBuffer: true,
})
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
controls.maxPolarAngle = Math.PI / 2.1
controls.maxDistance = 200
controls.minDistance = 1
controls.target.set(8, 2, 28)
const geometry = new THREE.PlaneGeometry(180, 180, 720, 720).rotateX(-Math.PI * 0.5)
const material = new THREE.MeshStandardMaterial({})
const ground = new THREE.Mesh(geometry, material)
scene.add(ground)
const textureLoader = new THREE.TextureLoader()
const texture = textureLoader.load('img/world.topo.bathy_tasmania.jpg')
material.map = texture
material.flatShading = true
textureLoader.load('img/gebco_tasmania.png', function (texture) {
/* Displacing in the Vertex Shader */
// material.displacementMap = texture
// material.displacementScale = 100
// material.displacementBias = -50
/* End Displacing in the Vertex Shader */
/* Displacing in the Javascript Layer */
const canvas = document.createElement('canvas') as HTMLCanvasElement
canvas.width = texture.image.width
canvas.height = texture.image.height
const context = canvas.getContext('2d') as CanvasRenderingContext2D
context.drawImage(texture.image, 0, 0)
const width = geometry.parameters.widthSegments + 1
const height = geometry.parameters.heightSegments + 1
const widthStep = texture.image.width / width
const heightStep = texture.image.height / height
const data = context.getImageData(0, 0, texture.image.width, texture.image.height)
const positions = geometry.attributes.position.array
let w, h, x, y
for (let i = 0; i < positions.length; i += 3) {
w = (i / 3) % width
h = i / 3 / width
x = Math.round(w * widthStep)
y = Math.round(h * heightStep)
const displacement = data.data[x * 4 + y * 4 * texture.image.width]
positions[i + 1] = displacement / 2.55 - 50
}
geometry.attributes.position.needsUpdate = true
geometry.computeVertexNormals()
/* End Displacing in the Javascript Layer */
})
const seaPlane = new THREE.Mesh(
new THREE.PlaneGeometry(180, 180, 1, 1),
new THREE.MeshStandardMaterial({
transparent: true,
opacity: 0.25,
metalness: 0.99,
roughness: 0.01,
})
)
seaPlane.rotateX(-Math.PI / 2)
scene.add(seaPlane)
const points = []
points.push(new THREE.Vector3(-90, 0, 0))
points.push(new THREE.Vector3(90, 0, 0))
const latLine = new THREE.Line(new THREE.BufferGeometry().setFromPoints(points), new THREE.LineBasicMaterial({ color: 0x00ff00 }))
scene.add(latLine)
const lonLine = latLine.clone()
lonLine.rotateY(Math.PI / 2)
scene.add(lonLine)
const altLine = latLine.clone()
altLine.rotateZ(Math.PI / 2)
scene.add(altLine)
const mouse = new THREE.Vector2()
const raycaster = new THREE.Raycaster()
function onDoubleClick(event: MouseEvent) {
mouse.set((event.clientX / renderer.domElement.clientWidth) * 2 - 1, -(event.clientY / renderer.domElement.clientHeight) * 2 + 1)
raycaster.setFromCamera(mouse, camera)
const intersects = raycaster.intersectObject(ground, false)
if (intersects.length > 0) {
const { point, uv } = intersects[0]
new JEASINGS.JEasing(controls.target)
.to(
{
x: point.x,
y: point.y,
z: point.z,
},
500
)
.easing(JEASINGS.Cubic.Out)
.start()
new JEASINGS.JEasing(latLine.position)
.to(
{
y: point.y,
z: point.z,
},
500
)
.easing(JEASINGS.Cubic.Out)
.start()
new JEASINGS.JEasing(lonLine.position)
.to(
{
x: point.x,
y: point.y,
},
500
)
.easing(JEASINGS.Cubic.Out)
.start()
new JEASINGS.JEasing(altLine.position)
.to(
{
x: point.x,
y: point.y,
z: point.z,
},
500
)
.easing(JEASINGS.Cubic.Out)
.start()
new JEASINGS.JEasing(coordinate)
.to(
{
lat: -38.0 - (1 - uv.y) * 7,
lon: 143.0 + uv.x * 7,
},
500
)
.start()
.onUpdate(function () {
elem.innerText = coordinate.lat.toFixed(6) + ' ' + coordinate.lon.toFixed(6)
})
}
}
renderer.domElement.addEventListener('dblclick', onDoubleClick, false)
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
render()
}
window.addEventListener('resize', onWindowResize, false)
const stats = new Stats()
document.body.appendChild(stats.dom)
function animate() {
requestAnimationFrame(animate)
controls.update()
JEASINGS.update()
render()
stats.update()
}
function render() {
renderer.render(scene, camera)
}
animate()
|