webgpu_texturegrad.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.module.js",
  19. "three/addons/": "./jsm/",
  20. "three/nodes": "./jsm/nodes/Nodes.js"
  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, tslFn, MeshBasicNodeMaterial } from 'three/nodes';
  27. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  28. // WebGPU Backend
  29. init();
  30. // WebGL Backend
  31. init( true );
  32. async function init( forceWebGL = false ) {
  33. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  34. const camera = new THREE.OrthographicCamera( - aspect, aspect );
  35. camera.position.z = 2;
  36. const scene = new THREE.Scene();
  37. // texture
  38. const material = new MeshBasicNodeMaterial( { color: 0xffffff } );
  39. // load async brick_diffuse
  40. const map = await new THREE.TextureLoader().loadAsync( 'textures/uv_grid_opengl.jpg' );
  41. const elapsedTime = timerLocal();
  42. material.colorNode = tslFn( () => {
  43. const color = vec4( 1. ).toVar();
  44. const vuv = uv().toVar();
  45. const blur = pow( float( 0.0625 ).sub( cos( vuv.x.mul( 20.0 ).add( elapsedTime ) ) ).mul( 0.0625 ), 2.0 );
  46. const grad = vec2( blur ).toVar();
  47. If( vuv.y.greaterThan( 0.5 ), () => {
  48. grad.assign( 0 );
  49. } );
  50. color.assign(
  51. texture( map, vuv.add( vec2( blur, blur ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 )
  52. .add( texture( map, vuv.add( vec2( blur, blur.negate() ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  53. .add( texture( map, vuv.add( vec2( blur.negate(), blur ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  54. .add( texture( map, vuv.add( vec2( blur.negate(), blur.negate() ).mul( 0.5 ) ) ).grad( grad, grad ).mul( 0.25 ) )
  55. );
  56. If( vuv.y.greaterThan( 0.497 ).and( vuv.y.lessThan( 0.503 ) ), () => {
  57. color.assign( 1 );
  58. } );
  59. return color;
  60. } )();
  61. //
  62. const box = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  63. scene.add( box );
  64. const renderer = new WebGPURenderer( { antialias: false, forceWebGL: forceWebGL, trackTimestamp: true } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  67. document.body.appendChild( renderer.domElement );
  68. renderer.domElement.style.position = 'absolute';
  69. renderer.domElement.style.top = '0';
  70. renderer.domElement.style.left = '0';
  71. renderer.domElement.style.width = '50%';
  72. renderer.domElement.style.height = '100%';
  73. if ( forceWebGL ) {
  74. renderer.domElement.style.left = '50%';
  75. scene.background = new THREE.Color( 0x212121 );
  76. } else {
  77. scene.background = new THREE.Color( 0x313131 );
  78. }
  79. //
  80. const animate = async function () {
  81. await renderer.renderAsync( scene, camera );
  82. };
  83. renderer.setAnimationLoop( animate );
  84. window.addEventListener( 'resize', onWindowResize );
  85. function onWindowResize() {
  86. renderer.setSize( window.innerWidth / 2, window.innerHeight );
  87. const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
  88. const frustumHeight = camera.top - camera.bottom;
  89. camera.left = - frustumHeight * aspect / 2;
  90. camera.right = frustumHeight * aspect / 2;
  91. camera.updateProjectionMatrix();
  92. renderer.render( scene, camera );
  93. }
  94. }
  95. </script>
  96. </body>
  97. </html>