2
0
Эх сурвалжийг харах

switch to glTF model and update

Lewy Blue 7 жил өмнө
parent
commit
41b2c0fc2d

+ 178 - 279
examples/js/Car.js

@@ -1,407 +1,306 @@
 /**
  * @author alteredq / http://alteredqualia.com/
+ * @author Lewy Blue https://github.com/looeee
+ *
+ * The model is expected to follow real world car proportions. You can try unusual car types
+ * but your results may be unexpected. Scaled models are also not supported
+ *
+ * Defaults below are for a Ferrari F50, taken from https://en.wikipedia.org/wiki/Ferrari_F50
+ *
  */
 
-THREE.Car = function () {
+THREE.Car = ( function ( ) {
 
-	var scope = this;
+	// private variables
+	var steeringWheelSpeed = 1.5;
+	var maxSteeringRotation = 0.6;
 
-	// car geometry manual parameters
+	var acceleration = 0;
 
-	this.modelScale = 1;
+	var maxSpeedReverse, accelerationReverse, deceleration;
 
-	this.backWheelOffset = 2;
+	var controlKeys = { LEFT: 37, UP: 38, RIGHT: 39, DOWN: 40, BRAKE: 32 };
 
-	this.autoWheelGeometry = true;
+	var wheelOrientation = 0;
+	var carOrientation = 0;
 
-	// car geometry parameters automatically set from wheel mesh
-	// 	- assumes wheel mesh is front left wheel in proper global
-	//    position with respect to body mesh
-	//	- other wheels are mirrored against car root
-	//	- if necessary back wheels can be offset manually
+	var root = null;
 
-	this.wheelOffset = new THREE.Vector3();
+	var frontLeftWheelRoot = null;
+	var frontRightWheelRoot = null;
 
-	this.wheelDiameter = 1;
+	var frontLeftWheel = new THREE.Group();
+	var frontRightWheel = new THREE.Group();
+	var backLeftWheel = null;
+	var backRightWheel = null;
 
-	// car "feel" parameters
+	var steeringWheel = null;
 
-	this.MAX_SPEED = 2200;
-	this.MAX_REVERSE_SPEED = - 1500;
+	var wheelDiameter = 1;
+	var length = 1;
 
-	this.MAX_WHEEL_ROTATION = 0.6;
+	var loaded = false;
 
-	this.FRONT_ACCELERATION = 1250;
-	this.BACK_ACCELERATION = 1500;
+	var controls = {
 
-	this.WHEEL_ANGULAR_ACCELERATION = 1.5;
-
-	this.FRONT_DECCELERATION = 750;
-	this.WHEEL_ANGULAR_DECCELERATION = 1.0;
-
-	this.STEERING_RADIUS_RATIO = 0.0023;
-
-	this.MAX_TILT_SIDES = 0.05;
-	this.MAX_TILT_FRONTBACK = 0.015;
-
-	// internal control variables
-
-	this.speed = 0;
-	this.acceleration = 0;
-
-	this.wheelOrientation = 0;
-	this.carOrientation = 0;
-
-	// car rigging
-
-	this.root = new THREE.Object3D();
-
-	this.frontLeftWheelRoot = new THREE.Object3D();
-	this.frontRightWheelRoot = new THREE.Object3D();
-
-	this.bodyMesh = null;
-
-	this.frontLeftWheelMesh = null;
-	this.frontRightWheelMesh = null;
-
-	this.backLeftWheelMesh = null;
-	this.backRightWheelMesh = null;
-
-	this.bodyGeometry = null;
-	this.wheelGeometry = null;
-
-	this.bodyMaterials = null;
-	this.wheelMaterials = null;
-
-	// internal helper variables
-
-	this.loaded = false;
-
-	this.meshes = [];
-
-	// API
-
-	this.enableShadows = function ( enable ) {
-
-		for ( var i = 0; i < this.meshes.length; i ++ ) {
-
-			this.meshes[ i ].castShadow = enable;
-			this.meshes[ i ].receiveShadow = enable;
-
-		}
-
-	};
-
-	this.setVisible = function ( enable ) {
-
-		for ( var i = 0; i < this.meshes.length; i ++ ) {
-
-			this.meshes[ i ].visible = enable;
-			this.meshes[ i ].visible = enable;
-
-		}
-
-	};
-
-	this.loadPartsJSON = function ( bodyURL, wheelURL ) {
-
-		var loader = new THREE.JSONLoader();
-
-		loader.load( bodyURL, function( geometry, materials ) {
-
-			createBody( geometry, materials )
-
-		} );
-		loader.load( wheelURL, function( geometry, materials ) {
-
-			createWheels( geometry, materials )
-
-		} );
-
-	};
-
-	this.loadPartsBinary = function ( bodyURL, wheelURL ) {
-
-		var loader = new THREE.BinaryLoader();
-
-		loader.load( bodyURL, function( geometry, materials ) {
-
-			createBody( geometry, materials )
-
-		} );
-		loader.load( wheelURL, function( geometry, materials ) {
-
-			createWheels( geometry, materials )
-
-		} );
+		brake: false,
+		moveForward: false,
+		moveBackward: false,
+		moveLeft: false,
+		moveRight: false
 
 	};
 
-	this.updateCarModel = function ( delta, controls ) {
-
-		// speed and wheels based on controls
+	function Car( maxSpeed, acceleration, brakePower, turningRadius, keys ) {
 
-		if ( controls.moveForward ) {
+		this.enabled = true;
 
-			this.speed = THREE.Math.clamp( this.speed + delta * this.FRONT_ACCELERATION, this.MAX_REVERSE_SPEED, this.MAX_SPEED );
-			this.acceleration = THREE.Math.clamp( this.acceleration + delta, - 1, 1 );
+		this.elemNames = {
+			flWheel: 'wheelFrontLeft',
+			frWheel: 'wheelFrontRight',
+			rlWheel: 'wheelBackLeft',
+			rrWheel: 'wheelBackRight',
+			steeringWheel: null, // disabled by default
+		};
 
-		}
+		// km/hr
+		this.maxSpeed = maxSpeed || 180;
+		maxSpeedReverse = - this.maxSpeed * 0.25;
 
-		if ( controls.moveBackward ) {
+		// m/s
+		this.acceleration = acceleration || 12;
+		accelerationReverse = this.acceleration * 0.5;
 
+		// metres
+		this.turningRadius = turningRadius || 24;
 
-			this.speed = THREE.Math.clamp( this.speed - delta * this.BACK_ACCELERATION, this.MAX_REVERSE_SPEED, this.MAX_SPEED );
-			this.acceleration = THREE.Math.clamp( this.acceleration - delta, - 1, 1 );
+		// m/s
+		deceleration = this.acceleration * 2;
 
-		}
+		// multiplied with deceleration, so breaking deceleration = ( acceleration * 2 * brakePower ) m/s
+		this.brakePower = brakePower || 10;
 
-		if ( controls.moveLeft ) {
+		// exposed so that a user can use this for various effect, e.g blur
+		// km / hr
+		this.speed = 0;
 
-			this.wheelOrientation = THREE.Math.clamp( this.wheelOrientation + delta * this.WHEEL_ANGULAR_ACCELERATION, - this.MAX_WHEEL_ROTATION, this.MAX_WHEEL_ROTATION );
+		// keys used to control car - by default the arrow keys and space to brake
+		controlKeys = keys || controlKeys;
 
-		}
+		// local axes of rotation
+		this.wheelRotationAxis = 'x';
+		this.wheelTurnAxis = 'z';
+		this.steeringWheelTurnAxis = 'z';
 
-		if ( controls.moveRight ) {
+		document.addEventListener( 'keydown', this.onKeyDown, false );
+		document.addEventListener( 'keyup', this.onKeyUp, false );
 
-			this.wheelOrientation = THREE.Math.clamp( this.wheelOrientation - delta * this.WHEEL_ANGULAR_ACCELERATION, - this.MAX_WHEEL_ROTATION, this.MAX_WHEEL_ROTATION );
+	}
 
-		}
+	Car.prototype = {
 
-		// speed decay
+		constructor: Car,
 
-		if ( ! ( controls.moveForward || controls.moveBackward ) ) {
+		onKeyDown: function ( event ) {
 
-			if ( this.speed > 0 ) {
+			switch ( event.keyCode ) {
 
-				var k = exponentialEaseOut( this.speed / this.MAX_SPEED );
+				case controlKeys.BRAKE:
+					controls.brake = true;
+					controls.moveForward = false;
+					controls.moveBackward = false;
+					break;
 
-				this.speed = THREE.Math.clamp( this.speed - k * delta * this.FRONT_DECCELERATION, 0, this.MAX_SPEED );
-				this.acceleration = THREE.Math.clamp( this.acceleration - k * delta, 0, 1 );
+				case controlKeys.UP: controls.moveForward = true; break;
 
-			} else {
+				case controlKeys.DOWN: controls.moveBackward = true; break;
 
-				var k = exponentialEaseOut( this.speed / this.MAX_REVERSE_SPEED );
+				case controlKeys.LEFT: controls.moveLeft = true; break;
 
-				this.speed = THREE.Math.clamp( this.speed + k * delta * this.BACK_ACCELERATION, this.MAX_REVERSE_SPEED, 0 );
-				this.acceleration = THREE.Math.clamp( this.acceleration + k * delta, - 1, 0 );
+				case controlKeys.RIGHT: controls.moveRight = true; break;
 
 			}
 
+		},
 
-		}
+		onKeyUp: function ( event ) {
 
-		// steering decay
+			switch ( event.keyCode ) {
 
-		if ( ! ( controls.moveLeft || controls.moveRight ) ) {
+				case controlKeys.BRAKE: controls.brake = false; break;
 
-			if ( this.wheelOrientation > 0 ) {
+				case controlKeys.UP: controls.moveForward = false; break;
 
-				this.wheelOrientation = THREE.Math.clamp( this.wheelOrientation - delta * this.WHEEL_ANGULAR_DECCELERATION, 0, this.MAX_WHEEL_ROTATION );
+				case controlKeys.DOWN: controls.moveBackward = false; break;
 
-			} else {
+				case controlKeys.LEFT: controls.moveLeft = false; break;
 
-				this.wheelOrientation = THREE.Math.clamp( this.wheelOrientation + delta * this.WHEEL_ANGULAR_DECCELERATION, - this.MAX_WHEEL_ROTATION, 0 );
+				case controlKeys.RIGHT: controls.moveRight = false; break;
 
 			}
 
-		}
-
-		// car update
+		},
 
-		var forwardDelta = this.speed * delta;
+		dispose: function () {
 
-		this.carOrientation += ( forwardDelta * this.STEERING_RADIUS_RATIO ) * this.wheelOrientation;
+			document.removeEventListener( 'keydown', this.onKeyDown, false );
+			document.removeEventListener( 'keyup', this.onKeyUp, false );
 
-		// displacement
+		},
 
-		this.root.position.x += Math.sin( this.carOrientation ) * forwardDelta;
-		this.root.position.z += Math.cos( this.carOrientation ) * forwardDelta;
+		update: function ( delta ) {
 
-		// steering
+			if ( ! loaded || ! this.enabled ) return;
 
-		this.root.rotation.y = this.carOrientation;
+			var brakingDeceleration = 1;
 
-		// tilt
+			if ( controls.brake ) brakingDeceleration = this.brakePower;
 
-		if ( this.loaded ) {
+			if ( controls.moveForward ) {
 
-			this.bodyMesh.rotation.z = this.MAX_TILT_SIDES * this.wheelOrientation * ( this.speed / this.MAX_SPEED );
-			this.bodyMesh.rotation.x = - this.MAX_TILT_FRONTBACK * this.acceleration;
+				this.speed = THREE.Math.clamp( this.speed + delta * this.acceleration, maxSpeedReverse, this.maxSpeed );
+				acceleration = THREE.Math.clamp( acceleration + delta, - 1, 1 );
 
-		}
-
-		// wheels rolling
-
-		var angularSpeedRatio = 1 / ( this.modelScale * ( this.wheelDiameter / 2 ) );
-
-		var wheelDelta = forwardDelta * angularSpeedRatio;
+			}
 
-		if ( this.loaded ) {
+			if ( controls.moveBackward ) {
 
-			this.frontLeftWheelMesh.rotation.x += wheelDelta;
-			this.frontRightWheelMesh.rotation.x += wheelDelta;
-			this.backLeftWheelMesh.rotation.x += wheelDelta;
-			this.backRightWheelMesh.rotation.x += wheelDelta;
+				this.speed = THREE.Math.clamp( this.speed - delta * accelerationReverse, maxSpeedReverse, this.maxSpeed );
+				acceleration = THREE.Math.clamp( acceleration - delta, - 1, 1 );
 
-		}
+			}
 
-		// front wheels steering
+			if ( controls.moveLeft ) {
 
-		this.frontLeftWheelRoot.rotation.y = this.wheelOrientation;
-		this.frontRightWheelRoot.rotation.y = this.wheelOrientation;
+				wheelOrientation = THREE.Math.clamp( wheelOrientation + delta * steeringWheelSpeed, - maxSteeringRotation, maxSteeringRotation );
 
-	};
+			}
 
-	// internal helper methods
+			if ( controls.moveRight ) {
 
-	function createBody ( geometry, materials ) {
+				wheelOrientation = THREE.Math.clamp( wheelOrientation - delta * steeringWheelSpeed, - maxSteeringRotation, maxSteeringRotation );
 
-		scope.bodyGeometry = geometry;
-		scope.bodyMaterials = materials;
+			}
 
-		createCar();
+			// this.speed decay
+			if ( ! ( controls.moveForward || controls.moveBackward ) ) {
 
-	}
+				if ( this.speed > 0 ) {
 
-	function createWheels ( geometry, materials ) {
+					var k = exponentialEaseOut( this.speed / this.maxSpeed );
 
-		scope.wheelGeometry = geometry;
-		scope.wheelMaterials = materials;
+					this.speed = THREE.Math.clamp( this.speed - k * delta * deceleration * brakingDeceleration, 0, this.maxSpeed );
+					acceleration = THREE.Math.clamp( acceleration - k * delta, 0, 1 );
 
-		createCar();
+				} else {
 
-	}
+					var k = exponentialEaseOut( this.speed / maxSpeedReverse );
 
-	function createCar () {
+					this.speed = THREE.Math.clamp( this.speed + k * delta * accelerationReverse * brakingDeceleration, maxSpeedReverse, 0 );
+					acceleration = THREE.Math.clamp( acceleration + k * delta, - 1, 0 );
 
-		if ( scope.bodyGeometry && scope.wheelGeometry ) {
+				}
 
-			// compute wheel geometry parameters
+			}
 
-			if ( scope.autoWheelGeometry ) {
+			// steering decay
+			if ( ! ( controls.moveLeft || controls.moveRight ) ) {
 
-				scope.wheelGeometry.computeBoundingBox();
+				if ( wheelOrientation > 0 ) {
 
-				var bb = scope.wheelGeometry.boundingBox;
+					wheelOrientation = THREE.Math.clamp( wheelOrientation - delta * steeringWheelSpeed, 0, maxSteeringRotation );
 
-				scope.wheelOffset.addVectors( bb.min, bb.max );
-				scope.wheelOffset.multiplyScalar( 0.5 );
+				} else {
 
-				scope.wheelDiameter = bb.max.y - bb.min.y;
+					wheelOrientation = THREE.Math.clamp( wheelOrientation + delta * steeringWheelSpeed, - maxSteeringRotation, 0 );
 
-				scope.wheelGeometry.center();
+				}
 
 			}
 
-			// rig the car
-
-			var s = scope.modelScale,
-				delta = new THREE.Vector3();
-
-			var bodyFaceMaterial = scope.bodyMaterials;
-			var wheelFaceMaterial = scope.wheelMaterials;
-
-			// body
-
-			scope.bodyMesh = new THREE.Mesh( scope.bodyGeometry, bodyFaceMaterial );
-			scope.bodyMesh.scale.set( s, s, s );
-
-			scope.root.add( scope.bodyMesh );
+			var forwardDelta = - this.speed * delta;
 
-			// front left wheel
+			carOrientation -= ( forwardDelta * this.turningRadius * 0.02 ) * wheelOrientation;
 
-			delta.multiplyVectors( scope.wheelOffset, new THREE.Vector3( s, s, s ) );
+			// movement of car
+			root.position.x += Math.sin( carOrientation ) * forwardDelta * length;
+			root.position.z += Math.cos( carOrientation ) * forwardDelta * length;
 
-			scope.frontLeftWheelRoot.position.add( delta );
+			// angle of car
+			root.rotation.y = carOrientation;
 
-			scope.frontLeftWheelMesh = new THREE.Mesh( scope.wheelGeometry, wheelFaceMaterial );
-			scope.frontLeftWheelMesh.scale.set( s, s, s );
+			// wheels rolling
+			var angularSpeedRatio = - 2 / wheelDiameter;
 
-			scope.frontLeftWheelRoot.add( scope.frontLeftWheelMesh );
-			scope.root.add( scope.frontLeftWheelRoot );
+			var wheelDelta = forwardDelta * angularSpeedRatio * length;
 
-			// front right wheel
+			frontLeftWheel.rotation[ this.wheelRotationAxis ] -= wheelDelta;
+			frontRightWheel.rotation[ this.wheelRotationAxis ] -= wheelDelta;
+			backLeftWheel.rotation[ this.wheelRotationAxis ] -= wheelDelta;
+			backRightWheel.rotation[ this.wheelRotationAxis ] -= wheelDelta;
 
-			delta.multiplyVectors( scope.wheelOffset, new THREE.Vector3( - s, s, s ) );
+			// rotation while steering
+			frontLeftWheelRoot.rotation[ this.wheelTurnAxis ] = wheelOrientation;
+			frontRightWheelRoot.rotation[ this.wheelTurnAxis ] = wheelOrientation;
 
-			scope.frontRightWheelRoot.position.add( delta );
+			steeringWheel.rotation[ this.steeringWheelTurnAxis ] = -wheelOrientation * 6;
 
-			scope.frontRightWheelMesh = new THREE.Mesh( scope.wheelGeometry, wheelFaceMaterial );
+		},
 
-			scope.frontRightWheelMesh.scale.set( s, s, s );
-			scope.frontRightWheelMesh.rotation.z = Math.PI;
+		setModel: function ( model, elemNames ) {
 
-			scope.frontRightWheelRoot.add( scope.frontRightWheelMesh );
-			scope.root.add( scope.frontRightWheelRoot );
+			if ( elemNames ) this.elemNames = elemNames;
 
-			// back left wheel
+			root = model;
 
-			delta.multiplyVectors( scope.wheelOffset, new THREE.Vector3( s, s, - s ) );
-			delta.z -= scope.backWheelOffset;
+			this.setupWheels();
+			this.computeDimensions();
 
-			scope.backLeftWheelMesh = new THREE.Mesh( scope.wheelGeometry, wheelFaceMaterial );
+			loaded = true;
 
-			scope.backLeftWheelMesh.position.add( delta );
-			scope.backLeftWheelMesh.scale.set( s, s, s );
+		},
 
-			scope.root.add( scope.backLeftWheelMesh );
+		setupWheels: function () {
 
-			// back right wheel
+			frontLeftWheelRoot = root.getObjectByName( this.elemNames.flWheel );
+			frontRightWheelRoot = root.getObjectByName( this.elemNames.frWheel );
+			backLeftWheel = root.getObjectByName( this.elemNames.rlWheel );
+			backRightWheel = root.getObjectByName( this.elemNames.rrWheel );
 
-			delta.multiplyVectors( scope.wheelOffset, new THREE.Vector3( - s, s, - s ) );
-			delta.z -= scope.backWheelOffset;
+			if ( this.elemNames.steeringWheel !== null ) steeringWheel = root.getObjectByName( this.elemNames.steeringWheel );
 
-			scope.backRightWheelMesh = new THREE.Mesh( scope.wheelGeometry, wheelFaceMaterial );
+			while ( frontLeftWheelRoot.children.length > 0 ) frontLeftWheel.add( frontLeftWheelRoot.children[ 0 ] );
+			while ( frontRightWheelRoot.children.length > 0 ) frontRightWheel.add( frontRightWheelRoot.children[ 0 ] );
 
-			scope.backRightWheelMesh.position.add( delta );
-			scope.backRightWheelMesh.scale.set( s, s, s );
-			scope.backRightWheelMesh.rotation.z = Math.PI;
+			frontLeftWheelRoot.add( frontLeftWheel );
+			frontRightWheelRoot.add( frontRightWheel );
 
-			scope.root.add( scope.backRightWheelMesh );
+		},
 
-			// cache meshes
+		computeDimensions: function () {
 
-			scope.meshes = [ scope.bodyMesh, scope.frontLeftWheelMesh, scope.frontRightWheelMesh, scope.backLeftWheelMesh, scope.backRightWheelMesh ];
+			var bb = new THREE.Box3().setFromObject( frontLeftWheelRoot );
 
-			// callback
+			var size = new THREE.Vector3();
+			bb.getSize( size );
 
-			scope.loaded = true;
+			wheelDiameter = Math.max( size.x, size.y, size.z );
 
-			if ( scope.callback ) {
+			bb.setFromObject( root );
 
-				scope.callback( scope );
-
-			}
+			size = bb.getSize( size );
+			length = Math.max( size.x, size.y, size.z );
 
 		}
 
-	}
-
-	function quadraticEaseOut( k ) {
-
-		return - k * ( k - 2 );
-
-	}
-	function cubicEaseOut( k ) {
-
-		return -- k * k * k + 1;
-
-	}
-	function circularEaseOut( k ) {
-
-		return Math.sqrt( 1 - -- k * k );
-
-	}
-	function sinusoidalEaseOut( k ) {
-
-		return Math.sin( k * Math.PI / 2 );
+	};
 
-	}
 	function exponentialEaseOut( k ) {
 
 		return k === 1 ? 1 : - Math.pow( 2, - 10 * k ) + 1;
 
 	}
 
-};
+	return Car;
+
+} )();

+ 242 - 454
examples/webgl_materials_cars.html

@@ -6,58 +6,53 @@
 		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
 		<style>
 			body {
-				background:#000;
-				color:#fff;
-				padding:0;
-				margin:0;
-				overflow:hidden;
-				font-family:georgia;
-				text-align:center;
+				font-family: Monospace;
+				background-color: #000;
+				color: #fff;
+				margin: 0px;
+				overflow: hidden;
+			}
+			#info {
+				color: #000;
+				position: absolute;
+				top: 10px;
+				width: 100%;
+				text-align: center;
+				z-index: 100;
+				display:block;
+			}
+			#info a {
+				color: red;
+				font-weight: bold;
+			}
+			span {
+				white-space: nowrap;
 			}
-			h1 { }
-			a { color:skyblue; text-decoration:none }
-			canvas { pointer-events:none; z-index:10; position:relative; }
-
-			#d { position:absolute; width: 100%; text-align:center; margin:1em 0 -4.5em 0; z-index:1000; }
-
-			.bwrap { margin:0.5em 0 0 0 }
-			button { font-family:georgia; border:0; background:#000; color:#fff; padding:0.2em 0.5em; cursor:pointer; border-radius:3px; }
-			button:hover { background:#333 }
-			#buttons_cars button { color:#fa0 }
-
-			#car_info { text-align:center; }
-			#car_name { font-size:1em }
-			#car_author { font-size:1em }
-
-			#oldie { background:rgb(50,0,0) !important; color:#fff !important; margin-top:7em!important }
 		</style>
 	</head>
 
 	<body>
-		<div id="d">
-			<div id="info">
-				<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl demo :
-				texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank" rel="noopener">Humus</a> :
-				<span id="car_info">
-					<span id="car_name">Bugatti Veyron model</span>
-					by <span id="car_author"><a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1129" target="_blank" rel="noopener">Troyano</a></span>
-				</span>
-
-			</div>
-
-			<div id="buttons_cars" class="bwrap">
-				<button id="veyron">Bugatti Veyron</button>
-				<button id="gallardo">Lamborghini Gallardo</button>
-				<button id="f50">Ferrari F50</button>
-				<button id="camaro">Chevrolet Camaro</button>
-			</div>
-
-			<div id="buttons_materials" class="bwrap"></div>
+		<div id="info">
+			<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> car materials demo :
+			Ferrari 458 Italia model by <a href="https://sketchfab.com/models/57bf6cc56931426e87494f554df1dab6" target="_blank" rel="noopener">vicent091036</a>
+			<br><br>
+			<strong>MATERIALS:</strong>
+			<span>Body: <select id="body-mat"></select></span>
+			<span>Rims / Trim: <select id="rim-mat"></select></span>
+			<span>Glass: <select id="glass-mat"></select></span><br>
+			<span><strong>Driving Mode</strong>(arrow keys and space)<input type="checkbox" id="drive-toggle"></span>
 		</div>
 
+		<div id="container"></div>
+
 		<script src="../build/three.js"></script>
 
-		<script src="js/loaders/BinaryLoader.js"></script>
+		<script src="js/loaders/DRACOLoader.js"></script>
+		<script src="js/loaders/GLTFLoader.js"></script>
+
+		<script src="js/Car.js"></script>
+
+		<script src="js/controls/OrbitControls.js"></script>
 
 		<script src="js/Detector.js"></script>
 		<script src="js/libs/stats.min.js"></script>
@@ -66,388 +61,254 @@
 
 			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
 
-			var STATS_ENABLED = false;
-
-			var CARS = {
-
-				"veyron": 	{
-
-					name:	"Bugatti Veyron",
-					url: 	"obj/veyron/VeyronNoUv_bin.js",
-					author: '<a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1129" target="_blank" rel="noopener">Troyano</a>',
-					init_rotation: [ 0, 0, 0 ],
-					scale: 5.5,
-					init_material: 4,
-					body_materials: [ 2 ],
-
-					object: null,
-					buttons: null,
-					materials: null
+			var camera, scene, renderer, controls, stats;
 
-				},
+			var bodyMatSelect = document.getElementById( 'body-mat' );
+			var rimMatSelect = document.getElementById( 'rim-mat' );
+			var glassMatSelect = document.getElementById( 'glass-mat' );
 
-				"gallardo": {
+			var driveModeToggle = document.getElementById( 'drive-toggle' );
+			driveModeToggle.addEventListener( 'change', onDriveModeToggle );
 
-					name: 	"Lamborghini Gallardo",
-					url:	"obj/gallardo/GallardoNoUv_bin.js",
-					author: '<a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1711" target="_blank" rel="noopener">machman_3d</a>',
-					init_rotation: [ 0, 0, 0 ],
-					scale: 3.7,
-					init_material: 9,
-					body_materials: [ 3 ],
+			var lightHolder = new THREE.Group();
 
-					object:	null,
-					buttons: null,
-					materials: null
-
-				},
-
-				"f50": {
-
-					name: 	"Ferrari F50",
-					url:	"obj/f50/F50NoUv_bin.js",
-					author: '<a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1687" target="_blank" rel="noopener">daniel sathya</a>',
-					init_rotation: [ 0, 0, 0 ],
-					scale: 0.175,
-					init_material: 2,
-					body_materials: [ 3, 6, 7, 8, 9, 10, 23, 24 ],
-
-					object:	null,
-					buttons: null,
-					materials: null
-
-				},
-
-				"camaro": {
-
-					name: 	"Chevrolet Camaro",
-					url:	"obj/camaro/CamaroNoUv_bin.js",
-					author: '<a href="http://www.turbosquid.com/3d-models/blender-camaro/411348" target="_blank" rel="noopener">dskfnwn</a>',
-					init_rotation: [ 0.0, 0.0, 0.0 /*0, 1, 0*/ ],
-					scale: 75,
-					init_material: 0,
-					body_materials: [ 0 ],
-
-					object:	null,
-					buttons: null,
-					materials: null
-
-				}
+			var car = new THREE.Car();
+			car.enabled = false;
+			var clock = new THREE.Clock();
 
+			var carParts = {
+				body: [],
+				rims:[],
+				glass: [],
 			};
 
+			var carModel, materialsLib;
 
-			var container, stats;
-
-			var camera, scene, renderer;
-
-			var m, mi;
-
-			var directionalLight, pointLight;
-
-			var mouseX = 0, mouseY = 0;
-
-			var windowHalfX = window.innerWidth / 2;
-			var windowHalfY = window.innerHeight / 2;
-
-			var loader = new THREE.BinaryLoader();
-
-			init();
-			animate();
+			var drivingMode = false;
+			var damping = 5.0;
+			var distance = -5;
+			var cameraTarget = new THREE.Vector3();
+			var origin = new THREE.Vector3();
 
 			function init() {
 
-				container = document.createElement( 'div' );
-				document.body.appendChild( container );
+				var container = document.getElementById( 'container' );
 
-				// CAMERAS
+				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 200 );
+				camera.position.set( 5, 2.5, -5 );
 
-				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 100000 );
-
-				// SCENE
-
-				var textureCube = new THREE.CubeTextureLoader()
-					.setPath( 'textures/cube/Bridge2/')
-					.load( [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ] );
+				controls = new THREE.OrbitControls( camera, container );
+				controls.enableDamping = true;
+				controls.dampingFactor = 0.25;
 
 				scene = new THREE.Scene();
-				scene.background = textureCube;
-
-				// LIGHTS
-
-				var ambient = new THREE.AmbientLight( 0x050505 );
-				scene.add( ambient );
-
-				directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
-				directionalLight.position.set( 2, 1.2, 10 ).normalize();
-				scene.add( directionalLight );
-
-				directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
-				directionalLight.position.set( -2, 1.2, -10 ).normalize();
-				scene.add( directionalLight );
-
-				pointLight = new THREE.PointLight( 0xffaa00, 2 );
-				pointLight.position.set( 2000, 1200, 10000 );
-				scene.add( pointLight );
-
-				//
-
-				renderer = new THREE.WebGLRenderer();
+				scene.fog = new THREE.Fog( 0xa0a0a0, 10, 60 );
+
+				var ground = new THREE.Mesh( new THREE.PlaneBufferGeometry( 600, 600 ), new THREE.MeshStandardMaterial( { color: 0xffffff, metalness: 0, roughness: 1 } ) );
+				ground.rotation.x = - Math.PI / 2;
+				ground.receiveShadow = true;
+				scene.add( ground );
+
+				var grid = new THREE.GridHelper( 600, 60, 0x000000, 0x000000 );
+				grid.material.opacity = 0.2;
+				grid.material.transparent = true;
+				scene.add( grid );
+
+				var hemiLight = new THREE.HemisphereLight( 0xfefeb4, 0x99ccff, 0.3 );
+				hemiLight.position.set( -3, 3, 3 );
+				scene.add( hemiLight );
+
+				shadowLight  = new THREE.DirectionalLight( 0xffffff, 0.3 );
+				shadowLight.position.set( -1.5, 1.5, 1.5 );
+				shadowLight.castShadow = true;
+				shadowLight.shadow.width = 1024;
+				shadowLight.shadow.height = 1024;
+				shadowLight.shadow.camera.top = 2;
+				shadowLight.shadow.camera.bottom = -2;
+				shadowLight.shadow.camera.left = -2.5;
+				shadowLight.shadow.camera.right = 2.5;
+				shadowLight.shadow.camera.far = 5.5;
+				shadowLight.shadow.bias = -0.025;
+
+				lightHolder.add( shadowLight, shadowLight.target );
+
+				renderer = new THREE.WebGLRenderer( { antialias: true } );
 				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.gammaOutput = true;
+				renderer.shadowMap.enabled = true;
 				renderer.setSize( window.innerWidth, window.innerHeight );
 
 				container.appendChild( renderer.domElement );
 
-				if ( STATS_ENABLED ) {
+				stats = new Stats();
+				container.appendChild( stats.dom );
 
-					stats = new Stats();
-					container.appendChild( stats.dom );
+				initCar();
+				initMaterials();
+				initMaterialSelection();
 
-				}
-
-				document.addEventListener( 'mousemove', onDocumentMouseMove, false );
-
-				// common materials
-
-				var mlib = {
-
-				"Orange": 	new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ),
-				"Blue": 	new THREE.MeshLambertMaterial( { color: 0x001133, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ),
-				"Red": 		new THREE.MeshLambertMaterial( { color: 0x660000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.25 } ),
-				"Black": 	new THREE.MeshLambertMaterial( { color: 0x000000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.15 } ),
-				"White":	new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.25 } ),
-
-				"Carmine": 	new THREE.MeshPhongMaterial( { color: 0x770000, specular:0xffaaaa, envMap: textureCube, combine: THREE.MultiplyOperation } ),
-				"Gold": 	new THREE.MeshPhongMaterial( { color: 0xaa9944, specular:0xbbaa99, shininess:50, envMap: textureCube, combine: THREE.MultiplyOperation } ),
-				"Bronze":	new THREE.MeshPhongMaterial( { color: 0x150505, specular:0xee6600, shininess:10, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.25 } ),
-				"Chrome": 	new THREE.MeshPhongMaterial( { color: 0xffffff, specular:0xffffff, envMap: textureCube, combine: THREE.MultiplyOperation } ),
-
-				"Orange metal": new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: textureCube, combine: THREE.MultiplyOperation } ),
-				"Blue metal": 	new THREE.MeshLambertMaterial( { color: 0x001133, envMap: textureCube, combine: THREE.MultiplyOperation } ),
-				"Red metal": 	new THREE.MeshLambertMaterial( { color: 0x770000, envMap: textureCube, combine: THREE.MultiplyOperation } ),
-				"Green metal": 	new THREE.MeshLambertMaterial( { color: 0x007711, envMap: textureCube, combine: THREE.MultiplyOperation } ),
-				"Black metal":	new THREE.MeshLambertMaterial( { color: 0x222222, envMap: textureCube, combine: THREE.MultiplyOperation } ),
-
-				"Pure chrome": 	new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube } ),
-				"Dark chrome":	new THREE.MeshLambertMaterial( { color: 0x444444, envMap: textureCube } ),
-				"Darker chrome":new THREE.MeshLambertMaterial( { color: 0x222222, envMap: textureCube } ),
-
-				"Black glass": 	new THREE.MeshLambertMaterial( { color: 0x101016, envMap: textureCube, opacity: 0.975, transparent: true } ),
-				"Dark glass":	new THREE.MeshLambertMaterial( { color: 0x101046, envMap: textureCube, opacity: 0.25, transparent: true } ),
-				"Blue glass":	new THREE.MeshLambertMaterial( { color: 0x668899, envMap: textureCube, opacity: 0.75, transparent: true } ),
-				"Light glass":	new THREE.MeshBasicMaterial( { color: 0x223344, envMap: textureCube, opacity: 0.25, transparent: true, combine: THREE.MixOperation, reflectivity: 0.25 } ),
-
-				"Red glass":	new THREE.MeshLambertMaterial( { color: 0xff0000, opacity: 0.75, transparent: true } ),
-				"Yellow glass":	new THREE.MeshLambertMaterial( { color: 0xffffaa, opacity: 0.75, transparent: true } ),
-				"Orange glass":	new THREE.MeshLambertMaterial( { color: 0x995500, opacity: 0.75, transparent: true } ),
+				window.addEventListener( 'resize', onWindowResize, false );
 
