-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
/
webgl_geometry_minecraft.html
250 lines (164 loc) · 6.38 KB
/
webgl_geometry_minecraft.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>
<title>three.js webgl - geometry - minecraft</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<style>
body {
background-color: #bfd1e5;
color: #61443e;
}
a {
color: #a06851;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - <a href="http://www.minecraft.net/" target="_blank" rel="noopener">minecraft</a> demo. featuring <a href="http://painterlypack.net/" target="_blank" rel="noopener">painterly pack</a><br />(left click: forward, right click: backward)</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
let container, stats;
let camera, controls, scene, renderer;
const worldWidth = 128, worldDepth = 128;
const worldHalfWidth = worldWidth / 2;
const worldHalfDepth = worldDepth / 2;
const data = generateHeight( worldWidth, worldDepth );
const clock = new THREE.Clock();
init();
function init() {
container = document.getElementById( 'container' );
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
camera.position.y = getY( worldHalfWidth, worldHalfDepth ) * 100 + 100;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xbfd1e5 );
// sides
const matrix = new THREE.Matrix4();
const pxGeometry = new THREE.PlaneGeometry( 100, 100 );
pxGeometry.attributes.uv.array[ 1 ] = 0.5;
pxGeometry.attributes.uv.array[ 3 ] = 0.5;
pxGeometry.rotateY( Math.PI / 2 );
pxGeometry.translate( 50, 0, 0 );
const nxGeometry = new THREE.PlaneGeometry( 100, 100 );
nxGeometry.attributes.uv.array[ 1 ] = 0.5;
nxGeometry.attributes.uv.array[ 3 ] = 0.5;
nxGeometry.rotateY( - Math.PI / 2 );
nxGeometry.translate( - 50, 0, 0 );
const pyGeometry = new THREE.PlaneGeometry( 100, 100 );
pyGeometry.attributes.uv.array[ 5 ] = 0.5;
pyGeometry.attributes.uv.array[ 7 ] = 0.5;
pyGeometry.rotateX( - Math.PI / 2 );
pyGeometry.translate( 0, 50, 0 );
const pzGeometry = new THREE.PlaneGeometry( 100, 100 );
pzGeometry.attributes.uv.array[ 1 ] = 0.5;
pzGeometry.attributes.uv.array[ 3 ] = 0.5;
pzGeometry.translate( 0, 0, 50 );
const nzGeometry = new THREE.PlaneGeometry( 100, 100 );
nzGeometry.attributes.uv.array[ 1 ] = 0.5;
nzGeometry.attributes.uv.array[ 3 ] = 0.5;
nzGeometry.rotateY( Math.PI );
nzGeometry.translate( 0, 0, - 50 );
//
const geometries = [];
for ( let z = 0; z < worldDepth; z ++ ) {
for ( let x = 0; x < worldWidth; x ++ ) {
const h = getY( x, z );
matrix.makeTranslation(
x * 100 - worldHalfWidth * 100,
h * 100,
z * 100 - worldHalfDepth * 100
);
const px = getY( x + 1, z );
const nx = getY( x - 1, z );
const pz = getY( x, z + 1 );
const nz = getY( x, z - 1 );
geometries.push( pyGeometry.clone().applyMatrix4( matrix ) );
if ( ( px !== h && px !== h + 1 ) || x === 0 ) {
geometries.push( pxGeometry.clone().applyMatrix4( matrix ) );
}
if ( ( nx !== h && nx !== h + 1 ) || x === worldWidth - 1 ) {
geometries.push( nxGeometry.clone().applyMatrix4( matrix ) );
}
if ( ( pz !== h && pz !== h + 1 ) || z === worldDepth - 1 ) {
geometries.push( pzGeometry.clone().applyMatrix4( matrix ) );
}
if ( ( nz !== h && nz !== h + 1 ) || z === 0 ) {
geometries.push( nzGeometry.clone().applyMatrix4( matrix ) );
}
}
}
const geometry = BufferGeometryUtils.mergeGeometries( geometries );
geometry.computeBoundingSphere();
const texture = new THREE.TextureLoader().load( 'textures/minecraft/atlas.png' );
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
const mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: texture, side: THREE.DoubleSide } ) );
scene.add( mesh );
const ambientLight = new THREE.AmbientLight( 0xeeeeee, 3 );
scene.add( ambientLight );
const directionalLight = new THREE.DirectionalLight( 0xffffff, 12 );
directionalLight.position.set( 1, 1, 0.5 ).normalize();
scene.add( directionalLight );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
container.appendChild( renderer.domElement );
controls = new FirstPersonControls( camera, renderer.domElement );
controls.movementSpeed = 1000;
controls.lookSpeed = 0.125;
controls.lookVertical = true;
stats = new Stats();
container.appendChild( stats.dom );
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
controls.handleResize();
}
function generateHeight( width, height ) {
const data = [], perlin = new ImprovedNoise(),
size = width * height, z = Math.random() * 100;
let quality = 2;
for ( let j = 0; j < 4; j ++ ) {
if ( j === 0 ) for ( let i = 0; i < size; i ++ ) data[ i ] = 0;
for ( let i = 0; i < size; i ++ ) {
const x = i % width, y = ( i / width ) | 0;
data[ i ] += perlin.noise( x / quality, y / quality, z ) * quality;
}
quality *= 4;
}
return data;
}
function getY( x, z ) {
return ( data[ x + z * worldWidth ] * 0.15 ) | 0;
}
//
function animate() {
render();
stats.update();
}
function render() {
controls.update( clock.getDelta() );
renderer.render( scene, camera );
}
</script>
</body>
</html>