Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Introduction to Computer Graphics</title>
- <script src="js/three.js"></script>
- <script src="js/THREEx.KeyboardState.js"></script>
- <script>
- /**
- * This sample demonstrates how to use Three.js to create a simple, interactive graphical scene.
- * Some of the code has been adapted from the Three.js documentation and from Lee Stemkoski's Three.js "tutorials by example".
- */
- function UpdatePosition(asteroid){
- asteroid.myMesh.position.x+=asteroid.velocity.x;
- asteroid.myMesh.position.y+=asteroid.velocity.y;
- asteroid.myMesh.position.z+=asteroid.velocity.z;
- }
- function BounceBox(asteroid){
- if(asteroid.myMesh.position.x>6 || asteroid.myMesh.position.x<-6){
- asteroid.velocity.x=asteroid.velocity.x*-1;
- }
- if(asteroid.myMesh.position.y>4 || asteroid.myMesh.position.y<-4){
- asteroid.velocity.y=asteroid.velocity.y*-1;
- }
- if(asteroid.myMesh.position.z>-16 || asteroid.myMesh.position.z<-40){
- asteroid.velocity.z=asteroid.velocity.z*-1;
- }
- }
- window.onload = function() {
- var renderer = new THREE.WebGLRenderer();
- renderer.setSize( window.innerWidth-10, window.innerHeight);
- document.body.appendChild( renderer.domElement );
- var scene = new THREE.Scene();
- var camera = new THREE.PerspectiveCamera(
- 35, // Field of view
- 800 / 600, // Aspect ratio
- 0.1, // Near plane
- 10000 // Far plane
- );
- camera.position.set( 0, 0, 0 );
- camera.lookAt( scene.position );
- var keyboard = new THREEx.KeyboardState();
- //Geometry of shapes-------------------------------------------------------------------------------------
- var IcosahedronGeometry = new THREE.TorusGeometry(2.5,0.1);
- var sphereGeometry2 = new THREE.IcosahedronGeometry(0.5,2);
- var cubeGeometry = new THREE.CubeGeometry(1,1,1)
- var aGeometry = new THREE.SphereGeometry(1,0);
- var aWingGeometry = new THREE.CylinderGeometry(1,2,1)
- var icosaMaterial = new THREE.MeshLambertMaterial('images/cockpit.gif' );
- var sphereMaterial2 = new THREE.MeshLambertMaterial({ color: 0x00FFFF });
- var crateTexture = new THREE.ImageUtils.loadTexture( 'images/cockpit.gif' );
- var sphereTexture = new THREE.MeshLambertMaterial( { color: 0xFF00FF });
- var cubeMaterial = new THREE.MeshLambertMaterial( { map:crateTexture } );
- var aMaterial = new THREE.MeshLambertMaterial('images/alien_body.gif');
- var aWingMaterial = new THREE.MeshLambertMaterial({color: 0xFF0000})
- //End of geometory of shapes----------------------------------------------------------------------------
- //body refures to the main space ship
- //Create cockpits
- var cubeGeometry = new THREE.CubeGeometry(1,1,1);
- var myObject = new THREE.Mesh( cubeGeometry,cubeMaterial );
- myObject.rotation.set(1,1,0);
- myObject.position.set(-0.7,1.59,-19);
- scene.add(myObject);
- //spacephip components
- var body = new Object();
- body.asteroid = new Object();
- body.asteroid.myMesh = new THREE.Mesh(IcosahedronGeometry,icosaMaterial );
- body.asteroid.myMesh.position.set(-0.7,1.59,-19);
- scene.add(body.asteroid.myMesh);
- //create blue balls
- var angOfRot = 0;
- body.power= new Array();
- var ship_radius = 2.2;
- var angle = 0;
- var pi = 3.1415926;
- for (i=0;i<6;i++){
- angle = i*pi/3;
- body.power[i]=new Object();
- body.power[i].myMesh = new THREE.Mesh(sphereGeometry2, sphereMaterial2);
- body.power[i].myMesh.position.set(-0.8+ship_radius*Math.cos(angle),1.6+ship_radius*Math.sin(angle),-19);
- scene.add(body.power[i].myMesh);
- }
- //adding alien components
- //alien components
- var aBody = new Object();
- var aWing = new Object();
- var aliens = new Array();
- for(z=0;z<10;z++);{
- aliens[z] = new Array();
- for(x=0;x<5;x++){
- //generate the alian's properties
- aliens[z][x] = new Object();
- aliens[z][x].aBody = new Object();
- aliens[z][x].aWing = new Object();
- //generate the alians body
- aliens[z][x].aBody.myMesh = new THREE.Mesh(aGeometry, aMaterial);
- aliens[z][x].aBody.myMesh.position.set(2*x-10,0,2*z-30);//-3,0,0
- scene.add(aliens[z][x].aBody.myMesh);
- //generate the alians wing
- aliens[z][x].aWing.myMesh = new THREE.Mesh(aWingGeometry, aWingMaterial);
- aliens[z][x].aWing.myMesh.position.set(2*x-10,-1,2*z-30);//-3,-1,0
- scene.add(aliens[z][x].aWing.myMesh);
- }
- }
- /* !!!! STEP 1:
- * Change the background colour to a medium shade of blue.
- */
- renderer.setClearColorHex( 0x000000, 1 );
- var planeGeometry = new THREE.PlaneGeometry(35,25,1,1);
- var planeTexture = new THREE.ImageUtils.loadTexture( 'images/milkyway.jpg' );
- var planeMaterial = new THREE.MeshLambertMaterial( { map:planeTexture } );
- /* !!!! STEP 2:
- * Create a sphere mesh and then add it to the scene.
- * The sphere's structure should be defined by the variable sphereGeometry
- * The sphere's appearance should be defined by the variable sphereMaterial
- */
- myPlane = new THREE.Mesh( planeGeometry,planeMaterial );
- myPlane.position.set(0,0,-40);
- scene.add(myPlane);
- //asteroid
- var asteroid = new Object();
- asteroid.velocity = new Object();
- asteroid.velocity.x =0.1;
- asteroid.velocity.y =0.1;
- asteroid.velocity.z =0.1;
- var sphereGeometry = new THREE.IcosahedronGeometry(1,-2);
- //var sphereGeometry = new THREE.PlaneGeometry(1,3,2,0);
- var sphereMaterial = new THREE.MeshLambertMaterial( { color: 0xFF0000 } );
- var crateTexture = new THREE.ImageUtils.loadTexture( 'images/crate.gif' );
- var cubeMaterial = new THREE.MeshLambertMaterial( { map:crateTexture } );
- /* !!!! STEP 2:
- * Create a sphere mesh and then add it to the scene.
- * The sphere's structure should be defined by the variable sphereGeometry
- * The sphere's appearance should be defined by the variable sphereMaterial
- */
- asteroid.myMesh = new THREE.Mesh( sphereGeometry,cubeMaterial );
- asteroid.myMesh.position.set(-1,1,-20);
- scene.add(asteroid.myMesh);
- /* !!!! STEP 3:
- * Position the sphere so that it is in the top left hand corner of the
- * screen and is slightly further away from the camera than it would be
- * by default.
- */
- /* !!!! STEP 4:
- * STEP 4.1: Add the light source to the scene.
- * STEP 4.2: Change the colour of the light so that the sphere appears purple.
- */
- var light = new THREE.PointLight( 0xFFFFFF );
- light.position.set( 10, 0, 10 );
- scene.add(light);
- /* !!!! STEP 5:
- * STEP 5.1: Create a 1 by 1 by 1 cube and add it to the scene.
- * STEP 5.2: Rotate the cube so that more than one of its faces are visible.
- * STEP 5.3: Use crateTexture as a surface for your cube
- */
- var cubeGeometry = new THREE.CubeGeometry(1,1,1);
- var myObject = new THREE.Mesh( cubeGeometry,cubeMaterial );
- myObject.rotation.set(0.7,0.5,0);
- myObject.position.set(-1,4,-20);
- scene.add(myObject);
- function update() {
- /* !!!! STEP 6:
- * Increment the rotation of your cube in the x and y dimensions so that
- * it rotates continually.
- */
- asteroid.myMesh.rotation.x+= 0.05;
- asteroid.myMesh.rotation.y+= 0.05;
- asteroid.myMesh.rotation.z+= 0.05;
- //move the asteroid about
- UpdatePosition(asteroid);
- BounceBox(asteroid);
- /* !!!! STEP 7:
- * Allow the user to change the position of the cube using the arrow keys
- * If the "up" key is pressed, the cube should move upwards.
- * If the "down" key is pressed, the cube should move downwards.
- * If the "left" key is pressed, the cube should move to the left.
- * If the "right" key is pressed, the cube should move to the right.
- */
- //MOVEMENT CONTROLLS
- if(keyboard.pressed("Y")){
- myObject.position.y+=0.1;
- }else if(keyboard.pressed("H")){
- myObject.position.y-=0.1;
- }else if(keyboard.pressed("G")){
- myObject.position.x-=0.1;
- }else if(keyboard.pressed("J")){
- myObject.position.x+=0.1;
- }else if(keyboard.pressed("T")){
- myObject.position.z-=0.1;
- }else if(keyboard.pressed("U")){
- myObject.position.z+=0.1;
- }
- //ROTATION CONTROLLS
- if(keyboard.pressed("W")){
- myObject.rotation.x+=0.05;
- }else if(keyboard.pressed("S")){
- myObject.rotation.x-=0.05;
- }else if(keyboard.pressed("A")){
- myObject.rotation.y-=0.05;
- }else if(keyboard.pressed("D")){
- myObject.rotation.y+=0.05;
- }else if(keyboard.pressed("Q")){
- myObject.rotation.z-=0.05;
- }else if(keyboard.pressed("E")){
- myObject.rotation.z+=0.05;
- }
- }
- //Render Loop
- function render() {
- //Call the update function
- update();
- //Re-draw the scene
- renderer.render(scene, camera);
- //Re-call the render function when the next frame is ready to be drawn
- requestAnimationFrame(render);
- }
- requestAnimationFrame(render);
- };
- </script>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment