webgpu_backdrop_area.html 5.6 KB

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