raytrace_scene.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. function initScene(width, height) {
  2. console.log('initScene', width, height);
  3. camera = new THREE.PerspectiveCamera( 60, width / height, 1, 1000 );
  4. camera.position.z = 600;
  5. scene = new THREE.Scene();
  6. // materials
  7. var phongMaterial = new THREE.MeshPhongMaterial( {
  8. color: 0xffffff,
  9. specular: 0x222222,
  10. shininess: 150,
  11. vertexColors: THREE.NoColors,
  12. shading: THREE.SmoothShading
  13. } );
  14. var phongMaterialBox = new THREE.MeshPhongMaterial( {
  15. color: 0xffffff,
  16. specular: 0x111111,
  17. shininess: 100,
  18. vertexColors: THREE.NoColors,
  19. shading: THREE.FlatShading
  20. } );
  21. var phongMaterialBoxBottom = new THREE.MeshPhongMaterial( {
  22. color: 0x666666,
  23. specular: 0x111111,
  24. shininess: 100,
  25. vertexColors: THREE.NoColors,
  26. shading: THREE.FlatShading
  27. } );
  28. var phongMaterialBoxLeft = new THREE.MeshPhongMaterial( {
  29. color: 0x990000,
  30. specular: 0x111111,
  31. shininess: 100,
  32. vertexColors: THREE.NoColors,
  33. shading: THREE.FlatShading
  34. } );
  35. var phongMaterialBoxRight = new THREE.MeshPhongMaterial( {
  36. color: 0x0066ff,
  37. specular: 0x111111,
  38. shininess: 100,
  39. vertexColors: THREE.NoColors,
  40. shading: THREE.FlatShading
  41. } );
  42. var mirrorMaterialFlat = new THREE.MeshPhongMaterial( {
  43. color: 0x000000,
  44. specular: 0xff8888,
  45. shininess: 10000,
  46. vertexColors: THREE.NoColors,
  47. shading: THREE.FlatShading
  48. } );
  49. mirrorMaterialFlat.mirror = true;
  50. mirrorMaterialFlat.reflectivity = 1;
  51. var mirrorMaterialFlatDark = new THREE.MeshPhongMaterial( {
  52. color: 0x000000,
  53. specular: 0xaaaaaa,
  54. shininess: 10000,
  55. vertexColors: THREE.NoColors,
  56. shading: THREE.FlatShading
  57. } );
  58. mirrorMaterialFlatDark.mirror = true;
  59. mirrorMaterialFlatDark.reflectivity = 1;
  60. var mirrorMaterialSmooth = new THREE.MeshPhongMaterial( {
  61. color: 0xffaa00,
  62. specular: 0x222222,
  63. shininess: 10000,
  64. vertexColors: THREE.NoColors,
  65. shading: THREE.SmoothShading
  66. } );
  67. mirrorMaterialSmooth.mirror = true;
  68. mirrorMaterialSmooth.reflectivity = 0.3;
  69. var glassMaterialFlat = new THREE.MeshPhongMaterial( {
  70. color: 0x000000,
  71. specular: 0x00ff00,
  72. shininess: 10000,
  73. vertexColors: THREE.NoColors,
  74. shading: THREE.FlatShading
  75. } );
  76. glassMaterialFlat.glass = true;
  77. glassMaterialFlat.reflectivity = 0.5;
  78. var glassMaterialSmooth = new THREE.MeshPhongMaterial( {
  79. color: 0x000000,
  80. specular: 0xffaa55,
  81. shininess: 10000,
  82. vertexColors: THREE.NoColors,
  83. shading: THREE.SmoothShading
  84. } );
  85. glassMaterialSmooth.glass = true;
  86. glassMaterialSmooth.reflectivity = 0.25;
  87. glassMaterialSmooth.refractionRatio = 0.6;
  88. // geometries
  89. var torusGeometry = new THREE.TorusKnotGeometry( 150 );
  90. var sphereGeometry = new THREE.SphereGeometry( 100, 16, 8 );
  91. var planeGeometry = new THREE.BoxGeometry( 600, 5, 600 );
  92. var boxGeometry = new THREE.BoxGeometry( 100, 100, 100 );
  93. // TorusKnot
  94. //torus = new THREE.Mesh( torusGeometry, phongMaterial );
  95. //scene.add( torus );
  96. // Sphere
  97. sphere = new THREE.Mesh( sphereGeometry, phongMaterial );
  98. sphere.scale.multiplyScalar( 0.5 );
  99. sphere.position.set( -50, -250+5, -50 );
  100. scene.add( sphere );
  101. sphere2 = new THREE.Mesh( sphereGeometry, mirrorMaterialSmooth );
  102. sphere2.scale.multiplyScalar( 0.5 );
  103. sphere2.position.set( 175, -250+5, -150 );
  104. scene.add( sphere2 );
  105. // Box
  106. box = new THREE.Mesh( boxGeometry, mirrorMaterialFlat );
  107. box.position.set( -175, -250+2.5, -150 );
  108. box.rotation.y = 0.5;
  109. scene.add( box );
  110. // Glass
  111. glass = new THREE.Mesh( sphereGeometry, glassMaterialSmooth );
  112. glass.scale.multiplyScalar( 0.5 );
  113. glass.position.set( 75, -250+5, -75 );
  114. glass.rotation.y = 0.5;
  115. scene.add( glass );
  116. // bottom
  117. plane = new THREE.Mesh( planeGeometry, phongMaterialBoxBottom );
  118. plane.position.set( 0, -300+2.5, -300 );
  119. scene.add( plane );
  120. // top
  121. plane = new THREE.Mesh( planeGeometry, phongMaterialBox );
  122. plane.position.set( 0, 300-2.5, -300 );
  123. scene.add( plane );
  124. // back
  125. plane = new THREE.Mesh( planeGeometry, phongMaterialBox );
  126. plane.rotation.x = 1.57;
  127. plane.position.set( 0, 0, -300 );
  128. scene.add( plane );
  129. plane = new THREE.Mesh( planeGeometry, mirrorMaterialFlatDark );
  130. plane.rotation.x = 1.57;
  131. plane.position.set( 0, 0, -300+10 );
  132. plane.scale.multiplyScalar( 0.85 );
  133. scene.add( plane );
  134. // left
  135. plane = new THREE.Mesh( planeGeometry, phongMaterialBoxLeft );
  136. plane.rotation.z = 1.57;
  137. plane.position.set( -300, 0, -300 );
  138. scene.add( plane );
  139. // right
  140. plane = new THREE.Mesh( planeGeometry, phongMaterialBoxRight );
  141. plane.rotation.z = 1.57;
  142. plane.position.set( 300, 0, -300 );
  143. scene.add( plane );
  144. // light
  145. var intensity = 70000;
  146. var light = new THREE.PointLight( 0xffaa55, intensity );
  147. light.position.set( -200, 100, 100 );
  148. light.physicalAttenuation = true;
  149. scene.add( light );
  150. var light = new THREE.PointLight( 0x55aaff, intensity );
  151. light.position.set( 200, 100, 100 );
  152. light.physicalAttenuation = true;
  153. scene.add( light );
  154. var light = new THREE.PointLight( 0xffffff, intensity * 1.5 );
  155. light.position.set( 0, 0, 300 );
  156. light.physicalAttenuation = true;
  157. scene.add( light );
  158. renderer = new THREE.RaytracingRenderer();
  159. renderer.setSize( width, height );
  160. }