webgl_shadowmap_performance.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <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 />
  12. move camera with WASD / RF + mouse
  13. </div>
  14. <script src="../build/three.js"></script>
  15. <script src="js/controls/FirstPersonControls.js"></script>
  16. <script src="js/loaders/GLTFLoader.js"></script>
  17. <script src="js/shaders/UnpackDepthRGBAShader.js"></script>
  18. <script src="js/WebGL.js"></script>
  19. <script src="js/libs/stats.min.js"></script>
  20. <script>
  21. if ( WEBGL.isWebGLAvailable() === false ) {
  22. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  23. }
  24. var SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 1024;
  25. var SCREEN_WIDTH = window.innerWidth;
  26. var SCREEN_HEIGHT = window.innerHeight;
  27. var FLOOR = - 250;
  28. var ANIMATION_GROUPS = 25;
  29. var camera, controls, scene, renderer;
  30. var container, stats;
  31. var NEAR = 5, FAR = 3000;
  32. var morph, morphs = [], mixer, animGroups = [];
  33. var light;
  34. var clock = new THREE.Clock();
  35. init();
  36. animate();
  37. function init() {
  38. container = document.createElement( 'div' );
  39. document.body.appendChild( container );
  40. // CAMERA
  41. camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
  42. camera.position.set( 700, 50, 1900 );
  43. // SCENE
  44. scene = new THREE.Scene();
  45. scene.background = new THREE.Color( 0x59472b );
  46. scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
  47. controls = new THREE.FirstPersonControls( camera );
  48. controls.lookSpeed = 0.0125;
  49. controls.movementSpeed = 500;
  50. controls.noFly = false;
  51. controls.lookVertical = true;
  52. controls.lookAt( scene.position );
  53. // LIGHTS
  54. var ambient = new THREE.AmbientLight( 0x444444 );
  55. scene.add( ambient );
  56. light = new THREE.SpotLight( 0xffffff, 1, 0, Math.PI / 2 );
  57. light.position.set( 0, 1500, 1000 );
  58. light.target.position.set( 0, 0, 0 );
  59. light.castShadow = true;
  60. light.shadow = new THREE.LightShadow( new THREE.PerspectiveCamera( 50, 1, 700, FAR ) );
  61. light.shadow.bias = 0.0001;
  62. light.shadow.mapSize.width = SHADOW_MAP_WIDTH;
  63. light.shadow.mapSize.height = SHADOW_MAP_HEIGHT;
  64. scene.add( light );
  65. createScene();
  66. // RENDERER
  67. renderer = new THREE.WebGLRenderer();
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  70. container.appendChild( renderer.domElement );
  71. renderer.autoClear = false;
  72. //
  73. renderer.shadowMap.enabled = true;
  74. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  75. // STATS
  76. stats = new Stats();
  77. container.appendChild( stats.dom );
  78. //
  79. window.addEventListener( 'resize', onWindowResize, false );
  80. }
  81. function onWindowResize() {
  82. SCREEN_WIDTH = window.innerWidth;
  83. SCREEN_HEIGHT = window.innerHeight;
  84. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  87. controls.handleResize();
  88. }
  89. function createScene( ) {
  90. // GROUND
  91. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  92. var planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffdd99 } );
  93. var ground = new THREE.Mesh( geometry, planeMaterial );
  94. ground.position.set( 0, FLOOR, 0 );
  95. ground.rotation.x = - Math.PI / 2;
  96. ground.scale.set( 100, 100, 100 );
  97. ground.castShadow = false;
  98. ground.receiveShadow = true;
  99. scene.add( ground );
  100. // TEXT
  101. var loader = new THREE.FontLoader();
  102. loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {
  103. var textGeo = new THREE.TextBufferGeometry( "THREE.JS", {
  104. font: font,
  105. size: 200,
  106. height: 50,
  107. curveSegments: 12,
  108. bevelThickness: 2,
  109. bevelSize: 5,
  110. bevelEnabled: true
  111. } );
  112. textGeo.computeBoundingBox();
  113. var centerOffset = - 0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  114. var textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff } );
  115. var mesh = new THREE.Mesh( textGeo, textMaterial );
  116. mesh.position.x = centerOffset;
  117. mesh.position.y = FLOOR + 67;
  118. mesh.castShadow = true;
  119. mesh.receiveShadow = true;
  120. scene.add( mesh );
  121. } );
  122. // CUBES
  123. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1500, 220, 150 ), planeMaterial );
  124. mesh.position.y = FLOOR - 50;
  125. mesh.position.z = 20;
  126. mesh.castShadow = true;
  127. mesh.receiveShadow = true;
  128. scene.add( mesh );
  129. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1600, 170, 250 ), planeMaterial );
  130. mesh.position.y = FLOOR - 50;
  131. mesh.position.z = 20;
  132. mesh.castShadow = true;
  133. mesh.receiveShadow = true;
  134. scene.add( mesh );
  135. mixer = new THREE.AnimationMixer( scene );
  136. for ( var i = 0; i !== ANIMATION_GROUPS; ++ i ) {
  137. var group = new THREE.AnimationObjectGroup();
  138. animGroups.push( group );
  139. }
  140. // MORPHS
  141. function addMorph( mesh, clip, speed, duration, x, y, z, fudgeColor, massOptimization ) {
  142. mesh = mesh.clone();
  143. mesh.material = mesh.material.clone();
  144. if ( fudgeColor ) {
  145. mesh.material.color.offsetHSL( 0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25 );
  146. }
  147. mesh.speed = speed;
  148. if ( massOptimization ) {
  149. var index = Math.floor( Math.random() * ANIMATION_GROUPS ),
  150. animGroup = animGroups[ index ];
  151. animGroup.add( mesh );
  152. if ( ! mixer.existingAction( clip, animGroup ) ) {
  153. var randomness = 0.6 * Math.random() - 0.3;
  154. var phase = ( index + randomness ) / ANIMATION_GROUPS;
  155. mixer.clipAction( clip, animGroup ).
  156. setDuration( duration ).
  157. startAt( - duration * phase ).
  158. play();
  159. }
  160. } else {
  161. mixer.clipAction( clip, mesh ).
  162. setDuration( duration ).
  163. startAt( - duration * Math.random() ).
  164. play();
  165. }
  166. mesh.position.set( x, y, z );
  167. mesh.rotation.y = Math.PI / 2;
  168. mesh.castShadow = true;
  169. mesh.receiveShadow = true;
  170. scene.add( mesh );
  171. morphs.push( mesh );
  172. }
  173. var loader = new THREE.GLTFLoader();
  174. loader.load( "models/gltf/Horse.glb", function ( gltf ) {
  175. var mesh = gltf.scene.children[ 0 ];
  176. var clip = gltf.animations[ 0 ];
  177. for ( var i = - 600; i < 601; i += 2 ) {
  178. addMorph( mesh, clip, 550, 1, 100 - Math.random() * 3000, FLOOR, i, true, true );
  179. }
  180. } );
  181. }
  182. //
  183. function animate() {
  184. requestAnimationFrame( animate );
  185. stats.begin();
  186. render();
  187. stats.end();
  188. }
  189. function render() {
  190. var delta = clock.getDelta();
  191. if ( mixer ) mixer.update( delta );
  192. for ( var i = 0; i < morphs.length; i ++ ) {
  193. morph = morphs[ i ];
  194. morph.position.x += morph.speed * delta;
  195. if ( morph.position.x > 2000 ) {
  196. morph.position.x = - 1000 - Math.random() * 500;
  197. }
  198. }
  199. controls.update( delta );
  200. renderer.clear();
  201. renderer.render( scene, camera );
  202. }
  203. </script>
  204. </body>
  205. </html>