1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141 |
- /**
- * @author alteredq / http://alteredqualia.com/
- */
- THREE.SceneLoader = function () {
- this.onLoadStart = function () {};
- this.onLoadProgress = function() {};
- this.onLoadComplete = function () {};
- this.callbackSync = function () {};
- this.callbackProgress = function () {};
- this.geometryHandlerMap = {};
- this.hierarchyHandlerMap = {};
- this.addGeometryHandler( "ascii", THREE.JSONLoader );
- this.addGeometryHandler( "binary", THREE.BinaryLoader );
- };
- THREE.SceneLoader.prototype.constructor = THREE.SceneLoader;
- THREE.SceneLoader.prototype.load = function ( url, callbackFinished ) {
- var scope = this;
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function () {
- if ( xhr.readyState === 4 ) {
- if ( xhr.status === 200 || xhr.status === 0 ) {
- var json = JSON.parse( xhr.responseText );
- scope.parse( json, callbackFinished, url );
- } else {
- console.error( "THREE.SceneLoader: Couldn't load [" + url + "] [" + xhr.status + "]" );
- }
- }
- };
- xhr.open( "GET", url, true );
- xhr.send( null );
- };
- THREE.SceneLoader.prototype.addGeometryHandler = function ( typeID, loaderClass ) {
- this.geometryHandlerMap[ typeID ] = { "loaderClass": loaderClass };
- };
- THREE.SceneLoader.prototype.addHierarchyHandler = function ( typeID, loaderClass ) {
- this.hierarchyHandlerMap[ typeID ] = { "loaderClass": loaderClass };
- };
- THREE.SceneLoader.prototype.parse = function ( json, callbackFinished, url ) {
- var scope = this;
- var urlBase = THREE.Loader.prototype.extractUrlBase( url );
- var dg, dm, dc, df, dt,
- g, m, l, d, p, r, q, s, c, t, f, tt, pp, u,
- geometry, material, camera, fog,
- texture, images,
- 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.geometryHandlerMap ) {
- var loaderClass = this.geometryHandlerMap[ typeID ][ "loaderClass" ];
- this.geometryHandlerMap[ typeID ][ "loaderObject" ] = new loaderClass();
- }
- // async hierachy loaders
- for ( var typeID in this.hierarchyHandlerMap ) {
- var loaderClass = this.hierarchyHandlerMap[ typeID ][ "loaderClass" ];
- this.hierarchyHandlerMap[ 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: {}
- };
- if ( data.transform ) {
- var position = data.transform.position,
- rotation = data.transform.rotation,
- scale = data.transform.scale;
- if ( position )
- result.scene.position.set( position[ 0 ], position[ 1 ], position [ 2 ] );
- if ( rotation )
- result.scene.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation [ 2 ] );
- if ( scale )
- result.scene.scale.set( scale[ 0 ], scale[ 1 ], scale [ 2 ] );
- 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 ) {
- for ( var dd in children ) {
- // check by id if child has already been handled,
- // if not, create new object
- if ( result.objects[ dd ] === undefined ) {
- var o = children[ dd ];
- var object = null;
- // meshes
- if ( o.type && ( o.type in scope.hierarchyHandlerMap ) && o.loading === undefined ) {
- var loaderParameters = {};
- for ( var parType in g ) {
- if ( parType !== "type" && parType !== "url" ) {
- loaderParameters[ parType ] = g[ parType ];
- }
- }
- material = result.materials[ o.material ];
- o.loading = true;
- var loader = scope.hierarchyHandlerMap[ o.type ][ "loaderObject" ];
- // OBJLoader
- if ( loader.addEventListener ) {
- loader.addEventListener( 'load', create_callback_hierachy( dd, parent, material, o ) );
- loader.load( get_url( o.url, data.urlBaseType ) );
- } else {
- // ColladaLoader
- if ( loader.options ) {
- loader.load( get_url( o.url, data.urlBaseType ), create_callback_hierachy( dd, parent, material, o ) );
- // UTF8Loader
- } else {
- loader.load( get_url( o.url, data.urlBaseType ), create_callback_hierachy( dd, parent, material, o ), loaderParameters );
- }
- }
- } else if ( o.geometry !== undefined ) {
- geometry = result.geometries[ o.geometry ];
- // geometry already loaded
- if ( geometry ) {
- var hasNormals = false;
- material = result.materials[ o.material ];
- hasNormals = material instanceof THREE.ShaderMaterial;
- if ( hasNormals ) {
- geometry.computeTangents();
- }
- p = o.position;
- r = o.rotation;
- q = o.quaternion;
- s = o.scale;
- m = o.matrix;
- // turn off quaternions, for the moment
- q = 0;
- // use materials from the model file
- // if there is no material specified in the object
- if ( ! o.material ) {
- material = new THREE.MeshFaceMaterial( result.face_materials[ o.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[ o.geometry ] );
- }
- if ( o.skin ) {
- object = new THREE.SkinnedMesh( geometry, material );
- } else if ( o.morph ) {
- object = new THREE.MorphAnimMesh( geometry, material );
- if ( o.duration !== undefined ) {
- object.duration = o.duration;
- }
- if ( o.time !== undefined ) {
- object.time = o.time;
- }
- if ( o.mirroredLoop !== undefined ) {
- object.mirroredLoop = o.mirroredLoop;
- }
- if ( material.morphNormals ) {
- geometry.computeMorphNormals();
- }
- } else {
- object = new THREE.Mesh( geometry, material );
- }
- object.name = dd;
- if ( m ) {
- object.matrixAutoUpdate = false;
- object.matrix.set(
- m[0], m[1], m[2], m[3],
- m[4], m[5], m[6], m[7],
- m[8], m[9], m[10], m[11],
- m[12], m[13], m[14], m[15]
- );
- } else {
- object.position.set( p[0], p[1], p[2] );
- if ( q ) {
- object.quaternion.set( q[0], q[1], q[2], q[3] );
- object.useQuaternion = true;
- } else {
- object.rotation.set( r[0], r[1], r[2] );
- }
- object.scale.set( s[0], s[1], s[2] );
- }
- object.visible = o.visible;
- object.castShadow = o.castShadow;
- object.receiveShadow = o.receiveShadow;
- parent.add( object );
- result.objects[ dd ] = object;
- }
- // lights
- } else if ( o.type === "DirectionalLight" || o.type === "PointLight" || o.type === "AmbientLight" ) {
- hex = ( o.color !== undefined ) ? o.color : 0xffffff;
- intensity = ( o.intensity !== undefined ) ? o.intensity : 1;
- if ( o.type === "DirectionalLight" ) {
- p = o.direction;
- light = new THREE.DirectionalLight( hex, intensity );
- light.position.set( p[0], p[1], p[2] );
- if ( o.target ) {
- target_array.push( { "object": light, "targetName" : o.target } );
- // kill existing default target
- // otherwise it gets added to scene when parent gets added
- light.target = null;
- }
- } else if ( o.type === "PointLight" ) {
- p = o.position;
- d = o.distance;
- light = new THREE.PointLight( hex, intensity, d );
- light.position.set( p[0], p[1], p[2] );
- } else if ( o.type === "AmbientLight" ) {
- light = new THREE.AmbientLight( hex );
- }
- parent.add( light );
- light.name = dd;
- result.lights[ dd ] = light;
- result.objects[ dd ] = light;
- // cameras
- } else if ( o.type === "PerspectiveCamera" || o.type === "OrthographicCamera" ) {
- if ( o.type === "PerspectiveCamera" ) {
- camera = new THREE.PerspectiveCamera( o.fov, o.aspect, o.near, o.far );
- } else if ( o.type === "OrthographicCamera" ) {
- camera = new THREE.OrthographicCamera( c.left, c.right, c.top, c.bottom, c.near, c.far );
- }
- p = o.position;
- camera.position.set( p[0], p[1], p[2] );
- parent.add( camera );
- camera.name = dd;
- result.cameras[ dd ] = camera;
- result.objects[ dd ] = camera;
- // pure Object3D
- } else {
- p = o.position;
- r = o.rotation;
- q = o.quaternion;
- s = o.scale;
- // turn off quaternions, for the moment
- q = 0;
- object = new THREE.Object3D();
- object.name = dd;
- object.position.set( p[0], p[1], p[2] );
- if ( q ) {
- object.quaternion.set( q[0], q[1], q[2], q[3] );
- object.useQuaternion = true;
- } else {
- object.rotation.set( r[0], r[1], r[2] );
- }
- object.scale.set( s[0], s[1], s[2] );
- object.visible = ( o.visible !== undefined ) ? o.visible : false;
- parent.add( object );
- result.objects[ dd ] = object;
- result.empties[ dd ] = object;
- }
- if ( object ) {
- if ( o.properties !== undefined ) {
- for ( var key in o.properties ) {
- var value = o.properties[ key ];
- object.properties[ key ] = value;
- }
- }
- if ( o.children !== undefined ) {
- handle_children( object, o.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, o ) {
- var p = o.position;
- var r = o.rotation;
- var q = o.quaternion;
- var s = o.scale;
- node.position.set( p[0], p[1], p[2] );
- if ( q ) {
- node.quaternion.set( q[0], q[1], q[2], q[3] );
- node.useQuaternion = true;
- } else {
- node.rotation.set( r[0], r[1], r[2] );
- }
- node.scale.set( s[0], s[1], s[2] );
- if ( material ) {
- node.traverse( function ( child ) {
- child.material = material;
- } );
- }
- parent.add( node );
- result.objects[ id ] = node;
- handle_objects();
- };
- function create_callback_geometry( id ) {
- return function( geo, mat ) {
- 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 EventTarget
- 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 ) {
- 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.properties.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 );
- };
- };
- // first go synchronous elements
- // fogs
- for ( df in data.fogs ) {
- f = data.fogs[ df ];
- if ( f.type === "linear" ) {
- fog = new THREE.Fog( 0x000000, f.near, f.far );
- } else if ( f.type === "exp2" ) {
- fog = new THREE.FogExp2( 0x000000, f.density );
- }
- c = f.color;
- fog.color.setRGB( c[0], c[1], c[2] );
- result.fogs[ df ] = fog;
- }
- // now come potentially asynchronous elements
- // geometries
- // count how many geometries will be loaded asynchronously
- for ( dg in data.geometries ) {
- g = data.geometries[ dg ];
- if ( g.type in this.geometryHandlerMap ) {
- counter_models += 1;
- scope.onLoadStart();
- }
- }
- // count how many hierarchies will be loaded asynchronously
- for ( var dd in data.objects ) {
- var o = data.objects[ dd ];
- if ( o.type && ( o.type in this.hierarchyHandlerMap ) ) {
- counter_models += 1;
- scope.onLoadStart();
- }
- }
- total_models = counter_models;
- for ( dg in data.geometries ) {
- g = data.geometries[ dg ];
- if ( g.type === "cube" ) {
- geometry = new THREE.CubeGeometry( g.width, g.height, g.depth, g.widthSegments, g.heightSegments, g.depthSegments );
- result.geometries[ dg ] = geometry;
- } else if ( g.type === "plane" ) {
- geometry = new THREE.PlaneGeometry( g.width, g.height, g.widthSegments, g.heightSegments );
- result.geometries[ dg ] = geometry;
- } else if ( g.type === "sphere" ) {
- geometry = new THREE.SphereGeometry( g.radius, g.widthSegments, g.heightSegments );
- result.geometries[ dg ] = geometry;
- } else if ( g.type === "cylinder" ) {
- geometry = new THREE.CylinderGeometry( g.topRad, g.botRad, g.height, g.radSegs, g.heightSegs );
- result.geometries[ dg ] = geometry;
- } else if ( g.type === "torus" ) {
- geometry = new THREE.TorusGeometry( g.radius, g.tube, g.segmentsR, g.segmentsT );
- result.geometries[ dg ] = geometry;
- } else if ( g.type === "icosahedron" ) {
- geometry = new THREE.IcosahedronGeometry( g.radius, g.subdivisions );
- result.geometries[ dg ] = geometry;
- } else if ( g.type in this.geometryHandlerMap ) {
- var loaderParameters = {};
- for ( var parType in g ) {
- if ( parType !== "type" && parType !== "url" ) {
- loaderParameters[ parType ] = g[ parType ];
- }
- }
- var loader = this.geometryHandlerMap[ g.type ][ "loaderObject" ];
- loader.load( get_url( g.url, data.urlBaseType ), create_callback_geometry( dg ), loaderParameters );
- } else if ( g.type === "embedded" ) {
- var modelJson = data.embeds[ g.id ],
- texture_path = "";
- // pass metadata along to jsonLoader so it knows the format version
- modelJson.metadata = data.metadata;
- if ( modelJson ) {
- var jsonLoader = this.geometryHandlerMap[ "ascii" ][ "loaderObject" ];
- jsonLoader.createModel( modelJson, create_callback_embed( dg ), texture_path );
- }
- }
- }
- // textures
- // count how many textures will be loaded asynchronously
- for ( dt in data.textures ) {
- tt = data.textures[ dt ];
- if ( tt.url instanceof Array ) {
- counter_textures += tt.url.length;
- for( var n = 0; n < tt.url.length; n ++ ) {
- scope.onLoadStart();
- }
- } else {
- counter_textures += 1;
- scope.onLoadStart();
- }
- }
- total_textures = counter_textures;
- for ( dt in data.textures ) {
- tt = data.textures[ dt ];
- if ( tt.mapping !== undefined && THREE[ tt.mapping ] !== undefined ) {
- tt.mapping = new THREE[ tt.mapping ]();
- }
- if ( tt.url instanceof Array ) {
- var count = tt.url.length;
- var url_array = [];
- for( var i = 0; i < count; i ++ ) {
- url_array[ i ] = get_url( tt.url[ i ], data.urlBaseType );
- }
- var isCompressed = url_array[ 0 ].endsWith( ".dds" );
- if ( isCompressed ) {
- texture = THREE.ImageUtils.loadCompressedTextureCube( url_array, tt.mapping, generateTextureCallback( count ) );
- } else {
- texture = THREE.ImageUtils.loadTextureCube( url_array, tt.mapping, generateTextureCallback( count ) );
- }
- } else {
- var isCompressed = tt.url.toLowerCase().endsWith( ".dds" );
- var fullUrl = get_url( tt.url, data.urlBaseType );
- var textureCallback = generateTextureCallback( 1 );
- if ( isCompressed ) {
- texture = THREE.ImageUtils.loadCompressedTexture( fullUrl, tt.mapping, textureCallback );
- } else {
- texture = THREE.ImageUtils.loadTexture( fullUrl, tt.mapping, textureCallback );
- }
- if ( THREE[ tt.minFilter ] !== undefined )
- texture.minFilter = THREE[ tt.minFilter ];
- if ( THREE[ tt.magFilter ] !== undefined )
- texture.magFilter = THREE[ tt.magFilter ];
- if ( tt.anisotropy ) texture.anisotropy = tt.anisotropy;
- if ( tt.repeat ) {
- texture.repeat.set( tt.repeat[ 0 ], tt.repeat[ 1 ] );
- if ( tt.repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
- if ( tt.repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
- }
- if ( tt.offset ) {
- texture.offset.set( tt.offset[ 0 ], tt.offset[ 1 ] );
- }
- // handle wrap after repeat so that default repeat can be overriden
- if ( tt.wrap ) {
- var wrapMap = {
- "repeat" : THREE.RepeatWrapping,
- "mirror" : THREE.MirroredRepeatWrapping
- }
- if ( wrapMap[ tt.wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ tt.wrap[ 0 ] ];
- if ( wrapMap[ tt.wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ tt.wrap[ 1 ] ];
- }
- }
- result.textures[ dt ] = texture;
- }
- // materials
- for ( dm in data.materials ) {
- m = data.materials[ dm ];
- for ( pp in m.parameters ) {
- if ( pp === "envMap" || pp === "map" || pp === "lightMap" || pp === "bumpMap" ) {
- m.parameters[ pp ] = result.textures[ m.parameters[ pp ] ];
- } else if ( pp === "shading" ) {
- m.parameters[ pp ] = ( m.parameters[ pp ] === "flat" ) ? THREE.FlatShading : THREE.SmoothShading;
- } else if ( pp === "side" ) {
- if ( m.parameters[ pp ] == "double" ) {
- m.parameters[ pp ] = THREE.DoubleSide;
- } else if ( m.parameters[ pp ] == "back" ) {
- m.parameters[ pp ] = THREE.BackSide;
- } else {
- m.parameters[ pp ] = THREE.FrontSide;
- }
- } else if ( pp === "blending" ) {
- m.parameters[ pp ] = m.parameters[ pp ] in THREE ? THREE[ m.parameters[ pp ] ] : THREE.NormalBlending;
- } else if ( pp === "combine" ) {
- m.parameters[ pp ] = ( m.parameters[ pp ] == "MixOperation" ) ? THREE.MixOperation : THREE.MultiplyOperation;
- } else if ( pp === "vertexColors" ) {
- if ( m.parameters[ pp ] == "face" ) {
- m.parameters[ pp ] = THREE.FaceColors;
- // default to vertex colors if "vertexColors" is anything else face colors or 0 / null / false
- } else if ( m.parameters[ pp ] ) {
- m.parameters[ pp ] = THREE.VertexColors;
- }
- } else if ( pp === "wrapRGB" ) {
- var v3 = m.parameters[ pp ];
- m.parameters[ pp ] = new THREE.Vector3( v3[ 0 ], v3[ 1 ], v3[ 2 ] );
- }
- }
- if ( m.parameters.opacity !== undefined && m.parameters.opacity < 1.0 ) {
- m.parameters.transparent = true;
- }
- if ( m.parameters.normalMap ) {
- var shader = THREE.ShaderUtils.lib[ "normal" ];
- var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
- var diffuse = m.parameters.color;
- var specular = m.parameters.specular;
- var ambient = m.parameters.ambient;
- var shininess = m.parameters.shininess;
- uniforms[ "tNormal" ].value = result.textures[ m.parameters.normalMap ];
- if ( m.parameters.normalScale ) {
- uniforms[ "uNormalScale" ].value.set( m.parameters.normalScale[ 0 ], m.parameters.normalScale[ 1 ] );
- }
- if ( m.parameters.map ) {
- uniforms[ "tDiffuse" ].value = m.parameters.map;
- uniforms[ "enableDiffuse" ].value = true;
- }
- if ( m.parameters.envMap ) {
- uniforms[ "tCube" ].value = m.parameters.envMap;
- uniforms[ "enableReflection" ].value = true;
- uniforms[ "uReflectivity" ].value = m.parameters.reflectivity;
- }
- if ( m.parameters.lightMap ) {
- uniforms[ "tAO" ].value = m.parameters.lightMap;
- uniforms[ "enableAO" ].value = true;
- }
- if ( m.parameters.specularMap ) {
- uniforms[ "tSpecular" ].value = result.textures[ m.parameters.specularMap ];
- uniforms[ "enableSpecular" ].value = true;
- }
- if ( m.parameters.displacementMap ) {
- uniforms[ "tDisplacement" ].value = result.textures[ m.parameters.displacementMap ];
- uniforms[ "enableDisplacement" ].value = true;
- uniforms[ "uDisplacementBias" ].value = m.parameters.displacementBias;
- uniforms[ "uDisplacementScale" ].value = m.parameters.displacementScale;
- }
- uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
- uniforms[ "uSpecularColor" ].value.setHex( specular );
- uniforms[ "uAmbientColor" ].value.setHex( ambient );
- uniforms[ "uShininess" ].value = shininess;
- if ( m.parameters.opacity ) {
- uniforms[ "uOpacity" ].value = m.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[ m.type ]( m.parameters );
- }
- result.materials[ dm ] = material;
- }
- // second pass through all materials to initialize MeshFaceMaterials
- // that could be referring to other materials out of order
- for ( dm in data.materials ) {
- m = data.materials[ dm ];
- if ( m.parameters.materials ) {
- var materialArray = [];
- for ( var i = 0; i < m.parameters.materials.length; i ++ ) {
- var label = m.parameters.materials[ i ];
- materialArray.push( result.materials[ label ] );
- }
- result.materials[ dm ].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 ];
- }
- c = data.defaults.bgcolor;
- result.bgColor = new THREE.Color();
- result.bgColor.setRGB( c[0], c[1], c[2] );
- result.bgColorAlpha = data.defaults.bgalpha;
- // synchronous callback
- scope.callbackSync( result );
- // just in case there are no async elements
- async_callback_gate();
- };
|