webgpu_backdrop_area.html 5.4 KB

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