-
Notifications
You must be signed in to change notification settings - Fork 1
/
perlinLandscape.html
211 lines (147 loc) · 5.85 KB
/
perlinLandscape.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
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="js/three.min.js"></script>
<script src="js/cachedRandom.js"></script>
<script src="js/perlinNoise.js"></script>
<script src="js/utils/controls/PointerLockControls.js"></script>
<script src="js/initPointerLock.js"></script>
</head>
<body style=''>
<div id="blocker">
<div id="instructions">
<span style="font-size:40px;">Click to play</span>
<br />
(W, A, S, D = Move, SPACE = Jump, MOUSE = Look around)
</div>
</div>
<script>
var camera, scene, renderer;
var geometry, material, mesh;
var light, terrain;
var controls, clock, delta, ray;
var light2;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.05, 100000 );
// camera.position.x = 300;
// camera.position.z = 1000;
// camera.rotation.y = 1.25;
// -338, 90, 400 is the center of the terrain
scene = new THREE.Scene();
clock = new THREE.Clock();
controls = new THREE.PointerLockControls( camera );
scene.add( controls.getObject() );
light = new THREE.PointLight( 0xffffff );
light.position = new THREE.Vector3(200, 1800, 2000);
scene.add( light );
light2 = new THREE.PointLight( 0xffffff );
light2.position = new THREE.Vector3(-14000.0, 1800.0, 14000.0);
scene.add( light2 );
var terrainSize = 200;
var vertexArray = generateMeshVertexArray(terrainSize,terrainSize);
material = new THREE.MeshLambertMaterial( { color: 0x0077dd, doubleSided: true } );
terrain = new THREE.Mesh(buildGeomObject(vertexArray), material );
//flip the terrain over
terrain.rotation.z = Math.PI;
terrain.position.x += 10*terrainSize
terrain.position.z -= 10*terrainSize
scene.add(terrain);
/*
geometry = new THREE.CubeGeometry( 200, 200, 200 );
//material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
*/
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
initPointerLock();
}
function animate()
{
// note: three.js includes requestAnimationFrame shim
requestAnimationFrame( animate );
controls.isOnObject( false );
delta = clock.getDelta();
controls.update(delta);
/*
light.position.x += -8;
light.position.z += 8;
*/
//terrain.rotation.x += 0.01;
//terrain.rotation.y += 0.02;
renderer.render( scene, camera );
}
function generateMeshVertexArray(width, height)
{
var spaceInbetweenGridPoints = 20; //WARNING: must be identical with PerlinNoise.GridUnit, fragile!!!
var vertexArray = new Array();
var perlinNoiseGen = new PerlinNoise();
for (var i = 0; i < width; i++)
{
vertexArray[i] = new Array();
for (var j = 0; j < height; j++)
{
vertexArray[i][j] = new THREE.Vector3(i*spaceInbetweenGridPoints
,perlinNoiseGen.PerlinNoise(i*spaceInbetweenGridPoints, j*spaceInbetweenGridPoints)
,j*spaceInbetweenGridPoints);
}
}
return vertexArray;
}
function buildGeomObject(vertexArray)
{
var geom = new THREE.Geometry();
for (var i = 0; i < vertexArray.length; i++)
{
for (var j = 0; j < vertexArray[i].length; j++)
{ geom.vertices.push(vertexArray[i][j]); } //push all our verticies into the geom array
}
// this math maybe assumes width and height of the grid/array are equal
var facesPerRow = vertexArray.length - 1;
var facesPerColumn = vertexArray[0].length - 1;
var verticiesPerRow = vertexArray.length;
var verticiesPerColumn = vertexArray[0].length;
for (var j = 0; j < (facesPerColumn * verticiesPerRow); j = j + verticiesPerColumn) // loop doing the vertex column index
{
for (var i = 0; i < facesPerRow; i++) // loop doing the vertex row index
{
// produce two Face3 objects for a square
// original call:
// var face = new THREE.Face3( i + j, i + j + verticiesPerRow, i + j + verticiesPerRow + 1, i + j + 1)
var face = new THREE.Face3( i + j, i + j + verticiesPerRow, i + j + verticiesPerRow + 1)
geom.faces.push(face);
var face = new THREE.Face3( i + j + verticiesPerRow + 1, i + j + 1, i + j)
geom.faces.push(face);
/* handy for seeing your face definitions
console.log("i: " + i);
console.log("j: " + j);
console.log("Face4:");
console.log(face);
*/
}
}
geom.computeFaceNormals();
geom.computeVertexNormals();
return geom;
/*
. . . .
. . . .
. . . .
. . . .
geom.faces.push( new THREE.Face4( 0, 4, 5, 1) );
geom.faces.push( new THREE.Face4( 1, 5, 6, 2) );
geom.faces.push( new THREE.Face4( 2, 6, 7, 3) );
geom.faces.push( new THREE.Face4( 4, 8, 9, 5) );
geom.faces.push( new THREE.Face4( 5, 9, 10, 6) );
geom.faces.push( new THREE.Face4( 6, 10, 11, 7) );
geom.faces.push( new THREE.Face4( 8, 9, 12, 13) );
geom.faces.push( new THREE.Face4( 9, 10, 13, 14) );
geom.faces.push( new THREE.Face4( 10, 11, 14, 15) );
*/
}
</script>
</body>
</html>