webgpu_shadowmap_vsm.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - vsm shadows example</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. <meta property="og:title" content="three.js webgpu - vsm shadows example">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_shadowmap_vsm.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_shadowmap_vsm.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>VSM Shadows</span>
  18. </div>
  19. <small>
  20. VSM Shadows example by <a href="https://github.com/supereggbert">Paul Brunt</a>.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { Inspector } from 'three/addons/inspector/Inspector.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. let camera, scene, renderer, timer;
  38. let dirLight, spotLight;
  39. let torusKnot, dirGroup;
  40. const config = {
  41. spotlightRadius: 4,
  42. spotlightSamples: 8,
  43. dirlightRadius: 4,
  44. dirlightSamples: 8,
  45. animate: true
  46. };
  47. init();
  48. function init() {
  49. initScene();
  50. initMisc();
  51. // Init gui
  52. const gui = renderer.inspector.createParameters( 'Settings' );
  53. const spotlightFolder = gui.addFolder( 'Spotlight' );
  54. spotlightFolder.add( config, 'spotlightRadius', 0, 25 ).name( 'radius' ).onChange( function ( value ) {
  55. spotLight.shadow.radius = value;
  56. } );
  57. spotlightFolder.add( config, 'spotlightSamples', 1, 25, 1 ).name( 'samples' ).onChange( function ( value ) {
  58. spotLight.shadow.blurSamples = value;
  59. } );
  60. const dirlightFolder = gui.addFolder( 'Directional Light' );
  61. dirlightFolder.add( config, 'dirlightRadius', 0, 25 ).name( 'radius' ).onChange( function ( value ) {
  62. dirLight.shadow.radius = value;
  63. } );
  64. dirlightFolder.add( config, 'dirlightSamples', 1, 25, 1 ).name( 'samples' ).onChange( function ( value ) {
  65. dirLight.shadow.blurSamples = value;
  66. } );
  67. gui.add( config, 'animate' );
  68. document.body.appendChild( renderer.domElement );
  69. window.addEventListener( 'resize', onWindowResize );
  70. }
  71. function initScene() {
  72. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  73. camera.position.set( 0, 10, 30 );
  74. scene = new THREE.Scene();
  75. scene.background = new THREE.Color( 0x222244 );
  76. scene.fog = new THREE.Fog( 0x222244, 50, 100 );
  77. // Lights
  78. scene.add( new THREE.AmbientLight( 0x444444 ) );
  79. spotLight = new THREE.SpotLight( 0xff8888, 400 );
  80. spotLight.angle = Math.PI / 5;
  81. spotLight.penumbra = 0.3;
  82. spotLight.position.set( 8, 10, 5 );
  83. spotLight.castShadow = true;
  84. spotLight.shadow.camera.near = 8;
  85. spotLight.shadow.camera.far = 200;
  86. spotLight.shadow.mapSize.width = 256;
  87. spotLight.shadow.mapSize.height = 256;
  88. spotLight.shadow.bias = - 0.002;
  89. spotLight.shadow.radius = 4;
  90. scene.add( spotLight );
  91. dirLight = new THREE.DirectionalLight( 0x8888ff, 3 );
  92. dirLight.position.set( 3, 12, 17 );
  93. dirLight.castShadow = true;
  94. dirLight.shadow.camera.near = 0.1;
  95. dirLight.shadow.camera.far = 500;
  96. dirLight.shadow.camera.right = 17;
  97. dirLight.shadow.camera.left = - 17;
  98. dirLight.shadow.camera.top = 17;
  99. dirLight.shadow.camera.bottom = - 17;
  100. dirLight.shadow.mapSize.width = 512;
  101. dirLight.shadow.mapSize.height = 512;
  102. dirLight.shadow.radius = 4;
  103. dirLight.shadow.bias = - 0.0005;
  104. dirGroup = new THREE.Group();
  105. dirGroup.add( dirLight );
  106. scene.add( dirGroup );
  107. // Geometry
  108. const geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 20 );
  109. const material = new THREE.MeshPhongMaterial( {
  110. color: 0x999999,
  111. shininess: 0,
  112. specular: 0x222222
  113. } );
  114. torusKnot = new THREE.Mesh( geometry, material );
  115. torusKnot.scale.multiplyScalar( 1 / 18 );
  116. torusKnot.position.y = 3;
  117. torusKnot.castShadow = true;
  118. torusKnot.receiveShadow = true;
  119. scene.add( torusKnot );
  120. const cylinderGeometry = new THREE.CylinderGeometry( 0.75, 0.75, 7, 32 );
  121. const pillar1 = new THREE.Mesh( cylinderGeometry, material );
  122. pillar1.position.set( 8, 3.5, 8 );
  123. pillar1.castShadow = true;
  124. pillar1.receiveShadow = true;
  125. const pillar2 = pillar1.clone();
  126. pillar2.position.set( 8, 3.5, - 8 );
  127. const pillar3 = pillar1.clone();
  128. pillar3.position.set( - 8, 3.5, 8 );
  129. const pillar4 = pillar1.clone();
  130. pillar4.position.set( - 8, 3.5, - 8 );
  131. scene.add( pillar1 );
  132. scene.add( pillar2 );
  133. scene.add( pillar3 );
  134. scene.add( pillar4 );
  135. const planeGeometry = new THREE.PlaneGeometry( 200, 200 );
  136. const planeMaterial = new THREE.MeshPhongMaterial( {
  137. color: 0x999999,
  138. shininess: 0,
  139. specular: 0x111111
  140. } );
  141. const ground = new THREE.Mesh( planeGeometry, planeMaterial );
  142. ground.rotation.x = - Math.PI / 2;
  143. ground.scale.multiplyScalar( 3 );
  144. ground.castShadow = true;
  145. ground.receiveShadow = true;
  146. scene.add( ground );
  147. }
  148. function initMisc() {
  149. renderer = new THREE.WebGPURenderer( { antialias: true } );
  150. renderer.setPixelRatio( window.devicePixelRatio );
  151. renderer.setSize( window.innerWidth, window.innerHeight );
  152. renderer.setAnimationLoop( animate );
  153. renderer.inspector = new Inspector();
  154. renderer.shadowMap.enabled = true;
  155. renderer.shadowMap.type = THREE.VSMShadowMap;
  156. // Mouse control
  157. const controls = new OrbitControls( camera, renderer.domElement );
  158. controls.target.set( 0, 2, 0 );
  159. controls.update();
  160. timer = new THREE.Timer();
  161. timer.connect( document );
  162. }
  163. function onWindowResize() {
  164. camera.aspect = window.innerWidth / window.innerHeight;
  165. camera.updateProjectionMatrix();
  166. renderer.setSize( window.innerWidth, window.innerHeight );
  167. }
  168. function animate( time ) {
  169. timer.update();
  170. const delta = timer.getDelta();
  171. if ( config.animate === true ) {
  172. torusKnot.rotation.x += 0.25 * delta;
  173. torusKnot.rotation.y += 0.5 * delta;
  174. torusKnot.rotation.z += 1 * delta;
  175. dirGroup.rotation.y += 0.7 * delta;
  176. dirLight.position.z = 17 + Math.sin( time * 0.001 ) * 5;
  177. }
  178. renderer.render( scene, camera );
  179. }
  180. </script>
  181. </body>
  182. </html>
粤ICP备19079148号