scene.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import * as THREE from '../../../build/three.module.js';
  2. let 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. const loader = new THREE.ImageBitmapLoader().setPath( path );
  13. loader.setOptions( { imageOrientation: 'flipY' } );
  14. loader.load( 'textures/matcaps/matcap-porcelain-white.jpg', function ( imageBitmap ) {
  15. const texture = new THREE.CanvasTexture( imageBitmap );
  16. const geometry = new THREE.IcosahedronGeometry( 5, 8 );
  17. const 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 ( let i = 0; i < 100; i ++ ) {
  24. const material = materials[ i % materials.length ];
  25. const 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. renderer.useLegacyLights = false;
  36. animate();
  37. } );
  38. }
  39. function animate() {
  40. // group.rotation.x = Date.now() / 4000;
  41. group.rotation.y = - Date.now() / 4000;
  42. renderer.render( scene, camera );
  43. if ( self.requestAnimationFrame ) {
  44. self.requestAnimationFrame( animate );
  45. } else {
  46. // Firefox
  47. }
  48. }
  49. // PRNG
  50. let seed = 1;
  51. function random() {
  52. const x = Math.sin( seed ++ ) * 10000;
  53. return x - Math.floor( x );
  54. }
  55. export default init;