webgpu_texturegather.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - texture gather</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <meta property="og:title" content="three.js webgpu - texture gather">
  7. <meta property="og:type" content="website">
  8. <meta property="og:url" content="https://threejs.org/examples/webgpu_texturegather.html">
  9. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_texturegather.jpg">
  10. <link type="text/css" rel="stylesheet" href="example.css">
  11. </head>
  12. <body>
  13. <div id="info">
  14. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  15. <div class="title-wrapper">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Texture Gather</span>
  17. </div>
  18. <small>
  19. This example demonstrates texture gather
  20. <br /> Left canvas is using WebGPU Backend, right canvas is WebGL Backend.
  21. <br /> The top half gathers color values and the bottom half gathers depth comparison.
  22. </small>
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/webgpu": "../build/three.webgpu.js",
  29. "three/tsl": "../build/three.tsl.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three/webgpu';
  36. import { If, vec4, uv, ivec2, texture, Fn } from 'three/tsl';
  37. // WebGPU Backend
  38. init();
  39. // WebGL Backend
  40. init( true );
  41. async function init( forceWebGL = false ) {
  42. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  43. const camera = new THREE.OrthographicCamera( - aspect, aspect );
  44. camera.position.z = 2;
  45. const scene = new THREE.Scene();
  46. // texture
  47. const material = new THREE.MeshBasicNodeMaterial( { color: 0xffffff } );
  48. const colorNode = texture();
  49. const depthNode = texture();
  50. material.colorNode = Fn( () => {
  51. const color = vec4( 1 ).toVar();
  52. const vuv = uv().toVar();
  53. If( vuv.y.greaterThan( 0.5 ), () => {
  54. color.assign(
  55. colorNode.sample( vuv.mul( 10 ) ).offset( ivec2( 0, 7 ) ).gather( 0 )
  56. );
  57. } ).Else( () => {
  58. color.assign(
  59. depthNode.sample( vuv ).offset( ivec2( 0, 7 ) ).gather( 0 ).compare( 1 )
  60. );
  61. } );
  62. return color;
  63. } )();
  64. //
  65. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  66. scene.add( plane );
  67. const renderer = new THREE.WebGPURenderer( { antialias: false, forceWebGL: forceWebGL } );
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  70. renderer.setAnimationLoop( animate );
  71. await renderer.init();
  72. document.body.appendChild( renderer.domElement );
  73. renderer.domElement.style.position = 'absolute';
  74. renderer.domElement.style.top = '0';
  75. renderer.domElement.style.left = '0';
  76. renderer.domElement.style.width = '50%';
  77. renderer.domElement.style.height = '100%';
  78. if ( forceWebGL ) {
  79. renderer.domElement.style.left = '50%';
  80. scene.background = new THREE.Color( 0x212121 );
  81. } else {
  82. scene.background = new THREE.Color( 0x313131 );
  83. }
  84. //
  85. const depthTexture = new THREE.DepthTexture();
  86. depthTexture.compareFunction = THREE.LessEqualCompare;
  87. const rt = new THREE.RenderTarget( 100, 100, {
  88. depthTexture,
  89. generateMipmaps: true,
  90. minFilter: THREE.LinearMipmapLinearFilter
  91. } );
  92. rt.texture.wrapS = THREE.RepeatWrapping;
  93. rt.texture.wrapT = THREE.RepeatWrapping;
  94. const cameraZ = 2.5;
  95. const rtScene = new THREE.Scene();
  96. rtScene.background = new THREE.Color( 0x808080 );
  97. const rtCamera = new THREE.PerspectiveCamera( 50, 1, cameraZ - 0.5 * Math.sqrt( 3 ), cameraZ + 0.5 * Math.sqrt( 3 ) );
  98. rtCamera.position.z = cameraZ;
  99. const dirLight = new THREE.DirectionalLight();
  100. dirLight.position.set( 1, 1, 0 );
  101. rtScene.add( dirLight );
  102. rtScene.add( new THREE.AmbientLight( 0xffffff, 0.1 ) );
  103. const box = new THREE.Mesh(
  104. new THREE.BoxGeometry(),
  105. new THREE.MeshStandardNodeMaterial( { color: 0xff0000 } )
  106. );
  107. box.rotation.set( Math.PI / 4, Math.PI / 4, 0 );
  108. rtScene.add( box );
  109. renderer.setRenderTarget( rt );
  110. renderer.render( rtScene, rtCamera );
  111. renderer.setRenderTarget( null );
  112. colorNode.value = rt.texture;
  113. depthNode.value = rt.depthTexture;
  114. //
  115. function animate() {
  116. renderer.render( scene, camera );
  117. }
  118. window.addEventListener( 'resize', onWindowResize );
  119. function onWindowResize() {
  120. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  121. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  122. const frustumHeight = camera.top - camera.bottom;
  123. camera.left = - frustumHeight * aspect / 2;
  124. camera.right = frustumHeight * aspect / 2;
  125. camera.updateProjectionMatrix();
  126. renderer.render( scene, camera );
  127. }
  128. }
  129. </script>
  130. </body>
  131. </html>
粤ICP备19079148号