raytracing_sandbox.html 7.9 KB

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