webgl_shadowmap_omnidirectional.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - Omni-directional Shadow map viewer example </title>
  5. <meta charset="utf-8">
  6. <style>
  7. body {
  8. font-family: Monospace;
  9. background-color: #000;
  10. color: #fff;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. position: absolute;
  16. top: 10px;
  17. width: 100%;
  18. text-align: center;
  19. z-index: 100;
  20. display:block;
  21. }
  22. #info a { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="info">
  27. <a href="http://threejs.org" target="_blank">three.js</a> - Omni-directional Shadow map viewer example by <a href="https://github.com/mkkellogg">mkkellogg</a>
  28. </div>
  29. <script src="../build/three.min.js"></script>
  30. <script src="js/controls/OrbitControls.js"></script>
  31. <script src="js/Detector.js"></script>
  32. <script src="js/libs/stats.min.js"></script>
  33. <script>
  34. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  35. var camera, scene, renderer, clock, stats;
  36. var dirLight, pointLight;
  37. var pointLightParent;
  38. var torusKnot, cube, cube2, cube3, cube4;
  39. var cubeMaterial;
  40. var wallMaterial;
  41. var ground;
  42. init();
  43. animate();
  44. function init() {
  45. initScene();
  46. initMisc();
  47. document.body.appendChild( renderer.domElement );
  48. window.addEventListener( 'resize', onWindowResize, false );
  49. }
  50. function initScene() {
  51. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.01, 1000 );
  52. camera.position.set( 0, 15, 35 );
  53. scene = new THREE.Scene();
  54. // Lights
  55. var ambient = new THREE.AmbientLight( 0x404040 );
  56. scene.add( ambient );
  57. pointLight = new THREE.PointLight( 0xffffff );
  58. pointLight.position.set( 0, 11, 4 );
  59. pointLight.castShadow = true;
  60. pointLight.shadowCameraNear = 1;
  61. pointLight.shadowCameraFar = 30;
  62. pointLight.shadowDarkness = 0.5;
  63. pointLight.shadowCameraVisible = true;
  64. pointLight.shadowMapWidth = 2048;
  65. pointLight.shadowMapHeight = 1024;
  66. pointLight.name = 'Point Light';
  67. scene.add( pointLight );
  68. /*dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  69. dirLight.position.set( 0, 50, 0 );
  70. dirLight.castShadow = true;
  71. dirLight.shadowCameraNear = 0.01;
  72. dirLight.shadowCameraFar = 100;
  73. dirLight.shadowCameraRight = 15;
  74. dirLight.shadowCameraLeft = -15;
  75. dirLight.shadowCameraTop = 15;
  76. dirLight.shadowCameraBottom = -15;
  77. dirLight.shadowDarkness = 0.5;
  78. dirLight.shadowCameraVisible = true;
  79. dirLight.shadowMapWidth = 1024;
  80. dirLight.shadowMapHeight = 1024;
  81. dirLight.name = 'Dir. Light';
  82. scene.add( dirLight );*/
  83. cubeMaterial = new THREE.MeshPhongMaterial( {
  84. color: 0xff0000,
  85. shininess: 150,
  86. specular: 0x222222,
  87. shading: THREE.SmoothShading,
  88. } );
  89. var cubeGeometry = new THREE.BoxGeometry( 3, 3, 3 );
  90. cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
  91. cube.name = "cube 1";
  92. cube.position.set( 8, 3, 6 );
  93. cube.castShadow = true;
  94. cube.receiveShadow = true;
  95. scene.add( cube );
  96. cube2 = new THREE.Mesh( cubeGeometry, cubeMaterial );
  97. cube2.name = "cube 2";
  98. cube2.position.set( -8, 3, 4 );
  99. cube2.castShadow = true;
  100. cube2.receiveShadow = true;
  101. scene.add( cube2 );
  102. cube3 = new THREE.Mesh( cubeGeometry, cubeMaterial );
  103. cube3.name = "cube 3";
  104. cube3.position.set( -4, 15, -6 );
  105. cube3.castShadow = true;
  106. cube3.receiveShadow = true;
  107. scene.add( cube3 );
  108. var torusGeometry = new THREE.TorusKnotGeometry( 25, 8, 75, 20 );
  109. torusKnot = new THREE.Mesh( torusGeometry, cubeMaterial );
  110. torusKnot.scale.multiplyScalar( 1 / 18 );
  111. torusKnot.position.set( -1, 3, -4 );
  112. torusKnot.castShadow = true;
  113. torusKnot.receiveShadow = true;
  114. scene.add( torusKnot );
  115. wallMaterial = new THREE.MeshPhongMaterial( {
  116. color: 0xa0adaf,
  117. shininess: 10,
  118. specular: 0x111111,
  119. shading: THREE.SmoothShading
  120. } );
  121. var wallGeometry = new THREE.BoxGeometry( 10, 0.15, 10 );
  122. ground = new THREE.Mesh( wallGeometry, wallMaterial );
  123. ground.name = "ground";
  124. ground.scale.multiplyScalar( 3 );
  125. ground.castShadow = false;
  126. ground.receiveShadow = true;
  127. scene.add( ground );
  128. ground.position.set( 0, -5, 0 );
  129. var ceiling = new THREE.Mesh( wallGeometry, wallMaterial );
  130. ceiling.name = "ceiling";
  131. ceiling.scale.multiplyScalar( 3 );
  132. ceiling.castShadow = false;
  133. ceiling.receiveShadow = true;
  134. scene.add( ceiling );
  135. ceiling.position.set( 0, 24, 0 );
  136. var wall = new THREE.Mesh( wallGeometry, wallMaterial );
  137. wall.name = "left wall";
  138. wall.scale.multiplyScalar( 3 );
  139. wall.castShadow = false;
  140. wall.receiveShadow = true;
  141. scene.add( wall );
  142. wall.position.set( -14, 10, 0 );
  143. wall.rotation.z = Math.PI / 2;
  144. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  145. wall.name = "right wall";
  146. wall.scale.multiplyScalar( 3 );
  147. wall.castShadow = false;
  148. wall.receiveShadow = true;
  149. scene.add( wall );
  150. wall.position.set(14,10,0);
  151. wall.rotation.z = Math.PI / 2;
  152. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  153. wall.name = "back wall";
  154. wall.scale.multiplyScalar( 3 );
  155. wall.castShadow = false;
  156. wall.receiveShadow = true;
  157. scene.add( wall );
  158. wall.position.set( 0, 10, -14 );
  159. wall.rotation.y = Math.PI / 2;
  160. wall.rotation.z = Math.PI / 2;
  161. /*wall = new THREE.Mesh( wallGeometry, wallMaterial );
  162. wall.name = "front wall";
  163. wall.scale.multiplyScalar( 3 );
  164. wall.castShadow = false;
  165. wall.receiveShadow = true;
  166. scene.add( wall );
  167. wall.position.set( 0, 10, 14 );
  168. wall.rotation.y = Math.PI / 2;
  169. wall.rotation.z = Math.PI / 2;*/
  170. var sphereGeometry = new THREE.SphereGeometry( 1, 32, 32 );
  171. var material = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  172. var sphere = new THREE.Mesh( sphereGeometry, material );
  173. sphere.castShadow = false;
  174. sphere.receiveShadow = false;
  175. sphere.position.set( 0, 11, 4 );
  176. scene.add( sphere );
  177. pointLightParent = new THREE.Object3D();
  178. pointLightParent.add( pointLight );
  179. pointLightParent.add( sphere );
  180. scene.add( pointLightParent );
  181. }
  182. function initMisc() {
  183. renderer = new THREE.WebGLRenderer();
  184. renderer.setSize( window.innerWidth, window.innerHeight );
  185. renderer.setClearColor( 0x000000 );
  186. renderer.shadowMap.enabled = true;
  187. renderer.shadowMap.type = THREE.PCFShadowMap;
  188. // Mouse control
  189. controls = new THREE.OrbitControls( camera, renderer.domElement );
  190. controls.target.set( 0, 2, 0 );
  191. controls.update();
  192. clock = new THREE.Clock();
  193. stats = new Stats();
  194. stats.domElement.style.position = 'absolute';
  195. stats.domElement.style.right = '0px';
  196. stats.domElement.style.top = '0px';
  197. document.body.appendChild( stats.domElement );
  198. }
  199. function onWindowResize() {
  200. camera.aspect = window.innerWidth / window.innerHeight;
  201. camera.updateProjectionMatrix();
  202. renderer.setSize( window.innerWidth, window.innerHeight );
  203. }
  204. function animate() {
  205. requestAnimationFrame( animate );
  206. render();
  207. stats.update();
  208. }
  209. function renderScene() {
  210. renderer.render( scene, camera );
  211. }
  212. function render() {
  213. var delta = clock.getDelta();
  214. pointLightParent.rotation.y += delta * 2;
  215. renderScene();
  216. cube.rotation.x += 0.25 * delta;
  217. cube.rotation.y += 2 * delta;
  218. cube.rotation.z += 1 * delta;
  219. cube2.rotation.x += 0.25 * delta;
  220. cube2.rotation.y += 2 * delta;
  221. cube2.rotation.z += 1 * delta;
  222. cube3.rotation.x += 0.25 * delta;
  223. cube3.rotation.y += 2 * delta;
  224. cube3.rotation.z += 1 * delta;
  225. }
  226. </script>
  227. </body>
  228. </html>