2
0

webgpu_backdrop_area.html 5.4 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.module.js",
  17. "three/addons/": "./jsm/",
  18. "three/nodes": "./jsm/nodes/Nodes.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { color, depth, depthTexture, toneMapping, viewportSharedTexture, viewportMipTexture, viewportTopLeft, checker, uv, modelScale, MeshBasicNodeMaterial } from 'three/nodes';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  27. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  28. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.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. if ( WebGPU.isAvailable() === false ) {
  35. document.body.appendChild( WebGPU.getErrorMessage() );
  36. throw new Error( 'No WebGPU support' );
  37. }
  38. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 25 );
  39. camera.position.set( 3, 2, 3 );
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0x333333 );
  42. camera.lookAt( 0, 1, 0 );
  43. clock = new THREE.Clock();
  44. // model
  45. const loader = new GLTFLoader();
  46. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  47. const object = gltf.scene;
  48. mixer = new THREE.AnimationMixer( object );
  49. const action = mixer.clipAction( gltf.animations[ 0 ] );
  50. action.play();
  51. scene.add( object );
  52. } );
  53. // volume
  54. const depthDistance = depthTexture().distance( depth );
  55. const depthAlphaNode = depthDistance.oneMinus().smoothstep( .90, 2 ).mul( 20 ).saturate();
  56. const depthBlurred = viewportMipTexture().bicubic( depthDistance.smoothstep( 0, .6 ).mul( 40 * 5 ).clamp( 0, 5 ) );
  57. const blurredBlur = new MeshBasicNodeMaterial();
  58. blurredBlur.backdropNode = depthBlurred.add( depthAlphaNode.mix( color( 0x0066ff ), 0 ) );
  59. blurredBlur.transparent = true;
  60. blurredBlur.side = THREE.DoubleSide;
  61. const volumeMaterial = new MeshBasicNodeMaterial();
  62. volumeMaterial.colorNode = color( 0x0066ff );
  63. volumeMaterial.backdropNode = viewportSharedTexture();
  64. volumeMaterial.backdropAlphaNode = depthAlphaNode;
  65. volumeMaterial.transparent = true;
  66. volumeMaterial.side = THREE.DoubleSide;
  67. const depthMaterial = new MeshBasicNodeMaterial();
  68. depthMaterial.backdropNode = depthAlphaNode;
  69. depthMaterial.transparent = true;
  70. depthMaterial.side = THREE.DoubleSide;
  71. const bicubicMaterial = new MeshBasicNodeMaterial();
  72. bicubicMaterial.backdropNode = viewportMipTexture().bicubic( 5 ); // @TODO: Move to alpha value [ 0, 1 ]
  73. bicubicMaterial.backdropAlphaNode = checker( uv().mul( 3 ).mul( modelScale.xy ) );
  74. bicubicMaterial.opacityNode = bicubicMaterial.backdropAlphaNode;
  75. bicubicMaterial.transparent = true;
  76. bicubicMaterial.side = THREE.DoubleSide;
  77. const pixelMaterial = new MeshBasicNodeMaterial();
  78. pixelMaterial.backdropNode = viewportSharedTexture( viewportTopLeft.mul( 100 ).floor().div( 100 ) );
  79. pixelMaterial.transparent = true;
  80. // box / floor
  81. const box = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), volumeMaterial );
  82. box.position.set( 0, 1, 0 );
  83. scene.add( box );
  84. const floor = new THREE.Mesh( new THREE.BoxGeometry( 1.99, .01, 1.99 ), new MeshBasicNodeMaterial( { color: 0x333333 } ) );
  85. floor.position.set( 0, 0, 0 );
  86. scene.add( floor );
  87. // renderer
  88. renderer = new WebGPURenderer( /*{ antialias: true }*/ );
  89. renderer.stencil = false;
  90. renderer.setPixelRatio( window.devicePixelRatio );
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. renderer.setAnimationLoop( animate );
  93. renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, .15 );
  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. 'volume': volumeMaterial,
  103. 'depth': depthMaterial,
  104. 'bicubic': bicubicMaterial,
  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>