Skip to content

Commit 45f80cf

Browse files
committedApr 18, 2012
Added experimental VTK Loader.
1 parent 21a5aa4 commit 45f80cf

File tree

3 files changed

+207
-1
lines changed

3 files changed

+207
-1
lines changed
 

‎examples/js/loaders/VTKLoader2.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @author mrdoob / http://mrdoob.com/
3+
*/
4+
5+
THREE.VTKLoader2 = function () {};
6+
7+
THREE.VTKLoader2.prototype = new THREE.Loader();
8+
THREE.VTKLoader2.prototype.constructor = THREE.VTKLoader2;
9+
10+
THREE.VTKLoader2.prototype.load = function ( url, callback ) {
11+
12+
var that = this;
13+
var xhr = new XMLHttpRequest();
14+
15+
xhr.onreadystatechange = function () {
16+
17+
if ( xhr.readyState == 4 ) {
18+
19+
if ( xhr.status == 200 || xhr.status == 0 ) {
20+
21+
callback( that.parse( xhr.responseText ) );
22+
23+
} else {
24+
25+
console.error( 'THREE.VTKLoader: Couldn\'t load ' + url + ' (' + xhr.status + ')' );
26+
27+
}
28+
29+
}
30+
31+
};
32+
33+
xhr.open( "GET", url, true );
34+
xhr.send( null );
35+
36+
};
37+
38+
THREE.VTKLoader2.prototype.parse = function ( data ) {
39+
40+
var geometry = new THREE.Geometry();
41+
42+
function vertex( x, y, z ) {
43+
44+
geometry.vertices.push( new THREE.Vector3( x, y, z ) );
45+
46+
}
47+
48+
function face3( a, b, c ) {
49+
50+
geometry.faces.push( new THREE.Face3( a, b, c ) );
51+
52+
}
53+
54+
var pattern, result;
55+
56+
// float float float
57+
58+
pattern = /([\d|\.|\+|\-|e]+) ([\d|\.|\+|\-|e]+) ([\d|\.|\+|\-|e]+)/g;
59+
60+
while ( ( result = pattern.exec( data ) ) != null ) {
61+
62+
// ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
63+
64+
vertex( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
65+
66+
}
67+
68+
// 3 int int int
69+
70+
pattern = /3 ([\d]+) ([\d]+) ([\d]+) /g;
71+
72+
while ( ( result = pattern.exec( data ) ) != null ) {
73+
74+
// ["3 1 2 3", "1", "2", "3"]
75+
76+
face3( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
77+
78+
}
79+
80+
geometry.computeCentroids();
81+
geometry.computeFaceNormals();
82+
geometry.computeVertexNormals();
83+
geometry.computeBoundingSphere();
84+
85+
return geometry;
86+
87+
}

‎examples/webgl_loader_vtk.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html>
22
<html lang="en">
33
<head>
4-
<title>three.js webgl - io - vtk loader</title>
4+
<title>three.js webgl - loaders - vtk loader</title>
55
<meta charset="utf-8">
66
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
77
<style>

‎examples/webgl_loader_vtk2.html

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>three.js webgl - loaders - vtk loader (experimental)</title>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
7+
<style>
8+
body {
9+
font-family: Monospace;
10+
background-color: #000;
11+
color: #fff;
12+
margin: 0px;
13+
overflow: hidden;
14+
}
15+
#info {
16+
color: #fff;
17+
position: absolute;
18+
top: 10px;
19+
width: 100%;
20+
text-align: center;
21+
z-index: 100;
22+
display:block;
23+
}
24+
#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
25+
</style>
26+
</head>
27+
28+
<body>
29+
<div id="info">
30+
<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> -
31+
vtk format loader test -
32+
model from <a href="http://www.cc.gatech.edu/projects/large_models/" target="_blank">The GeorgiaTech Lagre Geometric Model Archive</a>,
33+
</div>
34+
35+
<script src="../build/Three.js"></script>
36+
<script src="js/loaders/VTKLoader2.js"></script>
37+
38+
<script src="js/Detector.js"></script>
39+
<script src="js/Stats.js"></script>
40+
41+
<script>
42+
43+
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
44+
45+
var container, stats;
46+
47+
var camera, controls, scene, renderer;
48+
49+
var cross;
50+
51+
52+
// scene and camera
53+
54+
scene = new THREE.Scene();
55+
56+
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
57+
camera.position.z = 0.2;
58+
59+
60+
scene.add( camera );
61+
62+
controls = new THREE.TrackballControls( camera );
63+
64+
controls.rotateSpeed = 5.0;
65+
controls.zoomSpeed = 5;
66+
controls.panSpeed = 2;
67+
68+
controls.noZoom = false;
69+
controls.noPan = false;
70+
71+
controls.staticMoving = true;
72+
controls.dynamicDampingFactor = 0.3;
73+
74+
// light
75+
76+
var dirLight = new THREE.DirectionalLight( 0xffffff );
77+
dirLight.position.set( 200, 200, 1000 ).normalize();
78+
camera.add( dirLight );
79+
camera.add( dirLight.target );
80+
81+
// renderer
82+
83+
renderer = new THREE.WebGLRenderer( { antialias: false } );
84+
renderer.setClearColorHex( 0x000000, 1 );
85+
renderer.setSize( window.innerWidth, window.innerHeight );
86+
87+
container = document.createElement( 'div' );
88+
document.body.appendChild( container );
89+
container.appendChild( renderer.domElement );
90+
91+
stats = new Stats();
92+
stats.domElement.style.position = 'absolute';
93+
stats.domElement.style.top = '0px';
94+
container.appendChild( stats.domElement );
95+
96+
97+
var material = new THREE.MeshLambertMaterial( { color:0xffffff} );
98+
99+
var loader=new THREE.VTKLoader2();
100+
loader.load ("models/vtk/bunny.vtk", function(geom){
101+
var mesh = new THREE.Mesh(geom, material );
102+
mesh.doubleSided=true;
103+
mesh.position.setY(-0.09);
104+
scene.add( mesh );
105+
animate();
106+
});
107+
108+
function animate() {
109+
110+
requestAnimationFrame( animate );
111+
controls.update();
112+
renderer.render( scene, camera );
113+
stats.update();
114+
115+
}
116+
</script>
117+
118+
</body>
119+
</html>

0 commit comments

Comments
 (0)