Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 612455c

Browse files
committed
Merge branch 'IndexedFaceSetImport' of https://github.com/bartmcleod/three.js into dev
Conflicts: examples/js/loaders/VRMLLoader.js
2 parents 4b324c3 + 8a258a4 commit 612455c

File tree

3 files changed

+5327
-28
lines changed

3 files changed

+5327
-28
lines changed

examples/js/loaders/VRMLLoader.js

Lines changed: 143 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ THREE.VRMLLoader = function () {};
66

77
THREE.VRMLLoader.prototype = {
88

9-
constructor: THREE.VTKLoader,
9+
constructor: THREE.VRMLLoader,
1010

1111
load: function ( url, callback ) {
1212

@@ -52,18 +52,33 @@ THREE.VRMLLoader.prototype = {
5252

5353
var tree = { 'string': 'Scene', children: [] };
5454
var current = tree;
55+
var matches;
5556

5657
for ( var i = 0; i < lines.length; i ++ ) {
5758

59+
var comment = '';
60+
5861
var line = lines[ i ];
5962

60-
if ( /^#/.exec( line ) ) {
63+
// omit whitespace only lines
64+
if ( null !== ( result = /^\s+?$/g.exec( line ) ) ) {
65+
continue;
66+
}
67+
68+
if ( /#/.exec( line ) ) {
69+
70+
var parts = line.split('#');
6171

62-
continue;
72+
// discard everything after the #, it is a comment
73+
line = parts[0];
6374

64-
} else if ( /{/.exec( line ) ) {
75+
// well, let's also keep the comment
76+
comment = parts[1];
77+
}
78+
// todo: add collection like coordIndex and colorIndex who are delimited by [ ]
79+
if ( matches = /([^\s]*){1}\s?{/.exec( line ) ) { // first subpattern should match the Node name
6580

66-
var block = { 'string': line, 'parent': current, 'children': [] };
81+
var block = { 'nodeType' : matches[1], 'string': line, 'parent': current, 'children': [],'comment' : comment };
6782
current.children.push( block );
6883
current = block;
6984

@@ -80,13 +95,13 @@ THREE.VRMLLoader.prototype = {
8095

8196
} else if ( line !== '' ) {
8297

83-
current.children.push( line );
98+
current.children.push( line );
8499

85100
}
86101

87102
}
88103

89-
return tree;
104+
return tree;
90105

91106
}
92107

@@ -103,16 +118,29 @@ THREE.VRMLLoader.prototype = {
103118

104119
if ( /USE/.exec( data ) ) {
105120

106-
if ( /appearance/.exec( data ) ) {
121+
var defineKey = /USE\s+?(\w+)/.exec( data )[ 1 ];
107122

108-
parent.material = defines[ /USE (\w+)/.exec( data )[ 1 ] ].clone();
123+
if (undefined == defines[defineKey]) {
124+
debugger;
125+
console.warn(defineKey + ' is not defined.');
109126

110-
} else {
127+
} else {
111128

112-
var object = defines[ /USE (\w+)/.exec( data )[ 1 ] ].clone();
113-
parent.add( object );
129+
if ( /appearance/.exec( data ) && defineKey ) {
114130

115-
}
131+
parent.material = defines[ defineKey].clone();
132+
133+
} else if ( /geometry/.exec( data ) && defineKey ) {
134+
135+
parent.geometry = defines[ defineKey].clone();
136+
137+
} else if (defineKey){
138+
var object = defines[ defineKey ].clone();
139+
parent.add( object );
140+
141+
}
142+
143+
}
116144

117145
}
118146

@@ -123,12 +151,11 @@ THREE.VRMLLoader.prototype = {
123151
var object = parent;
124152

125153
if ( /Transform/.exec( data.string ) || /Group/.exec( data.string ) ) {
126-
127154
object = new THREE.Object3D();
128155

129156
if ( /DEF/.exec( data.string ) ) {
130157

131-
object.name = /DEF (\w+)/.exec( data.string )[ 1 ];
158+
object.name = /DEF\s+(\w+)/.exec( data.string )[ 1 ];
132159
defines[ object.name ] = object;
133160

134161
}
@@ -151,13 +178,14 @@ THREE.VRMLLoader.prototype = {
151178

152179
var result = float4_pattern.exec( child );
153180

154-
object.quaternion.set(
155-
parseFloat( result[ 1 ] ),
156-
parseFloat( result[ 2 ] ),
157-
parseFloat( result[ 3 ] ),
158-
parseFloat( result[ 4 ] )
159-
);
181+
var quaternion = new THREE.Quaternion();
160182

183+
var x = parseFloat( result[ 1 ] );
184+
var y = parseFloat(result[ 2 ]);
185+
var z = parseFloat(result[ 3 ]);
186+
var w = parseFloat(result[ 4 ]);
187+
188+
object.quaternion.setFromAxisAngle( new THREE.Vector3( x, y, z), w );
161189
} else if ( /scale/.exec( child ) ) {
162190

163191
var result = float3_pattern.exec( child );
@@ -179,10 +207,8 @@ THREE.VRMLLoader.prototype = {
179207
object = new THREE.Mesh();
180208

181209
if ( /DEF/.exec( data.string ) ) {
182-
183210
object.name = /DEF (\w+)/.exec( data.string )[ 1 ];
184211
defines[ object.name ] = object;
185-
186212
}
187213

188214
parent.add( object );
@@ -261,6 +287,96 @@ THREE.VRMLLoader.prototype = {
261287

262288
parent.geometry = new THREE.SphereGeometry( parseFloat( result[ 1 ] ) );
263289

290+
} else if ( /IndexedFaceSet/.exec( data.string ) ) {
291+
292+
var geometry = new THREE.Geometry();
293+
294+
var isRecordingCoordinates = false;
295+
296+
for (var i = 0, j = data.children.length; i < j; i++) {
297+
298+
var child = data.children[i];
299+
300+
var result;
301+
var vec;
302+
303+
if ( /Coordinate/.exec (child.string)) {
304+
305+
for (var k = 0, l = child.children.length; k < l; k++) {
306+
307+
var point = child.children[k];
308+
309+
if (null != (result = float3_pattern.exec(point))) {
310+
311+
vec = new THREE.Vector3(
312+
parseFloat(result[1]),
313+
parseFloat(result[2]),
314+
parseFloat(result[3])
315+
);
316+
317+
geometry.vertices.push( vec );
318+
}
319+
}
320+
}
321+
322+
if (/coordIndex/.exec(child)) {
323+
isRecordingCoordinates = true;
324+
}
325+
326+
var coordIndex = false;
327+
var points = [];
328+
var skip = 0;
329+
var regex = /(-?\d+)/g;
330+
// read this: http://math.hws.edu/eck/cs424/notes2013/16_Threejs_Advanced.html
331+
while ( isRecordingCoordinates && null != (coordIndex = regex.exec(child) ) ) {
332+
// parse coordIndex lines
333+
coordIndex = parseInt(coordIndex, 10);
334+
335+
points.push(coordIndex);
336+
337+
// -1 indicates end of face points
338+
if (coordIndex === -1) {
339+
// reset the collection
340+
points = [];
341+
}
342+
343+
// vrml support multipoint indexed face sets (more then 3 vertices). You must calculate the composing triangles here
344+
345+
skip = points.length -3;
346+
skip = skip < 0 ? 0 : skip;
347+
348+
// Face3 only works with triangles, but IndexedFaceSet allows shapes with more then three vertices, build them of triangles
349+
if (points.length >= 3) {
350+
var face = new THREE.Face3(
351+
points[0],
352+
points[skip + 1],
353+
points[skip + 2],
354+
null // normal, will be added later
355+
// todo: pass in the color
356+
);
357+
358+
geometry.faces.push(face);
359+
360+
}
361+
362+
}
363+
364+
// stop recording if a ] is encountered after recording was turned on
365+
isRecordingCoordinates = (isRecordingCoordinates && null === (/]/.exec(child) ) );
366+
367+
}
368+
369+
geometry.computeFaceNormals();
370+
//geometry.computeVertexNormals(); // does not show
371+
geometry.computeBoundingSphere();
372+
373+
// see if it's a define
374+
if ( /DEF/.exec( data.string ) ) {
375+
geometry.name = /DEF (\w+)/.exec( data.string )[ 1 ];
376+
defines[ geometry.name ] = geometry;
377+
}
378+
379+
parent.geometry = geometry;
264380
}
265381

266382
return;
@@ -274,6 +390,7 @@ THREE.VRMLLoader.prototype = {
274390
if ( /Material/.exec( child.string ) ) {
275391

276392
var material = new THREE.MeshPhongMaterial();
393+
material.side = THREE.DoubleSide;
277394

278395
for ( var j = 0; j < child.children.length; j ++ ) {
279396

@@ -312,8 +429,8 @@ THREE.VRMLLoader.prototype = {
312429
} else if ( /transparency/.exec( parameter ) ) {
313430

314431
var result = /\s+([\d|\.|\+|\-|e]+)/.exec( parameter );
315-
316-
material.opacity = parseFloat( result[ 1 ] );
432+
// transparency is opposite of opacity
433+
material.opacity = Math.abs( 1 - parseFloat( result[ 1 ] ) );
317434
material.transparent = true;
318435

319436
}
@@ -323,6 +440,7 @@ THREE.VRMLLoader.prototype = {
323440
if ( /DEF/.exec( data.string ) ) {
324441

325442
material.name = /DEF (\w+)/.exec( data.string )[ 1 ];
443+
326444
defines[ material.name ] = material;
327445

328446
}

0 commit comments

Comments
 (0)