webgl_shadowmap.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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://github.com/mrdoob/three.js" 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
  30. </div>
  31. <script src="../build/Three.js"></script>
  32. <script src="js/ShaderExtras.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/RequestAnimationFrame.js"></script>
  35. <script src="js/Stats.js"></script>
  36. <script src="fonts/helvetiker_bold.typeface.js"></script>
  37. <script>
  38. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  39. var SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 1024;
  40. var MARGIN = 100;
  41. var SCREEN_WIDTH = window.innerWidth;
  42. var SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
  43. var FLOOR = -250;
  44. var camera, scene, renderer;
  45. var container, stats;
  46. var NEAR = 5, FAR = 3000;
  47. var sceneHUD, cameraOrtho, hudMaterial;
  48. var morphs = [];
  49. var light;
  50. init();
  51. animate();
  52. function init() {
  53. container = document.createElement( 'div' );
  54. document.body.appendChild( container );
  55. // SCENE CAMERA
  56. camera = new THREE.FirstPersonCamera( {
  57. fov: 23, aspect: SCREEN_WIDTH / SCREEN_HEIGHT, near: NEAR, far: FAR,
  58. lookSpeed: 0.0125, movementSpeed: 500, noFly: false, lookVertical: true,
  59. constrainVertical: true, verticalMin: 1.5, verticalMax: 2.0
  60. } );
  61. camera.position.set( 700, 50, 1900 );
  62. camera.lon = -110;
  63. // SCENE
  64. scene = new THREE.Scene();
  65. scene.fog = new THREE.Fog( 0xffaa55, 1000, FAR );
  66. THREE.ColorUtils.adjustHSV( scene.fog.color, 0.02, -0.15, -0.65 );
  67. // LIGHTS
  68. var ambient = new THREE.AmbientLight( 0x444444 );
  69. scene.add( ambient );
  70. light = new THREE.SpotLight( 0xffffff );
  71. light.position.set( 0, 1500, 1000 );
  72. light.target.position.set( 0, 0, 0 );
  73. light.castShadow = true;
  74. scene.add( light );
  75. createHUD();
  76. createScene();
  77. // RENDERER
  78. renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1, antialias: false } );
  79. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  80. renderer.domElement.style.position = "relative";
  81. renderer.domElement.style.top = MARGIN + 'px';
  82. container.appendChild( renderer.domElement );
  83. renderer.setClearColor( scene.fog.color, 1 );
  84. renderer.autoClear = false;
  85. renderer.shadowCameraNear = 3;
  86. renderer.shadowCameraFar = camera.far;
  87. renderer.shadowCameraFov = 50;
  88. renderer.shadowMapBias = 0.0039;
  89. renderer.shadowMapDarkness = 0.5;
  90. renderer.shadowMapWidth = SHADOW_MAP_WIDTH;
  91. renderer.shadowMapHeight = SHADOW_MAP_HEIGHT;
  92. renderer.shadowMapEnabled = true;
  93. renderer.shadowMapSoft = true;
  94. // STATS
  95. stats = new Stats();
  96. stats.domElement.style.position = 'absolute';
  97. stats.domElement.style.top = '0px';
  98. stats.domElement.style.zIndex = 100;
  99. //container.appendChild( stats.domElement );
  100. }
  101. function createHUD() {
  102. cameraOrtho = new THREE.OrthoCamera( SCREEN_WIDTH / - 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / - 2, -10, 1000 );
  103. cameraOrtho.position.z = 10;
  104. var shader = THREE.ShaderExtras[ "screen" ];
  105. var uniforms = new THREE.UniformsUtils.clone( shader.uniforms );
  106. hudMaterial = new THREE.ShaderMaterial( { vertexShader: shader.vertexShader, fragmentShader: shader.fragmentShader, uniforms: uniforms } );
  107. var hudGeo = new THREE.PlaneGeometry( SHADOW_MAP_WIDTH / 2, SHADOW_MAP_HEIGHT / 2 );
  108. var hudMesh = new THREE.Mesh( hudGeo, hudMaterial );
  109. hudMesh.position.x = ( SCREEN_WIDTH - SHADOW_MAP_WIDTH / 2 ) * -0.5;
  110. hudMesh.position.y = ( SCREEN_HEIGHT - SHADOW_MAP_HEIGHT / 2 ) * -0.5;
  111. sceneHUD = new THREE.Scene();
  112. sceneHUD.add( hudMesh );
  113. }
  114. function createScene( ) {
  115. // GROUND
  116. var geometry = new THREE.PlaneGeometry( 100, 100 );
  117. var planeMaterial = new THREE.MeshLambertMaterial( { color: 0xffdd99 } );
  118. THREE.ColorUtils.adjustHSV( planeMaterial.color, 0, 0, 0.9 );
  119. var ground = new THREE.Mesh( geometry, planeMaterial );
  120. ground.position.set( 0, FLOOR, 0 );
  121. ground.rotation.x = -Math.PI/2;
  122. ground.scale.set( 100, 100, 100 );
  123. ground.castShadow = false;
  124. ground.receiveShadow = true;
  125. scene.add( ground );
  126. // TEXT
  127. var textGeo = new THREE.TextGeometry( "THREE.JS", {
  128. size: 200,
  129. height: 50,
  130. curveSegments: 12,
  131. font: "helvetiker",
  132. weight: "bold",
  133. style: "normal",
  134. bevelThickness: 2,
  135. bevelSize: 5,
  136. bevelEnabled: true
  137. });
  138. textGeo.computeBoundingBox();
  139. var centerOffset = -0.5 * ( textGeo.boundingBox.x[ 1 ] - textGeo.boundingBox.x[ 0 ] );
  140. var textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff, ambient: 0xaa0000 } );
  141. var mesh = new THREE.Mesh( textGeo, textMaterial );
  142. mesh.position.x = centerOffset;
  143. mesh.position.y = FLOOR + 67;
  144. mesh.castShadow = true;
  145. mesh.receiveShadow = true;
  146. scene.add( mesh );
  147. // CUBES
  148. var mesh = new THREE.Mesh( new THREE.CubeGeometry( 1500, 220, 150 ), planeMaterial );
  149. mesh.position.y = FLOOR - 50;
  150. mesh.position.z = 20;
  151. mesh.castShadow = true;
  152. mesh.receiveShadow = true;
  153. scene.add( mesh );
  154. var mesh = new THREE.Mesh( new THREE.CubeGeometry( 1600, 170, 250 ), planeMaterial );
  155. mesh.position.y = FLOOR - 50;
  156. mesh.position.z = 20;
  157. mesh.castShadow = true;
  158. mesh.receiveShadow = true;
  159. scene.add( mesh );
  160. // MORPHS
  161. function addMorph( geometry, speed, duration, x, y, z, fudgeColor ) {
  162. var material = new THREE.MeshLambertMaterial( { color: 0xffaa55, morphTargets: true, vertexColors: THREE.FaceColors } );
  163. if ( fudgeColor ) THREE.ColorUtils.adjustHSV( material.color, 0, 0.5 - Math.random(), 0.5 - Math.random() );
  164. var meshAnim = new THREE.Mesh( geometry, material );
  165. meshAnim.position.set( x, y, z );
  166. meshAnim.rotation.y = Math.PI/2;
  167. meshAnim.castShadow = true;
  168. meshAnim.receiveShadow = false;
  169. scene.add( meshAnim );
  170. morphs.push( { mesh: meshAnim, lastKeyframe: 0, currentKeyframe: 0,
  171. offset: Math.random() * 6, speed: speed, duration: duration,
  172. oldTime: new Date().getTime() } );
  173. }
  174. function morphColorsToFaceColors( geometry ) {
  175. if ( geometry.morphColors && geometry.morphColors.length ) {
  176. var colorMap = geometry.morphColors[ 0 ];
  177. for ( var i = 0; i < colorMap.colors.length; i ++ ) {
  178. geometry.faces[ i ].color = colorMap.colors[ i ];
  179. }
  180. }
  181. }
  182. var loader = new THREE.JSONLoader();
  183. loader.load( { model: "models/animated/horse.js", callback: function( geometry ) {
  184. morphColorsToFaceColors( geometry );
  185. addMorph( geometry, 0.55, 1000, 100 - Math.random() * 1000, FLOOR, 300, true );
  186. addMorph( geometry, 0.55, 1000, 100 - Math.random() * 1000, FLOOR, 450, true );
  187. addMorph( geometry, 0.55, 1000, 100 - Math.random() * 1000, FLOOR, 600, true );
  188. addMorph( geometry, 0.55, 1000, 100 - Math.random() * 1000, FLOOR, -300, true );
  189. addMorph( geometry, 0.55, 1000, 100 - Math.random() * 1000, FLOOR, -450, true );
  190. addMorph( geometry, 0.55, 1000, 100 - Math.random() * 1000, FLOOR, -600, true );
  191. } } );
  192. /*
  193. loader.load( { model: "obj/morphs/fox.js", callback: function( geometry ) {
  194. morphColorsToFaceColors( geometry );
  195. addMorph( geometry, 0.2, 1000, 100 - Math.random() * 500, FLOOR - 5, 600 );
  196. } } );
  197. loader.load( { model: "obj/morphs/shdw3walk.js", callback: function( geometry ) {
  198. morphColorsToFaceColors( geometry );
  199. addMorph( geometry, 0.04, 2000, -500, FLOOR + 60, 245 );
  200. } } );
  201. loader.load( { model: "obj/morphs/flamingo.js", callback: function( geometry ) {
  202. morphColorsToFaceColors( geometry );
  203. addMorph( geometry, 0.5, 1000, 500 - Math.random() * 500, FLOOR + 350, 40 );
  204. } } );
  205. loader.load( { model: "obj/morphs/stork.js", callback: function( geometry ) {
  206. morphColorsToFaceColors( geometry );
  207. addMorph( geometry, 0.35, 1000, 500 - Math.random() * 500, FLOOR + 350, 340 );
  208. } } );
  209. loader.load( { model: "obj/morphs/mountainlion.js", callback: function( geometry ) {
  210. morphColorsToFaceColors( geometry );
  211. addMorph( geometry, 0.4, 1000, 500 - Math.random() * 500, FLOOR - 5, 700 );
  212. } } );
  213. loader.load( { model: "obj/morphs/bearBrown.js", callback: function( geometry ) {
  214. morphColorsToFaceColors( geometry );
  215. addMorph( geometry, 0.3, 2500, -500, FLOOR - 5, -750 );
  216. } } );
  217. loader.load( { model: "obj/morphs/parrot.js", callback: function( geometry ) {
  218. morphColorsToFaceColors( geometry );
  219. addMorph( geometry, 0.45, 500, 500 - Math.random() * 500, FLOOR + 300, 700 );
  220. } } );
  221. */
  222. }
  223. var t = 0, newTime, delta;
  224. function updateMorph( morph ) {
  225. // Alternate morph targets
  226. var interpolation = morph.duration / ( morph.mesh.geometry.morphTargets.length - 1 );
  227. var time = ( new Date().getTime() + morph.offset * 100 ) % morph.duration;
  228. var keyframe = Math.floor( time / interpolation ) + 1;
  229. var mesh = morph.mesh;
  230. if ( keyframe != morph.currentKeyframe ) {
  231. mesh.morphTargetInfluences[ morph.lastKeyframe ] = 0;
  232. mesh.morphTargetInfluences[ morph.currentKeyframe ] = 1;
  233. mesh.morphTargetInfluences[ keyframe ] = 0;
  234. morph.lastKeyframe = morph.currentKeyframe;
  235. morph.currentKeyframe = keyframe;
  236. }
  237. mesh.morphTargetInfluences[ keyframe ] = ( time % interpolation ) / interpolation;
  238. mesh.morphTargetInfluences[ morph.lastKeyframe ] = 1 - mesh.morphTargetInfluences[ keyframe ];
  239. var newTime = new Date().getTime();
  240. delta = newTime - morph.oldTime;
  241. morph.oldTime = newTime;
  242. mesh.position.x += morph.speed * delta;
  243. if ( mesh.position.x > 2000 ) {
  244. mesh.position.x = -1000 - Math.random() * 500;
  245. }
  246. }
  247. //
  248. function animate() {
  249. requestAnimationFrame( animate );
  250. render();
  251. stats.update();
  252. }
  253. function render() {
  254. for ( var i = 0; i < morphs.length; i++ ) updateMorph( morphs[ i ] );
  255. renderer.clear();
  256. renderer.render( scene, camera );
  257. // Render debug HUD with shadow map
  258. //hudMaterial.uniforms.tDiffuse.texture = renderer.shadowMap[ 0 ];
  259. //renderer.render( sceneHUD, cameraOrtho );
  260. }
  261. </script>
  262. </body>
  263. </html>