app.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var APP = {
  5. Player: function () {
  6. var loader = new THREE.ObjectLoader();
  7. var camera, scene, renderer;
  8. var scripts = {
  9. update: []
  10. };
  11. this.dom = undefined;
  12. this.load = function ( json ) {
  13. renderer = new THREE.WebGLRenderer( { antialias: true } );
  14. renderer.setPixelRatio( window.devicePixelRatio );
  15. camera = loader.parse( json.camera );
  16. scene = loader.parse( json.scene );
  17. scripts.update = [];
  18. for ( var uuid in json.scripts ) {
  19. var source = json.scripts[ uuid ];
  20. var object = scene.getObjectByProperty( 'uuid', uuid, true );
  21. var script = ( new Function( 'scene', 'time', source ).bind( object ) )();
  22. if ( script.update !== undefined ) {
  23. scripts.update.push( script.update );
  24. }
  25. }
  26. this.dom = renderer.domElement;
  27. };
  28. this.setSize = function ( width, height ) {
  29. camera.aspect = width / height;
  30. camera.updateProjectionMatrix();
  31. renderer.setSize( width, height );
  32. };
  33. var request;
  34. var animate = function ( time ) {
  35. request = requestAnimationFrame( animate );
  36. for ( var i = 0, l = scripts.update.length; i < l; i ++ ) {
  37. scripts.update[ i ]( scene, time );
  38. }
  39. renderer.render( scene, camera );
  40. };
  41. this.play = function () {
  42. request = requestAnimationFrame( animate );
  43. };
  44. this.stop = function () {
  45. cancelAnimationFrame( request );
  46. };
  47. }
  48. };