webgpu_shadowmap.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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> - webgpu shadow map
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/",
  18. "three/nodes": "./jsm/nodes/Nodes.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  25. import WebGL from 'three/addons/capabilities/WebGL.js';
  26. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. let camera, scene, renderer, clock;
  29. let dirLight, spotLight;
  30. let torusKnot, dirGroup;
  31. init();
  32. function init() {
  33. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  34. document.body.appendChild( WebGPU.getErrorMessage() );
  35. throw new Error( 'No WebGPU or WebGL2 support' );
  36. }
  37. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  38. camera.position.set( 0, 10, 20 );
  39. scene = new THREE.Scene();
  40. scene.background = new THREE.Color( 0x222244 );
  41. scene.fog = new THREE.Fog( 0x222244, 50, 100 );
  42. // lights
  43. scene.add( new THREE.AmbientLight( 0x444444, 2 ) );
  44. spotLight = new THREE.SpotLight( 0xff8888, 400 );
  45. spotLight.angle = Math.PI / 5;
  46. spotLight.penumbra = 0.3;
  47. spotLight.position.set( 8, 10, 5 );
  48. spotLight.castShadow = true;
  49. spotLight.shadow.camera.near = 8;
  50. spotLight.shadow.camera.far = 200;
  51. spotLight.shadow.mapSize.width = 2048;
  52. spotLight.shadow.mapSize.height = 2048;
  53. spotLight.shadow.bias = - 0.002;
  54. spotLight.shadow.radius = 4;
  55. scene.add( spotLight );
  56. dirLight = new THREE.DirectionalLight( 0x8888ff, 3 );
  57. dirLight.position.set( 3, 12, 17 );
  58. dirLight.castShadow = true;
  59. dirLight.shadow.camera.near = 0.1;
  60. dirLight.shadow.camera.far = 500;
  61. dirLight.shadow.camera.right = 17;
  62. dirLight.shadow.camera.left = - 17;
  63. dirLight.shadow.camera.top = 17;
  64. dirLight.shadow.camera.bottom = - 17;
  65. dirLight.shadow.mapSize.width = 2048;
  66. dirLight.shadow.mapSize.height = 2048;
  67. dirLight.shadow.radius = 4;
  68. dirLight.shadow.bias = - 0.0005;
  69. dirGroup = new THREE.Group();
  70. dirGroup.add( dirLight );
  71. scene.add( dirGroup );
  72. // geometry
  73. const geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 20 );
  74. const material = new THREE.MeshPhongMaterial( {
  75. color: 0x999999,
  76. shininess: 0,
  77. specular: 0x222222
  78. } );
  79. torusKnot = new THREE.Mesh( geometry, material );
  80. torusKnot.scale.multiplyScalar( 1 / 18 );
  81. torusKnot.position.y = 3;
  82. torusKnot.castShadow = true;
  83. torusKnot.receiveShadow = true;
  84. scene.add( torusKnot );
  85. const cylinderGeometry = new THREE.CylinderGeometry( 0.75, 0.75, 7, 32 );
  86. const pillar1 = new THREE.Mesh( cylinderGeometry, material );
  87. pillar1.position.set( 8, 3.5, 8 );
  88. pillar1.castShadow = true;
  89. pillar1.receiveShadow = true;
  90. const pillar2 = pillar1.clone();
  91. pillar2.position.set( 8, 3.5, - 8 );
  92. const pillar3 = pillar1.clone();
  93. pillar3.position.set( - 8, 3.5, 8 );
  94. const pillar4 = pillar1.clone();
  95. pillar4.position.set( - 8, 3.5, - 8 );
  96. scene.add( pillar1 );
  97. scene.add( pillar2 );
  98. scene.add( pillar3 );
  99. scene.add( pillar4 );
  100. const planeGeometry = new THREE.PlaneGeometry( 200, 200 );
  101. const planeMaterial = new THREE.MeshPhongMaterial( {
  102. color: 0x999999,
  103. shininess: 0,
  104. specular: 0x111111
  105. } );
  106. const ground = new THREE.Mesh( planeGeometry, planeMaterial );
  107. ground.rotation.x = - Math.PI / 2;
  108. ground.scale.multiplyScalar( 3 );
  109. ground.castShadow = true;
  110. ground.receiveShadow = true;
  111. scene.add( ground );
  112. // renderer
  113. renderer = new WebGPURenderer( { antialias: true } );
  114. renderer.setPixelRatio( window.devicePixelRatio );
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. renderer.setAnimationLoop( animate );
  117. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  118. document.body.appendChild( renderer.domElement );
  119. // Mouse control
  120. const controls = new OrbitControls( camera, renderer.domElement );
  121. controls.target.set( 0, 2, 0 );
  122. controls.minDistance = 7;
  123. controls.maxDistance = 40;
  124. controls.update();
  125. clock = new THREE.Clock();
  126. window.addEventListener( 'resize', resize );
  127. }
  128. function resize() {
  129. camera.aspect = window.innerWidth / window.innerHeight;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. }
  133. function animate( time ) {
  134. const delta = clock.getDelta();
  135. torusKnot.rotation.x += 0.25 * delta;
  136. torusKnot.rotation.y += 0.5 * delta;
  137. torusKnot.rotation.z += 1 * delta;
  138. dirGroup.rotation.y += 0.7 * delta;
  139. dirLight.position.z = 17 + Math.sin( time * 0.001 ) * 5;
  140. renderer.render( scene, camera );
  141. }
  142. </script>
  143. </body>
  144. </html>