-				"Orange glass 50":	new THREE.MeshLambertMaterial( { color: 0xffbb00, opacity: 0.5, transparent: true } ),
-				"Red glass 50": 	new THREE.MeshLambertMaterial( { color: 0xff0000, opacity: 0.5, transparent: true } ),
+				renderer.setAnimationLoop( function() {
 
-				"Fullblack rough":	new THREE.MeshLambertMaterial( { color: 0x000000 } ),
-				"Black rough":		new THREE.MeshLambertMaterial( { color: 0x050505 } ),
-				"Darkgray rough":	new THREE.MeshLambertMaterial( { color: 0x090909 } ),
-				"Red rough":		new THREE.MeshLambertMaterial( { color: 0x330500 } ),
+					render();
+					update();
 
-				"Darkgray shiny":	new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x050505 } ),
-				"Gray shiny":		new THREE.MeshPhongMaterial( { color: 0x050505, shininess: 20 } )
+				} );
 
-				};
+			}
 
-				// Gallardo materials
+			var envMap = new THREE.CubeTextureLoader()
+				.setPath( 'textures/cube/skybox/')
+				.load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
 
-				CARS[ "gallardo" ].materials = {
 
-					body: [
+			function initCar() {
 
-						[ "Orange", 	mlib[ "Orange" ] ],
-						[ "Blue", 		mlib[ "Blue" ] ],
-						[ "Red", 		mlib[ "Red" ] ],
-						[ "Black", 		mlib[ "Black" ] ],
-						[ "White", 		mlib[ "White" ] ],
+				THREE.DRACOLoader.setDecoderPath( 'js/libs/draco/gltf/' );
 
-						[ "Orange metal", 	mlib[ "Orange metal" ] ],
-						[ "Blue metal", 	mlib[ "Blue metal" ] ],
-						[ "Green metal", 	mlib[ "Green metal" ] ],
-						[ "Black metal", 	mlib[ "Black metal" ] ],
+				var loader = new THREE.GLTFLoader();
+				loader.setDRACOLoader( new THREE.DRACOLoader() );
 
-						[ "Carmine", 	mlib[ "Carmine" ] ],
-						[ "Gold", 		mlib[ "Gold" ] ],
-						[ "Bronze", 	mlib[ "Bronze" ] ],
-						[ "Chrome", 	mlib[ "Chrome" ] ]
+				loader.load( 'models/fbx/ferrari.glb', function( gltf ) {
 
-					]
+					carModel = gltf.scene.children[ 0 ];
+					carModel.add( lightHolder );
 
-				};
+					// carModel.scale.z = -1;
 
-				m = CARS[ "gallardo" ].materials;
-				mi = CARS[ "gallardo" ].init_material;
+					car.setModel( carModel, {
+							flWheel: 'wheel_fl',
+							frWheel: 'wheel_fr',
+							rlWheel: 'wheel_rl',
+							rrWheel: 'wheel_rr',
+							steeringWheel: 'steering_wheel',
+						}
+					);
 
-				CARS[ "gallardo" ].mmap = {
+					car.steeringWheelTurnAxis = 'y';
 
-					0: mlib[ "Pure chrome" ], 	// wheels chrome
-					1: mlib[ "Black rough" ],   // tire
-					2: mlib[ "Black glass" ], 	// windshield
-					3: m.body[ mi ][ 1 ], 		// body
-					4: mlib[ "Red glass" ],    	// back lights
-					5: mlib[ "Yellow glass" ],  // front lights
-					6: mlib[ "Dark chrome" ]	// windshield rim
+					carModel.traverse( function ( child ) {
 
-				};
+						if ( child.isMesh ) {
 
-				// Veyron materials
+							child.castShadow = true;
+							child.receiveShadow = true;
+							child.material.envMap = envMap;
 
-				CARS[ "veyron" ].materials = {
+						}
 
-					body: [
+					} );
 
-						[ "Orange metal", 	mlib[ "Orange metal" ] ],
-						[ "Blue metal", 	mlib[ "Blue metal" ] ],
-						[ "Red metal", 		mlib[ "Red metal" ] ],
-						[ "Green metal",	mlib[ "Green metal" ] ],
-						[ "Black metal", 	mlib[ "Black metal" ] ],
+					scene.add( carModel );
 
-						[ "Gold", 		mlib[ "Gold" ] ],
-						[ "Bronze", 	mlib[ "Bronze" ] ],
-						[ "Chrome", 	mlib[ "Chrome" ] ]
+					carParts.body.push( carModel.getObjectByName( 'body' ) );
 
-					]
+					carParts.rims.push(
+						carModel.getObjectByName( 'rim_fl' ),
+						carModel.getObjectByName( 'rim_fr' ),
+						carModel.getObjectByName( 'rim_rr' ),
+						carModel.getObjectByName( 'rim_rl' ),
+						carModel.getObjectByName( 'trim' ),
+					);
 
-				};
+					carParts.glass.push(
+						carModel.getObjectByName( 'glass' ),
+					 );
 
-				m = CARS[ "veyron" ].materials;
-				mi = CARS[ "veyron" ].init_material;
+					updateMaterials();
 
-				CARS[ "veyron" ].mmap = {
+				}, null, function( error ) { console.log( error ); } );
 
-					0: mlib[ "Black rough" ],		// tires + inside
-					1: mlib[ "Pure chrome" ],		// wheels + extras chrome
-					2: m.body[ mi ][ 1 ], 			// back / top / front torso
-					3: mlib[ "Dark glass" ],		// glass
-					4: mlib[ "Pure chrome" ],		// sides torso
-					5: mlib[ "Pure chrome" ],		// engine
-					6: mlib[ "Red glass 50" ],		// backlights
-					7: mlib[ "Orange glass 50" ]	// backsignals
+			}
 
-				};
+			function initMaterials() {
 
-				// F50 materials
 
-				CARS[ "f50" ].materials = {
+				scene.background = envMap;
 
-					body: [
+				envMapRefraction = envMap.clone();
+				envMapRefraction.mapping = THREE.CubeRefractionMapping;
+				envMapRefraction.needsUpdate = true;
 
-						[ "Orange", 	mlib[ "Orange" ] ],
-						[ "Blue", 		mlib[ "Blue" ] ],
-						[ "Red", 		mlib[ "Red" ] ],
-						[ "Black", 		mlib[ "Black" ] ],
-						[ "White", 		mlib[ "White" ] ],
+				materialsLib = {
 
-						[ "Orange metal", 	mlib[ "Orange metal" ] ],
-						[ "Blue metal", 	mlib[ "Blue metal" ] ],
-						[ "Black metal", 	mlib[ "Black metal" ] ],
+					main: [
 
-						[ "Carmine", 	mlib[ "Carmine" ] ],
-						[ "Gold", 		mlib[ "Gold" ] ],
-						[ "Bronze", 	mlib[ "Bronze" ] ],
-						[ "Chrome", 	mlib[ "Chrome" ] ]
+						new THREE.MeshStandardMaterial( { color: 0xff6600, envMap: envMap, metalness: 0.25, roughness: 0.15, name: 'orange' } ),
+						new THREE.MeshStandardMaterial( { color: 0x001133, envMap: envMap, metalness: 0.25, roughness: 0.15, name: 'blue' } ),
+						new THREE.MeshStandardMaterial( { color: 0x660000, envMap: envMap, metalness: 0.25, roughness: 0.15, name: 'red' } ),
+						new THREE.MeshStandardMaterial( { color: 0x000000, envMap: envMap, metalness: 1, roughness: 0, name: 'black' } ),
+						new THREE.MeshStandardMaterial( { color: 0xffffff, envMap: envMap, metalness: 0.25, roughness: 0.15, name: 'white' } ),
+						new THREE.MeshStandardMaterial( { color: 0xcccccc, envMap: envMap, metalness: 1, roughness: 0, name: 'metallic' } ),
 
-					]
+					],
 
-				};
+					glass: [
 
-				m = CARS[ "f50" ].materials;
-				mi = CARS[ "f50" ].init_material;
+						new THREE.MeshStandardMaterial( { color: 0x000000, envMap: envMapRefraction, metalness: 0.0, roughness: 0.25, opacity: 0.5, transparent: true, refractionRatio: 0.25, name: 'clear'} ),
+						new THREE.MeshStandardMaterial( { color: 0x000000, envMap: envMapRefraction, metalness: 0, roughness: 0.25, opacity: 0.75, transparent: true, refractionRatio: 0.75, name: 'smoked' } ),
+						new THREE.MeshStandardMaterial( { color: 0x001133, envMap: envMapRefraction, metalness: 0, roughness: 0.25, opacity: 0.5, transparent: true, refractionRatio: 0.75, name: 'blue' } ),
 
-				CARS[ "f50" ].mmap = {
+					],
 
-					0:  mlib[ "Dark chrome" ], 		// interior + rim
-					1:  mlib[ "Pure chrome" ], 		// wheels + gears chrome
-					2:  mlib[ "Blue glass" ], 		// glass
-					3:  m.body[ mi ][ 1 ], 			// torso mid + front spoiler
-					4:  mlib[ "Darkgray shiny" ], 	// interior + behind seats
-					5:  mlib[ "Darkgray shiny" ], 	// tiny dots in interior
-					6:  m.body[ mi ][ 1 ], 			// back torso
-					7:  m.body[ mi ][ 1 ], 			// right mirror decal
-					8:  m.body[ mi ][ 1 ], 			// front decal
-					9:  m.body[ mi ][ 1 ], 			// front torso
-					10: m.body[ mi ][ 1 ], 			// left mirror decal
-					11: mlib[ "Pure chrome" ], 		// engine
-					12: mlib[ "Darkgray rough" ],	// tires side
-					13: mlib[ "Darkgray rough" ],	// tires bottom
-					14: mlib[ "Darkgray shiny" ], 	// bottom
-					15: mlib[ "Black rough" ],		// ???
-					16: mlib[ "Orange glass" ],		// front signals
-					17: mlib[ "Dark chrome" ], 		// wheels center
-					18: mlib[ "Red glass" ], 		// back lights
-					19: mlib[ "Black rough" ], 		// ???
-					20: mlib[ "Red rough" ], 		// seats
-					21: mlib[ "Black rough" ], 		// back plate
-					22: mlib[ "Black rough" ], 		// front light dots
-					23: m.body[ mi ][ 1 ], 			// back torso
-					24: m.body[ mi ][ 1 ] 			// back torso center
+				}
 
-				};
+			}
 
