canvas_materials_depth.html 7.2 KB
Newer Older
M
Mr.doob 已提交
1
<!doctype html>
M
Mr.doob 已提交
2 3
<html lang="en">
	<head>
4
		<title>three.js canvas - depth material</title>
M
Mr.doob 已提交
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>
M
Mr.doob 已提交
8 9 10 11 12 13 14 15 16 17
			body {
				font-family: Monospace;
				background-color: #000000;
				margin: 0px;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

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

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

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

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

27
			var camera, scene, renderer;
M
Mr.doob 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

			var cube, plane;

			var targetRotation = 0;
			var targetRotationOnMouseDown = 0;

			var mouseX = 0;
			var mouseXOnMouseDown = 0;

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

			var moveForward = false,
			moveBackwards = false,
			moveUp = false,
			moveDown = false,
			moveLeft = false,
			moveRight = false,

			yawLeft = false,
			yawRight = false,
			pitchUp = false,
			pitchDown = false,
			rollLeft = false,
			rollRight = false;

			var debugContext;

			init();
57
			animate();
M
Mr.doob 已提交
58 59 60 61 62 63

			function init() {

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

64
				camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
M
Mr.doob 已提交
65 66 67 68 69 70 71 72 73
				camera.position.x = 1000;
				camera.position.y = 1000;
				camera.position.z = 1000;
				// camera.target.position.y = 150;

				scene = new THREE.Scene();

				// Plane

74
				var material = new THREE.MeshDepthMaterial();
M
Mr.doob 已提交
75

76
				plane = new THREE.Mesh( new THREE.PlaneGeometry( 1000, 1000, 10, 10 ), material );
M
Mr.doob 已提交
77 78 79
				plane.overdraw = true;
				plane.doubleSided = true;
				
M
Mr.doob 已提交
80
				plane.rotation.x = - 90 * ( Math.PI / 180 );
81
				plane.position.y = - 100;
M
Mr.doob 已提交
82

83
				scene.add( plane );
M
Mr.doob 已提交
84 85 86

				// Spheres

87
				geometry = new THREE.CubeGeometry( 100, 100, 100 );
M
Mr.doob 已提交
88 89 90 91 92 93 94 95
				material = new THREE.MeshDepthMaterial( { near: 1, far: 2000 } );

				for (var i = 0; i < 20; i ++ ) {

					cube = new THREE.Mesh( geometry, material );
					cube.overdraw = true;

					cube.position.x = ( i % 5 ) * 200 - 400;
96
					cube.position.z = Math.floor( i / 5 ) * 200 - 350;
M
Mr.doob 已提交
97 98 99 100 101

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

					cube.rotation.x = Math.random() * 200 - 100;
					cube.rotation.y = Math.random() * 200 - 100;
					cube.rotation.z = Math.random() * 200 - 100;
107 108

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

112
					scene.add(cube);
M
Mr.doob 已提交
113 114 115 116 117 118

				}

				// Lights

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

				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();
126
				scene.add( directionalLight );
M
Mr.doob 已提交
127 128

				var pointLight = new THREE.PointLight( 0xff0000, 1 );
129
				scene.add( pointLight );
M
Mr.doob 已提交
130 131

				renderer = new THREE.CanvasRenderer();
132
				renderer.setSize( window.innerWidth, window.innerHeight );
M
Mr.doob 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

				container.appendChild( renderer.domElement );

				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 = '#808080';

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

				document.addEventListener( 'keydown', onDocumentKeyDown, false );
				document.addEventListener( 'keyup', onDocumentKeyUp, false );
			}

			function onDocumentKeyDown( event ) {

				switch( event.keyCode ) {

					case 38: moveForward = true; break; // up
					case 40: moveBackwards = true; break; // down
					case 37: moveLeft = true; break; // left
					case 39: moveRight = true; break; // right
					case 65: yawLeft = true; break; // a
					case 68: yawRight = true; break; // d
					case 87: moveUp/*pitchUp*/ = true; break; // w
					case 83: moveDown/*pitchDown*/ = true; break; // s
					case 90: rollLeft = true; break; // z
					case 67: rollRight = true; break; // c

				}

			}

			function onDocumentKeyUp( event ) {

				switch( event.keyCode ) {

					case 38: moveForward = false; break; // up
					case 40: moveBackwards = false; break; // down
					case 37: moveLeft = false; break; // left
					case 39: moveRight = false; break; // right
					case 65: yawLeft = false; break; // a
					case 68: yawRight = false; break; // d
					case 87: moveUp/*pitchUp*/ = false; break; // w
					case 83: moveDown/*pitchDown*/ = false; break; // s
					case 90: rollLeft = false; break; // z
					case 67: rollRight = false; break; // c

				}

			}

			//

198 199 200 201 202 203 204 205 206 207
			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {
M
Mr.doob 已提交
208

209 210
				if ( moveForward ) camera.position.z -= 10; // camera.moveZ( 10 );
				if ( moveBackwards ) camera.position.z += 10; // camera.moveZ( - 10 );
M
Mr.doob 已提交
211

212 213
				if ( moveUp ) camera.position.y += 10; // camera.moveZ( 10 );
				if ( moveDown ) camera.position.y -= 10; // camera.moveZ( - 10 );
M
Mr.doob 已提交
214

215 216
				if ( moveLeft ) camera.position.x -= 10; // camera.moveX( - 10 );
				if ( moveRight ) camera.position.x += 10; // camera.moveX( 10 );
M
Mr.doob 已提交
217 218 219 220

				if ( pitchUp ) camera.rotation.x += 0.01; // camera.rotateX( 1 );
				if ( pitchDown ) camera.rotation.x -= 0.01; // camera.rotateX( - 1 );

221 222
				if ( yawLeft ) camera.target.position.x -= 10; // camera.rotation.y += 0.01; // camera.rotateY( 1 );
				if ( yawRight ) camera.target.position.x += 10; // camera.rotation.y -= 0.01; // camera.rotateY( - 1 );
M
Mr.doob 已提交
223 224 225 226

				if ( rollLeft ) camera.rotation.z += 0.01; // camera.rotateZ( 1 );
				if ( rollRight ) camera.rotation.z -= 0.01; // camera.rotateZ( - 1 );

227
				debugContext.clearRect( - 256, - 256, 512, 512 );
M
Mr.doob 已提交
228 229 230 231

				debugContext.beginPath();

				// center
232
				debugContext.moveTo( - 10, 0 );
M
Mr.doob 已提交
233
				debugContext.lineTo( 10, 0 );
234
				debugContext.moveTo( 0, - 10 );
M
Mr.doob 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
				debugContext.lineTo( 0, 10 );

				// camera

				debugContext.moveTo( camera.position.x * 0.1, camera.position.z * 0.1 );
				debugContext.lineTo( camera.target.position.x * 0.1, camera.target.position.z * 0.1 );
				debugContext.rect( camera.position.x * 0.1 - 5, camera.position.z * 0.1 - 5, 10, 10 );
				debugContext.rect( camera.target.position.x * 0.1 - 5, camera.target.position.z * 0.1 - 5, 10, 10 );
				debugContext.rect( - 50, - 50, 100, 100 );

				for ( var i = 1; i < scene.objects.length; i++ ) {

					var object = scene.objects[i];

					object.rotation.x += 0.01;
					object.rotation.y += 0.005;
251
					object.position.y = Math.sin( object.rotation.x ) * 200 + 200;
M
Mr.doob 已提交
252 253 254 255 256 257 258 259

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

				}

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

260
				renderer.render( scene, camera );
M
Mr.doob 已提交
261 262 263 264 265 266 267

			}

		</script>

	</body>
</html>