2
0

webgl_shadowmap.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 HUD_MARGIN = 0.05;
  42. var SCREEN_WIDTH = window.innerWidth;
  43. var SCREEN_HEIGHT = window.innerHeight;
  44. var FLOOR = -250;
  45. var camera, controls, scene, renderer;
  46. var container, stats;
  47. var NEAR = 10, FAR = 3000;
  48. var sceneHUD, cameraOrtho, hudMesh;
  49. var morph, morphs = [];
  50. var light;
  51. var clock = new THREE.Clock();
  52. var showHUD = false;
  53. init();
  54. animate();
  55. function init() {
  56. container = document.createElement( 'div' );
  57. document.body.appendChild( container );
  58. // SCENE CAMERA
  59. camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
  60. camera.position.set( 700, 50, 1900 );
  61. controls = new THREE.FirstPersonControls( camera );
  62. controls.lookSpeed = 0.0125;
  63. controls.movementSpeed = 500;
  64. controls.noFly = false;
  65. controls.lookVertical = true;
  66. controls.constrainVertical = true;
  67. controls.verticalMin = 1.5;
  68. controls.verticalMax = 2.0;
  69. controls.lon = 250;
  70. controls.lat = 30;
  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 } );
  94. renderer.setClearColor( scene.fog.color );
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  97. container.appendChild( renderer.domElement );
  98. renderer.autoClear = false;
  99. //
  100. renderer.shadowMap.enabled = true;
  101. renderer.shadowMap.type = THREE.PCFShadowMap;
  102. // STATS
  103. stats = new Stats();
  104. stats.domElement.style.position = 'absolute';
  105. stats.domElement.style.top = '0px';
  106. stats.domElement.style.zIndex = 100;
  107. //container.appendChild( stats.domElement );
  108. //
  109. window.addEventListener( 'resize', onWindowResize, false );
  110. window.addEventListener( 'keydown', onKeyDown, false );
  111. }
  112. function onWindowResize() {
  113. SCREEN_WIDTH = window.innerWidth;
  114. SCREEN_HEIGHT = window.innerHeight;
  115. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  116. camera.updateProjectionMatrix();
  117. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  118. var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  119. cameraOrtho.left = - aspect;
  120. cameraOrtho.right = aspect;
  121. cameraOrtho.top = 1;
  122. cameraOrtho.bottom = - 1;
  123. cameraOrtho.updateProjectionMatrix();
  124. hudMesh.position.x = cameraOrtho.left + HUD_MARGIN;
  125. hudMesh.position.y = cameraOrtho.bottom + HUD_MARGIN;
  126. controls.handleResize();
  127. }
  128. function onKeyDown ( event ) {
  129. switch( event.keyCode ) {
  130. case 84: /*t*/ showHUD = !showHUD; break;
  131. }
  132. }
  133. function createHUD() {
  134. var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  135. cameraOrtho = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 1, 10 );
  136. cameraOrtho.position.z = 5;
  137. var shader = THREE.UnpackDepthRGBAShader;
  138. var uniforms = new THREE.UniformsUtils.clone( shader.uniforms );
  139. uniforms.tDiffuse.value = light.shadowMap;
  140. var hudMaterial = new THREE.ShaderMaterial( {
  141. uniforms: uniforms,
  142. vertexShader: shader.vertexShader,
  143. fragmentShader: shader.fragmentShader
  144. } );
  145. var hudHeight = 2 / 3;
  146. var hudWidth = hudHeight * SHADOW_MAP_WIDTH / SHADOW_MAP_HEIGHT;
  147. var hudGeo = new THREE.PlaneBufferGeometry( hudWidth, hudHeight );
  148. hudGeo.translate( hudWidth / 2, hudHeight / 2, 0 );
  149. hudMesh = new THREE.Mesh( hudGeo, hudMaterial );
  150. hudMesh.position.x = cameraOrtho.left + HUD_MARGIN;
  151. hudMesh.position.y = cameraOrtho.bottom + HUD_MARGIN;
  152. sceneHUD = new THREE.Scene();
  153. sceneHUD.add( hudMesh );
  154. cameraOrtho.lookAt( sceneHUD.position );
  155. }
  156. function createScene( ) {
  157. // GROUND
  158. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  159. var planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffdd99 } );
  160. var ground = new THREE.Mesh( geometry, planeMaterial );
  161. ground.position.set( 0, FLOOR, 0 );
  162. ground.rotation.x = - Math.PI / 2;
  163. ground.scale.set( 100, 100, 100 );
  164. ground.castShadow = false;
  165. ground.receiveShadow = true;
  166. scene.add( ground );
  167. // TEXT
  168. var textGeo = new THREE.TextGeometry( "THREE.JS", {
  169. size: 200,
  170. height: 50,
  171. curveSegments: 12,
  172. font: "helvetiker",
  173. weight: "bold",
  174. style: "normal",
  175. bevelThickness: 2,
  176. bevelSize: 5,
  177. bevelEnabled: true
  178. });
  179. textGeo.computeBoundingBox();
  180. var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  181. var textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff } );
  182. var mesh = new THREE.Mesh( textGeo, textMaterial );
  183. mesh.position.x = centerOffset;
  184. mesh.position.y = FLOOR + 67;
  185. mesh.castShadow = true;
  186. mesh.receiveShadow = true;
  187. scene.add( mesh );
  188. // CUBES
  189. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 1500, 220, 150 ), planeMaterial );
  190. mesh.position.y = FLOOR - 50;
  191. mesh.position.z = 20;
  192. mesh.castShadow = true;
  193. mesh.receiveShadow = true;
  194. scene.add( mesh );
  195. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 1600, 170, 250 ), planeMaterial );
  196. mesh.position.y = FLOOR - 50;
  197. mesh.position.z = 20;
  198. mesh.castShadow = true;
  199. mesh.receiveShadow = true;
  200. scene.add( mesh );
  201. // MORPHS
  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 meshAnim = new THREE.MorphAnimMesh( geometry, material );
  208. meshAnim.speed = speed;
  209. meshAnim.duration = duration;
  210. meshAnim.time = 600 * Math.random();
  211. meshAnim.position.set( x, y, z );
  212. meshAnim.rotation.y = Math.PI/2;
  213. meshAnim.castShadow = true;
  214. meshAnim.receiveShadow = true;
  215. scene.add( meshAnim );
  216. morphs.push( meshAnim );
  217. }
  218. function morphColorsToFaceColors( geometry ) {
  219. if ( geometry.morphColors && geometry.morphColors.length ) {
  220. var colorMap = geometry.morphColors[ 0 ];
  221. for ( var i = 0; i < colorMap.colors.length; i ++ ) {
  222. geometry.faces[ i ].color = colorMap.colors[ i ];
  223. }
  224. }
  225. }
  226. var loader = new THREE.JSONLoader();
  227. loader.load( "models/animated/horse.js", function( geometry ) {
  228. morphColorsToFaceColors( geometry );
  229. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, 300, true );
  230. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, 450, true );
  231. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, 600, true );
  232. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, -300, true );
  233. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, -450, true );
  234. addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, -600, true );
  235. } );
  236. loader.load( "models/animated/flamingo.js", function( geometry ) {
  237. morphColorsToFaceColors( geometry );
  238. addMorph( geometry, 500, 1000, 500 - Math.random() * 500, FLOOR + 350, 40 );
  239. } );
  240. loader.load( "models/animated/stork.js", function( geometry ) {
  241. morphColorsToFaceColors( geometry );
  242. addMorph( geometry, 350, 1000, 500 - Math.random() * 500, FLOOR + 350, 340 );
  243. } );
  244. loader.load( "models/animated/parrot.js", function( geometry ) {
  245. morphColorsToFaceColors( geometry );
  246. addMorph( geometry, 450, 500, 500 - Math.random() * 500, FLOOR + 300, 700 );
  247. } );
  248. }
  249. function animate() {
  250. requestAnimationFrame( animate );
  251. render();
  252. stats.update();
  253. }
  254. function render() {
  255. var delta = clock.getDelta();
  256. for ( var i = 0; i < morphs.length; i ++ ) {
  257. morph = morphs[ i ];
  258. morph.updateAnimation( 1000 * delta );
  259. morph.position.x += morph.speed * delta;
  260. if ( morph.position.x > 2000 ) {
  261. morph.position.x = -1000 - Math.random() * 500;
  262. }
  263. }
  264. controls.update( delta );
  265. renderer.clear();
  266. renderer.render( scene, camera );
  267. // Render debug HUD with shadow map
  268. if ( showHUD ) {
  269. renderer.clearDepth();
  270. renderer.render( sceneHUD, cameraOrtho );
  271. }
  272. }
  273. </script>
  274. </body>
  275. </html>