webgpu_texturegrad.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - texture gradient</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="main.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a>
  11. <br />This example demonstrate texture gradient
  12. <br /> Left canvas is using WebGPU Backend, right canvas is WebGL Backend.
  13. <br /> The bottom half of the texture benefits from the gradient to achieve better blur quality.
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.webgpu.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { If, vec4, float, timerLocal, cos, pow, vec2, uv, texture, Fn } from 'three/tsl';
  27. // WebGPU Backend
  28. init();
  29. // WebGL Backend
  30. init( true );
  31. async function init( forceWebGL = false ) {
  32. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  33. const camera = new THREE.OrthographicCamera( - aspect, aspect );
  34. camera.position.z = 2;
  35. const scene = new THREE.Scene();
  36. // texture
  37. const material = new THREE.MeshBasicNodeMaterial( { color: 0xffffff } );
  38. // load async brick_diffuse
  39. const map = await new THREE.TextureLoader().loadAsync( 'textures/uv_grid_opengl.jpg' );
  40. const elapsedTime = timerLocal();
  41. material.colorNode = Fn( () => {
  42. const color = vec4( 1. ).toVar();
  43. const vuv = uv().toVar();
  44. const blur = pow( float( 0.0625 ).sub( cos( vuv.x.mul( 20.0 ).add( elapsedTime ) ) ).mul( 0.0625 ), 2.0 );
  45. const grad = vec2( blur ).toVar();
  46. If( vuv.y.greaterThan( 0.5 ), () => {
  47. grad.assign( 0 );
  48. } );
  49. color.assign(
  50. texture( map, vuv.add( vec2( blur, blur ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 )
  51. .add( texture( map, vuv.add( vec2( blur, blur.negate() ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  52. .add( texture( map, vuv.add( vec2( blur.negate(), blur ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  53. .add( texture( map, vuv.add( vec2( blur.negate(), blur.negate() ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  54. );
  55. If( vuv.y.greaterThan( 0.497 ).and( vuv.y.lessThan( 0.503 ) ), () => {
  56. color.assign( 1 );
  57. } );
  58. return color;
  59. } )();
  60. //
  61. const box = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  62. scene.add( box );
  63. const renderer = new THREE.WebGPURenderer( { antialias: false, forceWebGL: forceWebGL, trackTimestamp: true } );
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  66. document.body.appendChild( renderer.domElement );
  67. renderer.domElement.style.position = 'absolute';
  68. renderer.domElement.style.top = '0';
  69. renderer.domElement.style.left = '0';
  70. renderer.domElement.style.width = '50%';
  71. renderer.domElement.style.height = '100%';
  72. if ( forceWebGL ) {
  73. renderer.domElement.style.left = '50%';
  74. scene.background = new THREE.Color( 0x212121 );
  75. } else {
  76. scene.background = new THREE.Color( 0x313131 );
  77. }
  78. //
  79. const animate = async function () {
  80. await renderer.renderAsync( scene, camera );
  81. };
  82. renderer.setAnimationLoop( animate );
  83. window.addEventListener( 'resize', onWindowResize );
  84. function onWindowResize() {
  85. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  86. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  87. const frustumHeight = camera.top - camera.bottom;
  88. camera.left = - frustumHeight * aspect / 2;
  89. camera.right = frustumHeight * aspect / 2;
  90. camera.updateProjectionMatrix();
  91. renderer.render( scene, camera );
  92. }
  93. }
  94. </script>
  95. </body>
  96. </html>