webgpu_texturegrad.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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="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 Gradient</span>
  13. </div>
  14. <small>
  15. This example demonstrate texture gradient
  16. <br /> Left canvas is using WebGPU Backend, right canvas is WebGL Backend.
  17. <br /> The bottom half of the texture benefits from the gradient to achieve better blur quality.
  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, float, time, cos, pow, vec2, uv, 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. // load async brick_diffuse
  45. const map = await new THREE.TextureLoader().loadAsync( 'textures/uv_grid_opengl.jpg' );
  46. material.colorNode = Fn( () => {
  47. const color = vec4( 1. ).toVar();
  48. const vuv = uv().toVar();
  49. const blur = pow( float( 0.0625 ).sub( cos( vuv.x.mul( 20.0 ).add( time ) ) ).mul( 0.0625 ), 2.0 );
  50. const grad = vec2( blur ).toVar();
  51. If( vuv.y.greaterThan( 0.5 ), () => {
  52. grad.assign( 0 );
  53. } );
  54. color.assign(
  55. texture( map, vuv.add( vec2( blur, blur ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 )
  56. .add( texture( map, vuv.add( vec2( blur, blur.negate() ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  57. .add( texture( map, vuv.add( vec2( blur.negate(), blur ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  58. .add( texture( map, vuv.add( vec2( blur.negate(), blur.negate() ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  59. );
  60. If( vuv.y.greaterThan( 0.497 ).and( vuv.y.lessThan( 0.503 ) ), () => {
  61. color.assign( 1 );
  62. } );
  63. return color;
  64. } )();
  65. //
  66. const box = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  67. scene.add( box );
  68. const renderer = new THREE.WebGPURenderer( { antialias: false, forceWebGL: forceWebGL } );
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  71. renderer.setAnimationLoop( animate );
  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. function animate() {
  86. renderer.render( scene, camera );
  87. }
  88. window.addEventListener( 'resize', onWindowResize );
  89. function onWindowResize() {
  90. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  91. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  92. const frustumHeight = camera.top - camera.bottom;
  93. camera.left = - frustumHeight * aspect / 2;
  94. camera.right = frustumHeight * aspect / 2;
  95. camera.updateProjectionMatrix();
  96. renderer.render( scene, camera );
  97. }
  98. }
  99. </script>
  100. </body>
  101. </html>
粤ICP备19079148号