-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrain.ts
132 lines (76 loc) · 3.2 KB
/
terrain.ts
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
// terrain generator from three js examples: https://threejs.org/examples/#webgl_geometry_terrain
import * as THREE from 'three';
import { ImprovedNoise } from 'three/examples/jsm/math/ImprovedNoise.js';
const worldWidth = 256, worldDepth = 256;
let seed = Math.PI / 4;
function random () {
const x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
};
const data = generateHeight(worldWidth, worldDepth);
const geometry = new THREE.PlaneGeometry(7500, 7500, worldWidth - 1, worldDepth - 1);
geometry.rotateX(- Math.PI / 2);
const vertices = geometry.attributes.position.array;
for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) {
//@ts-expect-error
vertices[j + 1] = data[i] * 10;
}
const texture = new THREE.CanvasTexture(generateTexture(data, worldWidth, worldDepth));
texture.wrapS = THREE.ClampToEdgeWrapping;
texture.wrapT = THREE.ClampToEdgeWrapping;
export const terrain = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ map: texture }));
function generateHeight(width:number, height:number) {
const size = width * height, data = new Uint8Array(size);
const perlin = new ImprovedNoise(), z = random() * 100;
let quality = 1;
for (let j = 0; j < 4; j++) {
for (let i = 0; i < size; i++) {
const x = i % width, y = ~ ~(i / width);
data[i] += Math.abs(perlin.noise(x / quality, y / quality, z) * quality * 1.75);
}
quality *= 5;
}
return data;
}
function generateTexture(data:Uint8Array, width:number, height:number) {
let context:CanvasRenderingContext2D, image:ImageData, imageData:Uint8ClampedArray, shade:number;
const vector3 = new THREE.Vector3(0, 0, 0);
const sun = new THREE.Vector3(1, 1, 1);
sun.normalize();
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
context = canvas.getContext('2d')!;
context.fillStyle = '#000';
context.fillRect(0, 0, width, height);
image = context.getImageData(0, 0, canvas.width, canvas.height);
imageData = image.data;
for (let i = 0, j = 0, l = imageData.length; i < l; i += 4, j++) {
vector3.x = data[j - 2] - data[j + 2];
vector3.y = 2;
vector3.z = data[j - width * 2] - data[j + width * 2];
vector3.normalize();
shade = vector3.dot(sun);
imageData[i] = (96 + shade * 128) * (0.5 + data[j] * 0.007);
imageData[i + 1] = (32 + shade * 96) * (0.5 + data[j] * 0.007);
imageData[i + 2] = (shade * 96) * (0.5 + data[j] * 0.007);
}
context.putImageData(image, 0, 0);
// Scaled 4x
const canvasScaled = document.createElement('canvas');
canvasScaled.width = width * 4;
canvasScaled.height = height * 4;
context = canvasScaled.getContext('2d')!;
context.scale(4, 4);
context.drawImage(canvas, 0, 0);
image = context.getImageData(0, 0, canvasScaled.width, canvasScaled.height);
imageData = image.data;
for (let i = 0, l = imageData.length; i < l; i += 4) {
const v = ~ ~(Math.random() * 5);
imageData[i] += v;
imageData[i + 1] += v;
imageData[i + 2] += v;
}
context.putImageData(image, 0, 0);
return canvasScaled;
}