SceneUtils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.SceneUtils = {
  5. addMesh : function ( scene, geometry, scale, x, y, z, rx, ry, rz, material ) {
  6. var mesh = new THREE.Mesh( geometry, material );
  7. mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
  8. mesh.position.x = x;
  9. mesh.position.y = y;
  10. mesh.position.z = z;
  11. mesh.rotation.x = rx;
  12. mesh.rotation.y = ry;
  13. mesh.rotation.z = rz;
  14. scene.addObject( mesh );
  15. return mesh;
  16. },
  17. addPanoramaCubeWebGL : function ( scene, size, textureCube ) {
  18. var shader = THREE.ShaderUtils.lib["cube"];
  19. shader.uniforms["tCube"].texture = textureCube;
  20. var material = new THREE.MeshShaderMaterial( {
  21. fragmentShader: shader.fragmentShader,
  22. vertexShader: shader.vertexShader,
  23. uniforms: shader.uniforms
  24. } ),
  25. mesh = new THREE.Mesh( new THREE.CubeGeometry( size, size, size, 1, 1, 1, null, true ), material );
  26. scene.addObject( mesh );
  27. return mesh;
  28. },
  29. addPanoramaCube : function( scene, size, images ) {
  30. var materials = [], mesh;
  31. materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[ 0 ] ) } ) );
  32. materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[ 1 ] ) } ) );
  33. materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[ 2 ] ) } ) );
  34. materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[ 3 ] ) } ) );
  35. materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[ 4 ] ) } ) );
  36. materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[ 5 ] ) } ) );
  37. mesh = new THREE.Mesh( new THREE.Cube( size, size, size, 1, 1, materials, true ), new THREE.MeshFaceMaterial() );
  38. scene.addObject( mesh );
  39. return mesh;
  40. },
  41. addPanoramaCubePlanes : function ( scene, size, images ) {
  42. var hsize = size / 2, plane = new THREE.Plane( size, size ), pi = Math.PI, pi2 = Math.PI / 2;
  43. THREE.SceneUtils.addMesh( scene, plane, 1, 0, 0, -hsize, 0, 0, 0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[5] ) } ) );
  44. THREE.SceneUtils.addMesh( scene, plane, 1, -hsize, 0, 0, 0, pi2, 0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[0] ) } ) );
  45. THREE.SceneUtils.addMesh( scene, plane, 1, hsize, 0, 0, 0, -pi2, 0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[1] ) } ) );
  46. THREE.SceneUtils.addMesh( scene, plane, 1, 0, hsize, 0, pi2, 0, pi, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[2] ) } ) );
  47. THREE.SceneUtils.addMesh( scene, plane, 1, 0, -hsize, 0, -pi2, 0, pi, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[3] ) } ) );
  48. },
  49. showHierarchy : function ( root, visible ) {
  50. THREE.SceneUtils.traverseHierarchy( root, function( node ) { node.visible = visible; } );
  51. },
  52. traverseHierarchy : function ( root, callback ) {
  53. var n, i, l = root.children.length;
  54. for ( i = 0; i < l; i ++ ) {
  55. n = root.children[ i ];
  56. callback( n );
  57. THREE.SceneUtils.traverseHierarchy( n, callback );
  58. }
  59. }
  60. };