-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
284 lines (249 loc) · 9.07 KB
/
script.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*jshint esversion: 6 */
var scene, camera, renderer, controls;
const CLOUD_RADIUS = 8;
const WATER_LEVEL = 4;
const ZOOM = 20;
const TERRAIN_SIZE = 40;
function generateCubeGeometry() {
const boxUnit = 1;
const boxWidth = boxUnit;
const boxHeight = boxUnit;
const boxDepth = boxUnit;
return new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
}
function generateRainGeometry() {
const boxUnit = 1;
const boxWidth = boxUnit/4;
const boxHeight = boxUnit;
const boxDepth = boxUnit/4;
return new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
}
function hsl(h, s, l) {
return (new THREE.Color()).setHSL(h, s, l);
}
const loader = new THREE.TextureLoader();
loader.setPath( 'textures/' );
const GrassMaterials = [
new THREE.MeshPhongMaterial({map: loader.load('grass_block_side.png')}),
new THREE.MeshPhongMaterial({map: loader.load('grass_block_side.png')}),
new THREE.MeshPhongMaterial({map: loader.load('grass_block_top.png')}),
new THREE.MeshPhongMaterial({map: loader.load('dirt.png')}),
new THREE.MeshPhongMaterial({map: loader.load('grass_block_side.png')}),
new THREE.MeshPhongMaterial({map: loader.load('grass_block_side.png')}),
];
const DirtMaterials = [
new THREE.MeshPhongMaterial({map: loader.load('dirt.png')}),
new THREE.MeshPhongMaterial({map: loader.load('dirt.png')}),
new THREE.MeshPhongMaterial({map: loader.load('dirt.png')}),
new THREE.MeshPhongMaterial({map: loader.load('dirt.png')}),
new THREE.MeshPhongMaterial({map: loader.load('dirt.png')}),
new THREE.MeshPhongMaterial({map: loader.load('dirt.png')}),
];
const waterColor = hsl(5 / 8, 1, 0.5);
const WaterMaterials = [
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0, transparent: true})),
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0, transparent: true})),
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0.5, transparent: true})),
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0, transparent: true})),
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0, transparent: true})),
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0, transparent: true})),
];
const rainColor = hsl(5 / 8, 1, 0.5);
const RainMaterials = [
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0.5, transparent: true})),
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0.5, transparent: true})),
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0.5, transparent: true})),
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0.5, transparent: true})),
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0.5, transparent: true})),
new THREE.MeshPhongMaterial(({color: waterColor, opacity: 0.5, transparent: true})),
];
const cloudColor = hsl(0, 1, 1);
const CloudMaterials = [
new THREE.MeshPhongMaterial(({color: cloudColor, opacity: 1, transparent: true})),
new THREE.MeshPhongMaterial(({color: cloudColor, opacity: 1, transparent: true})),
new THREE.MeshPhongMaterial(({color: cloudColor, opacity: 1, transparent: true})),
new THREE.MeshPhongMaterial(({color: cloudColor, opacity: 1, transparent: true})),
new THREE.MeshPhongMaterial(({color: cloudColor, opacity: 1, transparent: true})),
new THREE.MeshPhongMaterial(({color: cloudColor, opacity: 1, transparent: true})),
];
function makeInstance(geometry, x, y = 0, z = 0, material = "dirt") {
var materials = DirtMaterials;
switch (material) {
case "grass":
materials = GrassMaterials;
break;
case "dirt":
materials = DirtMaterials;
break;
case "water":
materials = WaterMaterials;
break;
case "cloud":
materials = CloudMaterials;
break;
default:
}
const cube = new THREE.Mesh(geometry, materials);
scene.add(cube);
cube.position.x = x;
cube.position.y = y;
cube.position.z = z;
return cube;
}
var rainDataX = new Array();
var rainDataY = new Array();
var rainDataZ = new Array();
var rainData = new Array();
var cloudHeight = 0;
function generateCloud(height, x, y) {
const cubeGeometry = generateCubeGeometry();
const rainGeometry = generateRainGeometry();
cloudHeight = height;
x = x - CLOUD_RADIUS/2;
y = y - CLOUD_RADIUS/2;
for (var i = 0; i < CLOUD_RADIUS; i++) {
for (var j = 0; j < CLOUD_RADIUS; j++) {
const variableheight = Math.floor(Math.random() * height);
const coorX = x+i;
const coorY = height;
const coorZ = y+j;
makeInstance(cubeGeometry, coorX, coorY, coorZ, "cloud");
rainDataX[i*CLOUD_RADIUS + j] = coorX;
rainDataY[i*CLOUD_RADIUS + j] = coorY - variableheight;
rainDataZ[i*CLOUD_RADIUS + j] = coorZ;
// var drop
rainData[i*CLOUD_RADIUS + j] = new THREE.Mesh(rainGeometry, RainMaterials);
// drop = rainData[i*CLOUD_RADIUS + j];
scene.add(rainData[i*CLOUD_RADIUS + j]);
rainData[i*CLOUD_RADIUS + j].position.x = coorX;
rainData[i*CLOUD_RADIUS + j].position.y = coorY - variableheight;
rainData[i*CLOUD_RADIUS + j].position.z = coorZ;
}
}
}
var terrainData = new Array();
function generateTerrain(width, length) {
function isEdgeCube(x, y, z, maxY){
// return true;
if(x == 0 || y == -1 || z == 0) {
return true;
}
if(x == width-1 || y == maxY-1 || z == length - 1) {
return true;
}
return false;
}
function isHiddenCube(x, y, z, terrainData) {
if(y == terrainData[x][z]-1){
return false;
}
if(isEdgeCube(x, y, z, terrainData[x][z])){
return false;
}
if(y > terrainData[x-1][z]-1 || y > terrainData[x][z-1]-1 || y > terrainData[x+1][z]-1 || y > terrainData[x][z+1]-1){
return false;
}
return true;
}
const cubeGeometry = generateCubeGeometry();
noise.seed(Math.random());
var maxHeight = 0;
//march cube through all topological cubes
for(var x = 0; x < width; x++) {
terrainData[x] = new Array();
for(var z = 0; z < length; z++) {
//for every topological coordinate get generated height
//perlin2 takes float, divide our coordinate to make smaller than 0
const genValue = noise.perlin2(x / ZOOM, z / ZOOM);
const y = Math.floor(Math.abs(genValue) * ZOOM);
terrainData[x][z] = y;
maxHeight = Math.max(maxHeight, y);
}
}
//march cube through ALL cubes in space
for(var x = 0; x < width; x++) {
for(var z = 0; z < length; z++) {
const maxY = terrainData[x][z];
const isBelowWater = maxY < WATER_LEVEL;
if(isBelowWater) {
makeInstance(cubeGeometry, x - width/2, WATER_LEVEL-1.1, z-length/2, "water");
}
for(var y = -1; y < maxY; y++) {
if(!isHiddenCube(x,y,z, terrainData)) {
const isTop = y == maxY-1;
makeInstance(cubeGeometry, x - width/2, y, z-length/2, isTop && !isBelowWater ? "grass" : "dirt");
}
}
}
}
generateCloud(maxHeight+3, 0, 0);
}
function main() {
const canvas = document.querySelector('#c');
renderer = new THREE.WebGLRenderer({canvas});
const fov = 75;
const aspect = 2; // the canvas default
const near = 0.1;
const far = 200;
camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.z = 20;
camera.position.y = 20;
scene = new THREE.Scene();
// Add OrbitControls so that we can pan around with the mouse.
controls = new THREE.OrbitControls(camera, renderer.domElement);
{
const color = 0xFFFFFF;
const intensity = 1;
// const light = new THREE.DirectionalLight(color, intensity);
// light.position.set(-1, 2, 4);
const light = new THREE.HemisphereLight( 0xffffbb, 0x080820, 1 );
scene.add(light);
scene.add(light);
}
generateTerrain(TERRAIN_SIZE,TERRAIN_SIZE);
function resizeRendererToDisplaySize(renderer) {
const canvas = renderer.domElement;
const pixelRatio = window.devicePixelRatio;
const width = canvas.clientWidth * pixelRatio | 0;
const height = canvas.clientHeight * pixelRatio | 0;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
function render(time) {
// console.log("frame")
time *= 0.001;
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
const rainSpeed = 0.5;
for (var i = 0; i < rainDataX.length; i++) {
// console.log("loop");
if(i == 0){
// console.log("before");
// console.log(rainDataY[i]);
}
rainDataY[i] = rainDataY[i] - (rainSpeed);
rainData[i].position.y = rainDataY[i];
// const rainX = CLOUD_RADIUS - rainDataX[i];
// const rainZ = CLOUD_RADIUS - rainDataZ[i];
// const terrainHeight = terrainData[rainX][rainZ];
if(rainDataY[i] <= 0 || rainDataY[i] <= WATER_LEVEL){
rainDataY[i] = cloudHeight;
}
}
// rainData.forEach((rain, i) => {
// const speed = 1 + i * 0.1;
// rain.position.y -= speed;
// });
renderer.render(scene, camera);
controls.update();
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
main();