webgl_shadowmap.html 8.9 KB

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