webgl_shadowmap.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - shadow map</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. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. #info a { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="http://threejs.org" target="_blank">three.js</a> - shadowmap - models by <a href="http://mirada.com/">mirada</a> from <a href="http://ro.me">rome</a></br>
  29. move camera with WASD / RF + mouse<br/>
  30. t: toggle HUD</a>
  31. </div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/controls/FirstPersonControls.js"></script>
  34. <script src="js/shaders/UnpackDepthRGBAShader.js"></script>
  35. <script src="js/utils/ShadowMapViewer.js"></script>
  36. <script src="js/Detector.js"></script>
  37. <script src="js/libs/stats.min.js"></script>
  38. <script>
  39. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  40. var SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 1024;
  41. var HUD_MARGIN = 0.05;
  42. var SCREEN_WIDTH = window.innerWidth;
  43. var SCREEN_HEIGHT = window.innerHeight;
  44. var FLOOR = - 250;
  45. var camera, controls, scene, renderer;
  46. var container, stats;
  47. var NEAR = 10, FAR = 3000;
  48. var mixer, morphs = [];
  49. var light;
  50. var lightShadowMapViewer;
  51. var clock = new THREE.Clock();
  52. var showHUD = false;
  53. init();
  54. animate();
  55. function init() {
  56. container = document.createElement( 'div' );
  57. document.body.appendChild( container );
  58. // SCENE CAMERA
  59. camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
  60. camera.position.set( 700, 50, 1900 );
  61. controls = new THREE.FirstPersonControls( camera );
  62. controls.lookSpeed = 0.0125;
  63. controls.movementSpeed = 500;
  64. controls.noFly = false;
  65. controls.lookVertical = true;
  66. controls.constrainVertical = true;
  67. controls.verticalMin = 1.5;
  68. controls.verticalMax = 2.0;
  69. controls.lon = 250;
  70. controls.lat = 30;
  71. // SCENE
  72. scene = new THREE.Scene();
  73. scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
  74. // LIGHTS
  75. var ambient = new THREE.AmbientLight( 0x444444 );
  76. scene.add( ambient );
  77. light = new THREE.SpotLight( 0xffffff, 1, 0, Math.PI / 2 );
  78. light.position.set( 0, 1500, 1000 );
  79. light.target.position.set( 0, 0, 0 );
  80. light.castShadow = true;
  81. light.shadow = new THREE.LightShadow( new THREE.PerspectiveCamera( 50, 1, 1200, 2500 ) );
  82. light.shadow.bias = 0.0001;
  83. light.shadow.mapSize.width = SHADOW_MAP_WIDTH;
  84. light.shadow.mapSize.height = SHADOW_MAP_HEIGHT;
  85. scene.add( light );
  86. createHUD();
  87. createScene();
  88. // RENDERER
  89. renderer = new THREE.WebGLRenderer( { antialias: true } );
  90. renderer.setClearColor( scene.fog.color );
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  93. container.appendChild( renderer.domElement );
  94. renderer.autoClear = false;
  95. //
  96. renderer.shadowMap.enabled = true;
  97. renderer.shadowMap.type = THREE.PCFShadowMap;
  98. // STATS
  99. stats = new Stats();
  100. //container.appendChild( stats.dom );
  101. //
  102. window.addEventListener( 'resize', onWindowResize, false );
  103. window.addEventListener( 'keydown', onKeyDown, false );
  104. }
  105. function onWindowResize() {
  106. SCREEN_WIDTH = window.innerWidth;
  107. SCREEN_HEIGHT = window.innerHeight;
  108. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  111. controls.handleResize();
  112. }
  113. function onKeyDown ( event ) {
  114. switch ( event.keyCode ) {
  115. case 84: /*t*/
  116. showHUD = ! showHUD;
  117. break;
  118. }
  119. }
  120. function createHUD() {
  121. lightShadowMapViewer = new THREE.ShadowMapViewer( light );
  122. lightShadowMapViewer.position.x = 10;
  123. lightShadowMapViewer.position.y = SCREEN_HEIGHT - ( SHADOW_MAP_HEIGHT / 4 ) - 10;
  124. lightShadowMapViewer.size.width = SHADOW_MAP_WIDTH / 4;
  125. lightShadowMapViewer.size.height = SHADOW_MAP_HEIGHT / 4;
  126. lightShadowMapViewer.update();
  127. }
  128. function createScene( ) {
  129. // GROUND
  130. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  131. var planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffdd99 } );
  132. var ground = new THREE.Mesh( geometry, planeMaterial );
  133. ground.position.set( 0, FLOOR, 0 );
  134. ground.rotation.x = - Math.PI / 2;
  135. ground.scale.set( 100, 100, 100 );
  136. ground.castShadow = false;
  137. ground.receiveShadow = true;
  138. scene.add( ground );
  139. // TEXT
  140. var loader = new THREE.FontLoader();
  141. loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {
  142. var textGeo = new THREE.TextGeometry( "THREE.JS", {
  143. font: font,
  144. size: 200,
  145. height: 50,
  146. curveSegments: 12,
  147. bevelThickness: 2,
  148. bevelSize: 5,
  149. bevelEnabled: true
  150. } );
  151. textGeo.computeBoundingBox();
  152. var centerOffset = - 0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  153. var textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff } );
  154. var mesh = new THREE.Mesh( textGeo, textMaterial );
  155. mesh.position.x = centerOffset;
  156. mesh.position.y = FLOOR + 67;
  157. mesh.castShadow = true;
  158. mesh.receiveShadow = true;
  159. scene.add( mesh );
  160. } );
  161. // CUBES
  162. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 1500, 220, 150 ), planeMaterial );
  163. mesh.position.y = FLOOR - 50;
  164. mesh.position.z = 20;
  165. mesh.castShadow = true;
  166. mesh.receiveShadow = true;
  167. scene.add( mesh );
  168. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 1600, 170, 250 ), planeMaterial );
  169. mesh.position.y = FLOOR - 50;
  170. mesh.position.z = 20;
  171. mesh.castShadow = true;
  172. mesh.receiveShadow = true;
  173. scene.add( mesh );
  174. // MORPHS
  175. mixer = new THREE.AnimationMixer( scene );
  176. function addMorph( geometry, speed, duration, x, y, z, fudgeColor ) {
  177. var material = new THREE.MeshLambertMaterial( { color: 0xffaa55, morphTargets: true, vertexColors: THREE.FaceColors } );
  178. if ( fudgeColor ) {
  179. material.color.offsetHSL( 0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25 );
  180. }
  181. var mesh = new THREE.Mesh( geometry, material );
  182. mesh.speed = speed;
  183. var clip = geometry.animations[ 0 ];
  184. mixer.clipAction( clip, mesh ).
  185. setDuration( duration ).
  186. // to shift the playback out of phase:
  187. startAt( - duration * Math.random() ).
  188. play();
  189. mesh.position.set( x, y, z );
  190. mesh.rotation.y = Math.PI / 2;
  191. mesh.castShadow = true;
  192. mesh.receiveShadow = true;
  193. scene.add( mesh );
  194. morphs.push( mesh );
  195. }
  196. var loader = new THREE.JSONLoader();
  197. loader.load( "models/animated/horse.js", function( geometry ) {
  198. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, 300, true );
  199. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, 450, true );
  200. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, 600, true );
  201. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, - 300, true );
  202. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, - 450, true );
  203. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, - 600, true );
  204. } );
  205. loader.load( "models/animated/flamingo.js", function( geometry ) {
  206. addMorph( geometry, 500, 1, 500 - Math.random() * 500, FLOOR + 350, 40 );
  207. } );
  208. loader.load( "models/animated/stork.js", function( geometry ) {
  209. addMorph( geometry, 350, 1, 500 - Math.random() * 500, FLOOR + 350, 340 );
  210. } );
  211. loader.load( "models/animated/parrot.js", function( geometry ) {
  212. addMorph( geometry, 450, 0.5, 500 - Math.random() * 500, FLOOR + 300, 700 );
  213. } );
  214. }
  215. function animate() {
  216. requestAnimationFrame( animate );
  217. render();
  218. stats.update();
  219. }
  220. function render() {
  221. var delta = clock.getDelta();
  222. mixer.update( delta );
  223. for ( var i = 0; i < morphs.length; i ++ ) {
  224. morph = morphs[ i ];
  225. morph.position.x += morph.speed * delta;
  226. if ( morph.position.x > 2000 ) {
  227. morph.position.x = - 1000 - Math.random() * 500;
  228. }
  229. }
  230. controls.update( delta );
  231. renderer.clear();
  232. renderer.render( scene, camera );
  233. // Render debug HUD with shadow map
  234. if ( showHUD ) {
  235. lightShadowMapViewer.render( renderer );
  236. }
  237. }
  238. </script>
  239. </body>
  240. </html>