+			function initMaterialSelection() {
 
-				// Camero materials
+				function addOption( name, menu ) {
 
-				CARS[ "camaro" ].materials = {
+					var option = document.createElement( 'option' );
+					option.text = name;
+					option.value = name;
+					menu.add( option );
 
-					body: [
+				}
 
-						[ "Orange", 	mlib[ "Orange" ] ],
-						[ "Blue", 		mlib[ "Blue" ] ],
-						[ "Red", 		mlib[ "Red" ] ],
-						[ "Black", 		mlib[ "Black" ] ],
-						[ "White", 		mlib[ "White" ] ],
+				materialsLib.main.forEach( function( material ) {
 
-						[ "Orange metal", 	mlib[ "Orange metal" ] ],
-						[ "Blue metal", 	mlib[ "Blue metal" ] ],
-						[ "Red metal", 		mlib[ "Red metal" ] ],
-						[ "Green metal", 	mlib[ "Green metal" ] ],
-						[ "Black metal", 	mlib[ "Black metal" ] ],
+					addOption( material.name, bodyMatSelect );
+					addOption( material.name, rimMatSelect );
 
-						[ "Gold", 		mlib[ "Gold" ] ],
-						[ "Bronze", 	mlib[ "Bronze" ] ],
-						[ "Chrome", 	mlib[ "Chrome" ] ]
+				} );
 
-					]
+				materialsLib.glass.forEach( function( material ) {
 
-				};
+					addOption( material.name, glassMatSelect );
 
-				m = CARS[ "camaro" ].materials;
-				mi = CARS[ "camaro" ].init_material;
+				} );
 
-				CARS[ "camaro" ].mmap = {
+				bodyMatSelect.selectedIndex = 2;
+				rimMatSelect.selectedIndex = 5;
+				glassMatSelect.selectedIndex = 0;
 
-					0: m.body[ mi ][ 1 ], 			// car body
-					1: mlib[ "Pure chrome" ], 		// wheels chrome
-					2: mlib[ "Pure chrome" ], 		// grille chrome
-					3: mlib[ "Dark chrome" ], 		// door lines
-					4: mlib[ "Light glass" ], 		// windshield
-					5: mlib[ "Gray shiny" ],        // interior
-					6: mlib[ "Black rough" ],       // tire
-					7: mlib[ "Fullblack rough" ],   // tireling
-					8: mlib[ "Fullblack rough" ]    // behind grille
+				bodyMatSelect.addEventListener( 'change', updateMaterials );
+				rimMatSelect.addEventListener( 'change', updateMaterials );
+				glassMatSelect.addEventListener( 'change', updateMaterials );
 
-				};
+			}
 
