12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265 |
- /**
- * @author alteredq / http://alteredqualia.com/
- */
- THREE.SceneLoader = function ( manager ) {
- this.onLoadStart = function () {};
- this.onLoadProgress = function() {};
- this.onLoadComplete = function () {};
- this.callbackSync = function () {};
- this.callbackProgress = function () {};
- this.geometryHandlers = {};
- this.hierarchyHandlers = {};
- this.addGeometryHandler( "ascii", THREE.JSONLoader );
- this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
- };
- THREE.SceneLoader.prototype = {
- constructor: THREE.SceneLoader,
- load: function ( url, onLoad, onProgress, onError ) {
- var scope = this;
- var loader = new THREE.XHRLoader( scope.manager );
- loader.setCrossOrigin( this.crossOrigin );
- loader.load( url, function ( text ) {
- scope.parse( JSON.parse( text ), onLoad, url );
- }, onProgress, onError );
- },
- setCrossOrigin: function ( value ) {
- this.crossOrigin = value;
- },
- addGeometryHandler: function ( typeID, loaderClass ) {
- this.geometryHandlers[ typeID ] = { "loaderClass": loaderClass };
- },
- addHierarchyHandler: function ( typeID, loaderClass ) {
- this.hierarchyHandlers[ typeID ] = { "loaderClass": loaderClass };
- },
- parse: function ( json, callbackFinished, url ) {
- var scope = this;
- var urlBase = THREE.Loader.prototype.extractUrlBase( url );
- var geometry, material, camera, fog,
- texture, images, color,
- light, hex, intensity,
- counter_models, counter_textures,
- total_models, total_textures,
- result;
- var target_array = [];
- var data = json;
- // async geometry loaders
- for ( var typeID in this.geometryHandlers ) {
- var loaderClass = this.geometryHandlers[ typeID ][ "loaderClass" ];
- this.geometryHandlers[ typeID ][ "loaderObject" ] = new loaderClass();
- }
- // async hierachy loaders
- for ( var typeID in this.hierarchyHandlers ) {
- var loaderClass = this.hierarchyHandlers[ typeID ][ "loaderClass" ];
- this.hierarchyHandlers[ typeID ][ "loaderObject" ] = new loaderClass();
- }
- counter_models = 0;
- counter_textures = 0;
- result = {
- scene: new THREE.Scene(),
- geometries: {},
- face_materials: {},
- materials: {},
- textures: {},
- objects: {},
- cameras: {},
- lights: {},
- fogs: {},
- empties: {},
- groups: {}
- };
- if ( data.transform ) {
- var position = data.transform.position,
- rotation = data.transform.rotation,
- scale = data.transform.scale;
- if ( position ) {
- result.scene.position.fromArray( position );
- }
- if ( rotation ) {
- result.scene.rotation.fromArray( rotation );
- }
- if ( scale ) {
- result.scene.scale.fromArray( scale );
- }
- if ( position || rotation || scale ) {
- result.scene.updateMatrix();
- result.scene.updateMatrixWorld();
- }
- }
- function get_url( source_url, url_type ) {
- if ( url_type == "relativeToHTML" ) {
- return source_url;
- } else {
- return urlBase + source_url;
- }
- };
- // toplevel loader function, delegates to handle_children
- function handle_objects() {
- handle_children( result.scene, data.objects );
- }
- // handle all the children from the loaded json and attach them to given parent
- function handle_children( parent, children ) {
- var mat, dst, pos, rot, scl, quat;
- for ( var objID in children ) {
- // check by id if child has already been handled,
- // if not, create new object
- var object = result.objects[ objID ];
- var objJSON = children[ objID ];
- if ( object === undefined ) {
- // meshes
- if ( objJSON.type && ( objJSON.type in scope.hierarchyHandlers ) ) {
- if ( objJSON.loading === undefined ) {
- var reservedTypes = {
- "type": 1, "url": 1, "material": 1,
- "position": 1, "rotation": 1, "scale" : 1,
- "visible": 1, "children": 1, "userData": 1,
- "skin": 1, "morph": 1, "mirroredLoop": 1, "duration": 1
- };
- var loaderParameters = {};
- for ( var parType in objJSON ) {
- if ( ! ( parType in reservedTypes ) ) {
- loaderParameters[ parType ] = objJSON[ parType ];
- }
- }
- material = result.materials[ objJSON.material ];
- objJSON.loading = true;
- var loader = scope.hierarchyHandlers[ objJSON.type ][ "loaderObject" ];
- // ColladaLoader
- if ( loader.options ) {
- loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ) );
- // UTF8Loader
- // OBJLoader
- } else {
- loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ), loaderParameters );
- }
- }
- } else if ( objJSON.geometry !== undefined ) {
- geometry = result.geometries[ objJSON.geometry ];
- // geometry already loaded
- if ( geometry ) {
- var needsTangents = false;
- material = result.materials[ objJSON.material ];
- needsTangents = material instanceof THREE.ShaderMaterial;
- pos = objJSON.position;
- rot = objJSON.rotation;
- scl = objJSON.scale;
- mat = objJSON.matrix;
- quat = objJSON.quaternion;
- // use materials from the model file
- // if there is no material specified in the object
- if ( ! objJSON.material ) {
- material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] );
- }
- // use materials from the model file
- // if there is just empty face material
- // (must create new material as each model has its own face material)
- if ( ( material instanceof THREE.MeshFaceMaterial ) && material.materials.length === 0 ) {
- material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] );
- }
- if ( material instanceof THREE.MeshFaceMaterial ) {
- for ( var i = 0; i < material.materials.length; i ++ ) {
- needsTangents = needsTangents || ( material.materials[ i ] instanceof THREE.ShaderMaterial );
- }
- }
- if ( needsTangents ) {
- geometry.computeTangents();
- }
- if ( objJSON.skin ) {
- object = new THREE.SkinnedMesh( geometry, material );
- } else if ( objJSON.morph ) {
- object = new THREE.MorphAnimMesh( geometry, material );
- if ( objJSON.duration !== undefined ) {
- object.duration = objJSON.duration;
- }
- if ( objJSON.time !== undefined ) {
- object.time = objJSON.time;
- }
- if ( objJSON.mirroredLoop !== undefined ) {
- object.mirroredLoop = objJSON.mirroredLoop;
- }
- if ( material.morphNormals ) {
- geometry.computeMorphNormals();
- }
- } else {
- object = new THREE.Mesh( geometry, material );
- }
- object.name = objID;
- if ( mat ) {
- object.matrixAutoUpdate = false;
- object.matrix.set(
- mat[0], mat[1], mat[2], mat[3],
- mat[4], mat[5], mat[6], mat[7],
- mat[8], mat[9], mat[10], mat[11],
- mat[12], mat[13], mat[14], mat[15]
- );
- } else {
- object.position.fromArray( pos );
- if ( quat ) {
- object.quaternion.fromArray( quat );
- } else {
- object.rotation.fromArray( rot );
- }
- object.scale.fromArray( scl );
- }
- object.visible = objJSON.visible;
- object.castShadow = objJSON.castShadow;
- object.receiveShadow = objJSON.receiveShadow;
- parent.add( object );
- result.objects[ objID ] = object;
- }
- // lights
- } else if ( objJSON.type === "AmbientLight" || objJSON.type === "PointLight" ||
- objJSON.type === "DirectionalLight" || objJSON.type === "SpotLight" ||
- objJSON.type === "HemisphereLight" || objJSON.type === "AreaLight" ) {
- var color = objJSON.color;
- var intensity = objJSON.intensity;
- var distance = objJSON.distance;
- var position = objJSON.position;
- var rotation = objJSON.rotation;
- switch ( objJSON.type ) {
- case 'AmbientLight':
- light = new THREE.AmbientLight( color );
- break;
- case 'PointLight':
- light = new THREE.PointLight( color, intensity, distance );
- light.position.fromArray( position );
- break;
- case 'DirectionalLight':
- light = new THREE.DirectionalLight( color, intensity );
- light.position.fromArray( objJSON.direction );
- break;
- case 'SpotLight':
- light = new THREE.SpotLight( color, intensity, distance, 1 );
- light.angle = objJSON.angle;
- light.position.fromArray( position );
- light.target.set( position[ 0 ], position[ 1 ] - distance, position[ 2 ] );
- light.target.applyEuler( new THREE.Euler( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ], 'XYZ' ) );
- break;
- case 'HemisphereLight':
- light = new THREE.DirectionalLight( color, intensity, distance );
- light.target.set( position[ 0 ], position[ 1 ] - distance, position[ 2 ] );
- light.target.applyEuler( new THREE.Euler( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ], 'XYZ' ) );
- break;
- case 'AreaLight':
- light = new THREE.AreaLight(color, intensity);
- light.position.fromArray( position );
- light.width = objJSON.size;
- light.height = objJSON.size_y;
- break;
- }
- parent.add( light );
- light.name = objID;
- result.lights[ objID ] = light;
- result.objects[ objID ] = light;
- // cameras
- } else if ( objJSON.type === "PerspectiveCamera" || objJSON.type === "OrthographicCamera" ) {
- pos = objJSON.position;
- rot = objJSON.rotation;
- quat = objJSON.quaternion;
- if ( objJSON.type === "PerspectiveCamera" ) {
- camera = new THREE.PerspectiveCamera( objJSON.fov, objJSON.aspect, objJSON.near, objJSON.far );
- } else if ( objJSON.type === "OrthographicCamera" ) {
- camera = new THREE.OrthographicCamera( objJSON.left, objJSON.right, objJSON.top, objJSON.bottom, objJSON.near, objJSON.far );
- }
- camera.name = objID;
- camera.position.fromArray( pos );
- if ( quat !== undefined ) {
- camera.quaternion.fromArray( quat );
- } else if ( rot !== undefined ) {
- camera.rotation.fromArray( rot );
- } else if ( objJSON.target ) {
- camera.lookAt( new THREE.Vector3().fromArray( objJSON.target ) );
- }
- parent.add( camera );
- result.cameras[ objID ] = camera;
- result.objects[ objID ] = camera;
- // pure Object3D
- } else {
- pos = objJSON.position;
- rot = objJSON.rotation;
- scl = objJSON.scale;
- quat = objJSON.quaternion;
- object = new THREE.Object3D();
- object.name = objID;
- object.position.fromArray( pos );
- if ( quat ) {
- object.quaternion.fromArray( quat );
- } else {
- object.rotation.fromArray( rot );
- }
- object.scale.fromArray( scl );
- object.visible = ( objJSON.visible !== undefined ) ? objJSON.visible : false;
- parent.add( object );
- result.objects[ objID ] = object;
- result.empties[ objID ] = object;
- }
- if ( object ) {
- if ( objJSON.userData !== undefined ) {
- for ( var key in objJSON.userData ) {
- var value = objJSON.userData[ key ];
- object.userData[ key ] = value;
- }
- }
- if ( objJSON.groups !== undefined ) {
- for ( var i = 0; i < objJSON.groups.length; i ++ ) {
- var groupID = objJSON.groups[ i ];
- if ( result.groups[ groupID ] === undefined ) {
- result.groups[ groupID ] = [];
- }
- result.groups[ groupID ].push( objID );
- }
- }
- }
- }
- if ( object !== undefined && objJSON.children !== undefined ) {
- handle_children( object, objJSON.children );
- }
- }
- };
- function handle_mesh( geo, mat, id ) {
- result.geometries[ id ] = geo;
- result.face_materials[ id ] = mat;
- handle_objects();
- };
- function handle_hierarchy( node, id, parent, material, obj ) {
- var p = obj.position;
- var r = obj.rotation;
- var q = obj.quaternion;
- var s = obj.scale;
- node.position.fromArray( p );
- if ( q ) {
- node.quaternion.fromArray( q );
- } else {
- node.rotation.fromArray( r );
- }
- node.scale.fromArray( s );
- // override children materials
- // if object material was specified in JSON explicitly
- if ( material ) {
- node.traverse( function ( child ) {
- child.material = material;
- } );
- }
- // override children visibility
- // with root node visibility as specified in JSON
- var visible = ( obj.visible !== undefined ) ? obj.visible : true;
- node.traverse( function ( child ) {
- child.visible = visible;
- } );
- parent.add( node );
- node.name = id;
- result.objects[ id ] = node;
- handle_objects();
- };
- function create_callback_geometry( id ) {
- return function ( geo, mat ) {
- geo.name = id;
- handle_mesh( geo, mat, id );
- counter_models -= 1;
- scope.onLoadComplete();
- async_callback_gate();
- }
- };
- function create_callback_hierachy( id, parent, material, obj ) {
- return function ( event ) {
- var result;
- // loaders which use EventDispatcher
- if ( event.content ) {
- result = event.content;
- // ColladaLoader
- } else if ( event.dae ) {
- result = event.scene;
- // UTF8Loader
- } else {
- result = event;
- }
- handle_hierarchy( result, id, parent, material, obj );
- counter_models -= 1;
- scope.onLoadComplete();
- async_callback_gate();
- }
- };
- function create_callback_embed( id ) {
- return function ( geo, mat ) {
- geo.name = id;
- result.geometries[ id ] = geo;
- result.face_materials[ id ] = mat;
- }
- };
- function async_callback_gate() {
- var progress = {
- totalModels : total_models,
- totalTextures : total_textures,
- loadedModels : total_models - counter_models,
- loadedTextures : total_textures - counter_textures
- };
- scope.callbackProgress( progress, result );
- scope.onLoadProgress();
- if ( counter_models === 0 && counter_textures === 0 ) {
- finalize();
- callbackFinished( result );
- }
- };
- function finalize() {
- // take care of targets which could be asynchronously loaded objects
- for ( var i = 0; i < target_array.length; i ++ ) {
- var ta = target_array[ i ];
- var target = result.objects[ ta.targetName ];
- if ( target ) {
- ta.object.target = target;
- } else {
- // if there was error and target of specified name doesn't exist in the scene file
- // create instead dummy target
- // (target must be added to scene explicitly as parent is already added)
- ta.object.target = new THREE.Object3D();
- result.scene.add( ta.object.target );
- }
- ta.object.target.userData.targetInverse = ta.object;
- }
- };
- var callbackTexture = function ( count ) {
- counter_textures -= count;
- async_callback_gate();
- scope.onLoadComplete();
- };
- // must use this instead of just directly calling callbackTexture
- // because of closure in the calling context loop
- var generateTextureCallback = function ( count ) {
- return function () {
- callbackTexture( count );
- };
- };
- function traverse_json_hierarchy( objJSON, callback ) {
- callback( objJSON );
- if ( objJSON.children !== undefined ) {
- for ( var objChildID in objJSON.children ) {
- traverse_json_hierarchy( objJSON.children[ objChildID ], callback );
- }
- }
- };
- // first go synchronous elements
- // fogs
- var fogID, fogJSON;
- for ( fogID in data.fogs ) {
- fogJSON = data.fogs[ fogID ];
- if ( fogJSON.type === "linear" ) {
- fog = new THREE.Fog( 0x000000, fogJSON.near, fogJSON.far );
- } else if ( fogJSON.type === "exp2" ) {
- fog = new THREE.FogExp2( 0x000000, fogJSON.density );
- }
- color = fogJSON.color;
- fog.color.setRGB( color[0], color[1], color[2] );
- result.fogs[ fogID ] = fog;
- }
- // now come potentially asynchronous elements
- // geometries
- // count how many geometries will be loaded asynchronously
- var geoID, geoJSON;
- for ( geoID in data.geometries ) {
- geoJSON = data.geometries[ geoID ];
- if ( geoJSON.type in this.geometryHandlers ) {
- counter_models += 1;
- scope.onLoadStart();
- }
- }
- // count how many hierarchies will be loaded asynchronously
- for ( var objID in data.objects ) {
- traverse_json_hierarchy( data.objects[ objID ], function ( objJSON ) {
- if ( objJSON.type && ( objJSON.type in scope.hierarchyHandlers ) ) {
- counter_models += 1;
- scope.onLoadStart();
- }
- });
- }
- total_models = counter_models;
- for ( geoID in data.geometries ) {
- geoJSON = data.geometries[ geoID ];
- if ( geoJSON.type === "cube" ) {
- geometry = new THREE.BoxGeometry( geoJSON.width, geoJSON.height, geoJSON.depth, geoJSON.widthSegments, geoJSON.heightSegments, geoJSON.depthSegments );
- geometry.name = geoID;
- result.geometries[ geoID ] = geometry;
- } else if ( geoJSON.type === "plane" ) {
- geometry = new THREE.PlaneGeometry( geoJSON.width, geoJSON.height, geoJSON.widthSegments, geoJSON.heightSegments );
- geometry.name = geoID;
- result.geometries[ geoID ] = geometry;
- } else if ( geoJSON.type === "sphere" ) {
- geometry = new THREE.SphereGeometry( geoJSON.radius, geoJSON.widthSegments, geoJSON.heightSegments );
- geometry.name = geoID;
- result.geometries[ geoID ] = geometry;
- } else if ( geoJSON.type === "cylinder" ) {
- geometry = new THREE.CylinderGeometry( geoJSON.topRad, geoJSON.botRad, geoJSON.height, geoJSON.radSegs, geoJSON.heightSegs );
- geometry.name = geoID;
- result.geometries[ geoID ] = geometry;
- } else if ( geoJSON.type === "torus" ) {
- geometry = new THREE.TorusGeometry( geoJSON.radius, geoJSON.tube, geoJSON.segmentsR, geoJSON.segmentsT );
- geometry.name = geoID;
- result.geometries[ geoID ] = geometry;
- } else if ( geoJSON.type === "icosahedron" ) {
- geometry = new THREE.IcosahedronGeometry( geoJSON.radius, geoJSON.subdivisions );
- geometry.name = geoID;
- result.geometries[ geoID ] = geometry;
- } else if ( geoJSON.type in this.geometryHandlers ) {
- var loaderParameters = {};
- for ( var parType in geoJSON ) {
- if ( parType !== "type" && parType !== "url" ) {
- loaderParameters[ parType ] = geoJSON[ parType ];
- }
- }
- var loader = this.geometryHandlers[ geoJSON.type ][ "loaderObject" ];
- loader.load( get_url( geoJSON.url, data.urlBaseType ), create_callback_geometry( geoID ), loaderParameters );
- } else if ( geoJSON.type === "embedded" ) {
- var modelJson = data.embeds[ geoJSON.id ],
- texture_path = "";
- // pass metadata along to jsonLoader so it knows the format version
- modelJson.metadata = data.metadata;
- if ( modelJson ) {
- var jsonLoader = this.geometryHandlers[ "ascii" ][ "loaderObject" ];
- var model = jsonLoader.parse( modelJson, texture_path );
- create_callback_embed( geoID )( model.geometry, model.materials );
- }
- }
- }
- // textures
- // count how many textures will be loaded asynchronously
- var textureID, textureJSON;
- for ( textureID in data.textures ) {
- textureJSON = data.textures[ textureID ];
- if ( textureJSON.url instanceof Array ) {
- counter_textures += textureJSON.url.length;
- for( var n = 0; n < textureJSON.url.length; n ++ ) {
- scope.onLoadStart();
- }
- } else {
- counter_textures += 1;
- scope.onLoadStart();
- }
- }
- total_textures = counter_textures;
- for ( textureID in data.textures ) {
- textureJSON = data.textures[ textureID ];
- if ( textureJSON.mapping !== undefined && THREE[ textureJSON.mapping ] !== undefined ) {
- textureJSON.mapping = new THREE[ textureJSON.mapping ]();
- }
- var texture;
- if ( textureJSON.url instanceof Array ) {
- var count = textureJSON.url.length;
- var url_array = [];
- for ( var i = 0; i < count; i ++ ) {
- url_array[ i ] = get_url( textureJSON.url[ i ], data.urlBaseType );
- }
- var loader = THREE.Loader.Handlers.get( url_array[ 0 ] );
- if ( loader !== null ) {
- texture = loader.load( url_array, generateTextureCallback( count ) );
- texture.mapping = textureJSON.mapping;
- } else {
- texture = THREE.ImageUtils.loadTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) );
- }
- } else {
- var fullUrl = get_url( textureJSON.url, data.urlBaseType );
- var textureCallback = generateTextureCallback( 1 );
- var loader = THREE.Loader.Handlers.get( fullUrl );
- if ( loader !== null ) {
- texture = loader.load( fullUrl, textureCallback );
- } else {
- texture = new THREE.Texture();
- loader = new THREE.ImageLoader();
- ( function ( texture ) {
- loader.load( fullUrl, function ( image ) {
- texture.image = image;
- texture.needsUpdate = true;
- textureCallback();
- } );
- } )( texture )
- }
- texture.mapping = textureJSON.mapping;
- if ( THREE[ textureJSON.minFilter ] !== undefined )
- texture.minFilter = THREE[ textureJSON.minFilter ];
- if ( THREE[ textureJSON.magFilter ] !== undefined )
- texture.magFilter = THREE[ textureJSON.magFilter ];
- if ( textureJSON.anisotropy ) texture.anisotropy = textureJSON.anisotropy;
- if ( textureJSON.repeat ) {
- texture.repeat.set( textureJSON.repeat[ 0 ], textureJSON.repeat[ 1 ] );
- if ( textureJSON.repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
- if ( textureJSON.repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
- }
- if ( textureJSON.offset ) {
- texture.offset.set( textureJSON.offset[ 0 ], textureJSON.offset[ 1 ] );
- }
- // handle wrap after repeat so that default repeat can be overriden
- if ( textureJSON.wrap ) {
- var wrapMap = {
- "repeat": THREE.RepeatWrapping,
- "mirror": THREE.MirroredRepeatWrapping
- }
- if ( wrapMap[ textureJSON.wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ textureJSON.wrap[ 0 ] ];
- if ( wrapMap[ textureJSON.wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ textureJSON.wrap[ 1 ] ];
- }
- }
- result.textures[ textureID ] = texture;
- }
- // materials
- var matID, matJSON;
- var parID;
- for ( matID in data.materials ) {
- matJSON = data.materials[ matID ];
- for ( parID in matJSON.parameters ) {
- if ( parID === "envMap" || parID === "map" || parID === "lightMap" || parID === "bumpMap" || parID === "alphaMap" ) {
- matJSON.parameters[ parID ] = result.textures[ matJSON.parameters[ parID ] ];
- } else if ( parID === "shading" ) {
- matJSON.parameters[ parID ] = ( matJSON.parameters[ parID ] === "flat" ) ? THREE.FlatShading : THREE.SmoothShading;
- } else if ( parID === "side" ) {
- if ( matJSON.parameters[ parID ] == "double" ) {
- matJSON.parameters[ parID ] = THREE.DoubleSide;
- } else if ( matJSON.parameters[ parID ] == "back" ) {
- matJSON.parameters[ parID ] = THREE.BackSide;
- } else {
- matJSON.parameters[ parID ] = THREE.FrontSide;
- }
- } else if ( parID === "blending" ) {
- matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.NormalBlending;
- } else if ( parID === "combine" ) {
- matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.MultiplyOperation;
- } else if ( parID === "vertexColors" ) {
- if ( matJSON.parameters[ parID ] == "face" ) {
- matJSON.parameters[ parID ] = THREE.FaceColors;
- // default to vertex colors if "vertexColors" is anything else face colors or 0 / null / false
- } else if ( matJSON.parameters[ parID ] ) {
- matJSON.parameters[ parID ] = THREE.VertexColors;
- }
- } else if ( parID === "wrapRGB" ) {
- var v3 = matJSON.parameters[ parID ];
- matJSON.parameters[ parID ] = new THREE.Vector3( v3[ 0 ], v3[ 1 ], v3[ 2 ] );
- }
- }
- if ( matJSON.parameters.opacity !== undefined && matJSON.parameters.opacity < 1.0 ) {
- matJSON.parameters.transparent = true;
- }
- if ( matJSON.parameters.normalMap ) {
- var shader = THREE.ShaderLib[ "normalmap" ];
- var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
- var diffuse = matJSON.parameters.color;
- var specular = matJSON.parameters.specular;
- var ambient = matJSON.parameters.ambient;
- var shininess = matJSON.parameters.shininess;
- uniforms[ "tNormal" ].value = result.textures[ matJSON.parameters.normalMap ];
- if ( matJSON.parameters.normalScale ) {
- uniforms[ "uNormalScale" ].value.set( matJSON.parameters.normalScale[ 0 ], matJSON.parameters.normalScale[ 1 ] );
- }
- if ( matJSON.parameters.map ) {
- uniforms[ "tDiffuse" ].value = matJSON.parameters.map;
- uniforms[ "enableDiffuse" ].value = true;
- }
- if ( matJSON.parameters.envMap ) {
- uniforms[ "tCube" ].value = matJSON.parameters.envMap;
- uniforms[ "enableReflection" ].value = true;
- uniforms[ "reflectivity" ].value = matJSON.parameters.reflectivity;
- }
- if ( matJSON.parameters.lightMap ) {
- uniforms[ "tAO" ].value = matJSON.parameters.lightMap;
- uniforms[ "enableAO" ].value = true;
- }
- if ( matJSON.parameters.specularMap ) {
- uniforms[ "tSpecular" ].value = result.textures[ matJSON.parameters.specularMap ];
- uniforms[ "enableSpecular" ].value = true;
- }
- if ( matJSON.parameters.displacementMap ) {
- uniforms[ "tDisplacement" ].value = result.textures[ matJSON.parameters.displacementMap ];
- uniforms[ "enableDisplacement" ].value = true;
- uniforms[ "uDisplacementBias" ].value = matJSON.parameters.displacementBias;
- uniforms[ "uDisplacementScale" ].value = matJSON.parameters.displacementScale;
- }
- uniforms[ "diffuse" ].value.setHex( diffuse );
- uniforms[ "specular" ].value.setHex( specular );
- uniforms[ "ambient" ].value.setHex( ambient );
- uniforms[ "shininess" ].value = shininess;
- if ( matJSON.parameters.opacity ) {
- uniforms[ "opacity" ].value = matJSON.parameters.opacity;
- }
- var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: true };
- material = new THREE.ShaderMaterial( parameters );
- } else {
- material = new THREE[ matJSON.type ]( matJSON.parameters );
- }
- material.name = matID;
- result.materials[ matID ] = material;
- }
- // second pass through all materials to initialize MeshFaceMaterials
- // that could be referring to other materials out of order
- for ( matID in data.materials ) {
- matJSON = data.materials[ matID ];
- if ( matJSON.parameters.materials ) {
- var materialArray = [];
- for ( var i = 0; i < matJSON.parameters.materials.length; i ++ ) {
- var label = matJSON.parameters.materials[ i ];
- materialArray.push( result.materials[ label ] );
- }
- result.materials[ matID ].materials = materialArray;
- }
- }
- // objects ( synchronous init of procedural primitives )
- handle_objects();
- // defaults
- if ( result.cameras && data.defaults.camera ) {
- result.currentCamera = result.cameras[ data.defaults.camera ];
- }
- if ( result.fogs && data.defaults.fog ) {
- result.scene.fog = result.fogs[ data.defaults.fog ];
- }
- // synchronous callback
- scope.callbackSync( result );
- // just in case there are no async elements
- async_callback_gate();
- }
- }
|