123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js - raytracing renderer with web workers</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - raytracing renderer (using <span id="workers"></span>
- <button id="removeWorker">-</button><button id="addWorker">+</button> web workers) <br/>
- <button id="rearrange">Rearrange</button><button id="render">Render</button>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { RaytracingRenderer } from './jsm/renderers/RaytracingRenderer.js';
- var hash = location.hash ? location.hash.substring( 1 ) : '3';
- var WORKERS = + hash || navigator.hardwareConcurrency || 3;
- var camera, scene, renderer;
- var group;
- init();
- render();
- function initScene( width, height ) {
- camera = new THREE.PerspectiveCamera( 60, width / height, 1, 1000 );
- camera.position.z = 600;
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xf0f0f0 );
- // materials
- var phongMaterial = new THREE.MeshPhongMaterial( {
- color: 0xffffff,
- specular: 0x222222,
- shininess: 150,
- vertexColors: THREE.NoColors,
- flatShading: false
- } );
- var phongMaterialBox = new THREE.MeshPhongMaterial( {
- color: 0xffffff,
- specular: 0x111111,
- shininess: 100,
- vertexColors: THREE.NoColors,
- flatShading: true
- } );
- var phongMaterialBoxBottom = new THREE.MeshPhongMaterial( {
- color: 0x666666,
- specular: 0x111111,
- shininess: 100,
- vertexColors: THREE.NoColors,
- flatShading: true
- } );
- var phongMaterialBoxLeft = new THREE.MeshPhongMaterial( {
- color: 0x990000,
- specular: 0x111111,
- shininess: 100,
- vertexColors: THREE.NoColors,
- flatShading: true
- } );
- var phongMaterialBoxRight = new THREE.MeshPhongMaterial( {
- color: 0x0066ff,
- specular: 0x111111,
- shininess: 100,
- vertexColors: THREE.NoColors,
- flatShading: true
- } );
- var mirrorMaterialFlat = new THREE.MeshPhongMaterial( {
- color: 0x000000,
- specular: 0xff8888,
- shininess: 10000,
- vertexColors: THREE.NoColors,
- flatShading: true
- } );
- mirrorMaterialFlat.mirror = true;
- mirrorMaterialFlat.reflectivity = 1;
- var mirrorMaterialFlatDark = new THREE.MeshPhongMaterial( {
- color: 0x000000,
- specular: 0xaaaaaa,
- shininess: 10000,
- vertexColors: THREE.NoColors,
- flatShading: true
- } );
- mirrorMaterialFlatDark.mirror = true;
- mirrorMaterialFlatDark.reflectivity = 1;
- var mirrorMaterialSmooth = new THREE.MeshPhongMaterial( {
- color: 0xffaa00,
- specular: 0x222222,
- shininess: 10000,
- vertexColors: THREE.NoColors,
- flatShading: false
- } );
- mirrorMaterialSmooth.mirror = true;
- mirrorMaterialSmooth.reflectivity = 0.3;
- var glassMaterialFlat = new THREE.MeshPhongMaterial( {
- color: 0x000000,
- specular: 0x00ff00,
- shininess: 10000,
- vertexColors: THREE.NoColors,
- flatShading: true
- } );
- glassMaterialFlat.glass = true;
- glassMaterialFlat.reflectivity = 0.5;
- var glassMaterialSmooth = new THREE.MeshPhongMaterial( {
- color: 0x000000,
- specular: 0xffaa55,
- shininess: 10000,
- vertexColors: THREE.NoColors,
- flatShading: false
- } );
- glassMaterialSmooth.glass = true;
- glassMaterialSmooth.reflectivity = 0.25;
- glassMaterialSmooth.refractionRatio = 0.6;
- //
- group = new THREE.Group();
- scene.add( group );
- // geometries
- var sphereGeometry = new THREE.SphereBufferGeometry( 100, 16, 8 );
- var planeGeometry = new THREE.BoxBufferGeometry( 600, 5, 600 );
- var boxGeometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
- // Sphere
- var sphere = new THREE.Mesh( sphereGeometry, phongMaterial );
- sphere.scale.multiplyScalar( 0.5 );
- sphere.position.set( - 50, - 250 + 5, - 50 );
- group.add( sphere );
- var sphere2 = new THREE.Mesh( sphereGeometry, mirrorMaterialSmooth );
- sphere2.scale.multiplyScalar( 0.5 );
- sphere2.position.set( 175, - 250 + 5, - 150 );
- group.add( sphere2 );
- // Box
- var box = new THREE.Mesh( boxGeometry, mirrorMaterialFlat );
- box.position.set( - 175, - 250 + 2.5, - 150 );
- box.rotation.y = 0.5;
- group.add( box );
- // Glass
- var glass = new THREE.Mesh( sphereGeometry, glassMaterialSmooth );
- glass.scale.multiplyScalar( 0.5 );
- glass.position.set( 75, - 250 + 5, - 75 );
- glass.rotation.y = 0.5;
- scene.add( glass );
- // bottom
- var plane = new THREE.Mesh( planeGeometry, phongMaterialBoxBottom );
- plane.position.set( 0, - 300 + 2.5, - 300 );
- scene.add( plane );
- // top
- var plane = new THREE.Mesh( planeGeometry, phongMaterialBox );
- plane.position.set( 0, 300 - 2.5, - 300 );
- scene.add( plane );
- // back
- var plane = new THREE.Mesh( planeGeometry, phongMaterialBox );
- plane.rotation.x = 1.57;
- plane.position.set( 0, 0, - 300 );
- scene.add( plane );
- var plane = new THREE.Mesh( planeGeometry, mirrorMaterialFlatDark );
- plane.rotation.x = 1.57;
- plane.position.set( 0, 0, - 300 + 10 );
- plane.scale.multiplyScalar( 0.85 );
- scene.add( plane );
- // left
- var plane = new THREE.Mesh( planeGeometry, phongMaterialBoxLeft );
- plane.rotation.z = 1.57;
- plane.position.set( - 300, 0, - 300 );
- scene.add( plane );
- // right
- var plane = new THREE.Mesh( planeGeometry, phongMaterialBoxRight );
- plane.rotation.z = 1.57;
- plane.position.set( 300, 0, - 300 );
- scene.add( plane );
- // light
- var intensity = 70000;
- var light = new THREE.PointLight( 0xffaa55, intensity );
- light.position.set( - 200, 100, 100 );
- light.physicalAttenuation = true;
- scene.add( light );
- var light = new THREE.PointLight( 0x55aaff, intensity );
- light.position.set( 200, 100, 100 );
- light.physicalAttenuation = true;
- scene.add( light );
- var light = new THREE.PointLight( 0xffffff, intensity * 1.5 );
- light.position.set( 0, 0, 300 );
- light.physicalAttenuation = true;
- scene.add( light );
- }
- function init() {
- var buttonRearrange = document.getElementById( 'rearrange' );
- buttonRearrange.addEventListener( 'click', rearrange );
- var buttonRender = document.getElementById( 'render' );
- buttonRender.addEventListener( 'click', render );
- var buttonRemoveWorker = document.getElementById( 'removeWorker' );
- buttonRemoveWorker.addEventListener( 'click', removeWorker );
- var buttonAddWorker = document.getElementById( 'addWorker' );
- buttonAddWorker.addEventListener( 'click', addWorker );
- updateWorkers();
- //
- initScene( window.innerWidth, window.innerHeight );
- //
- renderer = new RaytracingRenderer( {
- workers: WORKERS,
- workerPath: 'js/renderers/RaytracingWorker.js',
- randomize: true,
- blockSize: 64
- } );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- window.addEventListener( 'resize', function () {
- renderer.setSize( innerWidth, innerHeight );
- } );
- }
- function addWorker() {
- updateWorkers( 1 );
- }
- function removeWorker() {
- updateWorkers( - 1 );
- }
- function updateWorkers( x ) {
- if ( x ) {
- WORKERS = Math.max( 1, WORKERS + x );
- renderer.setWorkers( WORKERS );
- }
- var labelWorkers = document.getElementById( 'workers' );
- labelWorkers.textContent = WORKERS;
- }
- 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;
- } );
- }
- function render() {
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|