Space background

Hello,

With an old version of three.js, I used this code to get a space background (I copied it somewhere):

  var starsGeometry = new THREE.Geometry();
  for ( var i = 0; i < 10000; i ++ ) {
    var star = new THREE.Vector3();
    star.x = THREE.Math.randFloatSpread( 2000 );
    star.y = THREE.Math.randFloatSpread( 2000 );
    star.z = THREE.Math.randFloatSpread( 2000 );
    starsGeometry.vertices.push( star );
  }
  var starsMaterial = new THREE.PointsMaterial( { color: 0x888888 } );
  var starField = new THREE.Points( starsGeometry, starsMaterial );
  scene.add( starField );

Now this code does not work anymore. How to do a space background with a recent version of three.js?

I’ve found!

  // -- space background ------------------------------------------------------
  var stars = new Array(0);
  for ( var i = 0; i < 10000; i ++ ) {
    let x = THREE.Math.randFloatSpread( 2000 );
    let y = THREE.Math.randFloatSpread( 2000 );
    let z = THREE.Math.randFloatSpread( 2000 );
    stars.push(x, y, z);
  }
  var starsGeometry = new THREE.BufferGeometry();
  starsGeometry.setAttribute(
    "position", new THREE.Float32BufferAttribute(stars, 3)
  );
  var starsMaterial = new THREE.PointsMaterial( { color: 0x888888 } );
  var starField = new THREE.Points( starsGeometry, starsMaterial );
  scene.add( starField );