webgpu_shadowmap.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 ) );
  46. spotLight = new THREE.SpotLight( 0xff8888, 300 );
  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 = 1000;
  53. spotLight.shadow.mapSize.width = 2048;
  54. spotLight.shadow.mapSize.height = 2048;
  55. spotLight.shadow.bias = .01;
  56. scene.add( spotLight );
  57. dirLight = new THREE.DirectionalLight( 0x8888ff, 2 );
  58. dirLight.position.set( 3, 20, 12 );
  59. dirLight.castShadow = true;
  60. dirLight.shadow.camera.near = 8;
  61. dirLight.shadow.camera.far = 500;
  62. dirLight.shadow.camera.right = 17;
  63. dirLight.shadow.camera.left = - 17;
  64. dirLight.shadow.camera.top = 17;
  65. dirLight.shadow.camera.bottom = - 17;
  66. dirLight.shadow.mapSize.width = 2048;
  67. dirLight.shadow.mapSize.height = 2048;
  68. dirLight.shadow.bias = .0001;
  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();
  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.update();
  123. clock = new THREE.Clock();
  124. window.addEventListener( 'resize', resize );
  125. }
  126. function resize() {
  127. camera.aspect = window.innerWidth / window.innerHeight;
  128. camera.updateProjectionMatrix();
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. }
  131. function animate( time ) {
  132. const delta = clock.getDelta();
  133. torusKnot.rotation.x += 0.25 * delta;
  134. torusKnot.rotation.y += 0.5 * delta;
  135. torusKnot.rotation.z += 1 * delta;
  136. dirGroup.rotation.y += 0.7 * delta;
  137. dirLight.position.z = 17 + Math.sin( time * 0.001 ) * 5;
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>