raytracing_sandbox.html 7.7 KB

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