-
Notifications
You must be signed in to change notification settings - Fork 115
/
va3c-viewer.js.bak
191 lines (152 loc) · 6.29 KB
/
va3c-viewer.js.bak
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
var VA3C = {};
// var info, stats, renderer, scene, camera, controls;
var obj, light;
// VA3C.fname = '../json/twoMobius.json';
// VA3C.fname = '../RvtVa3c/models/Wall.rvt.js';
// VA3C.fname = '../json/Project2.rvt.js';
VA3C.fname = '../json/Hex_01.js';
var pi = Math.PI, pi05 = pi * 0.5, pi2 = pi + pi;
var d2r = pi / 180, r2d = 180 / pi; // degrees / radians
var projector = new THREE.Projector();
var targetList = [];
var geometry, material, mesh;
function init() {
document.body.style.cssText = 'font: 600 12pt monospace; margin: 0; overflow: hidden' ;
VA3C.info = document.body.appendChild( document.createElement( 'div' ) );
VA3C.info.style.cssText = 'left: 20px; position: absolute; top: 0px; width: 100% ';
VA3C.info.innerHTML = '<h1>' + document.title + '<h1>' +
'<div id=msg></div>';
VA3C.stats = new Stats();
VA3C.stats.domElement.style.cssText = 'bottom: 0; position: absolute; left: 0; zIndex: 100; ';
document.body.appendChild( VA3C.stats.domElement );
VA3C.renderer = new THREE.WebGLRenderer( { alpha: 1, antialias: true, clearColor: 0xffffff } );
VA3C.renderer.setSize( window.innerWidth, window.innerHeight );
VA3C.renderer.shadowMapEnabled = true;
document.body.appendChild( VA3C.renderer.domElement );
VA3C.scene = new THREE.Scene();
VA3C.camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100000 );
VA3C.camera.position.set( 15000, 15000, 15000 );
VA3C.controls = new THREE.OrbitControls( VA3C.camera, VA3C.renderer.domElement );
document.addEventListener( 'mousedown', clickHandler, false );
loadJS( VA3C.fname );
}
function loadJS ( fname ) {
if ( obj ) VA3C.scene.remove( obj );
obj = new THREE.Object3D();
var loader = new THREE.ObjectLoader();
loader.load(fname, function(obj){
VA3C.scene = obj;
VA3C.scene.add(new THREE.AmbientLight(0x444444));
updateLight( 2014, 5, 18, 22, 30, 00, 42, -75 );
// axes
function v( x, y, z ){ return new THREE.Vector3( x, y, z ); }
VA3C.scene.add( new THREE.ArrowHelper( v(1, 0, 0), v(0, 0, 0), 30, 0xcc0000) );
VA3C.scene.add( new THREE.ArrowHelper( v(0, 1, 0), v(0, 0, 0), 30, 0x00cc00) );
VA3C.scene.add( new THREE.ArrowHelper( v(0, 0, 1), v(0, 0, 0), 30, 0x0000cc) );
// ground box
geometry = new THREE.BoxGeometry( 20000, 100, 20000 );
material = new THREE.MeshBasicMaterial( { color: 0xaaaaaa } );
mesh = new THREE.Mesh( geometry, material );
mesh.position.set( 0, -10, 0 );
mesh.castShadow = true;
mesh.receiveShadow = true;
VA3C.scene.add( mesh );
//call compute function
computeNormalsAndFaces();
});
}
function v( x, y, z ){ return new THREE.Vector3( x, y, z ); }
function updateLight() {
if ( light ) { VA3C.scene.remove( light ); }
light = new THREE.DirectionalLight( 0xffffff, 1 );
// (year, month, day, hour, minutes, sec, lat, long)
var latlon = sunPosition( 2014, month.value, day.value, hour.value, 60, 00, latlong[0], latlong[1] );
// console.log ( latlon );
var pos = convertPosition( latlon[0], latlon[1], 10000 );
// var pos = convertPosition( 43, -75, 10000 );
light.position = pos;
light.castShadow = true;
light.shadowMapWidth = 2048;
light.shadowMapHeight = 2048;
var d = 10000;
light.shadowCameraLeft = -d;
light.shadowCameraRight = d;
light.shadowCameraTop = d * 2;
light.shadowCameraBottom = -d * 2;
light.shadowCameraNear = 1000;
light.shadowCameraFar = 20000;
// light.shadowCameraVisible = true;
VA3C.scene.add( light );
}
function convertPosition( lat, lon, radius ) {
var rc = radius * Math.cos( lat * d2r );
return v( rc * Math.cos( lon * d2r ), radius * Math.sin( lat * d2r ), rc * Math.sin( lon * d2r) );
}
function getComboA(sel) {
// console.log( sel );
var value = sel.value;
//alert(value);
var latlong;
switch(value) {
case "New York":
latlong = [42.3482, -75.189];
break;
case "Sao Paulo":
latlong = [-23.55, -46.633];
break;
case "Paris":
latlong = [48.8567, 2.3508];
break;
case "Moscow":
latlong = [55.75, 37.6167];
break;
case "Tokyo":
latlong = [35.6895, 139.6917];
break;
default:
latlong = [42.3482, -75.189];
}
//tokyoLatitude:35.6895, Longitude:139.6917
//New York coordinates
//Latitude:42.3482, Longitude:-75.189
//saoaulo
//Latitude:-23.55, Longitude:-46.633
//Moscow coordinates
//Latitude:55.75, Longitude:37.6167
//Paris coordinates
//Latitude:48.8567, Longitude:2.3508
// alert(latlong);
updateLight( 2014, 5, 18, 22, 30, 00, latlong[0], latlong[2])
}
function animate() {
requestAnimationFrame( animate );
VA3C.renderer.render( VA3C.scene, VA3C.camera );
VA3C.controls.update( );
VA3C.stats.update();
}
function computeNormalsAndFaces() {
for(var i=0; i<VA3C.scene.children.length; i++){
if( VA3C.scene.children[i].hasOwnProperty("geometry")){
VA3C.scene.children[i].geometry.mergeVertices();
VA3C.scene.children[i].castShadow = true;
VA3C.scene.children[i].geometry.computeFaceNormals();
targetList.push(VA3C.scene.children[i])
}
}
}
function clickHandler(event){
event.preventDefault();
var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
projector.unprojectVector( vector, VA3C.camera );
var raycaster = new THREE.Raycaster( VA3C.camera.position, vector.sub( VA3C.camera.position ).normalize() );
//var raycaster = new THREE.Raycaster( VA3C.camera.position, vector.sub( ).normalize() );
var intersects = raycaster.intersectObjects( targetList );
//var intersects = raycaster.intersectObjects( VA3C.scene.children.geometry );
if ( intersects.length > 0 ) {
intersects[ 0 ].object.material.color.setHex( Math.random() * 0xffffff );
var particle = new THREE.Sprite( particleMaterial );
particle.position = intersects[ 0 ].point;
particle.scale.x = particle.scale.y = 16;
VA3C.scene.add( particle );
}
}