webgl_shadowmap_performance.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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="https://mirada.com/" target="_blank" rel="noopener">mirada</a> from <a href="http://www.ro.me" target="_blank" rel="noopener">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. import { FontLoader } from './jsm/loaders/FontLoader.js';
  20. import { TextGeometry } from './jsm/geometries/TextGeometry.js';
  21. const SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 1024;
  22. let SCREEN_WIDTH = window.innerWidth;
  23. let SCREEN_HEIGHT = window.innerHeight;
  24. const FLOOR = - 250;
  25. const ANIMATION_GROUPS = 25;
  26. let camera, controls, scene, renderer;
  27. let stats;
  28. const NEAR = 5, FAR = 3000;
  29. let morph, mixer;
  30. const morphs = [], animGroups = [];
  31. const clock = new THREE.Clock();
  32. init();
  33. animate();
  34. function init() {
  35. const container = document.createElement( 'div' );
  36. document.body.appendChild( container );
  37. // CAMERA
  38. camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
  39. camera.position.set( 700, 50, 1900 );
  40. // SCENE
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0x59472b );
  43. scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
  44. // LIGHTS
  45. const ambient = new THREE.AmbientLight( 0x444444 );
  46. scene.add( ambient );
  47. const light = new THREE.SpotLight( 0xffffff, 1, 0, Math.PI / 5, 0.3 );
  48. light.position.set( 0, 1500, 1000 );
  49. light.target.position.set( 0, 0, 0 );
  50. light.castShadow = true;
  51. light.shadow.camera.near = 1200;
  52. light.shadow.camera.far = 2500;
  53. light.shadow.bias = 0.0001;
  54. light.shadow.mapSize.width = SHADOW_MAP_WIDTH;
  55. light.shadow.mapSize.height = SHADOW_MAP_HEIGHT;
  56. scene.add( light );
  57. createScene();
  58. // RENDERER
  59. renderer = new THREE.WebGLRenderer();
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  62. container.appendChild( renderer.domElement );
  63. renderer.outputEncoding = THREE.sRGBEncoding;
  64. renderer.autoClear = false;
  65. //
  66. renderer.shadowMap.enabled = true;
  67. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  68. // CONTROLS
  69. controls = new FirstPersonControls( camera, renderer.domElement );
  70. controls.lookSpeed = 0.0125;
  71. controls.movementSpeed = 500;
  72. controls.noFly = false;
  73. controls.lookVertical = true;
  74. controls.lookAt( scene.position );
  75. // STATS
  76. stats = new Stats();
  77. container.appendChild( stats.dom );
  78. //
  79. window.addEventListener( 'resize', onWindowResize );
  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. const geometry = new THREE.PlaneGeometry( 100, 100 );
  92. const planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffb851 } );
  93. const 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. const loader = new FontLoader();
  102. loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {
  103. const textGeo = new TextGeometry( "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. const centerOffset = - 0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  114. const textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff } );
  115. const 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. const cubes1 = new THREE.Mesh( new THREE.BoxGeometry( 1500, 220, 150 ), planeMaterial );
  124. cubes1.position.y = FLOOR - 50;
  125. cubes1.position.z = 20;
  126. cubes1.castShadow = true;
  127. cubes1.receiveShadow = true;
  128. scene.add( cubes1 );
  129. const cubes2 = new THREE.Mesh( new THREE.BoxGeometry( 1600, 170, 250 ), planeMaterial );
  130. cubes2.position.y = FLOOR - 50;
  131. cubes2.position.z = 20;
  132. cubes2.castShadow = true;
  133. cubes2.receiveShadow = true;
  134. scene.add( cubes2 );
  135. mixer = new THREE.AnimationMixer( scene );
  136. for ( let i = 0; i !== ANIMATION_GROUPS; ++ i ) {
  137. const 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. const index = Math.floor( Math.random() * ANIMATION_GROUPS ),
  150. animGroup = animGroups[ index ];
  151. animGroup.add( mesh );
  152. if ( ! mixer.existingAction( clip, animGroup ) ) {
  153. const randomness = 0.6 * Math.random() - 0.3;
  154. const 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. const gltfLoader = new GLTFLoader();
  174. gltfLoader.load( "models/gltf/Horse.glb", function ( gltf ) {
  175. const mesh = gltf.scene.children[ 0 ];
  176. const clip = gltf.animations[ 0 ];
  177. for ( let 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. const delta = clock.getDelta();
  191. if ( mixer ) mixer.update( delta );
  192. for ( let 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>