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