canvas_sandbox.html 7.0 KB
Newer Older
M
Mr.doob 已提交
1
<!doctype html>
2 3
<html lang="en">
	<head>
4
		<title>three.js canvas - sandbox</title>
5
		<meta charset="utf-8">
M
Mr.doob 已提交
6
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
M
Mr.doob 已提交
7
		<style>
8 9 10 11 12 13 14 15 16 17
			body {
				font-family: Monospace;
				background-color: #f0f0f0;
				margin: 0px;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

M
Mr.doob 已提交
18
		<script src="../build/Three.js"></script>
19

M
Mr.doob 已提交
20
		<script src="js/Stats.js"></script>
21

M
Mr.doob 已提交
22
		<script>
23

M
Mr.doob 已提交
24
			var container, stats;
25

M
Mr.doob 已提交
26
			var camera, scene, renderer, objects;
27
			var pointLight;
28

M
Mr.doob 已提交
29
			var sphere;
30 31 32 33 34 35 36 37 38 39

			var targetRotation = 0;
			var targetRotationOnMouseDown = 0;

			var mouseX = 0;
			var mouseXOnMouseDown = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

M
Mr.doob 已提交
40 41 42 43 44 45
			var moveForward = false;
			var moveBackwards = false;
			var moveLeft = false;
			var moveRight = false;
			var moveUp = false;
			var moveDown = false;
46

M
Mr.doob 已提交
47 48
			var targetMoveLeft = false;
			var targetMoveRight = false;
49

50 51 52
			var debugContext;

			init();
53
			animate();
54 55 56

			function init() {

57 58
				container = document.createElement( 'div' );
				document.body.appendChild( container );
59

60
				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
61
				camera.position.set( 0, 150, 400 );
M
Mr.doob 已提交
62
				camera.target = new THREE.Vector3( 0, 150, 0 );
63 64 65

				scene = new THREE.Scene();

66 67 68
				scene.add( camera );


M
Mr.doob 已提交
69 70 71
				// Grid

				var geometry = new THREE.Geometry();
M
Mr.doob 已提交
72 73
				geometry.vertices.push( new THREE.Vector3( - 500, 0, 0 ) );
				geometry.vertices.push( new THREE.Vector3( 500, 0, 0 ) );
M
Mr.doob 已提交
74 75

				var material = new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.5 } );
76

M
Mr.doob 已提交
77
				for ( var i = 0; i <= 10; i ++ ) {
M
Mr.doob 已提交
78

M
Mr.doob 已提交
79 80
					var line = new THREE.Line( geometry, material );
					line.position.z = ( i * 100 ) - 500;
81
					scene.add( line );
M
Mr.doob 已提交
82 83 84 85

					var line = new THREE.Line( geometry, material );
					line.position.x = ( i * 100 ) - 500;
					line.rotation.y = 90 * Math.PI / 180;
86
					scene.add( line );
M
Mr.doob 已提交
87 88

				}
89

M
Mr.doob 已提交
90
				// Spheres
91

M
Mr.doob 已提交
92 93
				objects = [];

94
				geometry = new THREE.IcosahedronGeometry( 100, 1 );
95

96
				material = new THREE.MeshBasicMaterial( { envMap: THREE.ImageUtils.loadTexture( 'textures/metal.jpg', new THREE.SphericalReflectionMapping() ), overdraw: true } );
97

M
Mr.doob 已提交
98
				for ( var i = 0; i < 10; i ++ ) {
99

M
Mr.doob 已提交
100
					sphere = new THREE.Mesh( geometry, material );
101

M
Mr.doob 已提交
102 103 104
					sphere.position.x = Math.random() * 1000 - 500;
					sphere.position.y = Math.random() * 1000 - 500;
					sphere.position.z = Math.random() * 1000 - 500;
105

M
Mr.doob 已提交
106 107 108
					sphere.rotation.x = Math.random() * 200 - 100;
					sphere.rotation.y = Math.random() * 200 - 100;
					sphere.rotation.z = Math.random() * 200 - 100;
109

M
Mr.doob 已提交
110
					sphere.scale.x = sphere.scale.y = sphere.scale.z = Math.random() + 0.5;
111

M
Mr.doob 已提交
112 113
					objects.push( sphere );

114
					scene.add( sphere );
115 116 117

				}

M
Mr.doob 已提交
118
				// Lights
119

M
Mr.doob 已提交
120
				var ambientLight = new THREE.AmbientLight( Math.random() * 0x202020 );
121
				scene.add( ambientLight );
M
Mr.doob 已提交
122 123 124 125 126 127

				var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
				directionalLight.position.x = Math.random() - 0.5;
				directionalLight.position.y = Math.random() - 0.5;
				directionalLight.position.z = Math.random() - 0.5;
				directionalLight.position.normalize();
128
				scene.add( directionalLight );
M
Mr.doob 已提交
129

130
				pointLight = new THREE.PointLight( 0xff0000, 1 );
