webgl_shadowmap.html 8.4 KB

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