scenes.js 3.1 KB

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