raytracing_sandbox.html 7.3 KB

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