raytracing_sandbox_workers.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.min.js"></script>
  18. <script src="js/renderers/RaytracingWorkerRenderer.js"></script>
  19. <script>
  20. var hash = location.hash ? location.hash.substring(1) : '4';
  21. var WORKERS = +hash || navigator.hardwareConcurrency || 4;
  22. var container;
  23. var camera, controls, scene, renderer;
  24. var torus, cube;
  25. init();
  26. render();
  27. // this will be converted to a string to be passed to the workers
  28. function initScene(width, height) {
  29. console.log('initScene', width, height);
  30. camera = new THREE.PerspectiveCamera( 60, width / height, 1, 1000 );
  31. camera.position.z = 600;
  32. scene = new THREE.Scene();
  33. // materials
  34. var phongMaterial = new THREE.MeshPhongMaterial( {
  35. color: 0xffffff,
  36. specular: 0x222222,
  37. shininess: 150,
  38. vertexColors: THREE.NoColors,
  39. shading: THREE.SmoothShading
  40. } );
  41. var phongMaterialBox = new THREE.MeshPhongMaterial( {
  42. color: 0xffffff,
  43. specular: 0x111111,
  44. shininess: 100,
  45. vertexColors: THREE.NoColors,
  46. shading: THREE.FlatShading
  47. } );
  48. var phongMaterialBoxBottom = new THREE.MeshPhongMaterial( {
  49. color: 0x666666,
  50. specular: 0x111111,
  51. shininess: 100,
  52. vertexColors: THREE.NoColors,
  53. shading: THREE.FlatShading
  54. } );
  55. var phongMaterialBoxLeft = new THREE.MeshPhongMaterial( {
  56. color: 0x990000,
  57. specular: 0x111111,
  58. shininess: 100,
  59. vertexColors: THREE.NoColors,
  60. shading: THREE.FlatShading
  61. } );
  62. var phongMaterialBoxRight = new THREE.MeshPhongMaterial( {
  63. color: 0x0066ff,
  64. specular: 0x111111,
  65. shininess: 100,
  66. vertexColors: THREE.NoColors,
  67. shading: THREE.FlatShading
  68. } );
  69. var mirrorMaterialFlat = new THREE.MeshPhongMaterial( {
  70. color: 0x000000,
  71. specular: 0xff8888,
  72. shininess: 10000,
  73. vertexColors: THREE.NoColors,
  74. shading: THREE.FlatShading
  75. } );
  76. mirrorMaterialFlat.mirror = true;
  77. mirrorMaterialFlat.reflectivity = 1;
  78. var mirrorMaterialFlatDark = new THREE.MeshPhongMaterial( {
  79. color: 0x000000,
  80. specular: 0xaaaaaa,
  81. shininess: 10000,
  82. vertexColors: THREE.NoColors,
  83. shading: THREE.FlatShading
  84. } );
  85. mirrorMaterialFlatDark.mirror = true;
  86. mirrorMaterialFlatDark.reflectivity = 1;
  87. var mirrorMaterialSmooth = new THREE.MeshPhongMaterial( {
  88. color: 0xffaa00,
  89. specular: 0x222222,
  90. shininess: 10000,
  91. vertexColors: THREE.NoColors,
  92. shading: THREE.SmoothShading
  93. } );
  94. mirrorMaterialSmooth.mirror = true;
  95. mirrorMaterialSmooth.reflectivity = 0.3;
  96. var glassMaterialFlat = new THREE.MeshPhongMaterial( {
  97. color: 0x000000,
  98. specular: 0x00ff00,
  99. shininess: 10000,
  100. vertexColors: THREE.NoColors,
  101. shading: THREE.FlatShading
  102. } );
  103. glassMaterialFlat.glass = true;
  104. glassMaterialFlat.reflectivity = 0.5;
  105. var glassMaterialSmooth = new THREE.MeshPhongMaterial( {
  106. color: 0x000000,
  107. specular: 0xffaa55,
  108. shininess: 10000,
  109. vertexColors: THREE.NoColors,
  110. shading: THREE.SmoothShading
  111. } );
  112. glassMaterialSmooth.glass = true;
  113. glassMaterialSmooth.reflectivity = 0.25;
  114. glassMaterialSmooth.refractionRatio = 0.6;
  115. // geometries
  116. var torusGeometry = new THREE.TorusKnotGeometry( 150 );
  117. var sphereGeometry = new THREE.SphereGeometry( 100, 16, 8 );
  118. var planeGeometry = new THREE.BoxGeometry( 600, 5, 600 );
  119. var boxGeometry = new THREE.BoxGeometry( 100, 100, 100 );
  120. // TorusKnot
  121. //torus = new THREE.Mesh( torusGeometry, phongMaterial );
  122. //scene.add( torus );
  123. // Sphere
  124. sphere = new THREE.Mesh( sphereGeometry, phongMaterial );
  125. sphere.scale.multiplyScalar( 0.5 );
  126. sphere.position.set( -50, -250+5, -50 );
  127. scene.add( sphere );
  128. sphere2 = new THREE.Mesh( sphereGeometry, mirrorMaterialSmooth );
  129. sphere2.scale.multiplyScalar( 0.5 );
  130. sphere2.position.set( 175, -250+5, -150 );
  131. scene.add( 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. // Glass
  138. glass = new THREE.Mesh( sphereGeometry, glassMaterialSmooth );
  139. glass.scale.multiplyScalar( 0.5 );
  140. glass.position.set( 75, -250+5, -75 );
  141. glass.rotation.y = 0.5;
  142. scene.add( glass );
  143. // bottom
  144. plane = new THREE.Mesh( planeGeometry, phongMaterialBoxBottom );
  145. plane.position.set( 0, -300+2.5, -300 );
  146. scene.add( plane );
  147. // top
  148. plane = new THREE.Mesh( planeGeometry, phongMaterialBox );
  149. plane.position.set( 0, 300-2.5, -300 );
  150. scene.add( plane );
  151. // back
  152. plane = new THREE.Mesh( planeGeometry, phongMaterialBox );
  153. plane.rotation.x = 1.57;
  154. plane.position.set( 0, 0, -300 );
  155. scene.add( plane );
  156. plane = new THREE.Mesh( planeGeometry, mirrorMaterialFlatDark );
  157. plane.rotation.x = 1.57;
  158. plane.position.set( 0, 0, -300+10 );
  159. plane.scale.multiplyScalar( 0.85 );
  160. scene.add( plane );
  161. // left
  162. plane = new THREE.Mesh( planeGeometry, phongMaterialBoxLeft );
  163. plane.rotation.z = 1.57;
  164. plane.position.set( -300, 0, -300 );
  165. scene.add( plane );
  166. // right
  167. plane = new THREE.Mesh( planeGeometry, phongMaterialBoxRight );
  168. plane.rotation.z = 1.57;
  169. plane.position.set( 300, 0, -300 );
  170. scene.add( plane );
  171. // light
  172. var intensity = 70000;
  173. var light = new THREE.PointLight( 0xffaa55, intensity );
  174. light.position.set( -200, 100, 100 );
  175. light.physicalAttenuation = true;
  176. scene.add( light );
  177. var light = new THREE.PointLight( 0x55aaff, intensity );
  178. light.position.set( 200, 100, 100 );
  179. light.physicalAttenuation = true;
  180. scene.add( light );
  181. var light = new THREE.PointLight( 0xffffff, intensity * 1.5 );
  182. light.position.set( 0, 0, 300 );
  183. light.physicalAttenuation = true;
  184. scene.add( light );
  185. renderer = new THREE.RaytracingRenderer();
  186. renderer.setSize( width, height );
  187. }
  188. function init() {
  189. container = document.createElement( 'div' );
  190. document.body.appendChild( container );
  191. var info = document.createElement( 'div' );
  192. info.style.position = 'absolute';
  193. info.style.top = '10px';
  194. info.style.width = '100%';
  195. info.style.zIndex = '100';
  196. info.style.textAlign = 'center';
  197. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js<a/> - raytracing renderer (using ' + WORKERS + ' web workers)';
  198. container.appendChild( info );
  199. //
  200. renderer = new THREE.RaytracingWorkerRenderer({
  201. workers: WORKERS
  202. });
  203. renderer.setClearColor( 0xf0f0f0 );
  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. }
  210. function render() {
  211. renderer.render( scene, camera );
  212. }
  213. </script>
  214. </body>
  215. </html>