webgpu_texturegrad.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. <meta property="og:title" content="three.js webgpu - texture gradient">
  7. <meta property="og:type" content="website">
  8. <meta property="og:url" content="https://threejs.org/examples/webgpu_texturegrad.html">
  9. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_texturegrad.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 Gradient</span>
  17. </div>
  18. <small>
  19. This example demonstrate texture gradient
  20. <br /> Left canvas is using WebGPU Backend, right canvas is WebGL Backend.
  21. <br /> The bottom half of the texture benefits from the gradient to achieve better blur quality.
  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, float, time, cos, pow, vec2, uv, 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. // load async brick_diffuse
  49. const map = await new THREE.TextureLoader().loadAsync( 'textures/uv_grid_opengl.jpg' );
  50. material.colorNode = Fn( () => {
  51. const color = vec4( 1. ).toVar();
  52. const vuv = uv().toVar();
  53. const blur = pow( float( 0.0625 ).sub( cos( vuv.x.mul( 20.0 ).add( time ) ) ).mul( 0.0625 ), 2.0 );
  54. const grad = vec2( blur ).toVar();
  55. If( vuv.y.greaterThan( 0.5 ), () => {
  56. grad.assign( 0 );
  57. } );
  58. color.assign(
  59. texture( map, vuv.add( vec2( blur, blur ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 )
  60. .add( texture( map, vuv.add( vec2( blur, blur.negate() ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  61. .add( texture( map, vuv.add( vec2( blur.negate(), blur ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  62. .add( texture( map, vuv.add( vec2( blur.negate(), blur.negate() ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  63. );
  64. If( vuv.y.greaterThan( 0.497 ).and( vuv.y.lessThan( 0.503 ) ), () => {
  65. color.assign( 1 );
  66. } );
  67. return color;
  68. } )();
  69. //
  70. const box = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  71. scene.add( box );
  72. const renderer = new THREE.WebGPURenderer( { antialias: false, forceWebGL: forceWebGL } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  75. renderer.setAnimationLoop( animate );
  76. document.body.appendChild( renderer.domElement );
  77. renderer.domElement.style.position = 'absolute';
  78. renderer.domElement.style.top = '0';
  79. renderer.domElement.style.left = '0';
  80. renderer.domElement.style.width = '50%';
  81. renderer.domElement.style.height = '100%';
  82. if ( forceWebGL ) {
  83. renderer.domElement.style.left = '50%';
  84. scene.background = new THREE.Color( 0x212121 );
  85. } else {
  86. scene.background = new THREE.Color( 0x313131 );
  87. }
  88. //
  89. function animate() {
  90. renderer.render( scene, camera );
  91. }
  92. window.addEventListener( 'resize', onWindowResize );
  93. function onWindowResize() {
  94. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  95. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  96. const frustumHeight = camera.top - camera.bottom;
  97. camera.left = - frustumHeight * aspect / 2;
  98. camera.right = frustumHeight * aspect / 2;
  99. camera.updateProjectionMatrix();
  100. renderer.render( scene, camera );
  101. }
  102. }
  103. </script>
  104. </body>
  105. </html>
粤ICP备19079148号