webgpu_mrt_mask.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - mrt mask</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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>MRT Mask</span>
  14. </div>
  15. <small>
  16. The mask is applied followed by a gaussian blur only on some selected materials.
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { color, screenUV, mrt, output, pass, vec4 } from 'three/tsl';
  32. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  33. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. let camera, scene, renderer;
  36. let postProcessing;
  37. let spheres, rotate = true;
  38. let mixer, clock;
  39. init();
  40. function init() {
  41. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  42. camera.position.set( 1, 2, 3 );
  43. scene = new THREE.Scene();
  44. scene.backgroundNode = screenUV.y.mix( color( 0x66bbff ), color( 0x4466ff ) ).mul( .05 );
  45. camera.lookAt( 0, 1, 0 );
  46. clock = new THREE.Clock();
  47. // lights
  48. const light = new THREE.SpotLight( 0xffffff, 1 );
  49. light.power = 2000;
  50. camera.add( light );
  51. scene.add( camera );
  52. const loader = new GLTFLoader();
  53. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  54. const object = gltf.scene;
  55. mixer = new THREE.AnimationMixer( object );
  56. const material = object.children[ 0 ].children[ 0 ].material;
  57. // add glow effect
  58. material.mrtNode = mrt( { mask: output.add( 1 ) } );
  59. const action = mixer.clipAction( gltf.animations[ 0 ] );
  60. action.play();
  61. scene.add( object );
  62. } );
  63. // spheres
  64. const geometry = new THREE.SphereGeometry( .3, 32, 16 );
  65. spheres = new THREE.Group();
  66. scene.add( spheres );
  67. function addSphere( color, mrtNode = null ) {
  68. const distance = 1;
  69. const id = spheres.children.length;
  70. const rotation = THREE.MathUtils.degToRad( id * 90 );
  71. const material = new THREE.MeshStandardNodeMaterial( { color } );
  72. material.mrtNode = mrtNode;
  73. const mesh = new THREE.Mesh( geometry, material );
  74. mesh.position.set(
  75. Math.cos( rotation ) * distance,
  76. 1,
  77. Math.sin( rotation ) * distance
  78. );
  79. spheres.add( mesh );
  80. }
  81. addSphere( 0x0000ff, mrt( { mask: output } ) );
  82. addSphere( 0x00ff00 );
  83. addSphere( 0xff0000 );
  84. addSphere( 0x00ffff );
  85. // renderer
  86. renderer = new THREE.WebGPURenderer( { antialias: true } );
  87. renderer.setPixelRatio( window.devicePixelRatio );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. renderer.setAnimationLoop( animate );
  90. renderer.toneMapping = THREE.NeutralToneMapping;
  91. renderer.toneMappingExposure = 0.4;
  92. document.body.appendChild( renderer.domElement );
  93. // post processing
  94. const scenePass = pass( scene, camera );
  95. scenePass.setMRT( mrt( {
  96. output: output.renderOutput(),
  97. mask: vec4( 0 ) // empty as default, custom materials can set this
  98. } ) );
  99. const colorPass = scenePass.getTextureNode();
  100. const maskPass = scenePass.getTextureNode( 'mask' );
  101. postProcessing = new THREE.PostProcessing( renderer );
  102. postProcessing.outputColorTransform = false;
  103. postProcessing.outputNode = colorPass.add( gaussianBlur( maskPass, 1, 20 ).mul( .3 ) ).renderOutput();
  104. // controls
  105. const controls = new OrbitControls( camera, renderer.domElement );
  106. controls.target.set( 0, 1, 0 );
  107. controls.addEventListener( 'start', () => rotate = false );
  108. controls.addEventListener( 'end', () => rotate = true );
  109. controls.update();
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. function onWindowResize() {
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. }
  117. function animate() {
  118. const delta = clock.getDelta();
  119. if ( mixer ) mixer.update( delta );
  120. if ( rotate ) spheres.rotation.y += delta * 0.5;
  121. postProcessing.render();
  122. }
  123. </script>
  124. </body>
  125. </html>
粤ICP备19079148号