-				loader.load( CARS[ "veyron" ].url, function( geometry ) { createScene( geometry, "veyron" ) } );
+			// set materials to the current values of the selection menus
+			function updateMaterials() {
 
-				for( var c in CARS ) initCarButton( c );
+				var bodyMatName = bodyMatSelect.options[ bodyMatSelect.selectedIndex ].value;
+				var rimMatName = rimMatSelect.options[ rimMatSelect.selectedIndex ].value;
+				var glassMatName = glassMatSelect.options[ glassMatSelect.selectedIndex ].value;
 
-				//
+				var bodyMat = materialsLib.main[ bodyMatSelect.selectedIndex ];
+				var rimMat = materialsLib.main[ rimMatSelect.selectedIndex ];
+				var glassMat = materialsLib.glass[ glassMatSelect.selectedIndex ];
 
-				window.addEventListener( 'resize', onWindowResize, false );
+				carParts.body.forEach( function ( part ) { part.material = bodyMat; } );
+				carParts.rims.forEach( function ( part ) { part.material = rimMat; } );
+				carParts.glass.forEach( function ( part ) { part.material = glassMat; } );
 
 			}
 
 			function onWindowResize() {
 
-				windowHalfX = window.innerWidth / 2;
-				windowHalfY = window.innerHeight / 2;
-
 				camera.aspect = window.innerWidth / window.innerHeight;
 				camera.updateProjectionMatrix();
 
@@ -455,154 +316,81 @@
 
 			}
 
