webgl_shadowmap.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. a {
  24. color: #f00;
  25. font-weight: bold;
  26. text-decoration: underline;
  27. cursor: pointer
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="info">
  33. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - shadowmap - models by <a href="http://mirada.com/">mirada</a> from <a href="http://ro.me">rome</a><br />
  34. move camera with WASD / RF + mouse<br />
  35. t: toggle HUD
  36. </div>
  37. <script src="../build/three.js"></script>
  38. <script src="js/controls/FirstPersonControls.js"></script>
  39. <script src="js/loaders/GLTFLoader.js"></script>
  40. <script src="js/shaders/UnpackDepthRGBAShader.js"></script>
  41. <script src="js/utils/ShadowMapViewer.js"></script>
  42. <script src="js/WebGL.js"></script>
  43. <script src="js/libs/stats.min.js"></script>
  44. <script>
  45. if ( WEBGL.isWebGLAvailable() === false ) {
  46. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  47. }
  48. var SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 1024;
  49. var SCREEN_WIDTH = window.innerWidth;
  50. var SCREEN_HEIGHT = window.innerHeight;
  51. var FLOOR = - 250;
  52. var camera, controls, scene, renderer;
  53. var container, stats;
  54. var NEAR = 10, FAR = 3000;
  55. var mixer, morphs = [];
  56. var light;
  57. var lightShadowMapViewer;
  58. var clock = new THREE.Clock();
  59. var showHUD = false;
  60. init();
  61. animate();
  62. function init() {
  63. container = document.createElement( 'div' );
  64. document.body.appendChild( container );
  65. // CAMERA
  66. camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
  67. camera.position.set( 700, 50, 1900 );
  68. // SCENE
  69. scene = new THREE.Scene();
  70. scene.background = new THREE.Color( 0x59472b );
  71. scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
  72. controls = new THREE.FirstPersonControls( camera );
  73. controls.lookSpeed = 0.0125;
  74. controls.movementSpeed = 500;
  75. controls.noFly = false;
  76. controls.lookVertical = true;
  77. controls.lookAt( scene.position );
  78. // LIGHTS
  79. var ambient = new THREE.AmbientLight( 0x444444 );
  80. scene.add( ambient );
  81. light = new THREE.SpotLight( 0xffffff, 1, 0, Math.PI / 2 );
  82. light.position.set( 0, 1500, 1000 );
  83. light.target.position.set( 0, 0, 0 );
  84. light.castShadow = true;
  85. light.shadow = new THREE.LightShadow( new THREE.PerspectiveCamera( 50, 1, 1200, 2500 ) );
  86. light.shadow.bias = 0.0001;
  87. light.shadow.mapSize.width = SHADOW_MAP_WIDTH;
  88. light.shadow.mapSize.height = SHADOW_MAP_HEIGHT;
  89. scene.add( light );
  90. createHUD();
  91. createScene();
  92. // RENDERER
  93. renderer = new THREE.WebGLRenderer( { antialias: true } );
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  96. container.appendChild( renderer.domElement );
  97. renderer.gammaOutput = true;
  98. renderer.gammaFactor = 2.2;
  99. renderer.autoClear = false;
  100. //
  101. renderer.shadowMap.enabled = true;
  102. renderer.shadowMap.type = THREE.PCFShadowMap;
  103. // STATS
  104. stats = new Stats();
  105. //container.appendChild( stats.dom );
  106. //
  107. window.addEventListener( 'resize', onWindowResize, false );
  108. window.addEventListener( 'keydown', onKeyDown, false );
  109. }
  110. function onWindowResize() {
  111. SCREEN_WIDTH = window.innerWidth;
  112. SCREEN_HEIGHT = window.innerHeight;
  113. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  116. controls.handleResize();
  117. }
  118. function onKeyDown( event ) {
  119. switch ( event.keyCode ) {
  120. case 84: /*t*/
  121. showHUD = ! showHUD;
  122. break;
  123. }
  124. }
  125. function createHUD() {
  126. lightShadowMapViewer = new THREE.ShadowMapViewer( light );
  127. lightShadowMapViewer.position.x = 10;
  128. lightShadowMapViewer.position.y = SCREEN_HEIGHT - ( SHADOW_MAP_HEIGHT / 4 ) - 10;
  129. lightShadowMapViewer.size.width = SHADOW_MAP_WIDTH / 4;
  130. lightShadowMapViewer.size.height = SHADOW_MAP_HEIGHT / 4;
  131. lightShadowMapViewer.update();
  132. }
  133. function createScene( ) {
  134. // GROUND
  135. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  136. var planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffb851 } );
  137. var ground = new THREE.Mesh( geometry, planeMaterial );
  138. ground.position.set( 0, FLOOR, 0 );
  139. ground.rotation.x = - Math.PI / 2;
  140. ground.scale.set( 100, 100, 100 );
  141. ground.castShadow = false;
  142. ground.receiveShadow = true;
  143. scene.add( ground );
  144. // TEXT
  145. var loader = new THREE.FontLoader();
  146. loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {
  147. var textGeo = new THREE.TextBufferGeometry( "THREE.JS", {
  148. font: font,
  149. size: 200,
  150. height: 50,
  151. curveSegments: 12,
  152. bevelThickness: 2,
  153. bevelSize: 5,
  154. bevelEnabled: true
  155. } );
  156. textGeo.computeBoundingBox();
  157. var centerOffset = - 0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  158. var textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff } );
  159. var mesh = new THREE.Mesh( textGeo, textMaterial );
  160. mesh.position.x = centerOffset;
  161. mesh.position.y = FLOOR + 67;
  162. mesh.castShadow = true;
  163. mesh.receiveShadow = true;
  164. scene.add( mesh );
  165. } );
  166. // CUBES
  167. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1500, 220, 150 ), planeMaterial );
  168. mesh.position.y = FLOOR - 50;
  169. mesh.position.z = 20;
  170. mesh.castShadow = true;
  171. mesh.receiveShadow = true;
  172. scene.add( mesh );
  173. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1600, 170, 250 ), planeMaterial );
  174. mesh.position.y = FLOOR - 50;
  175. mesh.position.z = 20;
  176. mesh.castShadow = true;
  177. mesh.receiveShadow = true;
  178. scene.add( mesh );
  179. // MORPHS
  180. mixer = new THREE.AnimationMixer( scene );
  181. function addMorph( mesh, clip, speed, duration, x, y, z, fudgeColor ) {
  182. mesh = mesh.clone();
  183. mesh.material = mesh.material.clone();
  184. if ( fudgeColor ) {
  185. mesh.material.color.offsetHSL( 0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25 );
  186. }
  187. mesh.speed = speed;
  188. mixer.clipAction( clip, mesh ).
  189. setDuration( duration ).
  190. // to shift the playback out of phase:
  191. startAt( - duration * Math.random() ).
  192. play();
  193. mesh.position.set( x, y, z );
  194. mesh.rotation.y = Math.PI / 2;
  195. mesh.castShadow = true;
  196. mesh.receiveShadow = true;
  197. scene.add( mesh );
  198. morphs.push( mesh );
  199. }
  200. var loader = new THREE.GLTFLoader();
  201. loader.load( "models/gltf/Horse.glb", function ( gltf ) {
  202. var mesh = gltf.scene.children[ 0 ];
  203. var clip = gltf.animations[ 0 ];
  204. addMorph( mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, 300, true );
  205. addMorph( mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, 450, true );
  206. addMorph( mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, 600, true );
  207. addMorph( mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, - 300, true );
  208. addMorph( mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, - 450, true );
  209. addMorph( mesh, clip, 550, 1, 100 - Math.random() * 1000, FLOOR, - 600, true );
  210. } );
  211. loader.load( "models/gltf/Flamingo.glb", function ( gltf ) {
  212. var mesh = gltf.scene.children[ 0 ];
  213. var clip = gltf.animations[ 0 ];
  214. addMorph( mesh, clip, 500, 1, 500 - Math.random() * 500, FLOOR + 350, 40 );
  215. } );
  216. loader.load( "models/gltf/Stork.glb", function ( gltf ) {
  217. var mesh = gltf.scene.children[ 0 ];
  218. var clip = gltf.animations[ 0 ];
  219. addMorph( mesh, clip, 350, 1, 500 - Math.random() * 500, FLOOR + 350, 340 );
  220. } );
  221. loader.load( "models/gltf/Parrot.glb", function ( gltf ) {
  222. var mesh = gltf.scene.children[ 0 ];
  223. var clip = gltf.animations[ 0 ];
  224. addMorph( mesh, clip, 450, 0.5, 500 - Math.random() * 500, FLOOR + 300, 700 );
  225. } );
  226. }
  227. function animate() {
  228. requestAnimationFrame( animate );
  229. render();
  230. stats.update();
  231. }
  232. function render() {
  233. var delta = clock.getDelta();
  234. mixer.update( delta );
  235. for ( var i = 0; i < morphs.length; i ++ ) {
  236. var morph = morphs[ i ];
  237. morph.position.x += morph.speed * delta;
  238. if ( morph.position.x > 2000 ) {
  239. morph.position.x = - 1000 - Math.random() * 500;
  240. }
  241. }
  242. controls.update( delta );
  243. renderer.clear();
  244. renderer.render( scene, camera );
  245. // Render debug HUD with shadow map
  246. if ( showHUD ) {
  247. lightShadowMapViewer.render( renderer );
  248. }
  249. }
  250. </script>
  251. </body>
  252. </html>