webgpu_shadowmap.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Shadow Map</span>
  14. </div>
  15. <small>
  16. Shadow map example.
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { mx_fractal_noise_float, mx_fractal_noise_vec3, positionLocal, positionWorld, Fn, color } from 'three/tsl';
  32. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  33. import { Inspector } from 'three/addons/inspector/Inspector.js';
  34. let camera, scene, renderer, clock;
  35. let dirLight, spotLight;
  36. let torusKnot, dirGroup;
  37. init();
  38. function init() {
  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.backgroundNode = 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.radius = 4;
  56. scene.add( spotLight );
  57. dirLight = new THREE.DirectionalLight( 0x8888ff, 3 );
  58. dirLight.position.set( 3, 12, 17 );
  59. dirLight.castShadow = true;
  60. dirLight.shadow.camera.near = 0.1;
  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.radius = 4;
  69. dirGroup = new THREE.Group();
  70. dirGroup.add( dirLight );
  71. scene.add( dirGroup );
  72. // geometry
  73. const geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 80 );
  74. const material = new THREE.MeshPhongNodeMaterial( {
  75. color: 0x999999,
  76. shininess: 0,
  77. specular: 0x222222
  78. } );
  79. const materialCustomShadow = material.clone();
  80. materialCustomShadow.transparent = true;
  81. const discardNode = mx_fractal_noise_float( positionLocal.mul( 0.1 ) ).x.greaterThan( 0.0 );
  82. materialCustomShadow.maskNode = discardNode;
  83. torusKnot = new THREE.Mesh( geometry, materialCustomShadow );
  84. torusKnot.scale.multiplyScalar( 1 / 18 );
  85. torusKnot.position.y = 3;
  86. torusKnot.castShadow = true;
  87. torusKnot.receiveShadow = true;
  88. scene.add( torusKnot );
  89. const cylinderGeometry = new THREE.CylinderGeometry( 0.75, 0.75, 7, 32 );
  90. const pillar1 = new THREE.Mesh( cylinderGeometry, material );
  91. pillar1.position.set( 8, 3.5, 8 );
  92. pillar1.castShadow = true;
  93. const pillar2 = pillar1.clone();
  94. pillar2.position.set( 8, 3.5, - 8 );
  95. const pillar3 = pillar1.clone();
  96. pillar3.position.set( - 8, 3.5, 8 );
  97. const pillar4 = pillar1.clone();
  98. pillar4.position.set( - 8, 3.5, - 8 );
  99. scene.add( pillar1 );
  100. scene.add( pillar2 );
  101. scene.add( pillar3 );
  102. scene.add( pillar4 );
  103. const planeGeometry = new THREE.PlaneGeometry( 200, 200 );
  104. const planeMaterial = new THREE.MeshPhongNodeMaterial();
  105. planeMaterial.color.setHex( 0x999999 );
  106. planeMaterial.shininess = 0;
  107. planeMaterial.specular.setHex( 0x111111 );
  108. planeMaterial.receivedShadowPositionNode = Fn( () => {
  109. const pos = positionWorld.toVar();
  110. pos.xz.addAssign( mx_fractal_noise_vec3( positionWorld.mul( 2 ) ).saturate().xz );
  111. return pos;
  112. } )();
  113. planeMaterial.colorNode = Fn( () => {
  114. const pos = positionWorld.toVar();
  115. pos.xz.addAssign( mx_fractal_noise_vec3( positionWorld.mul( 2 ) ).saturate().xz );
  116. return mx_fractal_noise_vec3( positionWorld.mul( 2 ) ).saturate().zzz.mul( 0.2 ).add( .5 );
  117. } )();
  118. const ground = new THREE.Mesh( planeGeometry, planeMaterial );
  119. ground.rotation.x = - Math.PI / 2;
  120. ground.scale.multiplyScalar( 3 );
  121. ground.castShadow = true;
  122. ground.receiveShadow = true;
  123. scene.add( ground );
  124. // renderer
  125. renderer = new THREE.WebGPURenderer( { antialias: true } );
  126. renderer.setPixelRatio( window.devicePixelRatio );
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. renderer.setAnimationLoop( animate );
  129. renderer.shadowMap.enabled = true;
  130. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  131. renderer.inspector = new Inspector();
  132. document.body.appendChild( renderer.domElement );
  133. // Mouse control
  134. const controls = new OrbitControls( camera, renderer.domElement );
  135. controls.target.set( 0, 2, 0 );
  136. controls.minDistance = 7;
  137. controls.maxDistance = 40;
  138. controls.update();
  139. clock = new THREE.Clock();
  140. window.addEventListener( 'resize', resize );
  141. }
  142. function resize() {
  143. camera.aspect = window.innerWidth / window.innerHeight;
  144. camera.updateProjectionMatrix();
  145. renderer.setSize( window.innerWidth, window.innerHeight );
  146. }
  147. function animate( time ) {
  148. const delta = clock.getDelta();
  149. torusKnot.rotation.x += 0.25 * delta;
  150. torusKnot.rotation.y += 0.5 * delta;
  151. torusKnot.rotation.z += 1 * delta;
  152. dirGroup.rotation.y += 0.7 * delta;
  153. dirLight.position.z = 17 + Math.sin( time * 0.001 ) * 5;
  154. renderer.render( scene, camera );
  155. }
  156. </script>
  157. </body>
  158. </html>
粤ICP备19079148号