Code viewer for World: Solar Systeb

// Cloned by Theo Delettre on 7 Oct 2021 from World "Blank Three.js World" by Starter user 
// Please leave this clone trail here.


// ==== Starter World =================================================================================================
// This code is designed for use on the Ancient Brain site.
// This code may be freely copied and edited by anyone on the Ancient Brain site.
// To include a working run of this program on another site, see the "Embed code" links provided on Ancient Brain.
// ====================================================================================================================




// Starter World for Three.js experiments 

// No Mind
// Just World 







// ===================================================================================================================
// === Start of tweaker's box ======================================================================================== 
// ===================================================================================================================

// The easiest things to modify are in this box.
// You should be able to change things in this box without being a JavaScript programmer.
// Go ahead and change some of these. What's the worst that could happen?



AB.clockTick       = 10;    

	// Speed of run: Step every n milliseconds. Default 100.
	
AB.maxSteps        = 10000;    

	// Length of run: Maximum length of run in steps. Default 1000.

AB.screenshotStep  = 50;   
  
	// Take screenshot on this step. (All resources should have finished loading.) Default 50.



 const FILE_ARRAY = [
		"/uploads/starter/earth.1.jpg",
		"/uploads/starter/earth.2.jpg",
		"/uploads/starter/earth.3.jpg",
		"/uploads/starter/earth.4.jpg",
		"/uploads/starter/earth.5.jpg"
		];
	
 
 

 
const SKYCOLOR 	= 0xffffff;				// a number, not a string 

const ARMYSIZE = 500;       // an "army" of objects 

const objectsize = 300 ;

const WALKSTEP = 50;       // bounds of the random move per timestep 
                            // try 50 (vibrating sheet) versus 1000 (cloud)
         
const MAXPOS                = 20000 ;            // start things within these bounds                    
const startRadiusConst	 	= MAXPOS * 2 ;		// distance from centre to start the camera at
const maxRadiusConst 		= MAXPOS * 5 ;		// maximum distance from camera we will render things  



//--- change ABWorld defaults: -------------------------------

ABHandler.MAXCAMERAPOS = MAXPOS * 10 ;			// allow camera go far away 

ABWorld.drawCameraControls = false; 


// ===================================================================================================================
// === End of tweaker's box ==========================================================================================
// ===================================================================================================================


// You will need to be some sort of JavaScript programmer to change things below the tweaker's box.








 

    var THEARMY 	= new Array( ARMYSIZE );	
    
    var ArmySphere = new Array( ARMYSIZE );

	var textureArray = new Array ( FILE_ARRAY.length );
	
	var angularSpeed = new Array ( ARMYSIZE );
	
	var sun = new THREE.Mesh( new THREE.SphereGeometry ( objectsize*5, 15, 15 ) );
    
    var SunTexture;
	


function loadResources()		// asynchronous file loads - call initScene() when all finished 
{
    var loader = new THREE.TextureLoader();
    SunTexture = loader.load("/uploads/theonogo/1633705352.png");
    
	for ( var i = 0; i < FILE_ARRAY.length; i++ ) 
	  startFileLoad ( i );						// launch n asynchronous file loads
}

	
function startFileLoad ( n )				// asynchronous file load of texture n 
{
	var loader = new THREE.TextureLoader();

	loader.load ( FILE_ARRAY[n], function ( thetexture )  	 
	{
		thetexture.minFilter  = THREE.LinearFilter;
		textureArray[n] = thetexture;
		if ( asynchFinished() ) initArmy();
	});	
}
 
 
function asynchFinished()		// all file loads returned 
{
	for ( var i = 0; i < FILE_ARRAY.length; i++ ) 
		if ( ! textureArray[i] ) 
			return false;
		
	  return true;
}



function initArmy()		 // called when all textures ready 
{
 var t = 0;
 
 sun.material = new THREE.MeshBasicMaterial ( { map: SunTexture } ); 
 
 ABWorld.scene.add(sun);
 
 for ( var c=1 ; c <= ARMYSIZE ; c++ )
 {
        var shape = new THREE.SphereGeometry ( objectsize, 10, 10 ); 
     	//var shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
  
  	var theobject  = new THREE.Mesh( shape );
  	
  	var phi = AB.randomFloatAtoB ( 0, 2 * Math.PI );
  	var radius = AB.randomFloatAtoB ( 4000, MAXPOS );
  	var theta = AB.randomFloatAtoB ( 0, 2 *Math.PI );
  	
	angularSpeed[t] = [AB.randomFloatAtoB ( -0.01 , 0.01 ), AB.randomFloatAtoB ( -0.01 , 0.01 )];
	
 	var r = Math.floor(radius*5/MAXPOS);
 	
 	//var r = AB.randomIntAtoB ( 0, textureArray.length - 1 );    			 // random texture 
    
    theobject.material =  new THREE.MeshBasicMaterial ( { map: textureArray[r] } );   

 	ABWorld.scene.add(theobject);

 	ArmySphere[t] = [theta, phi, radius];
 	
 	var vec = cartesian(theta, phi, radius);
	theobject.position.x = vec.x;	            	// save it for later
	theobject.position.y = vec.y;	            	// save it for later
	theobject.position.z = vec.z;	            	// save it for later

	THEARMY[t]=theobject;
	t++; 
 }
 
  // can start the run loop
  
	ABWorld.render();		
  
	AB.removeLoading();
	
  	AB.runReady = true; 		
}


function moveArmy()		    // move all the objects 
{
 for ( var i = 0; i < THEARMY.length; i++ )
 { 
   
   
   if ( THEARMY[i] )		// in case initArmy() not called yet 
   { 
       
       
       //r= Math.sqrt( (THEARMY[i].position.x ** 2) + (THEARMY[i].position.z ** 2) + (THEARMY[i].position.y **2)) ;
       
       //theta = Math.atan(THEARMY[i].position.y/THEARMY[i].position.x);
      //theta = (theta + 0.002) % (2*Math.PI);
       
       //phi= Math.acos(THEARMY[i].position.z / r);
       
        ArmySphere[i][0] = ArmySphere[i][0] + angularSpeed[i][0];
        ArmySphere[i][1] = ArmySphere[i][1] + angularSpeed[i][1];
        
        var vec = cartesian(ArmySphere[i][0], ArmySphere[i][1], ArmySphere[i][2]);
        THEARMY[i].position.x = vec.x;	            	// save it for later
	    THEARMY[i].position.y = vec.y;	            	// save it for later
	    THEARMY[i].position.z = vec.z;	            	// save it for later
        ABWorld.scene.add( THEARMY[i] );
   }
 }
}

function cartesian(theta, phi, r) {
  	var x = r * Math.sin(phi) * Math.cos(theta);   	
  	var z = r * Math.sin(phi) * Math.sin(theta);    	
  	var y = r * Math.cos(phi) ; 
  	
  	return new THREE.Vector3(x,y,z);
}



AB.world.newRun = function() 
{
	AB.loadingScreen();

	AB.runReady = false;  

	ABWorld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 

	loadResources();		// aynch file loads		
							// calls initArmy() when it returns 
};



AB.world.nextStep = function()
{
 	moveArmy();
 	
 	sun.rotation.y += 0.005;
 	
 	ABWorld.scene.add(sun);
};