API | Uses canvas | Graphics libraries | AB framework | Worlds using this API | Starter Worlds |
Physics (AB) (r1) | Yes | Three.js, Physijs | Yes | 220 Worlds | Starter Worlds |
This API is for Three.js Worlds with physics using the Physijs library.
It comes with an Ancient Brain framework to make writing Worlds easier.
This API has the following features:
ABWorld; // instance of class ABWorldClass - basic functionality for this API ABHandler; // instance of class ABHandlerClass - mouse and touch handling for this API Physijs; // from Physijs library
ABWorld.camera; // Three.js camera: THREE.PerspectiveCamera ABWorld.renderer; // Three.js renderer: THREE.WebGLRenderer ABWorld.scene; // This is a Physijs scene: Physijs.Scene // This is NOT a normal Three.js scene: THREE.Scene
// start a 3D or 2D scene with camera on the canvas: ABWorld.init3d ( startRadius, maxRadius, color ); ABWorld.init2d ( startRadius, maxRadius, color ); // startRadius = start position of camera // maxRadius = camera frustum far plane // 2D Worlds are merely special cases of 3D Worlds. // In 2D Worlds, the camera is set to directly overhead. // The World should make objects of height about 1 pixel.
// these points are used in some camera modes - see below // they can change every step ABWorld.follow; // THREE.Vector3 ABWorld.lookat; // THREE.Vector3 // the camera modes // set these AFTER Initialisation ABWorld.cameraFixed(); // default mode // position: picks a plausible position // looks at: centre of the scene // can click to rotate and zoom ABWorld.cameraCustom ( position, lookat ); // set your own camera position and point to look at // can click to rotate and zoom ABWorld.cameraTrack(); // position: default // looks at: point defined by ABWorld.follow (can change every step) ABWorld.cameraMove(); // position: ABWorld.follow (can change every step) // looks at: ABWorld.lookat (can change every step) // draw camera control buttons (Fixed/Track/Move) ABWorld.drawCameraControls = true;
// Find size of loaded 3D object. // Size of object along each dimension: ABWorld.objectXsize ( theobject ); ABWorld.objectYsize ( theobject ); ABWorld.objectZsize ( theobject );
// "Ray casting" function for mouse/touch control of object. // Returns boolean - if click on that x,y screen position hits object in 3D World. ABWorld.hitsObject ( x, y, theobject ); // Return the 3D (x,y,z) point at which click/tap hits object // returns null if no hit ABWorld.hitsObjectPoint ( x, y, theobject );
To make your own scene, camera or renderer,
define them before calling the initialisation function.
Set the variables in ABWorld and then the initialisation function knows not to create them.
Sample usage:
ABWorld.renderer = new THREE.WebGLRenderer ( { antialias: true } ); ABWorld.init3d ( startRadius, maxRadius, color ); // will not create renderer, will use my one
ABWorld.renderer = new THREE.WebGLRenderer ( { preserveDrawingBuffer: false } );To solve this, you can make preserveDrawingBuffer always true, or make it true only on screenshot runs:
if ( AB.isScreenshotRun() ) ABWorld.renderer = new THREE.WebGLRenderer ( { preserveDrawingBuffer : true } ); else ABWorld.renderer = new THREE.WebGLRenderer ( { preserveDrawingBuffer : false } );
// multiply mouse scroll by some factor to increase/decrease how much camera zooms: ABHandler.SCROLLFACTOR = 1; // multiply touch pinch by some factor to increase/decrease how much camera zooms: ABHandler.PINCHFACTOR = 1; // for 2D worlds: // multiply mouse/touch drag by some factor to increase/decrease how much camera moves: ABHandler.DRAGFACTOR = 1; // maximum position of camera: ABHandler.MAXCAMERAPOS; // given some sensible default depending on World // does a "ground" exist at altitude zero (that camera should not zoom below): ABHandler.GROUNDZERO = false;
ABHandler.initCameraDrag ( x, y ); // start camera drag now, at screen position x,y ABHandler.cameraDrag ( x, y ); // drag went to new screen position x, y // move the camera accordingly ABHandler.cameraZoom ( delta ); // zoom the camera in or out by some amountTo define mouse and touch to move the camera, we assign these 6 functions as follows:
ABHandler.initMouseDrag = function ( x, y ) { ABHandler.initCameraDrag ( x, y ); }; ABHandler.mouseDrag = function ( x, y ) { ABHandler.cameraDrag ( x, y ); }; ABHandler.mouseZoom = function ( delta ) { ABHandler.cameraZoom ( delta ); }; ABHandler.initTouchDrag = function ( x, y ) { ABHandler.initCameraDrag ( x, y ); }; ABHandler.touchDrag = function ( x, y ) { ABHandler.cameraDrag ( x, y ); }; ABHandler.touchZoom = function ( delta ) { ABHandler.cameraZoom ( delta ); };
You can restore the default mouse/touch camera control at any time by doing:
ABHandler.defaultMouse(); // Restore default mouse camera control ABHandler.defaultTouch(); // Restore default touch camera control