webgl_shadowmap_performance.html 7.8 KB

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