webgl_shadowmap.html 8.5 KB

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