webgl_shadowmap_omnidirectional.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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, 1, 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.shadowBias = 0.1;
  67. pointLight.name = 'Point Light';
  68. scene.add( pointLight );
  69. /*dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  70. dirLight.position.set( 0, 50, 0 );
  71. dirLight.castShadow = true;
  72. dirLight.shadowCameraNear = 1;
  73. dirLight.shadowCameraFar = 100;
  74. dirLight.shadowCameraRight = 15;
  75. dirLight.shadowCameraLeft = -15;
  76. dirLight.shadowCameraTop = 15;
  77. dirLight.shadowCameraBottom = -15;
  78. dirLight.shadowDarkness = 0.5;
  79. dirLight.shadowCameraVisible = true;
  80. dirLight.shadowMapWidth = 1024;
  81. dirLight.shadowMapHeight = 1024;
  82. dirLight.name = 'Dir. Light';
  83. scene.add( dirLight );*/
  84. cubeMaterial = new THREE.MeshPhongMaterial( {
  85. color: 0xff0000,
  86. shininess: 150,
  87. specular: 0x222222,
  88. shading: THREE.SmoothShading,
  89. } );
  90. var cubeGeometry = new THREE.BoxGeometry( 3, 3, 3 );
  91. cube = new THREE.Mesh( cubeGeometry, cubeMaterial );
  92. cube.name = "cube 1";
  93. cube.position.set( 8, 3, 6 );
  94. cube.castShadow = true;
  95. cube.receiveShadow = true;
  96. scene.add( cube );
  97. cube2 = new THREE.Mesh( cubeGeometry, cubeMaterial );
  98. cube2.name = "cube 2";
  99. cube2.position.set( -8, 3, 4 );
  100. cube2.castShadow = true;
  101. cube2.receiveShadow = true;
  102. scene.add( cube2 );
  103. cube3 = new THREE.Mesh( cubeGeometry, cubeMaterial );
  104. cube3.name = "cube 3";
  105. cube3.position.set( -4, 15, -6 );
  106. cube3.castShadow = true;
  107. cube3.receiveShadow = true;
  108. scene.add( cube3 );
  109. var torusGeometry = new THREE.TorusKnotGeometry( 25, 8, 75, 20 );
  110. torusKnot = new THREE.Mesh( torusGeometry, cubeMaterial );
  111. torusKnot.scale.multiplyScalar( 1 / 18 );
  112. torusKnot.position.set( -1, 3, -4 );
  113. torusKnot.castShadow = true;
  114. torusKnot.receiveShadow = true;
  115. scene.add( torusKnot );
  116. wallMaterial = new THREE.MeshPhongMaterial( {
  117. color: 0xa0adaf,
  118. shininess: 10,
  119. specular: 0x111111,
  120. shading: THREE.SmoothShading
  121. } );
  122. var wallGeometry = new THREE.BoxGeometry( 10, 0.15, 10 );
  123. ground = new THREE.Mesh( wallGeometry, wallMaterial );
  124. ground.name = "ground";
  125. ground.scale.multiplyScalar( 3 );
  126. ground.castShadow = false;
  127. ground.receiveShadow = true;
  128. scene.add( ground );
  129. ground.position.set( 0, -5, 0 );
  130. var ceiling = new THREE.Mesh( wallGeometry, wallMaterial );
  131. ceiling.name = "ceiling";
  132. ceiling.scale.multiplyScalar( 3 );
  133. ceiling.castShadow = false;
  134. ceiling.receiveShadow = true;
  135. scene.add( ceiling );
  136. ceiling.position.set( 0, 24, 0 );
  137. var wall = new THREE.Mesh( wallGeometry, wallMaterial );
  138. wall.name = "left wall";
  139. wall.scale.multiplyScalar( 3 );
  140. wall.castShadow = false;
  141. wall.receiveShadow = true;
  142. scene.add( wall );
  143. wall.position.set( -14, 10, 0 );
  144. wall.rotation.z = Math.PI / 2;
  145. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  146. wall.name = "right wall";
  147. wall.scale.multiplyScalar( 3 );
  148. wall.castShadow = false;
  149. wall.receiveShadow = true;
  150. scene.add( wall );
  151. wall.position.set(14,10,0);
  152. wall.rotation.z = Math.PI / 2;
  153. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  154. wall.name = "back wall";
  155. wall.scale.multiplyScalar( 3 );
  156. wall.castShadow = false;
  157. wall.receiveShadow = true;
  158. scene.add( wall );
  159. wall.position.set( 0, 10, -14 );
  160. wall.rotation.y = Math.PI / 2;
  161. wall.rotation.z = Math.PI / 2;
  162. /*wall = new THREE.Mesh( wallGeometry, wallMaterial );
  163. wall.name = "front wall";
  164. wall.scale.multiplyScalar( 3 );
  165. wall.castShadow = false;
  166. wall.receiveShadow = true;
  167. scene.add( wall );
  168. wall.position.set( 0, 10, 14 );
  169. wall.rotation.y = Math.PI / 2;
  170. wall.rotation.z = Math.PI / 2;*/
  171. var sphereGeometry = new THREE.SphereGeometry( 1, 32, 32 );
  172. var material = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  173. var sphere = new THREE.Mesh( sphereGeometry, material );
  174. sphere.castShadow = false;
  175. sphere.receiveShadow = false;
  176. sphere.position.set( 0, 11, 4 );
  177. scene.add( sphere );
  178. pointLightParent = new THREE.Object3D();
  179. pointLightParent.add( pointLight );
  180. pointLightParent.add( sphere );
  181. scene.add( pointLightParent );
  182. }
  183. function initMisc() {
  184. renderer = new THREE.WebGLRenderer();
  185. renderer.setSize( window.innerWidth, window.innerHeight );
  186. renderer.setClearColor( 0x000000 );
  187. renderer.shadowMap.enabled = true;
  188. renderer.shadowMap.type = THREE.BasicShadowMap;
  189. // Mouse control
  190. controls = new THREE.OrbitControls( camera, renderer.domElement );
  191. controls.target.set( 0, 2, 0 );
  192. controls.update();
  193. clock = new THREE.Clock();
  194. stats = new Stats();
  195. stats.domElement.style.position = 'absolute';
  196. stats.domElement.style.right = '0px';
  197. stats.domElement.style.top = '0px';
  198. document.body.appendChild( stats.domElement );
  199. }
  200. function onWindowResize() {
  201. camera.aspect = window.innerWidth / window.innerHeight;
  202. camera.updateProjectionMatrix();
  203. renderer.setSize( window.innerWidth, window.innerHeight );
  204. }
  205. function animate() {
  206. requestAnimationFrame( animate );
  207. render();
  208. stats.update();
  209. }
  210. function renderScene() {
  211. renderer.render( scene, camera );
  212. }
  213. function render() {
  214. var delta = clock.getDelta();
  215. pointLightParent.rotation.y += delta * 2;
  216. renderScene();
  217. cube.rotation.x += 0.25 * delta;
  218. cube.rotation.y += 2 * delta;
  219. cube.rotation.z += 1 * delta;
  220. cube2.rotation.x += 0.25 * delta;
  221. cube2.rotation.y += 2 * delta;
  222. cube2.rotation.z += 1 * delta;
  223. cube3.rotation.x += 0.25 * delta;
  224. cube3.rotation.y += 2 * delta;
  225. cube3.rotation.z += 1 * delta;
  226. }
  227. </script>
  228. </body>
  229. </html>