webgpu_shadowmap.html 5.3 KB

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