|   | 1 | {{{ | 
                  
                          |   | 2 | #!html | 
                  
                          |   | 3 | <script src="three.js"></script> | 
                  
                          |   | 4 | <script src="TrackballControls.js"></script> | 
                  
                          |   | 5 | <script src="STLLoader.js"></script> | 
                  
                          |   | 6 |  | 
                  
                          |   | 7 | <script type="text/javascript"> | 
                  
                          |   | 8 |   function onLoad(){ | 
                  
                          |   | 9 |       initScene(); | 
                  
                          |   | 10 |       function initScene() { | 
                  
                          |   | 11 |          | 
                  
                          |   | 12 |           // Grab our canvas | 
                  
                          |   | 13 |           var myCanvas = document.getElementById("myCanvas"); | 
                  
                          |   | 14 |           //Create a new renderer and set the size | 
                  
                          |   | 15 |           renderer = new THREE.WebGLRenderer( { antialias: true, | 
                  
                          |   | 16 |                                                 alpha: true, | 
                  
                          |   | 17 |                                                 canvas: myCanvas} ); | 
                  
                          |   | 18 |           renderer.setSize(myCanvas.offsetWidth, myCanvas.offsetHeight); | 
                  
                          |   | 19 |    | 
                  
                          |   | 20 |           //Create a new scene | 
                  
                          |   | 21 |           scene = new THREE.Scene(); | 
                  
                          |   | 22 |    | 
                  
                          |   | 23 |           //Create a perspective camera | 
                  
                          |   | 24 |           camera = new THREE.PerspectiveCamera( 75,  myCanvas.offsetWidth / myCanvas.offsetHeight, 1, 1000 ); | 
                  
                          |   | 25 |           camera.position.z = 20; | 
                  
                          |   | 26 |              | 
                  
                          |   | 27 |           scene.add( camera ); | 
                  
                          |   | 28 |    | 
                  
                          |   | 29 |           //Add controls for interactively moving the camera with mouse | 
                  
                          |   | 30 |           controls = new THREE.TrackballControls(camera, renderer.domElement); | 
                  
                          |   | 31 |              | 
                  
                          |   | 32 |           controls.rotateSpeed = 1.0; | 
                  
                          |   | 33 |           controls.zoomSpeed = 1.2; | 
                  
                          |   | 34 |           controls.panSpeed = 0.2; | 
                  
                          |   | 35 |    | 
                  
                          |   | 36 |           controls.noZoom = false; | 
                  
                          |   | 37 |           controls.noPan = false; | 
                  
                          |   | 38 |    | 
                  
                          |   | 39 |           controls.staticMoving = false; | 
                  
                          |   | 40 |           controls.dynamicDampingFactor = 0.3; | 
                  
                          |   | 41 |    | 
                  
                          |   | 42 |           controls.minDistance = 1.1; | 
                  
                          |   | 43 |           controls.maxDistance = 100; | 
                  
                          |   | 44 |    | 
                  
                          |   | 45 |           controls.keys = [ 65, 83, 68 ]; // [ rotateKey, zoomKey, panKey ] | 
                  
                          |   | 46 |      | 
                  
                          |   | 47 |           //Add some lights | 
                  
                          |   | 48 |           var dirLight = new THREE.DirectionalLight(0xffffff, 1); | 
                  
                          |   | 49 |           dirLight.position.set(-3, 3, 7); | 
                  
                          |   | 50 |           dirLight.position.normalize(); | 
                  
                          |   | 51 |           scene.add(dirLight); | 
                  
                          |   | 52 |    | 
                  
                          |   | 53 |           var pointLight = new THREE.PointLight(0xFFFFFF, 5, 50); | 
                  
                          |   | 54 |           pointLight.position.set(10, 20, -10); | 
                  
                          |   | 55 |           scene.add(pointLight); | 
                  
                          |   | 56 |              | 
                  
                          |   | 57 |           var material =  new THREE.MeshLambertMaterial( | 
                  
                          |   | 58 |                   {color:0xff0000, shading: THREE.FlatShading } ); | 
                  
                          |   | 59 |  | 
                  
                          |   | 60 |           var material2 =  new THREE.MeshLambertMaterial( | 
                  
                          |   | 61 |               {color:0x00ff00, shading: THREE.FlatShading } ); | 
                  
                          |   | 62 |  | 
                  
                          |   | 63 |           var material3 = new THREE.MeshPhongMaterial( | 
                  
                          |   | 64 |               { ambient: 0x555555, color: 0xAAAAAA, | 
                  
                          |   | 65 |                 specular: 0x111111, shininess: 200 } ); | 
                  
                          |   | 66 |  | 
                  
                          |   | 67 |           var loader = new THREE.STLLoader(); | 
                  
                          |   | 68 |           loader.addEventListener( 'load', function ( event ) { | 
                  
                          |   | 69 |               var geometry = event.content; | 
                  
                          |   | 70 |               var mesh = new THREE.Mesh( geometry, material ); | 
                  
                          |   | 71 |               mesh.position.set( 0, 0, 0.0 ); | 
                  
                          |   | 72 |               mesh.rotation.set( -Math.PI/6, -Math.PI/6, 0 ); | 
                  
                          |   | 73 |               mesh.scale.set( 50, 50, 50 ); | 
                  
                          |   | 74 |               mesh.castShadow = true; | 
                  
                          |   | 75 |               mesh.receiveShadow = true; | 
                  
                          |   | 76 |               scene.add( mesh ); | 
                  
                          |   | 77 |                | 
                  
                          |   | 78 |           } ); | 
                  
                          |   | 79 |           loader.load( 'pr2_head_pan.stl' ); | 
                  
                          |   | 80 |              | 
                  
                          |   | 81 |  | 
                  
                          |   | 82 |           var loader = new THREE.STLLoader(); | 
                  
                          |   | 83 |           loader.addEventListener( 'load', function ( event ) { | 
                  
                          |   | 84 |               var geometry = event.content; | 
                  
                          |   | 85 |               var mesh = new THREE.Mesh( geometry, material2 ); | 
                  
                          |   | 86 |               mesh.position.set( 0, 0, 0.0 ); | 
                  
                          |   | 87 |               mesh.rotation.set( -Math.PI/6, -Math.PI/6, 0 ); | 
                  
                          |   | 88 |               mesh.scale.set( 50, 50, 50 ); | 
                  
                          |   | 89 |               mesh.castShadow = true; | 
                  
                          |   | 90 |               mesh.receiveShadow = true; | 
                  
                          |   | 91 |               scene.add( mesh ); | 
                  
                          |   | 92 |                | 
                  
                          |   | 93 |           } ); | 
                  
                          |   | 94 |           loader.load( 'pr2_head_tilt.stl' ); | 
                  
                          |   | 95 |  | 
                  
                          |   | 96 |           //Call the animate function | 
                  
                          |   | 97 |           animate(); | 
                  
                          |   | 98 |       } | 
                  
                          |   | 99 |       function animate() { | 
                  
                          |   | 100 |           //will not render if the browser window is inactive | 
                  
                          |   | 101 |           requestAnimationFrame( animate ); | 
                  
                          |   | 102 |           render(); | 
                  
                          |   | 103 |       }              | 
                  
                          |   | 104 |        | 
                  
                          |   | 105 |       function render() { | 
                  
                          |   | 106 |           controls.update(); //for cameras | 
                  
                          |   | 107 |           renderer.render( scene, camera ); | 
                  
                          |   | 108 |       }     | 
                  
                          |   | 109 |   }                | 
                  
                          |   | 110 | </script>   | 
                  
                          |   | 111 | <canvas id="myCanvas" width="600" height="400" | 
                  
                          |   | 112 |                 style="background:lightgrey" ></canvas> | 
                  
                          |   | 113 | }}} |