-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
250 lines (190 loc) · 8.47 KB
/
test.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="module">
'use strict';
import * as THREE from 'https://cdn.skypack.dev/three@';
import { OrbitControls } from 'https://cdn.skypack.dev/three/examples/jsm/controls/OrbitControls.js';
var camera, scene, renderer;
var cube, cube_geometry, cube_material;
var controls;
let i = 0;
let order = [];
let regularBlocks = []
init();
render();
function init() {
scene = new THREE.Scene();
// renderer
renderer = new THREE.WebGLRenderer({
alpha: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// controls
controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', render);
controls.enableZoom = true;
// mesh - cube
/*
cube_geometry = new THREE.CubeGeometry(5, 5, 5);
for (var i = 0; i < cube_geometry.faces.length; i += 2) {
var color = Math.random() * 0xffffff;
cube_geometry.faces[i].color.setHex(color);
cube_geometry.faces[i + 1].color.setHex(color);
}
cube_material = new THREE.MeshLambertMaterial({
color: 0xffffff,
vertexColors: THREE.FaceColors
});
cube = new THREE.Mesh(cube_geometry, cube_material);
scene.add(cube); */
/* const geometry = new THREE.CubeGeometry(1,1,1);
const material = new THREE.MeshPhongMaterial({
color: 0xcc00cc
});
const cube = new THREE.Mesh(geometry, material);
//console.log(i);
scene.add(cube);
*/
// Lights
var light = new THREE.DirectionalLight(0xffffff);
light.position.set(1, 1, 1);
scene.add(light);
var light = new THREE.DirectionalLight(0x002288);
light.position.set(-1, -1, -1);
scene.add(light);
var light = new THREE.AmbientLight(0x222222);
scene.add(light);
// events
window.addEventListener('resize', onWindowResize, false);
//scene.add( cube );
const START = new THREE.Vector3(0, 0, 0).floor();
const SIZE = new THREE.Vector3(10, 30, 10);
for (let i = 0; i < SIZE.y; i++) {
/*
Dig blocks in scanline order
*/
for (let j = 0; j < SIZE.x; j++) {
if (j % 2) {
for (let k = 0; k < SIZE.z; k++) {
const index = order.push(new THREE.Vector3(j, -i, k).add(START));
regularBlocks.push(index - 1);
}
} else {
for (let k = SIZE.z - 1; k >= 0; k--) {
const index = order.push(new THREE.Vector3(j, -i, k).add(START));
regularBlocks.push(index - 1);
}
}
}
// Add stairs to the side. Every x blocks in depth,
// reverse the stairs.
// This staircase will be on the x+ side
const frontStairX = SIZE.x;
const backStairX = SIZE.x + 1;
// Lateral position of stairs
if(!(~~(i / SIZE.z) % 2)){
const stairZ = ~~(i % SIZE.z)
console.log(stairZ);
// digging towards the front
order.push(new THREE.Vector3(frontStairX, -i, stairZ).add(START))
if(stairZ === 0){
order.push(new THREE.Vector3(frontStairX, -(i-1), stairZ +1).add(START));
}
if(stairZ === SIZE.z - 1){
// if it's the last stair before switching directions
// dig three vertically to the side
order.push(new THREE.Vector3(backStairX, -i, stairZ).add(START));
order.push(new THREE.Vector3(backStairX, -(i - 1), stairZ).add(START));
order.push(new THREE.Vector3(backStairX, -(i - 2), stairZ).add(START));
}else if(stairZ === SIZE.z - 2){
// On the second to last stair, just dig one block in front.
order.push(new THREE.Vector3(frontStairX, -i, stairZ + 1).add(START))
}else{
// dig two blocks in front
order.push(new THREE.Vector3(frontStairX, -i, stairZ + 1).add(START))
order.push(new THREE.Vector3(frontStairX, -i, stairZ + 2).add(START))
}
}else{
const stairZ = SIZE.z - (~~(i % SIZE.z)) - 1;
console.log(stairZ);
order.push(new THREE.Vector3(backStairX, -i, stairZ).add(START));
if(stairZ === 0){
order.push(new THREE.Vector3(frontStairX, -i, stairZ).add(START));
order.push(new THREE.Vector3(frontStairX, -(i-1), stairZ).add(START));
}else if(stairZ === 1){
order.push(new THREE.Vector3(backStairX, -i, stairZ-1).add(START));
order.push(new THREE.Vector3(frontStairX, -i, stairZ).add(START));
order.push(new THREE.Vector3(frontStairX, -i, stairZ-1).add(START));
}else if(stairZ === SIZE.z -1){
order.push(new THREE.Vector3(backStairX, -(i -1), stairZ-1).add(START));
order.push(new THREE.Vector3(backStairX, -(i -1), stairZ-2).add(START));
order.push(new THREE.Vector3(backStairX, -i, stairZ-1).add(START));
order.push(new THREE.Vector3(backStairX, -i, stairZ-2).add(START));
order.push(new THREE.Vector3(frontStairX, -(i -1), stairZ-2).add(START));
order.push(new THREE.Vector3(frontStairX, -i, stairZ-2).add(START));
}else{
order.push(new THREE.Vector3(backStairX, -i, stairZ-1).add(START));
order.push(new THREE.Vector3(backStairX, -i, stairZ-2).add(START));
order.push(new THREE.Vector3(frontStairX, -i, stairZ).add(START));
order.push(new THREE.Vector3(frontStairX, -i, stairZ-1).add(START));
order.push(new THREE.Vector3(frontStairX, -i, stairZ-2).add(START));
}
// digging towards the back
}
}
order.forEach((e,i) => {
if(!regularBlocks.includes(i)){
let color = (regularBlocks.includes(i)) ? 0x00aa00 : 0xaa0000;
const geometry = new THREE.BoxGeometry(0.9,0.9,0.9);
const material = new THREE.MeshLambertMaterial({
color: color
});
const cube = new THREE.Mesh(geometry, material);
cube.position.copy(e);
//console.log("");
scene.add(cube);
}
})
/* order.forEach((e, i) => {
setTimeout(() => {
const cube = new THREE.Mesh( geometry, material );
cube.position.set(e);
scene.add(cube);
//console.log(`${e.x},${e.y},${e.z}`);
render();
}, 100 * i);
}) */
}
function render(time) {
time *= 0.001; // convert time to seconds
/* if (time >= i * 0.1) {
const geometry = new THREE.BoxGeometry(0.9,0.9,0.9);
const material = new THREE.MeshLambertMaterial({
color: 0xcc00cc
});
const cube = new THREE.Mesh(geometry, material);
cube.position.copy(order[i]);
//console.log("");
scene.add(cube);
i++;
}*/
renderer.render(scene, camera);
requestAnimationFrame(render);
}
function onWindowResize(event) {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
</script>
</body>
</html>