webgl_shadowmap_performance.html 7.6 KB

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