JS viewer

Syntax-highlighted view of JS of World: 1. Shapes




/*  
Proposed port of "Shapes"
Adapted to the Ancient Brain "three" API
Awaiting permission to do this
*/

/*
 ! Excerpted from "3D Game Programming for Kids, Second Edition",
 ! published by The Pragmatic Bookshelf.
 ! Copyrights apply to this code. It may not be used to create training material,
 ! courses, books, articles, and the like. Contact us if you are in doubt.
 ! We make no guarantees that this code is fit for any purpose.
 ! Visit http://www.pragmaticprogrammer.com/titles/csjava2 for more book information.
*/

 
const startRadius       = 500;                 // distance from centre we start the camera at

const maxRadius         = startRadius * 10;     // maximum distance from camera we render things 

const skycolor          = 'LightBlue';          // sky color

const skycolorObject    = new THREE.Color ( skycolor.toLowerCase() );                              

// not using API camera control buttons (see API docs):

ABWorld.drawCameraControls = false;



	var clock = new THREE.Clock();
    var ball, box, tube, ground, donut;



AB.world.newRun = function()
{
 var shape, cover;
 
 // set up a 3D scene:
 ABWorld.init3d ( startRadius, maxRadius, skycolorObject ); 

 AB.msg ( " <p> Drag and zoom the camera (built-in). </p> " ); 


 var flat = {flatShading: true};
 
 var light = new THREE.AmbientLight('white', 0.8);
 ABWorld.scene.add( light );

   shape = new THREE.SphereGeometry(100, 20, 15);
   cover = new THREE.MeshNormalMaterial(flat);
   ball = new THREE.Mesh(shape, cover);
 ABWorld.scene.add(ball);
 ball.position.set(-250,250,-250);

   shape = new THREE.CubeGeometry(300, 100, 100);
   cover = new THREE.MeshNormalMaterial(flat);
   box = new THREE.Mesh(shape, cover);
 ABWorld.scene.add(box);
 box.rotation.set(0.5, 0.5, 0);
 box.position.set(250, 250, -250);

   shape = new THREE.CylinderGeometry(1, 100, 100, 4);
   cover = new THREE.MeshNormalMaterial(flat);
   tube = new THREE.Mesh(shape, cover);
 ABWorld.scene.add(tube);
 tube.rotation.set(0.5, 0, 0);
 tube.position.set(250, -250, -250);

   shape = new THREE.PlaneGeometry(100, 100);
   cover = new THREE.MeshNormalMaterial(flat);
   ground = new THREE.Mesh(shape, cover);
 ABWorld.scene.add(ground);
 ground.rotation.set(0.5, 0, 0);
 ground.position.set(-250, -250, -250);

   shape = new THREE.TorusGeometry(100, 25, 8, 25);
   cover = new THREE.MeshNormalMaterial(flat);
   donut = new THREE.Mesh(shape, cover);
 ABWorld.scene.add(donut);
};



AB.world.nextStep = function()		 
{
   var t = clock.getElapsedTime();

   ball.rotation.set(t, 2*t, 0);
   box.rotation.set(t, 2*t, 0);
   tube.rotation.set(t, 2*t, 0);
   ground.rotation.set(t, 2*t, 0);
   donut.rotation.set(t, 2*t, 0);
};