-
Notifications
You must be signed in to change notification settings - Fork 1
/
three.html
290 lines (231 loc) · 10.9 KB
/
three.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
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>NITS-Model</title>
<script type="text/javascript" src="libs/three.js"></script>
<script type="text/javascript" src="libs/OBJLoader.js"></script>
<script type="text/javascript" src="libs/OrbitControls.js"></script>
<script type="text/javascript" src="assets/terrain-data.js"></script>
<style>
body{
margin: 0;
overflow: hidden;
font-family: sans-serif;
}
#side{
width: 250px;
position: fixed;
right: 25px;
top: 50px;
font-size: 1.5em;
}
</style>
</head>
<body>
<div id="side"><b>WELCOME</b><br>Click on buildings for more info.</div>
</body>
<script type="text/javascript">
var scene, camera, renderer, orbitControls, clock, projector;
var side = document.getElementById("side");
var width = window.innerWidth-300;
var height = window.innerHeight
var aspectRatio = width/height;
function init(){
//Scene
scene = new THREE.Scene();
//Perspective Camera
var fov = 45;
camera = new THREE.PerspectiveCamera(fov, aspectRatio, 1, 1000);
camera.position.set(25,10,25);
camera.lookAt(new THREE.Vector3(0,0,0));
//WebGL Renderer
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(width, height);
renderer.setClearColor(0xdddddd);
// renderer.shadowMapEnabled = true;
//Terrain
var terrainMaterial = new THREE.MeshLambertMaterial({color:0xa67c52});
terrainMaterial.side = THREE.DoubleSide;
var terrainGeometry = new THREE.PlaneGeometry(60, 60, 1, 1);
// for(var i=0; i<terrainGeometry.vertices.length; i++){
// terrainGeometry.vertices[i].z = zCoords[i]/40;
// }
var terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.rotation.x = -Math.PI * 0.5;
terrain.rotation.z = Math.PI;
terrain.position.y = 2.75;
terrain.receiveShadow = true;
terrain.castShadow = true;
terrain.name = "ground";
scene.add(terrain);
// terrain.visible = false;
var terrainBGeometry = new THREE.PlaneGeometry(60,40,1,1);
var terrainB = new THREE.Mesh(terrainBGeometry, terrainMaterial);
terrainB.rotation.x = -Math.PI * 0.5;
// scene.add(terrainB);
var roadMaterial = new THREE.MeshLambertMaterial({color:0xababab});
var road1Geometry = new THREE.BoxGeometry(45, .05, 4.5);
var road1 = new THREE.Mesh(road1Geometry, roadMaterial);
road1.position.y = 2.8;
road1.position.z = 6;
scene.add(road1);
road1.name = "road1";
// road1.visible = false;
var road2Geometry = new THREE.BoxGeometry(4.5, .05, 28.5);
var road2 = new THREE.Mesh(road2Geometry, roadMaterial);
road2.position.x = -12;
road2.position.y = 2.8;
road2.position.z = -8.25;
scene.add(road2);
road2.name = "road2";
//OBJ Loader
var loader = new THREE.OBJLoader();
//Library
loader.load('assets/minimalLibrary.obj', function(loadedMesh){
var material = new THREE.MeshLambertMaterial({color: 0xfefefe});
material.side = THREE.DoubleSide;
loadedMesh.children.forEach(function(child){
child.material = material;
child.geometry.computeFaceNormals();
child.geometry.computeVertexNormals();
child.castShadow = true;
child.receiveShadow = true;
});
loadedMesh.scale.set(2, 1.8, 1.8);
loadedMesh.position.set(16, 2.8, -6)
loadedMesh.rotation.y = Math.PI;
scene.add(loadedMesh);
});
var libBaseGeom = new THREE.BoxGeometry(7,5,7);
var libBaseMat = new THREE.MeshLambertMaterial({color: 0xfefefe});
var libBase = new THREE.Mesh(libBaseGeom, libBaseMat);
libBase.rotation.x = -Math.PI * 0.5;
libBase.position.set(13.5,2.8,-5);
scene.add(libBase);
libBase.name = "Bharat Ratna APJ Abdul Kalam Learning Resource Centre";
libBase.material.visible = false;
//EBuilding
loader.load('assets/minimalEBuilding.obj', function(loadedE){
var material = new THREE.MeshLambertMaterial({color: 0xffefdc});
material.side = THREE.DoubleSide;
loadedE.children.forEach(function(child){
child.material = material;
child.geometry.computeFaceNormals();
child.geometry.computeVertexNormals();
child.castShadow = true;
child.receiveShadow = true;
});
loadedE.scale.set(1.5, 1.3, 1.5);
loadedE.position.set(1.6, 2.8, -2)
loadedE.rotation.y = 0.5 * Math.PI;
scene.add(loadedE);
});
var loadedEBaseGeom = new THREE.BoxGeometry(10,4,5.5);
var loadedEBase = new THREE.Mesh(loadedEBaseGeom, libBaseMat);
loadedEBase.position.set(1.7,2.8,-1);
loadedEBase.name = "SBI, Indovation Lab, Startup Centre";
scene.add(loadedEBase);
//8Building
loader.load('assets/minimal8Building.obj', function(loaded8){
var material = new THREE.MeshLambertMaterial({color: 0xffefdc});
material.side = THREE.DoubleSide;
loaded8.children.forEach(function(child){
child.material = material;
child.geometry.computeFaceNormals();
child.geometry.computeVertexNormals();
child.castShadow = true;
child.receiveShadow = true;
});
loaded8.scale.set(1.5, 1.5, 2);
loaded8.position.set(-2, 2.8, -7);
loaded8.rotation.y = 0.5 * Math.PI;
scene.add(loaded8);
var loaded82 = loaded8.clone();
loaded8.scale.set(1.5, 1.5, 2);
loaded8.position.set(6, 2.8, -14);
loaded8.rotation.y = 0.5 * Math.PI;
scene.add(loaded82)
});
var loaded81BaseGeom = new THREE.BoxGeometry(10,5,5.5);
var loaded81Base = new THREE.Mesh(loaded81BaseGeom, libBaseMat);
loaded81Base.position.set(-1,3.2,-8.5);
loaded81Base.name = "ECE Department, CSE Department, Central Computing Centre"
scene.add(loaded81Base);
var loaded82Base = loaded81Base.clone();
loaded82Base.position.set(7,3.2,-15.3);
loaded82Base.name = "Department Building (Under Construction)";
scene.add(loaded82Base);
//New Gallery
// loader.load('assets/minimalNewGallery.obj', function(loadedMesh){
//
// var material = new THREE.MeshLambertMaterial({color: 0xffffff});
// material.side = THREE.DoubleSide;
//
// loadedMesh.children.forEach(function(child){
// child.material = material;
// child.geometry.computeFaceNormals();
// child.geometry.computeVertexNormals();
// child.castShadow = true;
// child.receiveShadow = true;
// });
//
// loadedMesh.scale.set(1.5, 1.5, 1.5);
// loadedMesh.position.z = 10;
// loadedMesh.position.y = 2.8;
// scene.add(loadedMesh);
//
// });
//Directional Light
var directionalLight = new THREE.DirectionalLight('#ffffff');
directionalLight.position.set(-15, 25, 15);
directionalLight.castShadow = true;
directionalLight.shadowCameraNear = 2;
directionalLight.shadowCameraFar = 200;
directionalLight.shadowCameraLeft = -100;
directionalLight.shadowCameraRight = 100;
directionalLight.shadowCameraTop = 100;
directionalLight.shadowCameraBottom = -100;
directionalLight.distance = 0;
directionalLight.intensity = 1;
directionalLight.shadowMapHeight = 1024;
directionalLight.shadowMapWidth = 1024;
scene.add(directionalLight);
//Orbit Controls
orbitControls = new THREE.OrbitControls(camera);
orbitControls.autoRotate = true;
clock = new THREE.Clock();
//Add Renderer to DOM
document.body.appendChild(renderer.domElement);
//Projector
projector = new THREE.Projector();
renderer.domElement.addEventListener('mousedown', function(event){
var vector = new THREE.Vector3(
renderer.devicePixelRatio * (event.pageX - this.offsetLeft) / this.width * 2 - 1,
-renderer.devicePixelRatio * (event.pageY - this.offsetTop) / this.height * 2 + 1,
0
);
projector.unprojectVector(vector, camera);
var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
var intersects = raycaster.intersectObjects(scene.children);
if(intersects.length>0){
if(intersects[0].object.name){
var name = intersects[0].object.name;
side.innerHTML = name;
}
}
}, false);
//render
render();
}
//Render Function
function render(){
requestAnimationFrame(render);
var delta = clock.getDelta();
orbitControls.update(delta);
renderer.render(scene, camera);
}
//Window Onload
window.onload = init;
</script>
</html>