Skip to content

Commit c50d6e1

Browse files
committedMay 8, 2012
VTKLoader using EventTarget.
JSONLoader next...
1 parent 9c9cf2f commit c50d6e1

File tree

2 files changed

+107
-90
lines changed

2 files changed

+107
-90
lines changed
 

‎examples/js/loaders/VTKLoader.js

+59-52
Original file line numberDiff line numberDiff line change
@@ -2,104 +2,111 @@
22
* @author mrdoob / http://mrdoob.com/
33
*/
44

5-
THREE.VTKLoader = function () {};
5+
THREE.VTKLoader = function () {
66

7-
THREE.VTKLoader.prototype = new THREE.Loader();
8-
THREE.VTKLoader.prototype.constructor = THREE.VTKLoader;
7+
THREE.EventTarget.call( this );
98

10-
THREE.VTKLoader.prototype.load = function ( url, callback ) {
9+
};
10+
11+
THREE.VTKLoader.prototype = {
12+
13+
constructor: THREE.VTKLoader,
14+
15+
load: function ( url ) {
16+
17+
var scope = this;
18+
var xhr = new XMLHttpRequest();
1119

12-
var that = this;
13-
var xhr = new XMLHttpRequest();
20+
xhr.onreadystatechange = function () {
1421

15-
xhr.onreadystatechange = function () {
22+
if ( xhr.readyState == 4 ) {
1623

17-
if ( xhr.readyState == 4 ) {
24+
if ( xhr.status == 200 || xhr.status == 0 ) {
1825

19-
if ( xhr.status == 200 || xhr.status == 0 ) {
26+
scope.dispatchEvent( { type: 'complete', content: scope.parse( xhr.responseText ) } );
2027

21-
callback( that.parse( xhr.responseText ) );
28+
} else {
2229

23-
} else {
30+
scope.dispatchEvent( { type: 'error', status: xhr.status } );
2431

25-
console.error( 'THREE.VTKLoader: Couldn\'t load ' + url + ' (' + xhr.status + ')' );
32+
}
2633

2734
}
2835

29-
}
36+
};
3037

31-
};
38+
xhr.open( 'GET', url, true );
39+
xhr.send( null );
3240

33-
xhr.open( "GET", url, true );
34-
xhr.send( null );
41+
},
3542

36-
};
43+
parse: function ( data ) {
3744

38-
THREE.VTKLoader.prototype.parse = function ( data ) {
45+
var geometry = new THREE.Geometry();
3946

40-
var geometry = new THREE.Geometry();
47+
function vertex( x, y, z ) {
4148

42-
function vertex( x, y, z ) {
49+
geometry.vertices.push( new THREE.Vector3( x, y, z ) );
4350

44-
geometry.vertices.push( new THREE.Vector3( x, y, z ) );
51+
}
4552

46-
}
53+
function face3( a, b, c ) {
4754

48-
function face3( a, b, c ) {
55+
geometry.faces.push( new THREE.Face3( a, b, c ) );
4956

50-
geometry.faces.push( new THREE.Face3( a, b, c ) );
57+
}
5158

52-
}
59+
function face4( a, b, c, d ) {
5360

54-
function face4( a, b, c, d ) {
61+
geometry.faces.push( new THREE.Face4( a, b, c, d ) );
5562

56-
geometry.faces.push( new THREE.Face4( a, b, c, d ) );
63+
}
5764

58-
}
65+
var pattern, result;
5966

60-
var pattern, result;
67+
// float float float
6168

62-
// float float float
69+
pattern = /([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)/g;
6370

64-
pattern = /([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)/g;
71+
while ( ( result = pattern.exec( data ) ) != null ) {
6572

66-
while ( ( result = pattern.exec( data ) ) != null ) {
73+
// ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
6774

68-
// ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
75+
vertex( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
6976

70-
vertex( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
77+
}
7178

72-
}
79+
// 3 int int int
7380

74-
// 3 int int int
81+
pattern = /3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
7582

76-
pattern = /3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
83+
while ( ( result = pattern.exec( data ) ) != null ) {
7784

78-
while ( ( result = pattern.exec( data ) ) != null ) {
85+
// ["3 1 2 3", "1", "2", "3"]
7986

80-
// ["3 1 2 3", "1", "2", "3"]
87+
face3( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
8188

82-
face3( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
89+
}
8390

84-
}
91+
// 4 int int int int
8592

86-
// 4 int int int int
93+
pattern = /4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
8794

88-
pattern = /4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
95+
while ( ( result = pattern.exec( data ) ) != null ) {
8996

90-
while ( ( result = pattern.exec( data ) ) != null ) {
97+
// ["4 1 2 3 4", "1", "2", "3", "4"]
9198

92-
// ["4 1 2 3 4", "1", "2", "3", "4"]
99+
face4( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
93100

94-
face4( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
101+
}
95102

96-
}
103+
geometry.computeCentroids();
104+
geometry.computeFaceNormals();
105+
geometry.computeVertexNormals();
106+
geometry.computeBoundingSphere();
97107

98-
geometry.computeCentroids();
99-
geometry.computeFaceNormals();
100-
geometry.computeVertexNormals();
101-
geometry.computeBoundingSphere();
108+
return geometry;
102109

103-
return geometry;
110+
}
104111

105112
}

‎examples/webgl_loader_vtk.html

+48-38
Original file line numberDiff line numberDiff line change
@@ -48,71 +48,81 @@
4848

4949
var cross;
5050

51+
init();
52+
animate();
5153

52-
// scene and camera
54+
function init() {
5355

54-
scene = new THREE.Scene();
56+
// scene and camera
5557

56-
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
57-
camera.position.z = 0.2;
58+
scene = new THREE.Scene();
5859

60+
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
61+
camera.position.z = 0.2;
62+
scene.add( camera );
5963

60-
scene.add( camera );
64+
controls = new THREE.TrackballControls( camera );
6165

62-
controls = new THREE.TrackballControls( camera );
66+
controls.rotateSpeed = 5.0;
67+
controls.zoomSpeed = 5;
68+
controls.panSpeed = 2;
6369

64-
controls.rotateSpeed = 5.0;
65-
controls.zoomSpeed = 5;
66-
controls.panSpeed = 2;
70+
controls.noZoom = false;
71+
controls.noPan = false;
6772

68-
controls.noZoom = false;
69-
controls.noPan = false;
73+
controls.staticMoving = true;
74+
controls.dynamicDampingFactor = 0.3;
7075

71-
controls.staticMoving = true;
72-
controls.dynamicDampingFactor = 0.3;
76+
// light
7377

74-
// light
78+
var dirLight = new THREE.DirectionalLight( 0xffffff );
79+
dirLight.position.set( 200, 200, 1000 ).normalize();
80+
camera.add( dirLight );
81+
camera.add( dirLight.target );
7582

76-
var dirLight = new THREE.DirectionalLight( 0xffffff );
77-
dirLight.position.set( 200, 200, 1000 ).normalize();
78-
camera.add( dirLight );
79-
camera.add( dirLight.target );
83+
var material = new THREE.MeshLambertMaterial( { color:0xffffff} );
8084

81-
// renderer
85+
var loader = new THREE.VTKLoader();
86+
loader.addEventListener( 'complete', function ( event ) {
8287

83-
renderer = new THREE.WebGLRenderer( { antialias: false } );
84-
renderer.setClearColorHex( 0x000000, 1 );
85-
renderer.setSize( window.innerWidth, window.innerHeight );
88+
var geometry = event.content;
8689

87-
container = document.createElement( 'div' );
88-
document.body.appendChild( container );
89-
container.appendChild( renderer.domElement );
90+
var mesh = new THREE.Mesh( geometry, material );
91+
mesh.doubleSided = true;
92+
mesh.position.setY( - 0.09 );
93+
scene.add( mesh );
9094

91-
stats = new Stats();
92-
stats.domElement.style.position = 'absolute';
93-
stats.domElement.style.top = '0px';
94-
container.appendChild( stats.domElement );
95+
} );
96+
loader.load ( "models/vtk/bunny.vtk" );
9597

98+
// renderer
9699

97-
var material = new THREE.MeshLambertMaterial( { color:0xffffff} );
100+
renderer = new THREE.WebGLRenderer( { antialias: false } );
101+
renderer.setClearColorHex( 0x000000, 1 );
102+
renderer.setSize( window.innerWidth, window.innerHeight );
98103

99-
var loader=new THREE.VTKLoader();
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-
});
104+
container = document.createElement( 'div' );
105+
document.body.appendChild( container );
106+
container.appendChild( renderer.domElement );
107+
108+
stats = new Stats();
109+
stats.domElement.style.position = 'absolute';
110+
stats.domElement.style.top = '0px';
111+
container.appendChild( stats.domElement );
112+
113+
}
107114

108115
function animate() {
109116

110117
requestAnimationFrame( animate );
118+
111119
controls.update();
112120
renderer.render( scene, camera );
121+
113122
stats.update();
114123

115124
}
125+
116126
</script>
117127

118128
</body>

0 commit comments

Comments
 (0)
Please sign in to comment.