scenes.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. {
  25. geom = new THREE.BoxGeometry( 1, 1, 1 );
  26. scale.y = Math.random() * 200 + 100;
  27. scale.z = Math.random() * 200 + 100;
  28. color.setRGB( 0, 0, Math.random()+0.1 );
  29. }
  30. else if ( objectType == "sphere" )
  31. {
  32. geom = new THREE.IcosahedronGeometry( 1, 1 )
  33. scale.y = scale.z = scale.x;
  34. color.setRGB( Math.random()+0.1, 0, 0 );
  35. }
  36. // give the geom's vertices a random color, to be displayed
  37. applyVertexColors( geom, color );
  38. var mesh = new THREE.Mesh( geom );
  39. mesh.position.copy( position );
  40. mesh.rotation.copy( rotation );
  41. mesh.scale.copy( scale );
  42. mesh.updateMatrix();
  43. geometry.merge( mesh.geometry, mesh.matrix );
  44. }
  45. return geometry;
  46. }
  47. function Scene ( type, numObjects, cameraZ, fov, rotationSpeed, clearColor ) {
  48. this.clearColor = clearColor;
  49. this.camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 10000 );
  50. this.camera.position.z = cameraZ;
  51. // Setup scene
  52. this.scene = new THREE.Scene();
  53. this.scene.add( new THREE.AmbientLight( 0x555555 ) );
  54. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  55. light.position.set( 0, 500, 2000 );
  56. this.scene.add( light );
  57. this.rotationSpeed = rotationSpeed;
  58. defaultMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } );
  59. this.mesh = new THREE.Mesh( generateGeometry( type, numObjects ), defaultMaterial );
  60. this.scene.add( this.mesh );
  61. renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
  62. this.fbo = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, renderTargetParameters );
  63. this.render = function( delta, rtt ) {
  64. this.mesh.rotation.x += delta*this.rotationSpeed.x;
  65. this.mesh.rotation.y += delta*this.rotationSpeed.y;
  66. this.mesh.rotation.z += delta*this.rotationSpeed.z;
  67. renderer.setClearColor( this.clearColor, 1 );
  68. if (rtt)
  69. renderer.render( this.scene, this.camera, this.fbo, true );
  70. else
  71. renderer.render( this.scene, this.camera );
  72. };
  73. }