webgl_shadowmap.html 9.0 KB

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