raytracing_sandbox.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - raytracing renderer with web workers</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - raytracing renderer (using <span id="workers"></span>
  12. <button id="removeWorker">-</button><button id="addWorker">+</button> web workers) <br/>
  13. <button id="rearrange">Rearrange</button><button id="render">Render</button>
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import { RaytracingRenderer } from './jsm/renderers/RaytracingRenderer.js';
  18. var hash = location.hash ? location.hash.substring( 1 ) : '3';
  19. var WORKERS = + hash || navigator.hardwareConcurrency || 3;
  20. var camera, scene, renderer;
  21. var group;
  22. init();
  23. render();
  24. function initScene( width, height ) {
  25. camera = new THREE.PerspectiveCamera( 60, width / height, 1, 1000 );
  26. camera.position.z = 600;
  27. scene = new THREE.Scene();
  28. scene.background = new THREE.Color( 0xf0f0f0 );
  29. // materials
  30. var phongMaterial = new THREE.MeshPhongMaterial( {
  31. color: 0xffffff,
  32. specular: 0x222222,
  33. shininess: 150,
  34. vertexColors: THREE.NoColors,
  35. flatShading: false
  36. } );
  37. var phongMaterialBox = new THREE.MeshPhongMaterial( {
  38. color: 0xffffff,
  39. specular: 0x111111,
  40. shininess: 100,
  41. vertexColors: THREE.NoColors,
  42. flatShading: true
  43. } );
  44. var phongMaterialBoxBottom = new THREE.MeshPhongMaterial( {
  45. color: 0x666666,
  46. specular: 0x111111,
  47. shininess: 100,
  48. vertexColors: THREE.NoColors,
  49. flatShading: true
  50. } );
  51. var phongMaterialBoxLeft = new THREE.MeshPhongMaterial( {
  52. color: 0x990000,
  53. specular: 0x111111,
  54. shininess: 100,
  55. vertexColors: THREE.NoColors,
  56. flatShading: true
  57. } );
  58. var phongMaterialBoxRight = new THREE.MeshPhongMaterial( {
  59. color: 0x0066ff,
  60. specular: 0x111111,
  61. shininess: 100,
  62. vertexColors: THREE.NoColors,
  63. flatShading: true
  64. } );
  65. var mirrorMaterialFlat = new THREE.MeshPhongMaterial( {
  66. color: 0x000000,
  67. specular: 0xff8888,
  68. shininess: 10000,
  69. vertexColors: THREE.NoColors,
  70. flatShading: true
  71. } );
  72. mirrorMaterialFlat.mirror = true;
  73. mirrorMaterialFlat.reflectivity = 1;
  74. var mirrorMaterialFlatDark = new THREE.MeshPhongMaterial( {
  75. color: 0x000000,
  76. specular: 0xaaaaaa,
  77. shininess: 10000,
  78. vertexColors: THREE.NoColors,
  79. flatShading: true
  80. } );
  81. mirrorMaterialFlatDark.mirror = true;
  82. mirrorMaterialFlatDark.reflectivity = 1;
  83. var mirrorMaterialSmooth = new THREE.MeshPhongMaterial( {
  84. color: 0xffaa00,
  85. specular: 0x222222,
  86. shininess: 10000,
  87. vertexColors: THREE.NoColors,
  88. flatShading: false
  89. } );
  90. mirrorMaterialSmooth.mirror = true;
  91. mirrorMaterialSmooth.reflectivity = 0.3;
  92. var glassMaterialFlat = new THREE.MeshPhongMaterial( {
  93. color: 0x000000,
  94. specular: 0x00ff00,
  95. shininess: 10000,
  96. vertexColors: THREE.NoColors,
  97. flatShading: true
  98. } );
  99. glassMaterialFlat.glass = true;
  100. glassMaterialFlat.reflectivity = 0.5;
  101. var glassMaterialSmooth = new THREE.MeshPhongMaterial( {
  102. color: 0x000000,
  103. specular: 0xffaa55,
  104. shininess: 10000,
  105. vertexColors: THREE.NoColors,
  106. flatShading: false
  107. } );
  108. glassMaterialSmooth.glass = true;
  109. glassMaterialSmooth.reflectivity = 0.25;
  110. glassMaterialSmooth.refractionRatio = 0.6;
  111. //
  112. group = new THREE.Group();
  113. scene.add( group );
  114. // geometries
  115. var sphereGeometry = new THREE.SphereBufferGeometry( 100, 16, 8 );
  116. var planeGeometry = new THREE.BoxBufferGeometry( 600, 5, 600 );
  117. var boxGeometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
  118. // Sphere
  119. var sphere = new THREE.Mesh( sphereGeometry, phongMaterial );
  120. sphere.scale.multiplyScalar( 0.5 );
  121. sphere.position.set( - 50, - 250 + 5, - 50 );
  122. group.add( sphere );
  123. var sphere2 = new THREE.Mesh( sphereGeometry, mirrorMaterialSmooth );
  124. sphere2.scale.multiplyScalar( 0.5 );
  125. sphere2.position.set( 175, - 250 + 5, - 150 );
  126. group.add( sphere2 );
  127. // Box
  128. var box = new THREE.Mesh( boxGeometry, mirrorMaterialFlat );
  129. box.position.set( - 175, - 250 + 2.5, - 150 );
  130. box.rotation.y = 0.5;
  131. group.add( box );
  132. // Glass
  133. var glass = new THREE.Mesh( sphereGeometry, glassMaterialSmooth );
  134. glass.scale.multiplyScalar( 0.5 );
  135. glass.position.set( 75, - 250 + 5, - 75 );
  136. glass.rotation.y = 0.5;
  137. scene.add( glass );
  138. // bottom
  139. var plane = new THREE.Mesh( planeGeometry, phongMaterialBoxBottom );
  140. plane.position.set( 0, - 300 + 2.5, - 300 );
  141. scene.add( plane );
  142. // top
  143. var plane = new THREE.Mesh( planeGeometry, phongMaterialBox );
  144. plane.position.set( 0, 300 - 2.5, - 300 );
  145. scene.add( plane );
  146. // back
  147. var plane = new THREE.Mesh( planeGeometry, phongMaterialBox );
  148. plane.rotation.x = 1.57;
  149. plane.position.set( 0, 0, - 300 );
  150. scene.add( plane );
  151. var plane = new THREE.Mesh( planeGeometry, mirrorMaterialFlatDark );
  152. plane.rotation.x = 1.57;
  153. plane.position.set( 0, 0, - 300 + 10 );
  154. plane.scale.multiplyScalar( 0.85 );
  155. scene.add( plane );
  156. // left
  157. var plane = new THREE.Mesh( planeGeometry, phongMaterialBoxLeft );
  158. plane.rotation.z = 1.57;
  159. plane.position.set( - 300, 0, - 300 );
  160. scene.add( plane );
  161. // right
  162. var plane = new THREE.Mesh( planeGeometry, phongMaterialBoxRight );
  163. plane.rotation.z = 1.57;
  164. plane.position.set( 300, 0, - 300 );
  165. scene.add( plane );
  166. // light
  167. var intensity = 70000;
  168. var light = new THREE.PointLight( 0xffaa55, intensity );
  169. light.position.set( - 200, 100, 100 );
  170. light.physicalAttenuation = true;
  171. scene.add( light );
  172. var light = new THREE.PointLight( 0x55aaff, intensity );
  173. light.position.set( 200, 100, 100 );
  174. light.physicalAttenuation = true;
  175. scene.add( light );
  176. var light = new THREE.PointLight( 0xffffff, intensity * 1.5 );
  177. light.position.set( 0, 0, 300 );
  178. light.physicalAttenuation = true;
  179. scene.add( light );
  180. }
  181. function init() {
  182. var buttonRearrange = document.getElementById( 'rearrange' );
  183. buttonRearrange.addEventListener( 'click', rearrange );
  184. var buttonRender = document.getElementById( 'render' );
  185. buttonRender.addEventListener( 'click', render );
  186. var buttonRemoveWorker = document.getElementById( 'removeWorker' );
  187. buttonRemoveWorker.addEventListener( 'click', removeWorker );
  188. var buttonAddWorker = document.getElementById( 'addWorker' );
  189. buttonAddWorker.addEventListener( 'click', addWorker );
  190. updateWorkers();
  191. //
  192. initScene( window.innerWidth, window.innerHeight );
  193. //
  194. renderer = new RaytracingRenderer( {
  195. workers: WORKERS,
  196. workerPath: 'js/renderers/RaytracingWorker.js',
  197. randomize: true,
  198. blockSize: 64
  199. } );
  200. renderer.setSize( window.innerWidth, window.innerHeight );
  201. document.body.appendChild( renderer.domElement );
  202. window.addEventListener( 'resize', function () {
  203. renderer.setSize( innerWidth, innerHeight );
  204. } );
  205. }
  206. function addWorker() {
  207. updateWorkers( 1 );
  208. }
  209. function removeWorker() {
  210. updateWorkers( - 1 );
  211. }
  212. function updateWorkers( x ) {
  213. if ( x ) {
  214. WORKERS = Math.max( 1, WORKERS + x );
  215. renderer.setWorkers( WORKERS );
  216. }
  217. var labelWorkers = document.getElementById( 'workers' );
  218. labelWorkers.textContent = WORKERS;
  219. }
  220. function rearrange() {
  221. group.children.forEach( function ( o ) {
  222. o.position.y += ( Math.random() - 0.5 ) * 100;
  223. o.position.x += ( Math.random() - 0.5 ) * 400;
  224. o.position.z += ( Math.random() - 0.5 ) * 400;
  225. } );
  226. }
  227. function render() {
  228. renderer.render( scene, camera );
  229. }
  230. </script>
  231. </body>
  232. </html>