Skip to content

Instantly share code, notes, and snippets.

@cubicleDowns
Last active April 6, 2022 03:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cubicleDowns/8906535 to your computer and use it in GitHub Desktop.
Save cubicleDowns/8906535 to your computer and use it in GitHub Desktop.
Example of a THREEjs factory and an exposed API.
"use strict";
tttApp.factory('ThreeEnv', function ($http, $log, $rootScope ) {
var demo,
rotateCamera = true,
canvas = document.getElementById('ttt-canvas');
/**
* Initialize the 3D scene.
* @param {Object} params {}
*/
function init(params) {
demo = new Demo.Scene(params);
demo.init();
demo.game.init(params);
animate();
}
function animate () {
requestAnimationFrame(animate);
render();
}
///////////////////////////////////////////////////////////////////
///
/// EXTERNAL API CALLS
/// Try and keep as much logic as possible within this call
/// Minimize additional functions in the scene classes.
///
///////////////////////////////////////////////////////////////////
/**
* Renders the THREE.js scene graph.
* @return {screen} Rendered frame buffer of scene.
*/
function render() {
demo.renderer.render(demo.scene, demo.cameras.liveCam);
if(rotateCamera){
demo.cameras.rotateCamera();
} else {
demo.controls.update();
}
}
/**
* Make a
* @param {[type]} mouse [description]
* @return {[type]} [description]
*/
function makeSelection(mouse) {
console.info('selection: ', mouse);
$.event.trigger({
type: "userClick",
message: mouse
});
}
/**
* Single toggle interface to interact with three.js environment.
* @param {string} toggleType What kind of toggle needs to be executed.
* @return {[type]} [description]
*/
function toggle(toggleType) {
switch(toggleType){
case "arrows":
Demo.Util.toggleArrows(demo.arrows);
break;
case "rotate":
rotateCamera = (rotateCamera) ? false : true;
break;
case "wireframes":
Demo.Util.toggleWireframes(demo.collisions);
break;
default:
$log.error('ThreeEnv: no toggle param set');
break;
}
}
//-------------------------------------------------------
// EVENT WATCHERS
//-------------------------------------------------------
var tttGame = {
init: init,
toggle: toggle,
makeSelection: makeSelection,
};
return tttGame;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment