webgpu_texturegather.html 4.7 KB

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