webgl_shadowmap_performance.html 7.7 KB

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