webgl_shadowmap.html 8.6 KB

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