webgpu_backdrop_area.html 5.3 KB

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