webgl_shadowmap.html 8.9 KB

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