webgpu_shadowmap.html 5.1 KB

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