webgl_shadowmap.html 8.8 KB

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