webgl_shadowmap_performance.html 7.2 KB

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