-			function initCarButton( car ) {
-
-				$( car ).addEventListener( 'click', function() {
-
-					if ( ! CARS[ car ].object ) {
-
-						loader.load( CARS[ car ].url, function( geometry ) { createScene( geometry, car ) } );
-
-					} else {
-
-						switchCar( car );
-
-					}
-
-				}, false );
-
-			}
-
-			function $( id ) { return document.getElementById( id ) }
-			function button_name( car, index ) { return "m_" + car  + "_" + index }
-
-			function switchCar( car ) {
-
-				for ( var c in CARS ) {
-
-					if ( c != car && CARS[ c ].object ) {
-
-						CARS[ c ].object.visible = false;
-						CARS[ c ].buttons.style.display = "none";
+			function onDriveModeToggle() {
 
-					}
-				}
-
-				CARS[ car ].object.visible = true;
-				CARS[ car ].buttons.style.display = "block";
+				drivingMode = !drivingMode;
+				controls.enabled = !drivingMode;
+				car.enabled = drivingMode;
 
-				$( "car_name" ).innerHTML = CARS[ car ].name + " model";
-				$( "car_author" ).innerHTML = CARS[ car ].author;
+				controls.reset();
+				carModel.position.copy( origin );
 
 			}
 
-			function createButtons( materials, car ) {
-
-				var buttons, i, src = "";
-
-				for( i = 0; i < materials.length; i ++ ) {
-
-					src += '<button id="' + button_name( car, i ) + '">' + materials[ i ][ 0 ] + '</button> ';
-
-				}
-
-				buttons = document.createElement( "div" );
-				buttons.innerHTML = src;
-
-				$( "buttons_materials" ).appendChild( buttons );
+			function render() {
 
-				return buttons;
+				renderer.render( scene, camera );
 
 			}
 
-			function attachButtonMaterials( materials, faceMaterials, material_indices, car ) {
-
-				for( var i = 0; i < materials.length; i ++ ) {
-
-					$( button_name( car, i ) ).counter = i;
-					$( button_name( car, i ) ).addEventListener( 'click', function() {
-
-						for ( var j = 0; j < material_indices.length; j ++ ) {
+			function update() {
 
-							faceMaterials[ material_indices [ j ] ] = materials[ this.counter ][ 1 ];
+				var delta = clock.getDelta();
 
-						}
-
-					}, false );
+				if ( carModel && drivingMode ) {
 
-				}
+					car.update( delta );
 
-			}
+					updateCamera( delta );
 
-			function createScene( geometry, car ) {
+					resetPosition();
 
-				geometry.sortFacesByMaterialIndex();
+					// keep the light (and shadow) pointing in the same direction as the car rotates
+					lightHolder.rotation.y = -carModel.rotation.y;
 
-				var m = [],
-					s = CARS[ car ].scale * 1,
-					r = CARS[ car ].init_rotation,
-					materials = CARS[ car ].materials,
-					mi = CARS[ car ].init_material,
-					bm = CARS[ car ].body_materials;
+				} else {
 
-				for ( var i in CARS[ car ].mmap ) {
-
-					m[ i ] = CARS[ car ].mmap[ i ];
+					controls.update();
 
 				}
 
-				var mesh = new THREE.Mesh( geometry, m );
-
-				mesh.rotation.x = r[ 0 ];
-				mesh.rotation.y = r[ 1 ];
-				mesh.rotation.z = r[ 2 ];
-
-				mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
-
-				scene.add( mesh );
-
-				CARS[ car ].object = mesh;
-
-				CARS[ car ].buttons = createButtons( materials.body, car );
-				attachButtonMaterials( materials.body, m, bm, car );
-
-				switchCar( car );
+				stats.update();
 
 			}
 
-			function onDocumentMouseMove(event) {
+			function updateCamera( delta ) {
 
-				mouseY = ( event.clientY - window.innerHeight );
+				carModel.getWorldPosition( cameraTarget );
+				cameraTarget.y = camera.position.y;
+        cameraTarget.z += distance;
+				cameraTarget.x += distance;
 
-			}
+				camera.position.lerp( cameraTarget, delta * damping );
+        camera.lookAt( carModel.position );
 
-			//
+      }
 
-			function animate() {
+      function resetPosition() {
 
-				requestAnimationFrame( animate );
+				if( carModel.position.distanceTo( origin ) > 300 ) {
 
-				render();
+					carModel.position.set( 0, 0, 0 );
+					car.speed = 0;
+					car.enabled = false;
 
-			}
+					setTimeout( function() {
 
-			function render() {
+						car.enabled = true;
 
-				var timer = -0.0002 * Date.now();
+					}, 1500 )
 
-				camera.position.x = 1000 * Math.cos( timer );
-				camera.position.y += ( - mouseY - camera.position.y ) * .05;
-				camera.position.z = 1000 * Math.sin( timer );
-
-				camera.lookAt( scene.position );
-
-				renderer.render( scene, camera );
+				}
 
-				if ( STATS_ENABLED ) stats.update();
+      }
 
-			}
+			init();
 
 		</script>
 
 	</body>
-</html>
+</html>