-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
218 lines (156 loc) · 7.47 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>3D view - rocket flight control - kicad vrml viewer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a, .button {
color: #f00;
font-weight: bold;
text-decoration: underline;
cursor: pointer
}
</style>
</head>
<body>
<div id="info">
<a href="https://en.wikipedia.org/wiki/Launch_Vehicle_Digital_Computer" target="_blank" rel="noopener">flight computer</a> -
kicad vrml 3D viewer
<br>
The model may take long time to appear depending on network speed.
<br>
Designed by <a href="https://abhisandhi.netlify.app/">Amit Barman</a>
<br>
<!-- <a id="fit" title="Fit" href="" onclick="zoom_refit ();return false;">zoom fit</a>
<a id="rot" title="Rot" href="" onclick="toggle_rotation ();return false;">toggle rotation</a> -->
</div>
<script src="./special/three.js"></script>
<script src="./special/OrbitControls.js"></script>
<script src="./special/VRMLLoader.js"></script>
<script src="./special/WebGL.js"></script>
<script>
var rotate = false; var cameraZ;
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
function toggle_rotation () {
rotate = ! rotate;
}
function zoom_refit () {
r=cameraZ*1.25;
// r=cameraZ*1;
camera.position.x=r*Math.cos(2);
camera.position.z=r*Math.sin(2);
camera.lookAt(scene.position);
renderer.render( scene, camera );
}
function fitCameraToObject( camera, object, offset, controls ) {
offset = offset || 1.35;
const boundingBox = new THREE.Box3();
// get bounding box of object - this will be used to setup controls and camera
boundingBox.setFromObject( object );
//ERRORS HERE
const center = boundingBox.getCenter();
const size = boundingBox.getSize();
// get the max side of the bounding box (fits to width OR height as needed )
const maxDim = Math.max( size.x, size.y, size.z );
const fov = camera.fov * ( Math.PI / 180 );
cameraZ = Math.abs( maxDim / 2 * Math.tan( fov * 2 ) ); //Applied fifonik correction
cameraZ *= offset; // zoom out a little so that objects don't fill the screen
// <--- NEW CODE
//Method 1 to get object's world position
scene.updateMatrixWorld(); //Update world positions
var objectWorldPosition = new THREE.Vector3();
objectWorldPosition.setFromMatrixPosition( object.matrixWorld );
//Method 2 to get object's world position
//objectWorldPosition = object.getWorldPosition();
const directionVector = camera.position.sub(objectWorldPosition); //Get vector from camera to object
const unitDirectionVector = directionVector.normalize(); // Convert to unit vector
camera.position = unitDirectionVector.multiplyScalar(cameraZ); //Multiply unit vector times cameraZ distance
camera.lookAt(objectWorldPosition); //Look at object
// --->
const minZ = boundingBox.min.z;
const cameraToFarEdge = ( minZ < 0 ) ? -minZ + cameraZ : cameraZ - minZ;
camera.far = cameraToFarEdge * 3;
camera.updateProjectionMatrix();
if ( controls ) {
// set camera to rotate around center of loaded object
controls.target = center;
// prevent camera from zooming out far enough to create far plane cutoff
controls.maxDistance = cameraToFarEdge * 2;
// ERROR HERE
controls.saveState();
} else {
camera.lookAt( center )
}
}
var container; //var container, stats;
var camera, controls, scene, renderer;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
camera.position.z = 6;
controls = new THREE.OrbitControls( camera );
scene = new THREE.Scene();
scene.add( camera );
// light
var dirLight = new THREE.DirectionalLight( 0xffffff );
dirLight.position.set( 200, 200, 1000 ).normalize();
camera.add( dirLight );
camera.add( dirLight.target );
var loader = new THREE.VRMLLoader();
// loader.load( 'kicad-test.wrl', function ( object ) {
loader.load( 'special/rfc.wrl', function ( object ) {
scene.add( object );
fitCameraToObject ( camera, object, 1.35, controls );
} );
// renderer
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container = document.createElement( 'div' );
document.body.appendChild( container );
container.appendChild( renderer.domElement );
// stats = new Stats();
// container.appendChild( stats.dom );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
if (rotate) {
var timer=Date.now() * 0.0005;
r=cameraZ*1.25;
camera.position.x=r*Math.cos(timer);
camera.position.z=r*Math.sin(timer);
camera.lookAt(scene.position);
}
renderer.render( scene, camera );
// stats.update();
}
</script>
</body>
</html>