raytracing_sandbox.html 7.8 KB
Newer Older
M
Mr.doob 已提交
1 2 3
<!DOCTYPE html>
<html lang="en">
	<head>
4
		<title>three.js - raytracing renderer with web workers</title>
M
Mr.doob 已提交
5 6 7 8 9 10 11
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				color: #ffffff;
				margin: 0px;
12
				padding: 0px;
M
Mr.doob 已提交
13 14 15 16
			}
		</style>
	</head>
	<body>
W
WestLangley 已提交
17
		<script src="../build/three.js"></script>
M
Mr.doob 已提交
18 19 20 21
		<script src="js/renderers/RaytracingRenderer.js"></script>

		<script>

22 23 24 25 26
			var hash = location.hash ? location.hash.substring( 1 ) : '3';

			var WORKERS = + hash || navigator.hardwareConcurrency || 3;

			var container, info;
M
Mr.doob 已提交
27

L
Lewy Blue 已提交
28
			var camera, scene, renderer;
M
Mr.doob 已提交
29
			var group;
M
Mr.doob 已提交
30 31 32 33

			init();
			render();

34
			function initScene( width, height ) {
M
Mr.doob 已提交
35

36
				camera = new THREE.PerspectiveCamera( 60, width / height, 1, 1000 );
M
Mr.doob 已提交
37 38 39
				camera.position.z = 600;

				scene = new THREE.Scene();
40
				scene.background = new THREE.Color( 0xf0f0f0 );
M
Mr.doob 已提交
41

42
				// materials
M
Mr.doob 已提交
43

M
Mr.doob 已提交
44 45 46 47 48
				var phongMaterial = new THREE.MeshPhongMaterial( {
					color: 0xffffff,
					specular: 0x222222,
					shininess: 150,
					vertexColors: THREE.NoColors,
49
					flatShading: false
M
Mr.doob 已提交
50 51 52 53 54 55 56
				} );

				var phongMaterialBox = new THREE.MeshPhongMaterial( {
					color: 0xffffff,
					specular: 0x111111,
					shininess: 100,
					vertexColors: THREE.NoColors,
57
					flatShading: true
M
Mr.doob 已提交
58 59 60 61 62 63 64
				} );

				var phongMaterialBoxBottom = new THREE.MeshPhongMaterial( {
					color: 0x666666,
					specular: 0x111111,
					shininess: 100,
					vertexColors: THREE.NoColors,
65
					flatShading: true
M
Mr.doob 已提交
66 67 68 69 70 71 72
				} );

				var phongMaterialBoxLeft = new THREE.MeshPhongMaterial( {
					color: 0x990000,
					specular: 0x111111,
					shininess: 100,
					vertexColors: THREE.NoColors,
73
					flatShading: true
M
Mr.doob 已提交
74 75 76 77 78 79 80
				} );

				var phongMaterialBoxRight = new THREE.MeshPhongMaterial( {
					color: 0x0066ff,
					specular: 0x111111,
					shininess: 100,
					vertexColors: THREE.NoColors,
81
					flatShading: true
M
Mr.doob 已提交
82 83 84 85 86 87 88
				} );

				var mirrorMaterialFlat = new THREE.MeshPhongMaterial( {
					color: 0x000000,
					specular: 0xff8888,
					shininess: 10000,
					vertexColors: THREE.NoColors,
89
					flatShading: true
M
Mr.doob 已提交
90
				} );
91 92
				mirrorMaterialFlat.mirror = true;
				mirrorMaterialFlat.reflectivity = 1;
M
Mr.doob 已提交
93

M
Mr.doob 已提交
94 95 96 97 98
				var mirrorMaterialFlatDark = new THREE.MeshPhongMaterial( {
					color: 0x000000,
					specular: 0xaaaaaa,
					shininess: 10000,
					vertexColors: THREE.NoColors,
99
					flatShading: true
M
Mr.doob 已提交
100
				} );
101 102
				mirrorMaterialFlatDark.mirror = true;
				mirrorMaterialFlatDark.reflectivity = 1;
M
Mr.doob 已提交
103

M
Mr.doob 已提交
104 105 106 107 108
				var mirrorMaterialSmooth = new THREE.MeshPhongMaterial( {
					color: 0xffaa00,
					specular: 0x222222,
					shininess: 10000,
					vertexColors: THREE.NoColors,
109
					flatShading: false
M
Mr.doob 已提交
110
				} );
111 112
				mirrorMaterialSmooth.mirror = true;
				mirrorMaterialSmooth.reflectivity = 0.3;
M
Mr.doob 已提交
113

M
Mr.doob 已提交
114 115 116 117 118
				var glassMaterialFlat = new THREE.MeshPhongMaterial( {
					color: 0x000000,
					specular: 0x00ff00,
					shininess: 10000,
					vertexColors: THREE.NoColors,
119
					flatShading: true
M
Mr.doob 已提交
120
				} );
121 122
				glassMaterialFlat.glass = true;
				glassMaterialFlat.reflectivity = 0.5;
M
Mr.doob 已提交
123

M
Mr.doob 已提交
124 125 126 127 128
				var glassMaterialSmooth = new THREE.MeshPhongMaterial( {
					color: 0x000000,
					specular: 0xffaa55,
					shininess: 10000,
					vertexColors: THREE.NoColors,
129
					flatShading: false
M
Mr.doob 已提交
130
				} );
131 132 133
				glassMaterialSmooth.glass = true;
				glassMaterialSmooth.reflectivity = 0.25;
				glassMaterialSmooth.refractionRatio = 0.6;
M
Mr.doob 已提交
134

M
Mr.doob 已提交
135 136 137 138 139
				//

				group = new THREE.Group();
				scene.add( group );

140
				// geometries
M
Mr.doob 已提交
141

142 143 144
				var sphereGeometry = new THREE.SphereBufferGeometry( 100, 16, 8 );
				var planeGeometry = new THREE.BoxBufferGeometry( 600, 5, 600 );
				var boxGeometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
M
Mr.doob 已提交
145

146 147
				// Sphere

M
Mr.doob 已提交
148
				var sphere = new THREE.Mesh( sphereGeometry, phongMaterial );
149
				sphere.scale.multiplyScalar( 0.5 );
150
				sphere.position.set( - 50, - 250 + 5, - 50 );
M
Mr.doob 已提交
151
				group.add( sphere );
152

M
Mr.doob 已提交
153
				var sphere2 = new THREE.Mesh( sphereGeometry, mirrorMaterialSmooth );
154
				sphere2.scale.multiplyScalar( 0.5 );
155
				sphere2.position.set( 175, - 250 + 5, - 150 );
M
Mr.doob 已提交
156
				group.add( sphere2 );
157 158 159

				// Box

M
Mr.doob 已提交
160
				var box = new THREE.Mesh( boxGeometry, mirrorMaterialFlat );
161
				box.position.set( - 175, - 250 + 2.5, - 150 );
162
				box.rotation.y = 0.5;
M
Mr.doob 已提交
163
				group.add( box );
164 165 166

				// Glass

M
Mr.doob 已提交
167
				var glass = new THREE.Mesh( sphereGeometry, glassMaterialSmooth );
168
				glass.scale.multiplyScalar( 0.5 );
169
				glass.position.set( 75, - 250 + 5, - 75 );
170 171 172 173 174
				glass.rotation.y = 0.5;
				scene.add( glass );

				// bottom

M
Mr.doob 已提交
175
				var plane = new THREE.Mesh( planeGeometry, phongMaterialBoxBottom );
176
				plane.position.set( 0, - 300 + 2.5, - 300 );
177 178 179 180
				scene.add( plane );

				// top

M
Mr.doob 已提交
181
				var plane = new THREE.Mesh( planeGeometry, phongMaterialBox );
182
				plane.position.set( 0, 300 - 2.5, - 300 );
183 184 185 186
				scene.add( plane );

				// back

M
Mr.doob 已提交
187
				var plane = new THREE.Mesh( planeGeometry, phongMaterialBox );
188
				plane.rotation.x = 1.57;
189
				plane.position.set( 0, 0, - 300 );
190 191
				scene.add( plane );

M
Mr.doob 已提交
192
				var plane = new THREE.Mesh( planeGeometry, mirrorMaterialFlatDark );
193
				plane.rotation.x = 1.57;
194
				plane.position.set( 0, 0, - 300 + 10 );
195 196 197 198 199
				plane.scale.multiplyScalar( 0.85 );
				scene.add( plane );

				// left

M
Mr.doob 已提交
200
				var plane = new THREE.Mesh( planeGeometry, phongMaterialBoxLeft );
201
				plane.rotation.z = 1.57;
202
				plane.position.set( - 300, 0, - 300 );
203 204 205 206
				scene.add( plane );

				// right

M
Mr.doob 已提交
207
				var plane = new THREE.Mesh( planeGeometry, phongMaterialBoxRight );
208
				plane.rotation.z = 1.57;
209
				plane.position.set( 300, 0, - 300 );
210 211 212 213 214 215 216
				scene.add( plane );

				// light

				var intensity = 70000;

				var light = new THREE.PointLight( 0xffaa55, intensity );
217
				light.position.set( - 200, 100, 100 );
218 219 220 221 222 223
				light.physicalAttenuation = true;
				scene.add( light );

				var light = new THREE.PointLight( 0x55aaff, intensity );
				light.position.set( 200, 100, 100 );
				light.physicalAttenuation = true;
224 225
				scene.add( light );

226 227 228
				var light = new THREE.PointLight( 0xffffff, intensity * 1.5 );
				light.position.set( 0, 0, 300 );
				light.physicalAttenuation = true;
229 230
				scene.add( light );

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
			}

			function init() {

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

				info = document.createElement( 'div' );
				info.style.position = 'absolute';
				info.style.top = '10px';
				info.style.width = '100%';
				info.style.zIndex = '100';
				info.style.textAlign = 'center';
				container.appendChild( info );

246
				updateWorkers();
247 248 249 250 251

				//

				initScene( window.innerWidth, window.innerHeight );

252 253
				//

254 255
				renderer = new THREE.RaytracingRenderer( {
					workers: WORKERS,
256 257 258
					workerPath: 'js/renderers/RaytracingWorker.js',
					randomize: true,
					blockSize: 64
259
				} );
M
Mr.doob 已提交
260 261
				renderer.setSize( window.innerWidth, window.innerHeight );

262 263 264 265
				renderer.domElement.style.position = "absolute";
				renderer.domElement.style.top = "0px";
				renderer.domElement.style.left = "0px";

M
Mr.doob 已提交
266 267
				container.appendChild( renderer.domElement );

268

L
Lewy Blue 已提交
269
				window.addEventListener( 'resize', function () {
270 271 272 273 274

					renderer.setSize( innerWidth, innerHeight );

				} );

M
Mr.doob 已提交
275 276
			}

277 278 279 280 281 282 283 284 285
			function updateWorkers( x ) {

				if ( x ) {

					WORKERS = Math.max( 1, WORKERS + x );
					renderer.setWorkers( WORKERS );

				}

L
Lewy Blue 已提交
286
				info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js<a/> - raytracing renderer (using ' + WORKERS + ' <button onclick="updateWorkers(-1)">-</button><button onclick="updateWorkers(1)">+</button> web workers)' +
287 288 289 290 291
				'<br/><button onclick="rearrange()">Rearrange</button><button onclick="render()">Render</button>';


			}

L
Lewy Blue 已提交
292 293 294 295 296 297 298 299 300 301 302 303
			function rearrange() {

				group.children.forEach( function ( o ) {

 					o.position.y += ( Math.random() - 0.5 ) * 100;
					o.position.x += ( Math.random() - 0.5 ) * 400;
					o.position.z += ( Math.random() - 0.5 ) * 400;

				} );

 			}

M
Mr.doob 已提交
304 305 306 307 308 309
			function render() {

				renderer.render( scene, camera );

			}

310

M
Mr.doob 已提交
311 312 313 314
		</script>

	</body>
</html>