webgl_shadowmap.html 8.5 KB

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