webgl_shadowmap_performance.html 7.8 KB

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