webgl_shadowmap.html 8.8 KB

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