-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreex.proceduralcity2.js
390 lines (338 loc) · 13.8 KB
/
threex.proceduralcity2.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// from @mrdoob http://www.mrdoob.com/lab/javascript/webgl/city/01/
var THREEx = THREEx || {}
THREEx.ProceduralCity = function(){
// build the base geometry for each building
var geometry = new THREE.CubeGeometry( 1, 1, 1 );
// translate the geometry to place the pivot point at the bottom instead of the center
geometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, 0.5, 0 ) );
// get rid of the bottom face - it is never seen
geometry.faces.splice( 3, 1 );
geometry.faceVertexUvs[0].splice( 3, 1 );
// change UVs for the top face
// - it is the roof so it wont use the same texture as the side of the building
// - set the UVs to the single coordinate 0,0. so the roof will be the same color
// as a floor row.
geometry.faceVertexUvs[0][2][0].set( 0, 0 );
geometry.faceVertexUvs[0][2][1].set( 0, 0 );
geometry.faceVertexUvs[0][2][2].set( 0, 0 );
geometry.faceVertexUvs[0][2][3].set( 0, 0 );
// buildMesh
var buildingMesh= new THREE.Mesh( geometry );
this.createBuilding = function(){
return buildingMesh
}
//////////////////////////////////////////////////////////////////////////////////
// buildingTexture //
//////////////////////////////////////////////////////////////////////////////////
// generate the texture
var buildingTexture = new THREE.Texture( generateTextureCanvas() );
buildingTexture.anisotropy = renderer.getMaxAnisotropy();
buildingTexture.needsUpdate = true;
//////////////////////////////////////////////////////////////////////////////////
// lamp //
//////////////////////////////////////////////////////////////////////////////////
var lampGeometry= new THREE.CubeGeometry( 0.1, 3, 0.1)
var lampMesh = new THREE.Mesh(lampGeometry)
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
var nBlockX = 10
var nBlockZ = 10
var blockSizeX = 50
var blockSizeZ = 50
var blockDensity= 20
var roadW = 20
var roadD = 8
var buildingMaxW= 20
var buildingMaxD= 20
var sidewalkW = 2
var sidewalkH = 0.1
var sidewalkD = 2
var lampDensityW= 1
var lampDensityD= 1
var lampH = 7
this.createSquareGround = function(){
var geometry = new THREE.PlaneGeometry( 1, 1, 1 );
var material = new THREE.MeshLambertMaterial({
color : 0x222222
})
var ground = new THREE.Mesh(geometry, material)
ground.lookAt(new THREE.Vector3(0,1,0))
ground.scale.x = (nBlockZ)*blockSizeZ
ground.scale.y = (nBlockX)*blockSizeX
return ground
}
this.createSquareLamps = function(){
var object3d = new THREE.Object3D()
var lampGeometry= new THREE.CubeGeometry(1,1,1)
lampGeometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, 0.5, 0 ) );
var lampMesh = new THREE.Mesh(lampGeometry)
var lightsGeometry = new THREE.Geometry();
var lampsGeometry = new THREE.Geometry();
for( var blockZ = 0; blockZ < nBlockZ; blockZ++){
for( var blockX = 0; blockX < nBlockX; blockX++){
// lampMesh.position.x = 0
// lampMesh.position.z = 0
function addLamp(position){
//////////////////////////////////////////////////////////////////////////////////
// light //
//////////////////////////////////////////////////////////////////////////////////
var lightPosition = position.clone()
lightPosition.y = sidewalkH+lampH+0.1
// set position for block
lightPosition.x += (blockX+0.5-nBlockX/2)*blockSizeX
lightPosition.z += (blockZ+0.5-nBlockZ/2)*blockSizeZ
lightsGeometry.vertices.push(lightPosition );
//////////////////////////////////////////////////////////////////////////////////
// head //
//////////////////////////////////////////////////////////////////////////////////
// set base position
lampMesh.position.copy(position)
lampMesh.position.y = sidewalkH+lampH
// add poll offset
lampMesh.scale.set(0.2,0.2,0.2)
// colorify
for(var i = 0; i < lampMesh.geometry.faces.length; i++ ) {
lampMesh.geometry.faces[i].color.set('white' );
}
// set position for block
lampMesh.position.x += (blockX+0.5-nBlockX/2)*blockSizeX
lampMesh.position.z += (blockZ+0.5-nBlockZ/2)*blockSizeZ
// merge it with cityGeometry - very important for performance
THREE.GeometryUtils.merge( lampsGeometry, lampMesh )
//////////////////////////////////////////////////////////////////////////////////
// poll //
//////////////////////////////////////////////////////////////////////////////////
// set base position
lampMesh.position.copy(position)
lampMesh.position.y += sidewalkH
// add poll offset
lampMesh.scale.set(0.1,lampH,0.1)
// colorify
for(var i = 0; i < lampMesh.geometry.faces.length; i++ ) {
lampMesh.geometry.faces[i].color.set('grey' );
}
// set position for block
lampMesh.position.x += (blockX+0.5-nBlockX/2)*blockSizeX
lampMesh.position.z += (blockZ+0.5-nBlockZ/2)*blockSizeZ
// merge it with cityGeometry - very important for performance
THREE.GeometryUtils.merge( lampsGeometry, lampMesh )
//////////////////////////////////////////////////////////////////////////////////
// base //
//////////////////////////////////////////////////////////////////////////////////
// set base position
lampMesh.position.copy(position)
lampMesh.position.y += sidewalkH
// add poll offset
lampMesh.scale.set(0.12,0.4,0.12)
// colorify
for(var i = 0; i < lampMesh.geometry.faces.length; i++ ) {
lampMesh.geometry.faces[i].color.set('maroon' );
}
// set position for block
lampMesh.position.x += (blockX+0.5-nBlockX/2)*blockSizeX
lampMesh.position.z += (blockZ+0.5-nBlockZ/2)*blockSizeZ
// merge it with cityGeometry - very important for performance
THREE.GeometryUtils.merge( lampsGeometry, lampMesh );
}
// south
var position = new THREE.Vector3()
for(var i = 0; i < lampDensityW+1; i++){
position.x = (i/lampDensityW-0.5)*(blockSizeX-roadW-sidewalkW)
position.z = -0.5*(blockSizeZ-roadD-sidewalkD)
addLamp(position)
}
// north
for(var i = 0; i < lampDensityW+1; i++){
position.x = (i/lampDensityW-0.5)*(blockSizeX-roadW-sidewalkW)
position.z = +0.5*(blockSizeZ-roadD-sidewalkD)
addLamp(position)
}
// east
for(var i = 1; i < lampDensityD; i++){
position.x = +0.5*(blockSizeX-roadW-sidewalkW)
position.z = (i/lampDensityD-0.5)*(blockSizeZ-roadD-sidewalkD)
addLamp(position)
}
// west
for(var i = 1; i < lampDensityD; i++){
position.x = -0.5*(blockSizeX-roadW-sidewalkW)
position.z = (i/lampDensityD-0.5)*(blockSizeZ-roadD-sidewalkD)
addLamp(position)
}
}
}
// build the lamps Mesh
var material = new THREE.MeshLambertMaterial({
vertexColors : THREE.VertexColors
});
var lampsMesh = new THREE.Mesh(lampsGeometry, material );
object3d.add(lampsMesh)
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
var texture = THREE.ImageUtils.loadTexture( "lensflare2_alpha.png" );
var material = new THREE.ParticleBasicMaterial({
map : texture,
size : 8,
transparent : true
});
var lightParticles = new THREE.ParticleSystem( lightsGeometry, material );
lightParticles.sortParticles = true;
object3d.add( lightParticles );
return object3d
}
this.createSquareSideWalks = function(){
var buildingMesh= this.createBuilding()
var sidewalksGeometry= new THREE.Geometry();
for( var blockZ = 0; blockZ < nBlockZ; blockZ++){
for( var blockX = 0; blockX < nBlockX; blockX++){
// set position
buildingMesh.position.x = (blockX+0.5-nBlockX/2)*blockSizeX
buildingMesh.position.z = (blockZ+0.5-nBlockZ/2)*blockSizeZ
buildingMesh.scale.x = blockSizeX-roadW
buildingMesh.scale.y = sidewalkH
buildingMesh.scale.z = blockSizeZ-roadD
// merge it with cityGeometry - very important for performance
THREE.GeometryUtils.merge( sidewalksGeometry, buildingMesh );
}
}
// build the mesh
var material = new THREE.MeshLambertMaterial({
color : 0x444444
});
var sidewalksMesh = new THREE.Mesh(sidewalksGeometry, material );
return sidewalksMesh
}
this.createSquareBuildings = function(){
var buildingMesh= this.createBuilding()
var cityGeometry= new THREE.Geometry();
for( var blockZ = 0; blockZ < nBlockZ; blockZ++){
for( var blockX = 0; blockX < nBlockX; blockX++){
for( var i = 0; i < blockDensity; i++){
// set position
buildingMesh.position.x = (Math.random()-0.5)*(blockSizeX-buildingMaxW-roadW-sidewalkW)
buildingMesh.position.z = (Math.random()-0.5)*(blockSizeZ-buildingMaxD-roadD-sidewalkD)
// add position for the blocks
buildingMesh.position.x += (blockX+0.5-nBlockX/2)*blockSizeX
buildingMesh.position.z += (blockZ+0.5-nBlockZ/2)*blockSizeZ
// put a random scale
buildingMesh.scale.x = Math.min(Math.random() * 5 + 10, buildingMaxW);
buildingMesh.scale.y = (Math.random() * Math.random() * buildingMesh.scale.x) * 3 + 4;
buildingMesh.scale.z = Math.min(buildingMesh.scale.x, buildingMaxD)
this.colorifyBuilding(buildingMesh)
// merge it with cityGeometry - very important for performance
THREE.GeometryUtils.merge( cityGeometry, buildingMesh );
}
}
}
// build the city Mesh
var material = new THREE.MeshLambertMaterial({
map : buildingTexture,
vertexColors : THREE.VertexColors
});
var cityMesh = new THREE.Mesh(cityGeometry, material );
return cityMesh
}
this.createSquareCity = function(){
var object3d = new THREE.Object3D()
var lampsMesh = this.createSquareLamps()
object3d.add(lampsMesh)
var sidewalksMesh = this.createSquareSideWalks()
object3d.add(sidewalksMesh)
var buildingsMesh = this.createSquareBuildings()
object3d.add(buildingsMesh)
var groundMesh = this.createSquareGround()
object3d.add(groundMesh)
return object3d
}
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
this.createMrDoobCity = function(){
var buildingMesh= this.createBuilding()
var cityGeometry= new THREE.Geometry();
for( var i = 0; i < 20000; i ++ ){
// put a random position
buildingMesh.position.x = Math.floor( Math.random() * 200 - 100 ) * 10;
buildingMesh.position.z = Math.floor( Math.random() * 200 - 100 ) * 10;
// put a random rotation
buildingMesh.rotation.y = Math.random()*Math.PI*2;
// put a random scale
buildingMesh.scale.x = Math.random() * Math.random() * Math.random() * Math.random() * 50 + 10;
buildingMesh.scale.y = (Math.random() * Math.random() * Math.random() * buildingMesh.scale.x) * 8 + 8;
buildingMesh.scale.z = buildingMesh.scale.x
this.colorifyBuilding(buildingMesh)
// merge it with cityGeometry - very important for performance
THREE.GeometryUtils.merge( cityGeometry, buildingMesh );
}
// build the mesh
var material = new THREE.MeshLambertMaterial({
map : buildingTexture,
vertexColors : THREE.VertexColors
});
var cityMesh = new THREE.Mesh(cityGeometry, material );
return cityMesh
}
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
// base colors for vertexColors. light is for vertices at the top, shaddow is for the ones at the bottom
var light = new THREE.Color( 0xffffff )
var shadow = new THREE.Color( 0x303050 )
this.colorifyBuilding = function(buildingMesh){
// establish the base color for the buildingMesh
var value = 1 - Math.random() * Math.random();
var baseColor = new THREE.Color().setRGB( value + Math.random() * 0.1, value, value + Math.random() * 0.1 );
// set topColor/bottom vertexColors as adjustement of baseColor
var topColor = baseColor.clone().multiply( light );
var bottomColor = baseColor.clone().multiply( shadow );
// set .vertexColors for each face
var geometry = buildingMesh.geometry;
for ( var j = 0, jl = geometry.faces.length; j < jl; j ++ ) {
if ( j === 2 ) {
// set face.vertexColors on root face
geometry.faces[ j ].vertexColors = [ baseColor, baseColor, baseColor, baseColor ];
} else {
// set face.vertexColors on sides faces
geometry.faces[ j ].vertexColors = [ topColor, bottomColor, bottomColor, topColor ];
}
}
}
return
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
function generateTextureCanvas(){
// build a small canvas 32x64 and paint it in white
var canvas = document.createElement( 'canvas' );
canvas.width = 32;
canvas.height = 64;
var context = canvas.getContext( '2d' );
// plain it in white
context.fillStyle = '#ffffff';
context.fillRect( 0, 0, 32, 64 );
// draw the window rows - with a small noise to simulate light variations in each room
for( var y = 2; y < 64; y += 2 ){
for( var x = 0; x < 32; x += 2 ){
var value = Math.floor( Math.random() * 64 );
context.fillStyle = 'rgb(' + [value, value, value].join( ',' ) + ')';
context.fillRect( x, y, 2, 1 );
}
}
// build a bigger canvas and copy the small one in it
// This is a trick to upscale the texture without filtering
var canvas2 = document.createElement( 'canvas' );
canvas2.width = 512;
canvas2.height = 1024;
var context = canvas2.getContext( '2d' );
// disable smoothing
context.imageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.mozImageSmoothingEnabled = false;
// then draw the image
context.drawImage( canvas, 0, 0, canvas2.width, canvas2.height );
// return the just built canvas2
return canvas2;
}
}