123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- var SceneUtils = {
-
- loadScene : function( url, callback_sync, callback_async, callback_progress ) {
- var worker = new Worker( url );
- worker.postMessage( 0 );
- worker.onmessage = function( event ) {
- var dg, dm, dd, dl, dc, df, dt,
- g, o, m, l, p, c, t, f, tt, pp,
- geometry, material, camera, fog,
- texture, images,
- materials,
- data, loader,
- counter_models, counter_textures,
- total_models, total_textures,
- result;
- data = event.data;
- loader = new THREE.Loader();
-
- counter_models = 0;
- counter_textures = 0;
-
- result = {
-
- scene: new THREE.Scene(),
- geometries: {},
- materials: {},
- textures: {},
- objects: {},
- cameras: {},
- lights: {},
- fogs: {}
-
- };
-
- function handle_objects() {
-
- for( dd in data.objects ) {
-
- if ( !result.objects[ dd ] ) {
-
- o = data.objects[ dd ];
-
- geometry = result.geometries[ o.geometry ];
-
- if ( geometry ) {
-
- materials = [];
- for( i = 0; i < o.materials.length; i++ ) {
-
- materials[ i ] = result.materials[ o.materials[i] ];
-
- }
-
- p = o.position;
- r = o.rotation;
- s = o.scale;
-
- object = new THREE.Mesh( geometry, materials );
- object.position.set( p[0], p[1], p[2] );
- object.rotation.set( r[0], r[1], r[2] );
- object.scale.set( s[0], s[1], s[2] );
- object.visible = o.visible;
-
- result.scene.addObject( object );
-
- result.objects[ dd ] = object;
-
- }
-
- }
-
- }
-
- };
-
- function handle_mesh( geo, id ) {
-
- result.geometries[ id ] = geo;
- handle_objects();
-
- };
-
- function create_callback( id ) {
-
- return function( geo ) {
-
- handle_mesh( geo, id );
-
- counter_models -= 1;
-
- async_callback_gate();
-
- }
-
- };
-
- function async_callback_gate() {
-
- var progress = {
-
- total_models: total_models,
- total_textures: total_textures,
- loaded_models: total_models - counter_models,
- loaded_textures: total_textures - counter_textures
-
- };
-
- callback_progress( progress, result );
-
- if( counter_models == 0 && counter_textures == 0 ) {
-
- callback_async( result );
-
- }
-
- };
-
- var callback_texture = function( images ) {
-
- counter_textures -= 1;
- async_callback_gate();
-
- };
-
- // first go synchronous elements
-
- // cameras
-
- for( dc in data.cameras ) {
-
- c = data.cameras[ dc ];
-
- if ( c.type == "perspective" ) {
-
- camera = new THREE.Camera( c.fov, c.aspect, c.near, c.far );
-
- } else if ( c.type == "ortho" ) {
-
- camera = new THREE.Camera();
- camera.projectionMatrix = THREE.Matrix4.makeOrtho( c.left, c.right, c.top, c.bottom, c.near, c.far );
-
- }
-
- p = c.position;
- t = c.target;
- camera.position.set( p[0], p[1], p[2] );
- camera.target.position.set( t[0], t[1], t[2] );
-
- result.cameras[ dc ] = camera;
-
- }
-
- // lights
-
- for( dl in data.lights ) {
-
- l = data.lights[ dl ];
-
- if ( l.type == "directional" ) {
-
- p = l.direction;
-
- light = new THREE.DirectionalLight();
- light.position.set( p[0], p[1], p[2] );
- light.position.normalize();
-
- } else if ( l.type == "point" ) {
-
- p = l.position;
-
- light = new THREE.PointLight();
- light.position.set( p[0], p[1], p[2] );
-
- }
-
- c = l.color;
- i = l.intensity || 1;
- light.color.setRGB( c[0] * i, c[1] * i, c[2] * i );
-
- result.scene.addLight( light );
-
- result.lights[ dl ] = light;
-
- }
-
- // 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;
-
- }
-
- // 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;
- // now come potentially asynchronous elements
-
- // geometries
-
- // count how many models will be loaded asynchronously
-
- for( dg in data.geometries ) {
-
- g = data.geometries[ dg ];
-
- if ( g.type == "bin_mesh" || g.type == "ascii_mesh" ) {
-
- counter_models += 1;
-
- }
-
- }
-
- total_models = counter_models;
-
- for( dg in data.geometries ) {
-
- g = data.geometries[ dg ];
-
- if ( g.type == "cube" ) {
-
- geometry = new Cube( g.width, g.height, g.depth, g.segments_width, g.segments_height, null, g.flipped, g.sides );
- result.geometries[ dg ] = geometry;
-
- } else if ( g.type == "plane" ) {
-
- geometry = new Plane( g.width, g.height, g.segments_width, g.segments_height );
- result.geometries[ dg ] = geometry;
-
- } else if ( g.type == "sphere" ) {
-
- geometry = new Sphere( g.radius, g.segments_width, g.segments_height );
- result.geometries[ dg ] = geometry;
-
- } else if ( g.type == "cylinder" ) {
-
- geometry = new Cylinder( g.numSegs, g.topRad, g.botRad, g.height, g.topOffset, g.botOffset );
- result.geometries[ dg ] = geometry;
- } else if ( g.type == "torus" ) {
-
- geometry = new Torus( g.radius, g.tube, g.segmentsR, g.segmentsT );
- result.geometries[ dg ] = geometry;
- } else if ( g.type == "icosahedron" ) {
-
- geometry = new Icosahedron( g.subdivisions );
- result.geometries[ dg ] = geometry;
-
- } else if ( g.type == "bin_mesh" ) {
-
- loader.loadBinary( { model: g.url,
- callback: create_callback( dg )
- } );
-
- } else if ( g.type == "ascii_mesh" ) {
-
- loader.loadAscii( { model: g.url,
- callback: create_callback( dg )
- } );
-
- }
-
- }
- // 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;
-
- } else {
-
- counter_textures += 1;
-
- }
-
- }
-
- 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 ) {
-
- images = ImageUtils.loadArray( tt.url, callback_texture );
- texture = new THREE.Texture( images, tt.mapping );
-
- } else {
-
- texture = ImageUtils.loadTexture( tt.url, tt.mapping, callback_texture );
-
- if ( THREE[ tt.min_filter ] != undefined )
- texture.min_filter = THREE[ tt.min_filter ];
-
- if ( THREE[ tt.mag_filter ] != undefined )
- texture.mag_filter = THREE[ tt.mag_filter ];
-
- }
-
- result.textures[ dt ] = texture;
-
- }
-
- // materials
-
- for( dm in data.materials ) {
-
- m = data.materials[ dm ];
-
- for( pp in m.parameters ) {
-
- if ( pp == "env_map" || pp == "map" || pp == "light_map" ) {
-
- 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 == "blending" ) {
-
- m.parameters[ pp ] = THREE[ m.parameters[ pp ] ] ? THREE[ m.parameters[ pp ] ] : THREE.NormalBlending;
-
- } else if ( pp == "combine" ) {
-
- m.parameters[ pp ] = ( m.parameters[ pp ] == "MixOperation" ) ? THREE.MixOperation : THREE.MultiplyOperation;
-
- }
-
- }
-
- material = new THREE[ m.type ]( m.parameters );
- result.materials[ dm ] = material;
-
- }
-
- // objects ( synchronous init of procedural primitives )
-
- handle_objects();
-
- // synchronous callback
-
- callback_sync( result );
- };
-
- },
- addMesh: function ( scene, geometry, scale, x, y, z, rx, ry, rz, material ) {
- var mesh = new THREE.Mesh( geometry, material );
- mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
- mesh.position.x = x;
- mesh.position.y = y;
- mesh.position.z = z;
- mesh.rotation.x = rx;
- mesh.rotation.y = ry;
- mesh.rotation.z = rz;
- scene.addObject( mesh );
- return mesh;
- },
- addPanoramaCubeWebGL: function ( scene, size, textureCube ) {
- var shader = ShaderUtils.lib["cube"];
- shader.uniforms["tCube"].texture = textureCube;
- var material = new THREE.MeshShaderMaterial( { fragment_shader: shader.fragment_shader,
- vertex_shader: shader.vertex_shader,
- uniforms: shader.uniforms
- } ),
- mesh = new THREE.Mesh( new Cube( size, size, size, 1, 1, null, true ), material );
- scene.addObject( mesh );
- return mesh;
- },
- addPanoramaCube: function( scene, size, images ) {
- var materials = [], mesh;
- materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[0] ) } ) );
- materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[1] ) } ) );
- materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[2] ) } ) );
- materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[3] ) } ) );
- materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[4] ) } ) );
- materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[5] ) } ) );
- mesh = new THREE.Mesh( new Cube( size, size, size, 1, 1, materials, true ), new THREE.MeshFaceMaterial() );
- scene.addObject( mesh );
- return mesh;
- },
- addPanoramaCubePlanes: function ( scene, size, images ) {
- var hsize = size/2, plane = new Plane( size, size ), pi2 = Math.PI/2, pi = Math.PI;
- SceneUtils.addMesh( scene, plane, 1, 0, 0, -hsize, 0, 0, 0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[5] ) } ) );
- SceneUtils.addMesh( scene, plane, 1, -hsize, 0, 0, 0, pi2, 0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[0] ) } ) );
- SceneUtils.addMesh( scene, plane, 1, hsize, 0, 0, 0, -pi2, 0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[1] ) } ) );
- SceneUtils.addMesh( scene, plane, 1, 0, hsize, 0, pi2, 0, pi, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[2] ) } ) );
- SceneUtils.addMesh( scene, plane, 1, 0, -hsize, 0, -pi2, 0, pi, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[3] ) } ) );
- }
- };
|