1
0

webgpu_shadowmap.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 - webgpu shadow map
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three/webgpu';
  25. import { mx_fractal_noise_vec3, positionWorld, vec4, Fn, color, vertexIndex, hash } from 'three/tsl';
  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. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  33. camera.position.set( 0, 10, 20 );
  34. scene = new THREE.Scene();
  35. scene.backgroundNode = color( 0x222244 );
  36. scene.fog = new THREE.Fog( 0x222244, 50, 100 );
  37. // lights
  38. scene.add( new THREE.AmbientLight( 0x444444, 2 ) );
  39. spotLight = new THREE.SpotLight( 0xff8888, 400 );
  40. spotLight.angle = Math.PI / 5;
  41. spotLight.penumbra = 0.3;
  42. spotLight.position.set( 8, 10, 5 );
  43. spotLight.castShadow = true;
  44. spotLight.shadow.camera.near = 8;
  45. spotLight.shadow.camera.far = 200;
  46. spotLight.shadow.mapSize.width = 2048;
  47. spotLight.shadow.mapSize.height = 2048;
  48. spotLight.shadow.bias = - 0.002;
  49. spotLight.shadow.radius = 4;
  50. scene.add( spotLight );
  51. dirLight = new THREE.DirectionalLight( 0x8888ff, 3 );
  52. dirLight.position.set( 3, 12, 17 );
  53. dirLight.castShadow = true;
  54. dirLight.shadow.camera.near = 0.1;
  55. dirLight.shadow.camera.far = 500;
  56. dirLight.shadow.camera.right = 17;
  57. dirLight.shadow.camera.left = - 17;
  58. dirLight.shadow.camera.top = 17;
  59. dirLight.shadow.camera.bottom = - 17;
  60. dirLight.shadow.mapSize.width = 2048;
  61. dirLight.shadow.mapSize.height = 2048;
  62. dirLight.shadow.radius = 4;
  63. dirLight.shadow.bias = - 0.0005;
  64. dirGroup = new THREE.Group();
  65. dirGroup.add( dirLight );
  66. scene.add( dirGroup );
  67. // geometry
  68. const geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 80 );
  69. const material = new THREE.MeshPhongNodeMaterial( {
  70. color: 0x999999,
  71. shininess: 0,
  72. specular: 0x222222
  73. } );
  74. const materialCustomShadow = material.clone();
  75. materialCustomShadow.transparent = true;
  76. const materialColor = vec4( 1, 0, 1, .5 );
  77. const discardNode = hash( vertexIndex ).greaterThan( 0.5 );
  78. materialCustomShadow.colorNode = Fn( () => {
  79. discardNode.discard();
  80. return materialColor;
  81. } )();
  82. materialCustomShadow.castShadowNode = Fn( () => {
  83. discardNode.discard();
  84. return materialColor;
  85. } )();
  86. torusKnot = new THREE.Mesh( geometry, materialCustomShadow );
  87. torusKnot.scale.multiplyScalar( 1 / 18 );
  88. torusKnot.position.y = 3;
  89. torusKnot.castShadow = true;
  90. torusKnot.receiveShadow = true;
  91. scene.add( torusKnot );
  92. const cylinderGeometry = new THREE.CylinderGeometry( 0.75, 0.75, 7, 32 );
  93. const pillar1 = new THREE.Mesh( cylinderGeometry, material );
  94. pillar1.position.set( 8, 3.5, 8 );
  95. pillar1.castShadow = true;
  96. const pillar2 = pillar1.clone();
  97. pillar2.position.set( 8, 3.5, - 8 );
  98. const pillar3 = pillar1.clone();
  99. pillar3.position.set( - 8, 3.5, 8 );
  100. const pillar4 = pillar1.clone();
  101. pillar4.position.set( - 8, 3.5, - 8 );
  102. scene.add( pillar1 );
  103. scene.add( pillar2 );
  104. scene.add( pillar3 );
  105. scene.add( pillar4 );
  106. const planeGeometry = new THREE.PlaneGeometry( 200, 200 );
  107. const planeMaterial = new THREE.MeshPhongNodeMaterial();
  108. planeMaterial.color.setHex( 0x999999 );
  109. planeMaterial.shininess = 0;
  110. planeMaterial.specular.setHex( 0x111111 );
  111. planeMaterial.receivedShadowPositionNode = Fn( () => {
  112. const pos = positionWorld.toVar();
  113. pos.xz.addAssign( mx_fractal_noise_vec3( positionWorld.mul( 2 ) ).saturate().xz );
  114. return pos;
  115. } )();
  116. planeMaterial.colorNode = Fn( () => {
  117. const pos = positionWorld.toVar();
  118. pos.xz.addAssign( mx_fractal_noise_vec3( positionWorld.mul( 2 ) ).saturate().xz );
  119. return mx_fractal_noise_vec3( positionWorld.mul( 2 ) ).saturate().zzz.mul( 0.2 ).add( .5 );
  120. } )();
  121. const ground = new THREE.Mesh( planeGeometry, planeMaterial );
  122. ground.rotation.x = - Math.PI / 2;
  123. ground.scale.multiplyScalar( 3 );
  124. ground.castShadow = true;
  125. ground.receiveShadow = true;
  126. scene.add( ground );
  127. // renderer
  128. renderer = new THREE.WebGPURenderer( { antialias: true } );
  129. renderer.setPixelRatio( window.devicePixelRatio );
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. renderer.setAnimationLoop( animate );
  132. renderer.shadowMap.enabled = true;
  133. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  134. document.body.appendChild( renderer.domElement );
  135. // Mouse control
  136. const controls = new OrbitControls( camera, renderer.domElement );
  137. controls.target.set( 0, 2, 0 );
  138. controls.minDistance = 7;
  139. controls.maxDistance = 40;
  140. controls.update();
  141. clock = new THREE.Clock();
  142. window.addEventListener( 'resize', resize );
  143. }
  144. function resize() {
  145. camera.aspect = window.innerWidth / window.innerHeight;
  146. camera.updateProjectionMatrix();
  147. renderer.setSize( window.innerWidth, window.innerHeight );
  148. }
  149. function animate( time ) {
  150. const delta = clock.getDelta();
  151. torusKnot.rotation.x += 0.25 * delta;
  152. torusKnot.rotation.y += 0.5 * delta;
  153. torusKnot.rotation.z += 1 * delta;
  154. dirGroup.rotation.y += 0.7 * delta;
  155. dirLight.position.z = 17 + Math.sin( time * 0.001 ) * 5;
  156. renderer.render( scene, camera );
  157. }
  158. </script>
  159. </body>
  160. </html>
粤ICP备19079148号