scenes.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function generateGeometry( objectType, numObjects ) {
  2. var geometry = new THREE.Geometry();
  3. function applyVertexColors( g, c ) {
  4. g.faces.forEach( function( f ) {
  5. var n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  6. for ( var j = 0; j < n; j ++ ) {
  7. f.vertexColors[ j ] = c;
  8. }
  9. } );
  10. }
  11. for ( var i = 0; i < numObjects; i ++ ) {
  12. var position = new THREE.Vector3();
  13. position.x = Math.random() * 10000 - 5000;
  14. position.y = Math.random() * 6000 - 3000;
  15. position.z = Math.random() * 8000 - 4000;
  16. var rotation = new THREE.Euler();
  17. rotation.x = Math.random() * 2 * Math.PI;
  18. rotation.y = Math.random() * 2 * Math.PI;
  19. rotation.z = Math.random() * 2 * Math.PI;
  20. var scale = new THREE.Vector3();
  21. var geom, color = new THREE.Color();
  22. scale.x = Math.random() * 200 + 100;
  23. if ( objectType == "cube" ) {
  24. geom = new THREE.BoxGeometry( 1, 1, 1 );
  25. scale.y = Math.random() * 200 + 100;
  26. scale.z = Math.random() * 200 + 100;
  27. color.setRGB( 0, 0, Math.random() + 0.1 );
  28. } else if ( objectType == "sphere" ) {
  29. geom = new THREE.IcosahedronGeometry( 1, 1 );
  30. scale.y = scale.z = scale.x;
  31. color.setRGB( Math.random() + 0.1, 0, 0 );
  32. }
  33. // give the geom's vertices a random color, to be displayed
  34. applyVertexColors( geom, color );
  35. var mesh = new THREE.Mesh( geom );
  36. mesh.position.copy( position );
  37. mesh.rotation.copy( rotation );
  38. mesh.scale.copy( scale );
  39. mesh.updateMatrix();
  40. geometry.merge( mesh.geometry, mesh.matrix );
  41. }
  42. return geometry;
  43. }
  44. function Scene ( type, numObjects, cameraZ, fov, rotationSpeed, clearColor ) {
  45. this.clearColor = clearColor;
  46. this.camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 10000 );
  47. this.camera.position.z = cameraZ;
  48. // Setup scene
  49. this.scene = new THREE.Scene();
  50. this.scene.add( new THREE.AmbientLight( 0x555555 ) );
  51. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  52. light.position.set( 0, 500, 2000 );
  53. this.scene.add( light );
  54. this.rotationSpeed = rotationSpeed;
  55. defaultMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } );
  56. this.mesh = new THREE.Mesh( generateGeometry( type, numObjects ), defaultMaterial );
  57. this.scene.add( this.mesh );
  58. renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
  59. this.fbo = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, renderTargetParameters );
  60. this.render = function( delta, rtt ) {
  61. this.mesh.rotation.x += delta * this.rotationSpeed.x;
  62. this.mesh.rotation.y += delta * this.rotationSpeed.y;
  63. this.mesh.rotation.z += delta * this.rotationSpeed.z;
  64. renderer.setClearColor( this.clearColor );
  65. if ( rtt )
  66. renderer.render( this.scene, this.camera, this.fbo, true );
  67. else
  68. renderer.render( this.scene, this.camera );
  69. };
  70. }