webgpu_backdrop_area.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/",
  19. "three/nodes": "./jsm/nodes/Nodes.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { color, depth, depthTexture, toneMapping, viewportSharedTexture, viewportMipTexture, viewportTopLeft, checker, uv, modelScale, MeshBasicNodeMaterial } from 'three/nodes';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  28. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  29. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. let camera, scene, renderer;
  32. let mixer, clock;
  33. init();
  34. function init() {
  35. if ( WebGPU.isAvailable() === false ) {
  36. document.body.appendChild( WebGPU.getErrorMessage() );
  37. throw new Error( 'No WebGPU support' );
  38. }
  39. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 25 );
  40. camera.position.set( 3, 2, 3 );
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0x333333 );
  43. camera.lookAt( 0, 1, 0 );
  44. clock = new THREE.Clock();
  45. // model
  46. const loader = new GLTFLoader();
  47. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  48. const object = gltf.scene;
  49. mixer = new THREE.AnimationMixer( object );
  50. const action = mixer.clipAction( gltf.animations[ 0 ] );
  51. action.play();
  52. scene.add( object );
  53. } );
  54. // volume
  55. const depthDistance = depthTexture().distance( depth );
  56. const depthAlphaNode = depthDistance.oneMinus().smoothstep( .90, 2 ).mul( 20 ).saturate();
  57. const depthBlurred = viewportMipTexture().bicubic( depthDistance.smoothstep( 0, .6 ).mul( 40 * 5 ).clamp( 0, 5 ) );
  58. const blurredBlur = new MeshBasicNodeMaterial();
  59. blurredBlur.backdropNode = depthBlurred.add( depthAlphaNode.mix( color( 0x0066ff ), 0 ) );
  60. blurredBlur.transparent = true;
  61. blurredBlur.side = THREE.DoubleSide;
  62. const volumeMaterial = new MeshBasicNodeMaterial();
  63. volumeMaterial.colorNode = color( 0x0066ff );
  64. volumeMaterial.backdropNode = viewportSharedTexture();
  65. volumeMaterial.backdropAlphaNode = depthAlphaNode;
  66. volumeMaterial.transparent = true;
  67. volumeMaterial.side = THREE.DoubleSide;
  68. const depthMaterial = new MeshBasicNodeMaterial();
  69. depthMaterial.backdropNode = depthAlphaNode;
  70. depthMaterial.transparent = true;
  71. depthMaterial.side = THREE.DoubleSide;
  72. const bicubicMaterial = new MeshBasicNodeMaterial();
  73. bicubicMaterial.backdropNode = viewportMipTexture().bicubic( 5 ); // @TODO: Move to alpha value [ 0, 1 ]
  74. bicubicMaterial.backdropAlphaNode = checker( uv().mul( 3 ).mul( modelScale.xy ) );
  75. bicubicMaterial.opacityNode = bicubicMaterial.backdropAlphaNode;
  76. bicubicMaterial.transparent = true;
  77. bicubicMaterial.side = THREE.DoubleSide;
  78. const pixelMaterial = new MeshBasicNodeMaterial();
  79. pixelMaterial.backdropNode = viewportSharedTexture( viewportTopLeft.mul( 100 ).floor().div( 100 ) );
  80. pixelMaterial.transparent = true;
  81. // box / floor
  82. const box = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), volumeMaterial );
  83. box.position.set( 0, 1, 0 );
  84. scene.add( box );
  85. const floor = new THREE.Mesh( new THREE.BoxGeometry( 1.99, .01, 1.99 ), new MeshBasicNodeMaterial( { color: 0x333333 } ) );
  86. floor.position.set( 0, 0, 0 );
  87. scene.add( floor );
  88. // renderer
  89. renderer = new WebGPURenderer( /*{ antialias: true }*/ );
  90. renderer.stencil = false;
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. renderer.setAnimationLoop( animate );
  94. renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, .15 );
  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. 'volume': volumeMaterial,
  104. 'depth': depthMaterial,
  105. 'bicubic': bicubicMaterial,
  106. 'pixel': pixelMaterial
  107. };
  108. const gui = new GUI();
  109. const options = { material: 'blurred' };
  110. box.material = materials[ options.material ];
  111. gui.add( box.scale, 'x', 0.1, 2, 0.01 );
  112. gui.add( box.scale, 'z', 0.1, 2, 0.01 );
  113. gui.add( options, 'material', Object.keys( materials ) ).onChange( name => {
  114. box.material = materials[ name ];
  115. } );
  116. }
  117. function onWindowResize() {
  118. camera.aspect = window.innerWidth / window.innerHeight;
  119. camera.updateProjectionMatrix();
  120. renderer.setSize( window.innerWidth, window.innerHeight );
  121. }
  122. function animate() {
  123. const delta = clock.getDelta();
  124. if ( mixer ) mixer.update( delta );
  125. renderer.render( scene, camera );
  126. }
  127. </script>
  128. </body>
  129. </html>