webgpu_shadow_contact.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - contact shadows</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. <meta property="og:title" content="three.js webgpu - contact shadows">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_shadow_contact.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_shadow_contact.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info" class="invert">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Contact Shadows</span>
  18. </div>
  19. <small>Contact shadows using Gaussian blur for smooth rendering.</small>
  20. </div>
  21. <div id="container"></div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.webgpu.js",
  26. "three/webgpu": "../build/three.webgpu.js",
  27. "three/tsl": "../build/three.tsl.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three/webgpu';
  34. import { vec3, uniform, texture, depth, float } from 'three/tsl';
  35. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { Inspector } from 'three/addons/inspector/Inspector.js';
  38. let camera, scene, renderer;
  39. let shadowCamera, shadowGroup;
  40. let renderTarget;
  41. let plane, fillPlane, cameraHelper;
  42. let depthMaterial, shadowPlaneMaterial, fillPlaneMaterial;
  43. const meshes = [];
  44. const PLANE_WIDTH = 2.5;
  45. const PLANE_HEIGHT = 2.5;
  46. const CAMERA_HEIGHT = 0.3;
  47. const state = {
  48. shadow: { blur: 3.5, darkness: 1.0, opacity: 1.0 },
  49. plane: { color: '#ffffff', opacity: 1.0 },
  50. showWireframe: false
  51. };
  52. const uBlur = uniform( state.shadow.blur );
  53. const uDarkness = uniform( state.shadow.darkness );
  54. const uShadowOpacity = uniform( state.shadow.opacity );
  55. const uPlaneOpacity = uniform( state.plane.opacity );
  56. const uPlaneColor = uniform( new THREE.Color( state.plane.color ) );
  57. const uPlaneY = uniform( - 0.3 );
  58. init();
  59. function init() {
  60. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  61. camera.position.set( 0.5, 1, 2 );
  62. scene = new THREE.Scene();
  63. scene.background = new THREE.Color( 0xffffff );
  64. window.addEventListener( 'resize', onWindowResize );
  65. const geometries = [
  66. new THREE.BoxGeometry( 0.4, 0.4, 0.4 ),
  67. new THREE.IcosahedronGeometry( 0.3 ),
  68. new THREE.TorusKnotGeometry( 0.4, 0.05, 256, 24, 1, 3 )
  69. ];
  70. const material = new THREE.MeshNormalMaterial();
  71. for ( let i = 0, l = geometries.length; i < l; i ++ ) {
  72. const angle = ( i / l ) * Math.PI * 2;
  73. const mesh = new THREE.Mesh( geometries[ i ], material );
  74. mesh.position.y = 0.1;
  75. mesh.position.x = Math.cos( angle ) / 2.0;
  76. mesh.position.z = Math.sin( angle ) / 2.0;
  77. scene.add( mesh );
  78. meshes.push( mesh );
  79. }
  80. shadowGroup = new THREE.Group();
  81. shadowGroup.position.y = uPlaneY.value;
  82. scene.add( shadowGroup );
  83. renderTarget = new THREE.RenderTarget( 512, 512, { depthBuffer: true } );
  84. renderTarget.texture.generateMipmaps = false;
  85. const planeGeometry = new THREE.PlaneGeometry( PLANE_WIDTH, PLANE_HEIGHT ).rotateX( Math.PI / 2 );
  86. depthMaterial = new THREE.NodeMaterial();
  87. const alphaDepth = float( 1 ).sub( depth ).mul( uDarkness );
  88. depthMaterial.colorNode = vec3( 0 );
  89. depthMaterial.opacityNode = alphaDepth;
  90. depthMaterial.depthTest = false;
  91. depthMaterial.depthWrite = false;
  92. shadowPlaneMaterial = new THREE.NodeMaterial();
  93. shadowPlaneMaterial.transparent = true;
  94. shadowPlaneMaterial.depthWrite = false;
  95. if ( ! renderTarget.texture.image ) renderTarget.texture.image = { width: 512, height: 512 };
  96. const blurredShadow = gaussianBlur( texture( renderTarget.texture ), uBlur, 4, { premultipliedAlpha: false } );
  97. shadowPlaneMaterial.colorNode = vec3( 0 );
  98. shadowPlaneMaterial.opacityNode = blurredShadow.a.mul( uShadowOpacity );
  99. plane = new THREE.Mesh( planeGeometry, shadowPlaneMaterial );
  100. plane.renderOrder = 1;
  101. plane.scale.y = - 1;
  102. plane.scale.z = - 1;
  103. shadowGroup.add( plane );
  104. fillPlaneMaterial = new THREE.NodeMaterial();
  105. fillPlaneMaterial.transparent = true;
  106. fillPlaneMaterial.depthWrite = false;
  107. fillPlaneMaterial.colorNode = uPlaneColor;
  108. fillPlaneMaterial.opacityNode = uPlaneOpacity;
  109. fillPlane = new THREE.Mesh( planeGeometry, fillPlaneMaterial );
  110. fillPlane.rotateX( Math.PI );
  111. shadowGroup.add( fillPlane );
  112. shadowCamera = new THREE.OrthographicCamera( - PLANE_WIDTH / 2, PLANE_WIDTH / 2, PLANE_HEIGHT / 2, - PLANE_HEIGHT / 2, 0, CAMERA_HEIGHT );
  113. shadowCamera.rotation.x = Math.PI / 2;
  114. shadowGroup.add( shadowCamera );
  115. cameraHelper = new THREE.CameraHelper( shadowCamera );
  116. renderer = new THREE.WebGPURenderer( { antialias: true } );
  117. renderer.setPixelRatio( window.devicePixelRatio );
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. renderer.setAnimationLoop( animate );
  120. document.body.appendChild( renderer.domElement );
  121. renderer.inspector = new Inspector();
  122. const params = {
  123. shadowBlur: state.shadow.blur,
  124. shadowDarkness: state.shadow.darkness,
  125. shadowOpacity: state.shadow.opacity,
  126. planeColor: state.plane.color,
  127. planeOpacity: state.plane.opacity,
  128. showWireframe: state.showWireframe
  129. };
  130. const gui = renderer.inspector.createParameters( 'Settings' );
  131. gui.add( params, 'shadowBlur', 0, 15, 0.1 ).onChange( () => {
  132. state.shadow.blur = params.shadowBlur;
  133. uBlur.value = state.shadow.blur;
  134. } );
  135. gui.add( params, 'shadowDarkness', 0.1, 5, 0.1 ).onChange( () => {
  136. state.shadow.darkness = params.shadowDarkness;
  137. uDarkness.value = state.shadow.darkness;
  138. } );
  139. gui.add( params, 'shadowOpacity', 0, 1, 0.01 ).onChange( () => {
  140. state.shadow.opacity = params.shadowOpacity;
  141. uShadowOpacity.value = state.shadow.opacity;
  142. } );
  143. gui.addColor( params, 'planeColor' ).onChange( () => {
  144. state.plane.color = params.planeColor;
  145. uPlaneColor.value.set( state.plane.color );
  146. } );
  147. gui.add( params, 'planeOpacity', 0, 1, 0.01 ).onChange( () => {
  148. state.plane.opacity = params.planeOpacity;
  149. uPlaneOpacity.value = state.plane.opacity;
  150. } );
  151. gui.add( params, 'showWireframe' ).onChange( () => {
  152. if ( params.showWireframe ) scene.add( cameraHelper );
  153. else scene.remove( cameraHelper );
  154. } );
  155. new OrbitControls( camera, renderer.domElement );
  156. }
  157. function onWindowResize() {
  158. camera.aspect = window.innerWidth / window.innerHeight;
  159. camera.updateProjectionMatrix();
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. }
  162. function animate() {
  163. meshes.forEach( m => {
  164. m.rotation.x += 0.01;
  165. m.rotation.y += 0.02;
  166. } );
  167. const initialBackground = scene.background;
  168. scene.background = null;
  169. const prevOverride = scene.overrideMaterial;
  170. const prevHelperVisible = cameraHelper.visible;
  171. cameraHelper.visible = false;
  172. scene.overrideMaterial = depthMaterial;
  173. const initialAutoClear = renderer.autoClear;
  174. renderer.autoClear = true;
  175. const initialClearAlpha = renderer.getClearAlpha ? renderer.getClearAlpha() : undefined;
  176. if ( initialClearAlpha !== undefined ) renderer.setClearAlpha( 0 );
  177. renderer.setRenderTarget( renderTarget );
  178. renderer.clear();
  179. renderer.render( scene, shadowCamera );
  180. scene.overrideMaterial = prevOverride;
  181. renderer.setRenderTarget( null );
  182. renderer.autoClear = initialAutoClear;
  183. if ( initialClearAlpha !== undefined ) renderer.setClearAlpha( initialClearAlpha );
  184. scene.background = initialBackground;
  185. cameraHelper.visible = prevHelperVisible;
  186. renderer.render( scene, camera );
  187. }
  188. </script>
  189. </body>
  190. </html>
粤ICP备19079148号