-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexample.html
321 lines (274 loc) · 10.7 KB
/
example.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>VRML loading with ThreeJs and VrmlParser</title>
<meta charset="utf-8">
<meta name="X-Content-Type-Options" content="disabled">
<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;
}
canvas {
border: 1px solid red;
width: 100%;
height: 100%;
margin: 0;
}
#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
}
#viewpoints {
background-color: gray;
z-index: 101;
position: absolute;
width: 140px;
top: 10px;
left: 10px;
padding: 2px;
border: 1px solid white;
}
#viewpoints div {
width: inherit;
}
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank">three.js / VrmlParser</a> -
vrml format loader test using VrmlParser -
</div>
<div id="viewpoints">
<h4>Viewpoints from the VRML file</h4>
</div>
<!--<script src="node_modules/three/build/three.js"></script>-->
<!--<script src="node_modules/three/build/three.min.js"></script>-->
<!-- use scripts below for development and debugging -->
<script src="vrml.js"></script>
<script type="module" src="Renderer/ThreeJs.js"></script>
<script type="module" src="Renderer/ThreeJs/Animation.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode.js"></script>
<script type="module" src="Renderer/ThreeJs/Analyzer.js"></script>
<script type="module" src="Renderer/ThreeJs/SmoothEdges.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode/Interpolator.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode/OrientationInterpolator.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode/PositionInterpolator.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode/DirectionalLight.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode/PointLight.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode/Text.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode/Inline.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode/Viewpoint.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode/NavigationInfo.js"></script>
<script type="module" src="Renderer/ThreeJs/VrmlNode/IndexedFaceSet.js"></script>
<script src="node_modules/tween.js/src/Tween.js"></script>
<!-- use the minified version for integration with Three.js and for speed -->
<!--<script src="vrml.min.js"></script>-->
<script type="module" src="node_modules/three/examples/jsm/renderers/Projector.js"></script>
<script type="module" src="node_modules/three/examples/jsm/controls/OrbitControls.js"></script>
<script type="module" src="node_modules/three/examples/jsm/controls/FlyControls.js"></script>
<script type="module" src="node_modules/three/examples/jsm/controls/FirstPersonControls.js"></script>
<script type="module" src="node_modules/three/examples/jsm/controls/TrackballControls.js"></script>
<script type="module">
import VrmlParser from "./Renderer/ThreeJs.js";
// import {THREE as THREE} from "./node_modules/three/src/Three.d.ts";
import {Scene} from "./node_modules/three/src/scenes/Scene.js";
import {Vector3} from "./node_modules/three/src/math/Vector3.js";
import {FileLoader} from "./node_modules/three/src/loaders/FileLoader.js";
import {WebGLRenderer} from "./node_modules/three/src/renderers/WebGLRenderer.js";
import {OrbitControls} from "./node_modules/three/examples/jsm/controls/OrbitControls.js";
import {Clock} from "./node_modules/three/src/core/Clock.js";
import {PerspectiveCamera} from "./node_modules/three/src/cameras/PerspectiveCamera.js";
import {Fog} from "./node_modules/three/src/scenes/Fog.js";
VrmlParser.Renderer.ThreeJs.VrmlNode.Text.fontsDir = 'wrl/fonts';
var debug = true;
var container;
var camera, controls, scene, renderer;
// These variables are used for animating viewpoints, but not in use until interferance with controls has been solved
// var cameraTween, newCameraQuaternion, rotationDelta = 0;
var animation;
var analyzer = new VrmlParser.Renderer.ThreeJs.Analyzer(debug);
// setup a clock
var clock = new Clock();
init();
animate();
function resetControls(renderer) {
controls = new OrbitControls(camera, renderer.domElement);
controls.rotateSpeed = 1;
controls.zoomSpeed = 0.2;
controls.panSpeed = 0.2;
controls.enableZoom = true;
controls.enablePan = true;
}
function selectViewpoint(event, vrmlConverter) {
var viewpoint = event.target.dataset.name;
if ( debug ) {
console.log('clicked ' + viewpoint);
}
var c = vrmlConverter.viewpoints[ viewpoint ].getCamera();
var v = c.position;
var o = c.orientation;
camera.position.set(v.x, v.y, v.z);
// fake the name of a surrounding group for the camera, so that it can be animated as any other object.
camera.name = 'surrounding_' + viewpoint;
var vector3 = new Vector3(o.x, o.y, o.z);
camera.quaternion.setFromAxisAngle(vector3, o.radians);
camera.aspect = c.aspect;
camera.near = c.near;
camera.far = c.far;
camera.fov = c.fov;
camera.updateProjectionMatrix();
// now animate the camera to the position and rotation of the new camera
// Disabled until I find a way not to let it interfere with controls
// var newPosition = new Vector3(v.x, v.y, v.z);
// cameraTween = new TWEEN.Tween(camera.position)
// .to(newPosition)
// .start(+ new Date())
// ;
//
//
// var vector3 = new Vector3(o.x, o.y, o.z);
// newCameraQuaternion = new Quaternion();
// newCameraQuaternion.setFromAxisAngle(vector3, o.radians);
}
function init() {
camera = new PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 1e5);
camera.position.z = 4;
camera.position.y = 3;
camera.position.x = 0;
scene = new Scene();
// var light = new PointLight(new Color(1,1,1), 1);
// light.position.set(2,2,2);
// scene.add(light);
// @todo: only add fog when present in the scene
scene.fog = new Fog(0xffffff, 1, 5000);
scene.fog.color.setHSL(0.6, 0, 1);
scene.add(camera);
var vrmlConverter = new VrmlParser.Renderer.ThreeJs(debug);
renderer = new WebGLRenderer({ antialias: true });
// renderer.domElement = document.body;
// renderer
resetControls(renderer);
renderer.shadowMap.enabled = true;
renderer.setClearColor(0x000000, 1);
renderer.setSize(window.innerWidth, window.innerHeight);
// add support for animation and interaction
animation = new VrmlParser.Renderer.ThreeJs.Animation(debug);
animation.init(camera, renderer);
// initialize the viewpoinst with the camera from the global scope
vrmlConverter.viewpoints = {
'reset': {
getCamera: function () {
return {
// these are just some properties of the default camera the scene was initialized with
position: { x: 0, y: 3, z: 4 },
name: 'reset',
orientation: { x: 0, y: 1, z: 0, radians: 0 },
fov: 60,
aspect: window.innerWidth / window.innerHeight,
near: 0.01,
far: 1e5
}
}
}
}; // key value store of cameras based on VRML viewpoint nodes, stored by their name.
// VRML parser example:
var fileLoader = new FileLoader();
// onLoad, onProgress, onError
fileLoader.load('wrl/house.wrl', function (data) {
try {
var tree = vrmlParser.parse(data);
} catch ( e ) {
console.log('Exception with message ' + e.message);
if ( undefined !== e.location ) {
console.log('Exception at location start: offset: ' + e.location.start.offset + ' line: ' + e.location.start.line + ' column: ' + e.location.start.column);
console.log('Exception at location end: offset: ' + e.location.end.offset + ' line: ' + e.location.end.line + ' column: ' + e.location.end.column);
}
return;
}
if ( debug ) {
console.log(tree);
}
vrmlConverter.render(tree, scene, renderer, camera, controls);
// once the scene is rendered in threejs, a registry of mesh objects that responed to sensors can be build
animation.buildSensorRegistry(scene);
// @todo: move viewpointSelector to its own class
var viewpointSelector = document.getElementById('viewpoints');
if (debug) {
console.log(vrmlConverter.viewpoints);
}
for ( var a in vrmlConverter.viewpoints ) {
if ( typeof a === 'string' ) {
var option = document.createElement('div');
option.innerHTML = a;
option.setAttribute('data-name', a);
viewpointSelector.appendChild(option);
option.addEventListener('click', function (event) {
selectViewpoint(event, vrmlConverter);
});
}
}
}, function () {
}, function (error) {
var request = error.target;
if ( 404 === request.status ) {
console.log('VRML Document not found at ' + request.responseURL);
}
console.log(error);
});
container = document.createElement('div');
document.body.appendChild(container);
container.appendChild(renderer.domElement);
//
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);
var delta = clock.getDelta();
if ( undefined !== controls ) {
controls.update(delta);
}
// @todo: these animations are quite nice, but they get in the way of controls
// because they continue to update the camera position
// so you should probably register and deregister the animation on viewpoint change
// and tween / slerp complete
// animate camera position on viewpoint change
// disabled until I find a way to not have it interfere with controls
// if (cameraTween) {
// cameraTween.update(+ new Date());
// }
// animate camera rotation on viewpoint change
// if (newCameraQuaternion) {
// rotationDelta = (rotationDelta + 8e-3) % 1;
// camera.quaternion.slerp(newCameraQuaternion, rotationDelta);
// if (rotationDelta >= 1) {
// newCameraQuaternion = null;
// rotationDelta = 0;
// }
// }
animation.update(delta);
renderer.render(scene, camera);
}
</script>
</body>
</html>