webgl_shadowmap.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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">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</a>
  31. </div>
  32. <script src="../build/three.min.js"></script>
  33. <script src="js/controls/FirstPersonControls.js"></script>
  34. <script src="js/shaders/UnpackDepthRGBAShader.js"></script>
  35. <script src="js/Detector.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script>
  38. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  39. var SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 1024;
  40. var HUD_MARGIN = 0.05;
  41. var SCREEN_WIDTH = window.innerWidth;
  42. var SCREEN_HEIGHT = window.innerHeight;
  43. var FLOOR = -250;
  44. var camera, controls, scene, renderer;
  45. var container, stats;
  46. var NEAR = 10, FAR = 3000;
  47. var sceneHUD, cameraOrtho, hudMesh;
  48. var mixer, morphs = [];
  49. var light;
  50. var clock = new THREE.Clock();
  51. var showHUD = false;
  52. init();
  53. animate();
  54. function init() {
  55. container = document.createElement( 'div' );
  56. document.body.appendChild( container );
  57. // SCENE CAMERA
  58. camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
  59. camera.position.set( 700, 50, 1900 );
  60. controls = new THREE.FirstPersonControls( camera );
  61. controls.lookSpeed = 0.0125;
  62. controls.movementSpeed = 500;
  63. controls.noFly = false;
  64. controls.lookVertical = true;
  65. controls.constrainVertical = true;
  66. controls.verticalMin = 1.5;
  67. controls.verticalMax = 2.0;
  68. controls.lon = 250;
  69. controls.lat = 30;
  70. // SCENE
  71. scene = new THREE.Scene();
  72. scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
  73. // LIGHTS
  74. var ambient = new THREE.AmbientLight( 0x444444 );
  75. scene.add( ambient );
  76. light = new THREE.SpotLight( 0xffffff, 1, 0, Math.PI / 2 );
  77. light.position.set( 0, 1500, 1000 );
  78. light.target.position.set( 0, 0, 0 );
  79. light.castShadow = true;
  80. light.shadowCameraNear = 1200;
  81. light.shadowCameraFar = 2500;
  82. light.shadowCameraFov = 50;
  83. //light.shadowCameraVisible = true;
  84. light.shadowBias = 0.0001;
  85. light.shadowMapWidth = SHADOW_MAP_WIDTH;
  86. light.shadowMapHeight = SHADOW_MAP_HEIGHT;
  87. scene.add( light );
  88. createHUD();
  89. createScene();
  90. // RENDERER
  91. renderer = new THREE.WebGLRenderer( { antialias: true } );
  92. renderer.setClearColor( scene.fog.color );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  95. container.appendChild( renderer.domElement );
  96. renderer.autoClear = false;
  97. //
  98. renderer.shadowMap.enabled = true;
  99. renderer.shadowMap.type = THREE.PCFShadowMap;
  100. // STATS
  101. stats = new Stats();
  102. stats.domElement.style.position = 'absolute';
  103. stats.domElement.style.top = '0px';
  104. stats.domElement.style.zIndex = 100;
  105. //container.appendChild( stats.domElement );
  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. var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  117. cameraOrtho.left = - aspect;
  118. cameraOrtho.right = aspect;
  119. cameraOrtho.top = 1;
  120. cameraOrtho.bottom = - 1;
  121. cameraOrtho.updateProjectionMatrix();
  122. hudMesh.position.x = cameraOrtho.left + HUD_MARGIN;
  123. hudMesh.position.y = cameraOrtho.bottom + HUD_MARGIN;
  124. controls.handleResize();
  125. }
  126. function onKeyDown ( event ) {
  127. switch( event.keyCode ) {
  128. case 84: /*t*/ showHUD = !showHUD; break;
  129. }
  130. }
  131. function createHUD() {
  132. var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  133. cameraOrtho = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 1, 10 );
  134. cameraOrtho.position.z = 5;
  135. var shader = THREE.UnpackDepthRGBAShader;
  136. var uniforms = new THREE.UniformsUtils.clone( shader.uniforms );
  137. uniforms.tDiffuse.value = light.shadowMap;
  138. var hudMaterial = new THREE.ShaderMaterial( {
  139. uniforms: uniforms,
  140. vertexShader: shader.vertexShader,
  141. fragmentShader: shader.fragmentShader
  142. } );
  143. var hudHeight = 2 / 3;
  144. var hudWidth = hudHeight * SHADOW_MAP_WIDTH / SHADOW_MAP_HEIGHT;
  145. var hudGeo = new THREE.PlaneBufferGeometry( hudWidth, hudHeight );
  146. hudGeo.translate( hudWidth / 2, hudHeight / 2, 0 );
  147. hudMesh = new THREE.Mesh( hudGeo, hudMaterial );
  148. hudMesh.position.x = cameraOrtho.left + HUD_MARGIN;
  149. hudMesh.position.y = cameraOrtho.bottom + HUD_MARGIN;
  150. sceneHUD = new THREE.Scene();
  151. sceneHUD.add( hudMesh );
  152. cameraOrtho.lookAt( sceneHUD.position );
  153. }
  154. function createScene( ) {
  155. // GROUND
  156. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  157. var planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffdd99 } );
  158. var ground = new THREE.Mesh( geometry, planeMaterial );
  159. ground.position.set( 0, FLOOR, 0 );
  160. ground.rotation.x = - Math.PI / 2;
  161. ground.scale.set( 100, 100, 100 );
  162. ground.castShadow = false;
  163. ground.receiveShadow = true;
  164. scene.add( ground );
  165. // TEXT
  166. var loader = new THREE.FontLoader();
  167. loader.load( 'fonts/helvetiker_bold.typeface.js', function ( font ) {
  168. var textGeo = new THREE.TextGeometry( "THREE.JS", {
  169. font: font,
  170. size: 200,
  171. height: 50,
  172. curveSegments: 12,
  173. bevelThickness: 2,
  174. bevelSize: 5,
  175. bevelEnabled: true
  176. });
  177. textGeo.computeBoundingBox();
  178. var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  179. var textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff } );
  180. var mesh = new THREE.Mesh( textGeo, textMaterial );
  181. mesh.position.x = centerOffset;
  182. mesh.position.y = FLOOR + 67;
  183. mesh.castShadow = true;
  184. mesh.receiveShadow = true;
  185. scene.add( mesh );
  186. } );
  187. // CUBES
  188. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 1500, 220, 150 ), planeMaterial );
  189. mesh.position.y = FLOOR - 50;
  190. mesh.position.z = 20;
  191. mesh.castShadow = true;
  192. mesh.receiveShadow = true;
  193. scene.add( mesh );
  194. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 1600, 170, 250 ), planeMaterial );
  195. mesh.position.y = FLOOR - 50;
  196. mesh.position.z = 20;
  197. mesh.castShadow = true;
  198. mesh.receiveShadow = true;
  199. scene.add( mesh );
  200. // MORPHS
  201. mixer = new THREE.AnimationMixer( scene );
  202. function addMorph( geometry, speed, duration, x, y, z, fudgeColor ) {
  203. var material = new THREE.MeshLambertMaterial( { color: 0xffaa55, morphTargets: true, vertexColors: THREE.FaceColors } );
  204. if ( fudgeColor ) {
  205. material.color.offsetHSL( 0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25 );
  206. }
  207. var mesh = new THREE.Mesh( geometry, material );
  208. mesh.speed = speed;
  209. var clip = geometry.animations[ 0 ];
  210. mixer.clipAction( clip, mesh ).
  211. setDuration( duration ).
  212. // to shift the playback out of phase:
  213. startAt( - duration * Math.random() ).
  214. play();
  215. mesh.position.set( x, y, z );
  216. mesh.rotation.y = Math.PI/2;
  217. mesh.castShadow = true;
  218. mesh.receiveShadow = true;
  219. scene.add( mesh );
  220. morphs.push( mesh );
  221. }
  222. var loader = new THREE.JSONLoader();
  223. loader.load( "models/animated/horse.js", function( geometry ) {
  224. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, 300, true );
  225. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, 450, true );
  226. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, 600, true );
  227. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, -300, true );
  228. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, -450, true );
  229. addMorph( geometry, 550, 1, 100 - Math.random() * 1000, FLOOR, -600, true );
  230. } );
  231. loader.load( "models/animated/flamingo.js", function( geometry ) {
  232. addMorph( geometry, 500, 1, 500 - Math.random() * 500, FLOOR + 350, 40 );
  233. } );
  234. loader.load( "models/animated/stork.js", function( geometry ) {
  235. addMorph( geometry, 350, 1, 500 - Math.random() * 500, FLOOR + 350, 340 );
  236. } );
  237. loader.load( "models/animated/parrot.js", function( geometry ) {
  238. addMorph( geometry, 450, 0.5, 500 - Math.random() * 500, FLOOR + 300, 700 );
  239. } );
  240. }
  241. function animate() {
  242. requestAnimationFrame( animate );
  243. render();
  244. stats.update();
  245. }
  246. function render() {
  247. var delta = clock.getDelta();
  248. mixer.update( delta );
  249. for ( var i = 0; i < morphs.length; i ++ ) {
  250. morph = morphs[ i ];
  251. morph.position.x += morph.speed * delta;
  252. if ( morph.position.x > 2000 ) {
  253. morph.position.x = -1000 - Math.random() * 500;
  254. }
  255. }
  256. controls.update( delta );
  257. renderer.clear();
  258. renderer.render( scene, camera );
  259. // Render debug HUD with shadow map
  260. if ( showHUD ) {
  261. renderer.clearDepth();
  262. renderer.render( sceneHUD, cameraOrtho );
  263. }
  264. }
  265. </script>
  266. </body>
  267. </html>