forked from totalorder/citygen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
140 lines (122 loc) · 4.74 KB
/
index.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
<!doctype html>
<script src='three.min.js'></script>
<script src='Loader.js'></script>
<script src='ColladaLoader.js'></script>
<script src='FirstPersonControls.js'></script>
<script src='ApacheControls.js'></script>
<!--<script src='FirstPersonControls.js'></script>-->
<script src='threex.proceduralcity.js'></script>
<body style='margin: 0px; background-color: #111133; overflow: hidden;'><script>
var updateFcts = [];
var scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x151540, 0.0025 );
var renderer = new THREE.WebGLRenderer( { antialias: false } );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 3000)
camera.position.y = 80
//////////////////////////////////////////////////////////////////////////////////
// add an object and make it move //
//////////////////////////////////////////////////////////////////////////////////
var light = new THREE.HemisphereLight( 0xfffff0, 0x101020, 0.35 );
light.position.set( 0.75, 1, 0.25 );
scene.add( light );
var gridSize = 1000;
var material = new THREE.MeshBasicMaterial({ color: 0x101018 })
var geometry = new THREE.PlaneGeometry( gridSize, gridSize )
var plane = new THREE.Mesh( geometry, material );
plane.rotation.x= - 90 * Math.PI / 180;
scene.add( plane );
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
var city = new THREEx.ProceduralCity()
for( var iLights = 0; iLights < city.lights.length; iLights++) {
scene.add(city.lights[iLights]);
}
// scene.add(city);
var models = [
{name:"forvaltningshuset", x:80,z:320},
{name:"aulanordica", x:370, z:390},
{name:"univ", x:410, z:390},
{name:"Lindellhallen", x:400, z:180},
{name:"beteendevetarhuset", x:550, z:50},
{name:"sam", x:210, z:150}
];
models.forEach(function (model) {
var loader = new THREE.ColladaLoader();
loader.load('./models/'+model.name+'.dae', function (result) {
var dae = result.scene;
dae.rotation.x = Math.PI/2 * 3;
dae.position.x = model.x;
dae.position.z = model.z;
dae.updateMatrix();
scene.add(dae);
});
});
var addNeighbouringCityParts = function (x, z) {
function addNewCityPart(x, z) {
var newcity = new THREE.Mesh(city.geometry, city.material);
newcity.position.x = x * gridSize;
newcity.position.z = z * gridSize;
scene.add(newcity)
}
for (var i = scene.children.length - 1; i >= 2 + city.lights.length; i--) {
var child = scene.children[i];
scene.remove(child);
}
for (var i = -1; i <= 1; i++) {
for (var j = -1; j <= 1; j++) {
addNewCityPart(i + x, j + z);
}
}
};
addNeighbouringCityParts(0, 0);
//////////////////////////////////////////////////////////////////////////////////
// Camera Controls //
//////////////////////////////////////////////////////////////////////////////////
var controls = new THREE.FirstPersonControls( camera );
controls.movementSpeed = 70;
controls.lookSpeed = 0.05;
controls.lookVertical = true;
updateFcts.push(function(delta, now){
controls.update( delta );
})
//////////////////////////////////////////////////////////////////////////////////
// render the scene //
//////////////////////////////////////////////////////////////////////////////////
updateFcts.push(function(){
renderer.render( scene, camera );
})
var lastPosition = {x: 0, z: 0};
updateFcts.push(function () {
var cameraX = camera.position.x;
var cameraZ = camera.position.z;
var x = Math.round((cameraX / gridSize));
var z = Math.round((cameraZ / gridSize));
if (x != lastPosition.x || z != lastPosition.z) {
addNeighbouringCityParts(x, z);
}
lastPosition.x = x;
lastPosition.z = z;
});
//////////////////////////////////////////////////////////////////////////////////
// loop runner //
//////////////////////////////////////////////////////////////////////////////////
var lastTimeMsec= null
requestAnimationFrame(function animate(nowMsec){
// keep looping
requestAnimationFrame( animate );
// measure time
lastTimeMsec = lastTimeMsec || nowMsec-1000/60
var deltaMsec = Math.min(200, nowMsec - lastTimeMsec)
lastTimeMsec = nowMsec
// call each update function
updateFcts.forEach(function(updateFn){
updateFn(deltaMsec/1000, nowMsec/1000)
})
})
</script></body>