webgl_shadowmap_performance.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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
  30. </div>
  31. <script src="../build/three.js"></script>
  32. <script src="js/controls/FirstPersonControls.js"></script>
  33. <script src="js/loaders/GLTFLoader.js"></script>
  34. <script src="js/shaders/UnpackDepthRGBAShader.js"></script>
  35. <script src="js/WebGL.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script>
  38. if ( WEBGL.isWebGLAvailable() === false ) {
  39. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  40. }
  41. var SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 1024;
  42. var SCREEN_WIDTH = window.innerWidth;
  43. var SCREEN_HEIGHT = window.innerHeight;
  44. var FLOOR = -250;
  45. var ANIMATION_GROUPS = 25;
  46. var camera, controls, scene, renderer;
  47. var container, stats;
  48. var NEAR = 5, FAR = 3000;
  49. var morph, morphs = [], mixer, animGroups = [];
  50. var light;
  51. var clock = new THREE.Clock();
  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.background = new THREE.Color( 0x59472b );
  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 );
  78. light.position.set( 0, 1500, 1000 );
  79. light.target.position.set( 0, 0, 0 );
  80. light.castShadow = true;
  81. light.shadow = new THREE.LightShadow( new THREE.PerspectiveCamera( 50, 1, 700, FAR ) );
  82. light.shadow.bias = 0.0001;
  83. light.shadow.mapSize.width = SHADOW_MAP_WIDTH;
  84. light.shadow.mapSize.height = SHADOW_MAP_HEIGHT;
  85. scene.add( light );
  86. createScene();
  87. // RENDERER
  88. renderer = new THREE.WebGLRenderer();
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  91. container.appendChild( renderer.domElement );
  92. renderer.autoClear = false;
  93. //
  94. renderer.shadowMap.enabled = true;
  95. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  96. // STATS
  97. stats = new Stats();
  98. container.appendChild( stats.dom );
  99. //
  100. window.addEventListener( 'resize', onWindowResize, false );
  101. }
  102. function onWindowResize() {
  103. SCREEN_WIDTH = window.innerWidth;
  104. SCREEN_HEIGHT = window.innerHeight;
  105. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  108. controls.handleResize();
  109. }
  110. function createScene( ) {
  111. // GROUND
  112. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  113. var planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffdd99 } );
  114. var ground = new THREE.Mesh( geometry, planeMaterial );
  115. ground.position.set( 0, FLOOR, 0 );
  116. ground.rotation.x = - Math.PI / 2;
  117. ground.scale.set( 100, 100, 100 );
  118. ground.castShadow = false;
  119. ground.receiveShadow = true;
  120. scene.add( ground );
  121. // TEXT
  122. var loader = new THREE.FontLoader();
  123. loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {
  124. var textGeo = new THREE.TextBufferGeometry( "THREE.JS", {
  125. font: font,
  126. size: 200,
  127. height: 50,
  128. curveSegments: 12,
  129. bevelThickness: 2,
  130. bevelSize: 5,
  131. bevelEnabled: true
  132. });
  133. textGeo.computeBoundingBox();
  134. var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  135. var textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff } );
  136. var mesh = new THREE.Mesh( textGeo, textMaterial );
  137. mesh.position.x = centerOffset;
  138. mesh.position.y = FLOOR + 67;
  139. mesh.castShadow = true;
  140. mesh.receiveShadow = true;
  141. scene.add( mesh );
  142. } );
  143. // CUBES
  144. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1500, 220, 150 ), planeMaterial );
  145. mesh.position.y = FLOOR - 50;
  146. mesh.position.z = 20;
  147. mesh.castShadow = true;
  148. mesh.receiveShadow = true;
  149. scene.add( mesh );
  150. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1600, 170, 250 ), planeMaterial );
  151. mesh.position.y = FLOOR - 50;
  152. mesh.position.z = 20;
  153. mesh.castShadow = true;
  154. mesh.receiveShadow = true;
  155. scene.add( mesh );
  156. mixer = new THREE.AnimationMixer( scene );
  157. for ( var i = 0; i !== ANIMATION_GROUPS; ++ i ) {
  158. var group = new THREE.AnimationObjectGroup();
  159. animGroups.push( group );
  160. }
  161. // MORPHS
  162. function addMorph( mesh, clip, speed, duration, x, y, z, fudgeColor, massOptimization ) {
  163. mesh = mesh.clone();
  164. mesh.material = mesh.material.clone();
  165. if ( fudgeColor ) {
  166. mesh.material.color.offsetHSL( 0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25 );
  167. }
  168. mesh.speed = speed;
  169. if ( massOptimization ) {
  170. var index = Math.floor( Math.random() * ANIMATION_GROUPS ),
  171. animGroup = animGroups[ index ];
  172. animGroup.add( mesh );
  173. if ( ! mixer.existingAction( clip, animGroup ) ) {
  174. var randomness = 0.6 * Math.random() - 0.3;
  175. var phase = ( index + randomness ) / ANIMATION_GROUPS;
  176. mixer.clipAction( clip, animGroup ).
  177. setDuration( duration ).
  178. startAt( - duration * phase ).
  179. play();
  180. }
  181. } else {
  182. mixer.clipAction( clip, mesh ).
  183. setDuration( duration ).
  184. startAt( - duration * Math.random() ).
  185. play();
  186. }
  187. mesh.position.set( x, y, z );
  188. mesh.rotation.y = Math.PI/2;
  189. mesh.castShadow = true;
  190. mesh.receiveShadow = true;
  191. scene.add( mesh );
  192. morphs.push( mesh );
  193. }
  194. var loader = new THREE.GLTFLoader();
  195. loader.load( "models/gltf/Horse.glb", function( gltf ) {
  196. var mesh = gltf.scene.children[ 0 ];
  197. var clip = gltf.animations[ 0 ];
  198. for ( var i = - 600; i < 601; i += 2 ) {
  199. addMorph( mesh, clip, 550, 1, 100 - Math.random() * 3000, FLOOR, i, true, true );
  200. }
  201. } );
  202. }
  203. //
  204. function animate() {
  205. requestAnimationFrame( animate );
  206. stats.begin();
  207. render();
  208. stats.end();
  209. }
  210. function render() {
  211. var delta = clock.getDelta();
  212. if ( mixer ) mixer.update( delta );
  213. for ( var i = 0; i < morphs.length; i ++ ) {
  214. morph = morphs[ i ];
  215. morph.position.x += morph.speed * delta;
  216. if ( morph.position.x > 2000 ) {
  217. morph.position.x = -1000 - Math.random() * 500;
  218. }
  219. }
  220. controls.update( delta );
  221. renderer.clear();
  222. renderer.render( scene, camera );
  223. }
  224. </script>
  225. </body>
  226. </html>