webgl_shadowmap.html 9.1 KB

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