123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- THREE.VRMLLoader = function ( manager ) {
- this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
- };
- THREE.VRMLLoader.prototype = {
- constructor: THREE.VRMLLoader,
- // for IndexedFaceSet support
- isRecordingPoints: false,
- isRecordingFaces: false,
- points: [],
- indexes: [],
- // for Background support
- isRecordingAngles: false,
- isRecordingColors: false,
- angles: [],
- colors: [],
- recordingFieldname: null,
- crossOrigin: 'Anonymous',
- load: function ( url, onLoad, onProgress, onError ) {
- var scope = this;
- var loader = new THREE.FileLoader( this.manager );
- loader.load( url, function ( text ) {
- onLoad( scope.parse( text ) );
- }, onProgress, onError );
- },
- setCrossOrigin: function ( value ) {
- this.crossOrigin = value;
- },
- parse: function ( data ) {
- var texturePath = this.texturePath || '';
- var textureLoader = new THREE.TextureLoader( this.manager );
- textureLoader.setCrossOrigin( this.crossOrigin );
- function parseV1() {
- console.warn( 'THREE.VRMLLoader: V1.0 not supported yet.' );
- }
- function parseV2( lines, scene ) {
- var defines = {};
- var float_pattern = /(\b|\-|\+)([\d\.e]+)/;
- var float2_pattern = /([\d\.\+\-e]+)\s+([\d\.\+\-e]+)/g;
- var float3_pattern = /([\d\.\+\-e]+)\s+([\d\.\+\-e]+)\s+([\d\.\+\-e]+)/g;
- /**
- * Vertically paints the faces interpolating between the
- * specified colors at the specified angels. This is used for the Background
- * node, but could be applied to other nodes with multiple faces as well.
- *
- * When used with the Background node, default is directionIsDown is true if
- * interpolating the skyColor down from the Zenith. When interpolationg up from
- * the Nadir i.e. interpolating the groundColor, the directionIsDown is false.
- *
- * The first angle is never specified, it is the Zenith (0 rad). Angles are specified
- * in radians. The geometry is thought a sphere, but could be anything. The color interpolation
- * is linear along the Y axis in any case.
- *
- * You must specify one more color than you have angles at the beginning of the colors array.
- * This is the color of the Zenith (the top of the shape).
- *
- * @param geometry
- * @param radius
- * @param angles
- * @param colors
- * @param boolean topDown Whether to work top down or bottom up.
- */
- function paintFaces( geometry, radius, angles, colors, topDown ) {
- var direction = ( topDown === true ) ? 1 : - 1;
- var coord = [], A = {}, B = {}, applyColor = false;
- for ( var k = 0; k < angles.length; k ++ ) {
- // push the vector at which the color changes
- var vec = {
- x: direction * ( Math.cos( angles[ k ] ) * radius ),
- y: direction * ( Math.sin( angles[ k ] ) * radius )
- };
- coord.push( vec );
- }
- var index = geometry.index;
- var positionAttribute = geometry.attributes.position;
- var colorAttribute = new THREE.BufferAttribute( new Float32Array( geometry.attributes.position.count * 3 ), 3 );
- var position = new THREE.Vector3();
- var color = new THREE.Color();
- for ( var i = 0; i < index.count; i ++ ) {
- var vertexIndex = index.getX( i );
- position.fromBufferAttribute( positionAttribute, vertexIndex );
- for ( var j = 0; j < colors.length; j ++ ) {
- // linear interpolation between aColor and bColor, calculate proportion
- // A is previous point (angle)
- if ( j === 0 ) {
- A.x = 0;
- A.y = ( topDown === true ) ? radius : - 1 * radius;
- } else {
- A.x = coord[ j - 1 ].x;
- A.y = coord[ j - 1 ].y;
- }
- // B is current point (angle)
- B = coord[ j ];
- if ( B !== undefined ) {
- // p has to be between the points A and B which we interpolate
- applyColor = ( topDown === true ) ? ( position.y <= A.y && position.y > B.y ) : ( position.y >= A.y && position.y < B.y );
- if ( applyColor === true ) {
- var aColor = colors[ j ];
- var bColor = colors[ j + 1 ];
- // below is simple linear interpolation
- var t = Math.abs( position.y - A.y ) / ( A.y - B.y );
- // to make it faster, you can only calculate this if the y coord changes, the color is the same for points with the same y
- color.copy( aColor ).lerp( bColor, t );
- colorAttribute.setXYZ( vertexIndex, color.r, color.g, color.b );
- } else {
- var colorIndex = ( topDown === true ) ? colors.length - 1 : 0;
- var c = colors[ colorIndex ];
- colorAttribute.setXYZ( vertexIndex, c.r, c.g, c.b );
- }
- }
- }
- }
- geometry.addAttribute( 'color', colorAttribute );
- }
- var index = [];
- function parseProperty( node, line ) {
- var parts = [], part, property = {}, fieldName;
- /**
- * Expression for matching relevant information, such as a name or value, but not the separators
- * @type {RegExp}
- */
- var regex = /[^\s,\[\]]+/g;
- var point;
- while ( null !== ( part = regex.exec( line ) ) ) {
- parts.push( part[ 0 ] );
- }
- fieldName = parts[ 0 ];
- // trigger several recorders
- switch ( fieldName ) {
- case 'skyAngle':
- case 'groundAngle':
- this.recordingFieldname = fieldName;
- this.isRecordingAngles = true;
- this.angles = [];
- break;
- case 'skyColor':
- case 'groundColor':
- this.recordingFieldname = fieldName;
- this.isRecordingColors = true;
- this.colors = [];
- break;
- case 'point':
- this.recordingFieldname = fieldName;
- this.isRecordingPoints = true;
- this.points = [];
- break;
- case 'coordIndex':
- case 'texCoordIndex':
- this.recordingFieldname = fieldName;
- this.isRecordingFaces = true;
- this.indexes = [];
- break;
- }
- if ( this.isRecordingFaces ) {
- // the parts hold the indexes as strings
- if ( parts.length > 0 ) {
- for ( var ind = 0; ind < parts.length; ind ++ ) {
- // the part should either be positive integer or -1
- if ( ! /(-?\d+)/.test( parts[ ind ] ) ) {
- continue;
- }
- // end of current face
- if ( parts[ ind ] === '-1' ) {
- if ( index.length > 0 ) {
- this.indexes.push( index );
- }
- // start new one
- index = [];
- } else {
- index.push( parseInt( parts[ ind ] ) );
- }
- }
- }
- // end
- if ( /]/.exec( line ) ) {
- if ( index.length > 0 ) {
- this.indexes.push( index );
- }
- // start new one
- index = [];
- this.isRecordingFaces = false;
- node[ this.recordingFieldname ] = this.indexes;
- }
- } else if ( this.isRecordingPoints ) {
- if ( node.nodeType == 'Coordinate' ) {
- while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
- point = {
- x: parseFloat( parts[ 1 ] ),
- y: parseFloat( parts[ 2 ] ),
- z: parseFloat( parts[ 3 ] )
- };
- this.points.push( point );
- }
- }
- if ( node.nodeType == 'TextureCoordinate' ) {
- while ( null !== ( parts = float2_pattern.exec( line ) ) ) {
- point = {
- x: parseFloat( parts[ 1 ] ),
- y: parseFloat( parts[ 2 ] )
- };
- this.points.push( point );
- }
- }
- // end
- if ( /]/.exec( line ) ) {
- this.isRecordingPoints = false;
- node.points = this.points;
- }
- } else if ( this.isRecordingAngles ) {
- // the parts hold the angles as strings
- if ( parts.length > 0 ) {
- for ( var ind = 0; ind < parts.length; ind ++ ) {
- // the part should be a float
- if ( ! float_pattern.test( parts[ ind ] ) ) {
- continue;
- }
- this.angles.push( parseFloat( parts[ ind ] ) );
- }
- }
- // end
- if ( /]/.exec( line ) ) {
- this.isRecordingAngles = false;
- node[ this.recordingFieldname ] = this.angles;
- }
- } else if ( this.isRecordingColors ) {
- while ( null !== ( parts = float3_pattern.exec( line ) ) ) {
- var color = {
- r: parseFloat( parts[ 1 ] ),
- g: parseFloat( parts[ 2 ] ),
- b: parseFloat( parts[ 3 ] )
- };
- this.colors.push( color );
- }
- // end
- if ( /]/.exec( line ) ) {
- this.isRecordingColors = false;
- node[ this.recordingFieldname ] = this.colors;
- }
- } else if ( parts[ parts.length - 1 ] !== 'NULL' && fieldName !== 'children' ) {
- switch ( fieldName ) {
- case 'diffuseColor':
- case 'emissiveColor':
- case 'specularColor':
- case 'color':
- if ( parts.length !== 4 ) {
- console.warn( 'THREE.VRMLLoader: Invalid color format detected for %s.', fieldName );
- break;
- }
- property = {
- r: parseFloat( parts[ 1 ] ),
- g: parseFloat( parts[ 2 ] ),
- b: parseFloat( parts[ 3 ] )
- };
- break;
- case 'location':
- case 'direction':
- case 'translation':
- case 'scale':
- case 'size':
- if ( parts.length !== 4 ) {
- console.warn( 'THREE.VRMLLoader: Invalid vector format detected for %s.', fieldName );
- break;
- }
- property = {
- x: parseFloat( parts[ 1 ] ),
- y: parseFloat( parts[ 2 ] ),
- z: parseFloat( parts[ 3 ] )
- };
- break;
- case 'intensity':
- case 'cutOffAngle':
- case 'radius':
- case 'topRadius':
- case 'bottomRadius':
- case 'height':
- case 'transparency':
- case 'shininess':
- case 'ambientIntensity':
- if ( parts.length !== 2 ) {
- console.warn( 'THREE.VRMLLoader: Invalid single float value specification detected for %s.', fieldName );
- break;
- }
- property = parseFloat( parts[ 1 ] );
- break;
- case 'rotation':
- if ( parts.length !== 5 ) {
- console.warn( 'THREE.VRMLLoader: Invalid quaternion format detected for %s.', fieldName );
- break;
- }
- property = {
- x: parseFloat( parts[ 1 ] ),
- y: parseFloat( parts[ 2 ] ),
- z: parseFloat( parts[ 3 ] ),
- w: parseFloat( parts[ 4 ] )
- };
- break;
- case 'on':
- case 'ccw':
- case 'solid':
- case 'colorPerVertex':
- case 'convex':
- if ( parts.length !== 2 ) {
- console.warn( 'THREE.VRMLLoader: Invalid format detected for %s.', fieldName );
- break;
- }
- property = parts[ 1 ] === 'TRUE' ? true : false;
- break;
- }
- node[ fieldName ] = property;
- }
- return property;
- }
- function getTree( lines ) {
- var tree = { 'string': 'Scene', children: [] };
- var current = tree;
- var matches;
- var specification;
- for ( var i = 0; i < lines.length; i ++ ) {
- var comment = '';
- var line = lines[ i ];
- // omit whitespace only lines
- if ( null !== ( /^\s+?$/g.exec( line ) ) ) {
- continue;
- }
- line = line.trim();
- // skip empty lines
- if ( line === '' ) {
- continue;
- }
- if ( /#/.exec( line ) ) {
- var parts = line.split( '#' );
- // discard everything after the #, it is a comment
- line = parts[ 0 ];
- // well, let's also keep the comment
- comment = parts[ 1 ];
- }
- if ( matches = /([^\s]*){1}(?:\s+)?{/.exec( line ) ) {
- // first subpattern should match the Node name
- var block = { 'nodeType': matches[ 1 ], 'string': line, 'parent': current, 'children': [], 'comment': comment };
- current.children.push( block );
- current = block;
- if ( /}/.exec( line ) ) {
- // example: geometry Box { size 1 1 1 } # all on the same line
- specification = /{(.*)}/.exec( line )[ 1 ];
- // todo: remove once new parsing is complete?
- block.children.push( specification );
- parseProperty( current, specification );
- current = current.parent;
- }
- } else if ( /}/.exec( line ) ) {
- current = current.parent;
- } else if ( line !== '' ) {
- parseProperty( current, line );
- // todo: remove once new parsing is complete? we still do not parse geometry and appearance the new way
- current.children.push( line );
- }
- }
- return tree;
- }
- function parseNode( data, parent ) {
- var object;
- if ( typeof data === 'string' ) {
- if ( /USE/.exec( data ) ) {
- var defineKey = /USE\s+?([^\s]+)/.exec( data )[ 1 ];
- if ( undefined == defines[ defineKey ] ) {
- console.warn( 'THREE.VRMLLoader: %s is not defined.', defineKey );
- } else {
- if ( /appearance/.exec( data ) && defineKey ) {
- parent.material = defines[ defineKey ].clone();
- } else if ( /geometry/.exec( data ) && defineKey ) {
- parent.geometry = defines[ defineKey ].clone();
- // the solid property is not cloned with clone(), is only needed for VRML loading, so we need to transfer it
- if ( undefined !== defines[ defineKey ].solid && defines[ defineKey ].solid === false ) {
- parent.geometry.solid = false;
- parent.material.side = THREE.DoubleSide;
- }
- } else if ( defineKey ) {
- object = defines[ defineKey ].clone();
- parent.add( object );
- }
- }
- }
- return;
- }
- object = parent;
- if ( data.string.indexOf( 'AmbientLight' ) > - 1 && data.nodeType === 'PointLight' ) {
- data.nodeType = 'AmbientLight';
- }
- var l_visible = data.on !== undefined ? data.on : true;
- var l_intensity = data.intensity !== undefined ? data.intensity : 1;
- var l_color = new THREE.Color();
- if ( data.color ) {
- l_color.copy( data.color );
- }
- if ( data.nodeType === 'AmbientLight' ) {
- object = new THREE.AmbientLight( l_color, l_intensity );
- object.visible = l_visible;
- parent.add( object );
- } else if ( data.nodeType === 'PointLight' ) {
- var l_distance = 0;
- if ( data.radius !== undefined && data.radius < 1000 ) {
- l_distance = data.radius;
- }
- object = new THREE.PointLight( l_color, l_intensity, l_distance );
- object.visible = l_visible;
- parent.add( object );
- } else if ( data.nodeType === 'SpotLight' ) {
- var l_intensity = 1;
- var l_distance = 0;
- var l_angle = Math.PI / 3;
- var l_penumbra = 0;
- var l_visible = true;
- if ( data.radius !== undefined && data.radius < 1000 ) {
- l_distance = data.radius;
- }
- if ( data.cutOffAngle !== undefined ) {
- l_angle = data.cutOffAngle;
- }
- object = new THREE.SpotLight( l_color, l_intensity, l_distance, l_angle, l_penumbra );
- object.visible = l_visible;
- parent.add( object );
- } else if ( data.nodeType === 'Transform' || data.nodeType === 'Group' ) {
- object = new THREE.Object3D();
- if ( /DEF/.exec( data.string ) ) {
- object.name = /DEF\s+([^\s]+)/.exec( data.string )[ 1 ];
- defines[ object.name ] = object;
- }
- if ( data.translation !== undefined ) {
- var t = data.translation;
- object.position.set( t.x, t.y, t.z );
- }
- if ( data.rotation !== undefined ) {
- var r = data.rotation;
- object.quaternion.setFromAxisAngle( new THREE.Vector3( r.x, r.y, r.z ), r.w );
- }
- if ( data.scale !== undefined ) {
- var s = data.scale;
- object.scale.set( s.x, s.y, s.z );
- }
- parent.add( object );
- } else if ( data.nodeType === 'Shape' ) {
- object = new THREE.Mesh();
- if ( /DEF/.exec( data.string ) ) {
- object.name = /DEF\s+([^\s]+)/.exec( data.string )[ 1 ];
- defines[ object.name ] = object;
- }
- parent.add( object );
- } else if ( data.nodeType === 'Background' ) {
- var segments = 20;
- // sky (full sphere):
- var radius = 2e4;
- var skyGeometry = new THREE.SphereBufferGeometry( radius, segments, segments );
- var skyMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide } );
- if ( data.skyColor.length > 1 ) {
- paintFaces( skyGeometry, radius, data.skyAngle, data.skyColor, true );
- skyMaterial.vertexColors = THREE.VertexColors;
- } else {
- var color = data.skyColor[ 0 ];
- skyMaterial.color.setRGB( color.r, color.b, color.g );
- }
- scene.add( new THREE.Mesh( skyGeometry, skyMaterial ) );
- // ground (half sphere):
- if ( data.groundColor !== undefined ) {
- radius = 1.2e4;
- var groundGeometry = new THREE.SphereBufferGeometry( radius, segments, segments, 0, 2 * Math.PI, 0.5 * Math.PI, 1.5 * Math.PI );
- var groundMaterial = new THREE.MeshBasicMaterial( { fog: false, side: THREE.BackSide, vertexColors: THREE.VertexColors } );
- paintFaces( groundGeometry, radius, data.groundAngle, data.groundColor, false );
- scene.add( new THREE.Mesh( groundGeometry, groundMaterial ) );
- }
- } else if ( /geometry/.exec( data.string ) ) {
- if ( data.nodeType === 'Box' ) {
- var s = data.size;
- parent.geometry = new THREE.BoxBufferGeometry( s.x, s.y, s.z );
- } else if ( data.nodeType === 'Cylinder' ) {
- parent.geometry = new THREE.CylinderBufferGeometry( data.radius, data.radius, data.height );
- } else if ( data.nodeType === 'Cone' ) {
- parent.geometry = new THREE.CylinderBufferGeometry( data.topRadius, data.bottomRadius, data.height );
- } else if ( data.nodeType === 'Sphere' ) {
- parent.geometry = new THREE.SphereBufferGeometry( data.radius );
- } else if ( data.nodeType === 'IndexedFaceSet' ) {
- var geometry = new THREE.BufferGeometry();
- var positions = [];
- var uvs = [];
- var position, uv;
- var i, il, j, jl;
- for ( i = 0, il = data.children.length; i < il; i ++ ) {
- var child = data.children[ i ];
- // uvs
- if ( child.nodeType === 'TextureCoordinate' ) {
- if ( child.points ) {
- for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
- uv = child.points[ j ];
- uvs.push( uv.x, uv.y );
- }
- }
- }
- // positions
- if ( child.nodeType === 'Coordinate' ) {
- if ( child.points ) {
- for ( j = 0, jl = child.points.length; j < jl; j ++ ) {
- position = child.points[ j ];
- positions.push( position.x, position.y, position.z );
- }
- }
- if ( child.string.indexOf( 'DEF' ) > - 1 ) {
- var name = /DEF\s+([^\s]+)/.exec( child.string )[ 1 ];
- defines[ name ] = positions.slice( 0 );
- }
- if ( child.string.indexOf( 'USE' ) > - 1 ) {
- var defineKey = /USE\s+([^\s]+)/.exec( child.string )[ 1 ];
- positions = defines[ defineKey ];
- }
- }
- }
- var skip = 0;
- // some shapes only have vertices for use in other shapes
- if ( data.coordIndex ) {
- var newPositions = [];
- var newUvs = [];
- position = new THREE.Vector3();
- uv = new THREE.Vector2();
- for ( i = 0, il = data.coordIndex.length; i < il; i ++ ) {
- var indexes = data.coordIndex[ i ];
- // VRML support multipoint indexed face sets (more then 3 vertices). You must calculate the composing triangles here
- skip = 0;
- while ( indexes.length >= 3 && skip < ( indexes.length - 2 ) ) {
- if ( data.ccw === undefined ) data.ccw = true; // ccw is true by default
- var i1 = indexes[ 0 ];
- var i2 = indexes[ skip + ( data.ccw ? 1 : 2 ) ];
- var i3 = indexes[ skip + ( data.ccw ? 2 : 1 ) ];
- // create non indexed geometry, necessary for face normal generation
- position.fromArray( positions, i1 * 3 );
- uv.fromArray( uvs, i1 * 2 );
- newPositions.push( position.x, position.y, position.z );
- newUvs.push( uv.x, uv.y );
- position.fromArray( positions, i2 * 3 );
- uv.fromArray( uvs, i2 * 2 );
- newPositions.push( position.x, position.y, position.z );
- newUvs.push( uv.x, uv.y );
- position.fromArray( positions, i3 * 3 );
- uv.fromArray( uvs, i3 * 2 );
- newPositions.push( position.x, position.y, position.z );
- newUvs.push( uv.x, uv.y );
- skip ++;
- }
- }
- positions = newPositions;
- uvs = newUvs;
- } else {
- // do not add dummy mesh to the scene
- parent.parent.remove( parent );
- }
- if ( false === data.solid ) {
- parent.material.side = THREE.DoubleSide;
- }
- // we need to store it on the geometry for use with defines
- geometry.solid = data.solid;
- geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
- if ( uvs.length > 0 ) {
- geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
- }
- geometry.computeVertexNormals();
- geometry.computeBoundingSphere();
- // see if it's a define
- if ( /DEF/.exec( data.string ) ) {
- geometry.name = /DEF ([^\s]+)/.exec( data.string )[ 1 ];
- defines[ geometry.name ] = geometry;
- }
- parent.geometry = geometry;
- }
- return;
- } else if ( /appearance/.exec( data.string ) ) {
- for ( var i = 0; i < data.children.length; i ++ ) {
- var child = data.children[ i ];
- if ( child.nodeType === 'Material' ) {
- var material = new THREE.MeshPhongMaterial();
- if ( child.diffuseColor !== undefined ) {
- var d = child.diffuseColor;
- material.color.setRGB( d.r, d.g, d.b );
- }
- if ( child.emissiveColor !== undefined ) {
- var e = child.emissiveColor;
- material.emissive.setRGB( e.r, e.g, e.b );
- }
- if ( child.specularColor !== undefined ) {
- var s = child.specularColor;
- material.specular.setRGB( s.r, s.g, s.b );
- }
- if ( child.transparency !== undefined ) {
- var t = child.transparency;
- // transparency is opposite of opacity
- material.opacity = Math.abs( 1 - t );
- material.transparent = true;
- }
- if ( /DEF/.exec( data.string ) ) {
- material.name = /DEF ([^\s]+)/.exec( data.string )[ 1 ];
- defines[ material.name ] = material;
- }
- parent.material = material;
- }
- if ( child.nodeType === 'ImageTexture' ) {
- var textureName = /"([^"]+)"/.exec( child.children[ 0 ] );
- if ( textureName ) {
- parent.material.name = textureName[ 1 ];
- parent.material.map = textureLoader.load( texturePath + textureName[ 1 ] );
- }
- }
- }
- return;
- }
- for ( var i = 0, l = data.children.length; i < l; i ++ ) {
- parseNode( data.children[ i ], object );
- }
- }
- parseNode( getTree( lines ), scene );
- }
- var scene = new THREE.Scene();
- var lines = data.split( '\n' );
- // some lines do not have breaks
- for ( var i = lines.length - 1; i > - 1; i -- ) {
- // split lines with {..{ or {..[ - some have both
- if ( /{.*[{\[]/.test( lines[ i ] ) ) {
- var parts = lines[ i ].split( '{' ).join( '{\n' ).split( '\n' );
- parts.unshift( 1 );
- parts.unshift( i );
- lines.splice.apply( lines, parts );
- } else if ( /\].*}/.test( lines[ i ] ) ) {
- // split lines with ]..}
- var parts = lines[ i ].split( ']' ).join( ']\n' ).split( '\n' );
- parts.unshift( 1 );
- parts.unshift( i );
- lines.splice.apply( lines, parts );
- }
- if ( /}.*}/.test( lines[ i ] ) ) {
- // split lines with }..}
- var parts = lines[ i ].split( '}' ).join( '}\n' ).split( '\n' );
- parts.unshift( 1 );
- parts.unshift( i );
- lines.splice.apply( lines, parts );
- }
- // force the parser to create Coordinate node for empty coords
- // coord USE something -> coord USE something Coordinate {}
- if ( ( lines[ i ].indexOf( 'coord' ) > - 1 ) && ( lines[ i ].indexOf( '[' ) < 0 ) && ( lines[ i ].indexOf( '{' ) < 0 ) ) {
- lines[ i ] += ' Coordinate {}';
- }
- }
- var header = lines.shift();
- if ( /V1.0/.exec( header ) ) {
- parseV1( lines, scene );
- } else if ( /V2.0/.exec( header ) ) {
- parseV2( lines, scene );
- }
- return scene;
- }
- };
|