-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerrainGenerator.tsx
156 lines (125 loc) · 5.14 KB
/
TerrainGenerator.tsx
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
'use client'
import { location } from "./GlobeControls"
import { useEffect, useRef } from "react"
import { BufferAttribute, DoubleSide, Mesh } from "three"
import { radius, scale } from "@/lib/planet"
import SphericalMercator from "@mapbox/sphericalmercator"
import { useFrame } from "@react-three/fiber"
import { FROM_CLIENT_GET_TERRAIN, FROM_SERVER_SEND_TERRAIN, OnMessageInClient } from "@/lib/game"
import { socket } from "./Client"
import { messageEmitter } from "@/lib/client"
import { HeightmapType, terrainSize, terrainZoom } from "@/lib/terrain"
let currentTileX = -1
let currentTileY = -1
var merc = new SphericalMercator({
size: 256,
antimeridian: true
})
export default function TerrainGenerator() {
const meshRef = useRef<Mesh>(null!)
const vertices = useRef(new Float32Array((terrainSize + 1 - 2) ** 2 * 2 * 3 * 3))
const applyMesh = () => {
const { geometry } = meshRef.current
const { position } = geometry.attributes
position.array.set(vertices.current)
position.needsUpdate = true
geometry.computeVertexNormals()
}
function applyTile(heightmap: HeightmapType, tileX: number, tileY: number) {
for (let x = 0; x <= terrainSize - 2; x++) {
for (let y = 0; y <= terrainSize - 2; y++) {
const i = (y * (terrainSize - 1) + x) * 2 * 3 * 3
const isEmpty =
heightmap[y + 1][x] === null
|| heightmap[y + 1][x + 1] === null
|| heightmap[y][x + 1] === null
|| heightmap[y][x] === null
if (isEmpty) {
vertices.current[i] =
vertices.current[i + 1] =
vertices.current[i + 2] =
vertices.current[i + 3] =
vertices.current[i + 4] =
vertices.current[i + 5] =
vertices.current[i + 6] =
vertices.current[i + 7] =
vertices.current[i + 8] =
vertices.current[i + 9] =
vertices.current[i + 10] =
vertices.current[i + 11] =
vertices.current[i + 12] =
vertices.current[i + 13] =
vertices.current[i + 14] =
vertices.current[i + 15] =
vertices.current[i + 16] =
vertices.current[i + 17] = 0
} else {
const h0 = (radius + heightmap[y + 1][x]!) / radius
const h1 = (radius + heightmap[y + 1][x + 1]!) / radius
const h2 = (radius + heightmap[y][x + 1]!) / radius
const h3 = (radius + heightmap[y][x]!) / radius
const [lon0, lat0] = merc.ll([tileX * 256 + x, tileY * 256 + y], terrainZoom)
const [lon1, lat1] = merc.ll([tileX * 256 + x + 1, tileY * 256 + y + 1], terrainZoom)
const a0 = lon0 * Math.PI / 180
const a1 = lon1 * Math.PI / 180
const b0 = lat0 * Math.PI / 180
const b1 = lat1 * Math.PI / 180
vertices.current[i] =
vertices.current[i + 15] = Math.cos(a0) * Math.cos(b1) * h0 * scale
vertices.current[i + 1] =
vertices.current[i + 16] = Math.sin(b1) * h0 * scale
vertices.current[i + 2] =
vertices.current[i + 17] = -Math.sin(a0) * Math.cos(b1) * h0 * scale
vertices.current[i + 3] = Math.cos(a1) * Math.cos(b1) * h1 * scale
vertices.current[i + 4] = Math.sin(b1) * h1 * scale
vertices.current[i + 5] = -Math.sin(a1) * Math.cos(b1) * h1 * scale
vertices.current[i + 6] =
vertices.current[i + 9] = Math.cos(a1) * Math.cos(b0) * h2 * scale
vertices.current[i + 7] =
vertices.current[i + 10] = Math.sin(b0) * h2 * scale
vertices.current[i + 8] =
vertices.current[i + 11] = -Math.sin(a1) * Math.cos(b0) * h2 * scale
vertices.current[i + 12] = Math.cos(a0) * Math.cos(b0) * h3 * scale
vertices.current[i + 13] = Math.sin(b0) * h3 * scale
vertices.current[i + 14] = -Math.sin(a0) * Math.cos(b0) * h3 * scale
}
}
}
applyMesh()
}
useFrame(() => {
if (socket.readyState !== 1) return
let [newTileX, newTileY] = merc.px([location.lon, location.lat], terrainZoom)
.map((value: number) => Math.floor(value / 256))
if (newTileY < 0 || (2 ** terrainZoom <= newTileY)) return
if (newTileX === currentTileX && newTileY === currentTileY) return
currentTileX = newTileX
currentTileY = newTileY
socket.send(JSON.stringify([FROM_CLIENT_GET_TERRAIN, [newTileX, newTileY]]))
})
useEffect(applyMesh)
const onReceiveTerrain: OnMessageInClient = (id, value, ws) => {
switch (id) {
case FROM_SERVER_SEND_TERRAIN:
messageEmitter.isInvalidMessage = false
const [tileX, tileY, heightmap] = value
if (tileX !== currentTileX || tileY !== currentTileY) return
applyTile(heightmap, tileX, tileY)
break
default:
break
}
}
useEffect(() => {
messageEmitter.on('message', onReceiveTerrain)
return () => {
messageEmitter.off('message', onReceiveTerrain)
}
}, [])
return (
<mesh castShadow receiveShadow ref={meshRef}>
<bufferGeometry attributes={{ "position": new BufferAttribute(vertices.current, 3) }} />
<meshStandardMaterial shadowSide={DoubleSide} />
</mesh>
)
}