webgpu_shadowmap_vsm.html 6.4 KB

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