webgl_shadowmap.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 src="fonts/helvetiker_bold.typeface.js"></script>
  38. <script>
  39. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  40. var SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 1024;
  41. var MARGIN = 100;
  42. var HUD_MARGIN = 0.05;
  43. var SCREEN_WIDTH = window.innerWidth;
  44. var SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
  45. var FLOOR = -250;
  46. var camera, controls, scene, renderer;
  47. var container, stats;
  48. var NEAR = 10, FAR = 3000;
  49. var sceneHUD, cameraOrtho, hudMesh;
  50. var morph, morphs = [];
  51. var light;
  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. // SCENE
  72. scene = new THREE.Scene();
  73. scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
  74. // LIGHTS
  75. var ambient = new THREE.AmbientLight( 0x444444 );
  76. scene.add( ambient );
  77. light = new THREE.SpotLight( 0xffffff, 1, 0, Math.PI / 2, 1 );
  78. light.position.set( 0, 1500, 1000 );
  79. light.target.position.set( 0, 0, 0 );
  80. light.castShadow = true;
  81. light.shadowCameraNear = 1200;
  82. light.shadowCameraFar = 2500;
  83. light.shadowCameraFov = 50;
  84. //light.shadowCameraVisible = true;
  85. light.shadowBias = 0.0001;
  86. light.shadowDarkness = 0.5;
  87. light.shadowMapWidth = SHADOW_MAP_WIDTH;
  88. light.shadowMapHeight = SHADOW_MAP_HEIGHT;
  89. scene.add( light );
  90. createHUD();
  91. createScene();
  92. // RENDERER
  93. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: false } );
  94. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  95. renderer.domElement.style.position = "relative";
  96. renderer.domElement.style.top = MARGIN + 'px';
  97. container.appendChild( renderer.domElement );
  98. renderer.setClearColor( scene.fog.color, 1 );
  99. renderer.autoClear = false;
  100. //
  101. renderer.shadowMapEnabled = true;
  102. renderer.shadowMapType = THREE.PCFShadowMap;
  103. // STATS
  104. stats = new Stats();
  105. stats.domElement.style.position = 'absolute';
  106. stats.domElement.style.top = '0px';
  107. stats.domElement.style.zIndex = 100;
  108. //container.appendChild( stats.domElement );
  109. //
  110. window.addEventListener( 'resize', onWindowResize, false );
  111. window.addEventListener( 'keydown', onKeyDown, false );
  112. }
  113. function onWindowResize() {
  114. SCREEN_WIDTH = window.innerWidth;
  115. SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
  116. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  119. var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  120. cameraOrtho.left = - aspect;
  121. cameraOrtho.right = aspect;
  122. cameraOrtho.top = 1;
  123. cameraOrtho.bottom = - 1;
  124. cameraOrtho.updateProjectionMatrix();
  125. hudMesh.position.x = cameraOrtho.left + HUD_MARGIN;
  126. hudMesh.position.y = cameraOrtho.bottom + HUD_MARGIN;
  127. controls.handleResize();
  128. }
  129. function onKeyDown ( event ) {
  130. switch( event.keyCode ) {
  131. case 84: /*t*/ showHUD = !showHUD; break;
  132. }
  133. };
  134. function createHUD() {
  135. var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  136. cameraOrtho = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 1, 10 );
  137. cameraOrtho.position.z = 5;
  138. var shader = THREE.UnpackDepthRGBAShader;
  139. var uniforms = new THREE.UniformsUtils.clone( shader.uniforms );
  140. uniforms.tDiffuse.value = light.shadowMap;
  141. var hudMaterial = new THREE.ShaderMaterial( { vertexShader: shader.vertexShader, fragmentShader: shader.fragmentShader, uniforms: uniforms } );
  142. var hudHeight = 2 / 3;
  143. var hudWidth = hudHeight * SHADOW_MAP_WIDTH / SHADOW_MAP_HEIGHT;
  144. var hudGeo = new THREE.PlaneGeometry( hudWidth, hudHeight );
  145. hudGeo.applyMatrix( new THREE.Matrix4().makeTranslation( hudWidth / 2, hudHeight / 2, 0 ) );
  146. hudMesh = new THREE.Mesh( hudGeo, hudMaterial );
  147. hudMesh.position.x = cameraOrtho.left + HUD_MARGIN;
  148. hudMesh.position.y = cameraOrtho.bottom + HUD_MARGIN;
  149. sceneHUD = new THREE.Scene();
  150. sceneHUD.add( hudMesh );
  151. cameraOrtho.lookAt( sceneHUD.position );
  152. }
  153. function createScene( ) {
  154. // GROUND
  155. var geometry = new THREE.PlaneGeometry( 100, 100 );
  156. var planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffdd99 } );
  157. planeMaterial.ambient = planeMaterial.color;
  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 textGeo = new THREE.TextGeometry( "THREE.JS", {
  167. size: 200,
  168. height: 50,
  169. curveSegments: 12,
  170. font: "helvetiker",
  171. weight: "bold",
  172. style: "normal",
  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, ambient: 0xaa0000 } );
  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. // CUBES
  187. var mesh = new THREE.Mesh( new THREE.CubeGeometry( 1500, 220, 150 ), planeMaterial );
  188. mesh.position.y = FLOOR - 50;
  189. mesh.position.z = 20;
  190. mesh.castShadow = true;
  191. mesh.receiveShadow = true;
  192. scene.add( mesh );
  193. var mesh = new THREE.Mesh( new THREE.CubeGeometry( 1600, 170, 250 ), planeMaterial );
  194. mesh.position.y = FLOOR - 50;
  195. mesh.position.z = 20;
  196. mesh.castShadow = true;
  197. mesh.receiveShadow = true;
  198. scene.add( mesh );
  199. // MORPHS
  200. function addMorph( geometry, speed, duration, x, y, z, fudgeColor ) {
  201. var material = new THREE.MeshLambertMaterial( { color: 0xffaa55, morphTargets: true, vertexColors: THREE.FaceColors } );
  202. if ( fudgeColor ) {
  203. material.color.offsetHSL( 0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25 );
  204. material.ambient = material.color;
  205. }
  206. var meshAnim = new THREE.MorphAnimMesh( geometry, material );
  207. meshAnim.speed = speed;
  208. meshAnim.duration = duration;
  209. meshAnim.time = 600 * Math.random();
  210. meshAnim.position.set( x, y, z );
  211. meshAnim.rotation.y = Math.PI/2;
  212. meshAnim.castShadow = true;
  213. meshAnim.receiveShadow = true;
  214. scene.add( meshAnim );
  215. morphs.push( meshAnim );
  216. }
  217. function morphColorsToFaceColors( geometry ) {
  218. if ( geometry.morphColors && geometry.morphColors.length ) {
  219. var colorMap = geometry.morphColors[ 0 ];
  220. for ( var i = 0; i < colorMap.colors.length; i ++ ) {
  221. geometry.faces[ i ].color = colorMap.colors[ i ];
  222. }
  223. }
  224. }
  225. var loader = new THREE.JSONLoader();
  226. loader.load( "models/animated/horse.js", function( geometry ) {
  227. morphColorsToFaceColors( geometry );
  228. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, 300, true );
  229. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, 450, true );
  230. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, 600, true );
  231. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, -300, true );
  232. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, -450, true );
  233. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, -600, true );
  234. } );
  235. loader.load( "models/animated/flamingo.js", function( geometry ) {
  236. morphColorsToFaceColors( geometry );
  237. addMorph( geometry, 500, 1000, 500 - Math.random() * 500, FLOOR + 350, 40 );
  238. } );
  239. loader.load( "models/animated/stork.js", function( geometry ) {
  240. morphColorsToFaceColors( geometry );
  241. addMorph( geometry, 350, 1000, 500 - Math.random() * 500, FLOOR + 350, 340 );
  242. } );
  243. loader.load( "models/animated/parrot.js", function( geometry ) {
  244. morphColorsToFaceColors( geometry );
  245. addMorph( geometry, 450, 500, 500 - Math.random() * 500, FLOOR + 300, 700 );
  246. } );
  247. }
  248. function animate() {
  249. requestAnimationFrame( animate );
  250. render();
  251. stats.update();
  252. }
  253. function render() {
  254. var delta = clock.getDelta();
  255. for ( var i = 0; i < morphs.length; i ++ ) {
  256. morph = morphs[ i ];
  257. morph.updateAnimation( 1000 * delta );
  258. morph.position.x += morph.speed * delta;
  259. if ( morph.position.x > 2000 ) {
  260. morph.position.x = -1000 - Math.random() * 500;
  261. }
  262. }
  263. controls.update( delta );
  264. renderer.clear();
  265. renderer.render( scene, camera );
  266. // Render debug HUD with shadow map
  267. if ( showHUD ) {
  268. renderer.clearDepth();
  269. renderer.render( sceneHUD, cameraOrtho );
  270. }
  271. }
  272. </script>
  273. </body>
  274. </html>