131
				scene.add( pointLight );
132 133

				renderer = new THREE.CanvasRenderer();
M
Mr.doob 已提交
134
				renderer.setSize( window.innerWidth, window.innerHeight );
135

M
Mr.doob 已提交
136
				container.appendChild( renderer.domElement );
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

				var debugCanvas = document.createElement( 'canvas' );
				debugCanvas.width = 512;
				debugCanvas.height = 512;
				debugCanvas.style.position = 'absolute';
				debugCanvas.style.top = '0px';
				debugCanvas.style.left = '0px';

				container.appendChild( debugCanvas );

				debugContext = debugCanvas.getContext( '2d' );
				debugContext.setTransform( 1, 0, 0, 1, 256, 256 );
				debugContext.strokeStyle = '#000000';

				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.top = '0px';
				container.appendChild(stats.domElement);

M
Mr.doob 已提交
156 157
				document.addEventListener( 'keydown', onDocumentKeyDown, false );
				document.addEventListener( 'keyup', onDocumentKeyUp, false );
158 159 160 161 162 163 164 165 166

				//

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

167 168
				windowHalfX = window.innerWidth / 2;
				windowHalfY = window.innerHeight / 2;
169 170 171 172 173 174

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

175 176 177 178
			}

			function onDocumentKeyDown( event ) {

M
Mr.doob 已提交
179
				switch ( event.keyCode ) {
180 181 182 183 184

					case 38: moveForward = true; break; // up
					case 40: moveBackwards = true; break; // down
					case 37: moveLeft = true; break; // left
					case 39: moveRight = true; break; // right
M
Mr.doob 已提交
185 186 187 188
					case 87: moveUp = true; break; // w
					case 83: moveDown = true; break; // s
					case 65: targetMoveLeft = true; break; // a
					case 68: targetMoveRight = true; break; // d
189 190 191 192 193 194 195

				}

			}

			function onDocumentKeyUp( event ) {

M
Mr.doob 已提交
196
				switch ( event.keyCode ) {
197 198 199 200 201

					case 38: moveForward = false; break; // up
					case 40: moveBackwards = false; break; // down
					case 37: moveLeft = false; break; // left
					case 39: moveRight = false; break; // right
M
Mr.doob 已提交
202 203 204 205
					case 87: moveUp = false; break; // w
					case 83: moveDown = false; break; // s
					case 65: targetMoveLeft = false; break; // a
					case 68: targetMoveRight = false; break; // d
206 207 208 209 210 211 212

				}

			}

			//

213 214 215 216 217 218 219 220 221
			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

222
			function render() {
223

M
Mr.doob 已提交
224 225
				if ( moveForward ) camera.position.z -= 10;
				if ( moveBackwards ) camera.position.z += 10;
226

M
Mr.doob 已提交
227 228
				if ( moveLeft ) camera.position.x -= 10;
				if ( moveRight ) camera.position.x += 10;
229

M
Mr.doob 已提交
230 231
				if ( moveUp ) camera.position.y += 10;
				if ( moveDown ) camera.position.y -= 10;
232

M
Mr.doob 已提交
233 234
				if ( targetMoveLeft ) camera.target.x -= 10;
				if ( targetMoveRight ) camera.target.x += 10;
235

M
Mr.doob 已提交
236
				camera.lookAt( camera.target );
237

238 239 240 241 242 243 244 245 246 247 248 249 250
				debugContext.clearRect( -256, -256, 512, 512 );

				debugContext.beginPath();

				// center
				debugContext.moveTo( -10, 0 );
				debugContext.lineTo( 10, 0 );
				debugContext.moveTo( 0, -10 );
				debugContext.lineTo( 0, 10 );

				// camera

				debugContext.moveTo( camera.position.x * 0.1, camera.position.z * 0.1 );
M
Mr.doob 已提交
251
				debugContext.lineTo( camera.target.x * 0.1, camera.target.z * 0.1 );
252
				debugContext.rect( camera.position.x * 0.1 - 5, camera.position.z * 0.1 - 5, 10, 10 );
M
Mr.doob 已提交
253
				debugContext.rect( camera.target.x * 0.1 - 5, camera.target.z * 0.1 - 5, 10, 10 );
254 255
				debugContext.rect( - 50, - 50, 100, 100 );

M
Mr.doob 已提交
256 257 258
				for ( var i = 0, l = objects.length; i < l; i++ ) {

					var object = objects[ i ];
259

M
Mr.doob 已提交
260 261 262
					object.rotation.x += 0.01;
					object.rotation.y += 0.005;
					object.position.y = Math.sin( object.rotation.x ) * 200;
263 264 265 266 267 268 269 270 271

					debugContext.rect( object.position.x * 0.1 - 5, object.position.z * 0.1 - 5, 10, 10 );

				}


				debugContext.closePath();
				debugContext.stroke();

272
				renderer.render( scene, camera );
273

274 275 276 277 278 279
			}

		</script>

	</body>
</html>