webgpu_backdrop_area.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/es-module-shims@1.6.3/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 depthAlphaNode = depthTexture().distance( depth ).oneMinus().smoothstep( .90, 2 ).mul( 20 ).saturate();
  56. const volumeMaterial = new MeshBasicNodeMaterial();
  57. volumeMaterial.colorNode = color( 0x0066ff );
  58. volumeMaterial.backdropNode = viewportSharedTexture();
  59. volumeMaterial.backdropAlphaNode = depthAlphaNode;
  60. volumeMaterial.transparent = true;
  61. volumeMaterial.side = THREE.DoubleSide;
  62. const depthMaterial = new MeshBasicNodeMaterial();
  63. depthMaterial.backdropNode = depthAlphaNode;
  64. depthMaterial.transparent = true;
  65. depthMaterial.side = THREE.DoubleSide;
  66. const bicubicMaterial = new MeshBasicNodeMaterial();
  67. bicubicMaterial.backdropNode = viewportMipTexture().bicubic( 5 ); // @TODO: Move to alpha value [ 0, 1 ]
  68. bicubicMaterial.backdropAlphaNode = checker( uv().mul( 3 ).mul( modelScale.xy ) );
  69. bicubicMaterial.opacityNode = bicubicMaterial.backdropAlphaNode;
  70. bicubicMaterial.transparent = true;
  71. bicubicMaterial.side = THREE.DoubleSide;
  72. const pixelMaterial = new MeshBasicNodeMaterial();
  73. pixelMaterial.backdropNode = viewportSharedTexture( viewportTopLeft.mul( 100 ).floor().div( 100 ) );
  74. pixelMaterial.transparent = true;
  75. // box / floor
  76. const box = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), volumeMaterial );
  77. box.position.set( 0, 1, 0 );
  78. scene.add( box );
  79. const floor = new THREE.Mesh( new THREE.BoxGeometry( 1.99, .01, 1.99 ), new MeshBasicNodeMaterial( { color: 0x333333 } ) );
  80. floor.position.set( 0, 0, 0 );
  81. scene.add( floor );
  82. // renderer
  83. renderer = new WebGPURenderer();
  84. renderer.stencil = false;
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. renderer.setAnimationLoop( animate );
  88. renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, .15 );
  89. document.body.appendChild( renderer.domElement );
  90. const controls = new OrbitControls( camera, renderer.domElement );
  91. controls.target.set( 0, 1, 0 );
  92. controls.update();
  93. window.addEventListener( 'resize', onWindowResize );
  94. // gui
  95. const materials = {
  96. 'volume': volumeMaterial,
  97. 'depth': depthMaterial,
  98. 'bicubic': bicubicMaterial,
  99. 'pixel': pixelMaterial
  100. };
  101. const gui = new GUI();
  102. const options = { material: 'volume' };
  103. gui.add( box.scale, 'x', 0.1, 2, 0.01 );
  104. gui.add( box.scale, 'z', 0.1, 2, 0.01 );
  105. gui.add( options, 'material', Object.keys( materials ) ).onChange( name => {
  106. box.material = materials[ name ];
  107. } );
  108. }
  109. function onWindowResize() {
  110. camera.aspect = window.innerWidth / window.innerHeight;
  111. camera.updateProjectionMatrix();
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. }
  114. function animate() {
  115. const delta = clock.getDelta();
  116. if ( mixer ) mixer.update( delta );
  117. renderer.render( scene, camera );
  118. }
  119. </script>
  120. </body>
  121. </html>
粤ICP备19079148号