scene.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import * as THREE from '../../../build/three.module.js';
  2. var camera, scene, renderer, group;
  3. function init( canvas, width, height, pixelRatio, path ) {
  4. camera = new THREE.PerspectiveCamera( 40, width / height, 1, 1000 );
  5. camera.position.z = 200;
  6. scene = new THREE.Scene();
  7. scene.fog = new THREE.Fog( 0x444466, 100, 400 );
  8. scene.background = new THREE.Color( 0x444466 );
  9. group = new THREE.Group();
  10. scene.add( group );
  11. // we don't use ImageLoader since it has a DOM dependency (HTML5 image element)
  12. var loader = new THREE.ImageBitmapLoader().setPath( path );
  13. loader.setOptions( { imageOrientation: 'flipY' } );
  14. loader.load( 'textures/matcaps/matcap-porcelain-white.jpg', function ( imageBitmap ) {
  15. var texture = new THREE.CanvasTexture( imageBitmap );
  16. var geometry = new THREE.IcosahedronGeometry( 5, 8 );
  17. var materials = [
  18. new THREE.MeshMatcapMaterial( { color: 0xaa24df, matcap: texture } ),
  19. new THREE.MeshMatcapMaterial( { color: 0x605d90, matcap: texture } ),
  20. new THREE.MeshMatcapMaterial( { color: 0xe04a3f, matcap: texture } ),
  21. new THREE.MeshMatcapMaterial( { color: 0xe30456, matcap: texture } )
  22. ];
  23. for ( var i = 0; i < 100; i ++ ) {
  24. var material = materials[ i % materials.length ];
  25. var mesh = new THREE.Mesh( geometry, material );
  26. mesh.position.x = random() * 200 - 100;
  27. mesh.position.y = random() * 200 - 100;
  28. mesh.position.z = random() * 200 - 100;
  29. mesh.scale.setScalar( random() + 1 );
  30. group.add( mesh );
  31. }
  32. renderer = new THREE.WebGLRenderer( { antialias: true, canvas: canvas } );
  33. renderer.setPixelRatio( pixelRatio );
  34. renderer.setSize( width, height, false );
  35. animate();
  36. } );
  37. }
  38. function animate() {
  39. // group.rotation.x = Date.now() / 4000;
  40. group.rotation.y = - Date.now() / 4000;
  41. renderer.render( scene, camera );
  42. if ( self.requestAnimationFrame ) {
  43. self.requestAnimationFrame( animate );
  44. } else {
  45. // Firefox
  46. }
  47. }
  48. // PRNG
  49. var seed = 1;
  50. function random() {
  51. var x = Math.sin( seed ++ ) * 10000;
  52. return x - Math.floor( x );
  53. }
  54. export default init;