webgl_shadowmap_performance.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.noFly = false;
  87. controls.lookVertical = true;
  88. controls.lookAt( scene.position );
  89. // STATS
  90. stats = new Stats();
  91. container.appendChild( stats.dom );
  92. //
  93. window.addEventListener( 'resize', onWindowResize );
  94. }
  95. function onWindowResize() {
  96. SCREEN_WIDTH = window.innerWidth;
  97. SCREEN_HEIGHT = window.innerHeight;
  98. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  101. controls.handleResize();
  102. }
  103. function createScene( ) {
  104. // GROUND
  105. const geometry = new THREE.PlaneGeometry( 100, 100 );
  106. const planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffdd99 } );
  107. const ground = new THREE.Mesh( geometry, planeMaterial );
  108. ground.position.set( 0, FLOOR, 0 );
  109. ground.rotation.x = - Math.PI / 2;
  110. ground.scale.set( 100, 100, 100 );
  111. ground.castShadow = false;
  112. ground.receiveShadow = true;
  113. scene.add( ground );
  114. // TEXT
  115. const loader = new FontLoader();
  116. loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {
  117. const textGeo = new TextGeometry( 'THREE.JS', {
  118. font: font,
  119. size: 200,
  120. height: 50,
  121. curveSegments: 12,
  122. bevelThickness: 2,
  123. bevelSize: 5,
  124. bevelEnabled: true
  125. } );
  126. textGeo.computeBoundingBox();
  127. const centerOffset = - 0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  128. const textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff } );
  129. const mesh = new THREE.Mesh( textGeo, textMaterial );
  130. mesh.position.x = centerOffset;
  131. mesh.position.y = FLOOR + 67;
  132. mesh.castShadow = true;
  133. mesh.receiveShadow = true;
  134. scene.add( mesh );
  135. } );
  136. // CUBES
  137. const cubes1 = new THREE.Mesh( new THREE.BoxGeometry( 1500, 220, 150 ), planeMaterial );
  138. cubes1.position.y = FLOOR - 50;
  139. cubes1.position.z = 20;
  140. cubes1.castShadow = true;
  141. cubes1.receiveShadow = true;
  142. scene.add( cubes1 );
  143. const cubes2 = new THREE.Mesh( new THREE.BoxGeometry( 1600, 170, 250 ), planeMaterial );
  144. cubes2.position.y = FLOOR - 50;
  145. cubes2.position.z = 20;
  146. cubes2.castShadow = true;
  147. cubes2.receiveShadow = true;
  148. scene.add( cubes2 );
  149. mixer = new THREE.AnimationMixer( scene );
  150. for ( let i = 0; i !== ANIMATION_GROUPS; ++ i ) {
  151. const group = new THREE.AnimationObjectGroup();
  152. animGroups.push( group );
  153. }
  154. // MORPHS
  155. function addMorph( mesh, clip, speed, duration, x, y, z, fudgeColor, massOptimization ) {
  156. mesh = mesh.clone();
  157. mesh.material = mesh.material.clone();
  158. if ( fudgeColor ) {
  159. mesh.material.color.offsetHSL( 0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25 );
  160. }
  161. mesh.speed = speed;
  162. if ( massOptimization ) {
  163. const index = Math.floor( Math.random() * ANIMATION_GROUPS ),
  164. animGroup = animGroups[ index ];
  165. animGroup.add( mesh );
  166. if ( ! mixer.existingAction( clip, animGroup ) ) {
  167. const randomness = 0.6 * Math.random() - 0.3;
  168. const phase = ( index + randomness ) / ANIMATION_GROUPS;
  169. mixer.clipAction( clip, animGroup ).
  170. setDuration( duration ).
  171. startAt( - duration * phase ).
  172. play();
  173. }
  174. } else {
  175. mixer.clipAction( clip, mesh ).
  176. setDuration( duration ).
  177. startAt( - duration * Math.random() ).
  178. play();
  179. }
  180. mesh.position.set( x, y, z );
  181. mesh.rotation.y = Math.PI / 2;
  182. mesh.castShadow = true;
  183. mesh.receiveShadow = true;
  184. scene.add( mesh );
  185. morphs.push( mesh );
  186. }
  187. const gltfLoader = new GLTFLoader();
  188. gltfLoader.load( 'models/gltf/Horse.glb', function ( gltf ) {
  189. const mesh = gltf.scene.children[ 0 ];
  190. const clip = gltf.animations[ 0 ];
  191. for ( let i = - 600; i < 601; i += 2 ) {
  192. addMorph( mesh, clip, 550, 1, 100 - Math.random() * 3000, FLOOR, i, true, true );
  193. }
  194. } );
  195. }
  196. //
  197. function animate() {
  198. requestAnimationFrame( animate );
  199. stats.begin();
  200. render();
  201. stats.end();
  202. }
  203. function render() {
  204. const delta = clock.getDelta();
  205. if ( mixer ) mixer.update( delta );
  206. for ( let i = 0; i < morphs.length; i ++ ) {
  207. morph = morphs[ i ];
  208. morph.position.x += morph.speed * delta;
  209. if ( morph.position.x > 2000 ) {
  210. morph.position.x = - 1000 - Math.random() * 500;
  211. }
  212. }
  213. controls.update( delta );
  214. renderer.clear();
  215. renderer.render( scene, camera );
  216. }
  217. </script>
  218. </body>
  219. </html>