webgpu_backdrop_area.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - WebGPU - Backdrop Area</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>Backdrop Area</span>
  14. </div>
  15. <small>
  16. Backdrop area effect.
  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 { color, positionWorld, linearDepth, viewportLinearDepth, viewportSharedTexture, screenUV, hue, time, checker, uv, modelScale } from 'three/tsl';
  32. import { hashBlur } from 'three/addons/tsl/display/hashBlur.js';
  33. import { Inspector } from 'three/addons/inspector/Inspector.js';
  34. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. let camera, scene, renderer;
  37. let mixer, clock;
  38. init();
  39. function init() {
  40. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 25 );
  41. camera.position.set( 3, 2, 3 );
  42. scene = new THREE.Scene();
  43. scene.backgroundNode = hue( screenUV.y.mix( color( 0x66bbff ), color( 0x4466ff ) ), time.mul( 0.1 ) );
  44. camera.lookAt( 0, 1, 0 );
  45. clock = new THREE.Clock();
  46. const ambient = new THREE.AmbientLight( 0xffffff, 2.5 );
  47. scene.add( ambient );
  48. // model
  49. const loader = new GLTFLoader();
  50. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  51. const object = gltf.scene;
  52. mixer = new THREE.AnimationMixer( object );
  53. const action = mixer.clipAction( gltf.animations[ 0 ] );
  54. action.play();
  55. scene.add( object );
  56. } );
  57. // volume
  58. // compare depth from viewportLinearDepth with linearDepth() to create a distance field
  59. // viewportLinearDepth return the linear depth of the scene
  60. // linearDepth() returns the linear depth of the mesh
  61. const depthDistance = viewportLinearDepth.distance( linearDepth() );
  62. const depthAlphaNode = depthDistance.oneMinus().smoothstep( .90, 2 ).mul( 10 ).saturate();
  63. const depthBlurred = hashBlur( viewportSharedTexture(), depthDistance.smoothstep( 0, .6 ).mul( 40 ).clamp().mul( .1 ) );
  64. const blurredBlur = new THREE.MeshBasicNodeMaterial();
  65. blurredBlur.backdropNode = depthBlurred.add( depthAlphaNode.mix( color( 0x003399 ).mul( .3 ), 0 ) );
  66. blurredBlur.transparent = true;
  67. blurredBlur.side = THREE.DoubleSide;
  68. const depthMaterial = new THREE.MeshBasicNodeMaterial();
  69. depthMaterial.backdropNode = depthAlphaNode;
  70. depthMaterial.transparent = true;
  71. depthMaterial.side = THREE.DoubleSide;
  72. const checkerMaterial = new THREE.MeshBasicNodeMaterial();
  73. checkerMaterial.backdropNode = hashBlur( viewportSharedTexture(), .05 );
  74. checkerMaterial.backdropAlphaNode = checker( uv().mul( 3 ).mul( modelScale.xy ) );
  75. checkerMaterial.opacityNode = checkerMaterial.backdropAlphaNode;
  76. checkerMaterial.transparent = true;
  77. checkerMaterial.side = THREE.DoubleSide;
  78. const pixelMaterial = new THREE.MeshBasicNodeMaterial();
  79. pixelMaterial.backdropNode = viewportSharedTexture( screenUV.mul( 100 ).floor().div( 100 ) );
  80. pixelMaterial.transparent = true;
  81. // box / floor
  82. const box = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), blurredBlur );
  83. box.position.set( 0, 1, 0 );
  84. box.renderOrder = 1;
  85. scene.add( box );
  86. const floor = new THREE.Mesh( new THREE.BoxGeometry( 5, .01, 5 ), new THREE.MeshBasicNodeMaterial( {
  87. color: 0xff6600,
  88. opacityNode: positionWorld.xz.distance( 0 ).oneMinus().clamp(),
  89. transparent: true,
  90. depthWrite: false
  91. } ) );
  92. floor.position.set( 0, 0, 0 );
  93. scene.add( floor );
  94. // renderer
  95. renderer = new THREE.WebGPURenderer( /*{ antialias: true }*/ );
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. renderer.setAnimationLoop( animate );
  99. renderer.toneMapping = THREE.NeutralToneMapping;
  100. renderer.toneMappingExposure = .9;
  101. renderer.inspector = new Inspector();
  102. document.body.appendChild( renderer.domElement );
  103. const controls = new OrbitControls( camera, renderer.domElement );
  104. controls.target.set( 0, 1, 0 );
  105. controls.update();
  106. window.addEventListener( 'resize', onWindowResize );
  107. // gui
  108. const materials = {
  109. 'blurred': blurredBlur,
  110. 'depth': depthMaterial,
  111. 'checker': checkerMaterial,
  112. 'pixel': pixelMaterial
  113. };
  114. const options = { material: 'blurred' };
  115. box.material = materials[ options.material ];
  116. const gui = renderer.inspector.createParameters( 'Scene settings' );
  117. gui.add( box.scale, 'x', 0.1, 2, 0.01 ).name( 'box scale x' );
  118. gui.add( box.scale, 'y', 0.1, 2, 0.01 ).name( 'box scale y' );
  119. gui.add( options, 'material', Object.keys( materials ) ).onChange( name => {
  120. box.material = materials[ name ];
  121. } );
  122. }
  123. function onWindowResize() {
  124. camera.aspect = window.innerWidth / window.innerHeight;
  125. camera.updateProjectionMatrix();
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. }
  128. function animate() {
  129. const delta = clock.getDelta();
  130. if ( mixer ) mixer.update( delta );
  131. renderer.render( scene, camera );
  132. }
  133. </script>
  134. </body>
  135. </html>
粤ICP备19079148号