webgpu_rtt.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - rtt</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> webgpu - rtt
  11. </div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.webgpu.js",
  16. "three/tsl": "../build/three.webgpu.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { texture, uniform, saturation, hue } from 'three/tsl';
  24. let camera, scene, renderer;
  25. const mouse = new THREE.Vector2();
  26. let quadMesh, renderTarget;
  27. let box;
  28. const dpr = window.devicePixelRatio;
  29. init();
  30. function init() {
  31. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  32. camera.position.z = 3;
  33. scene = new THREE.Scene();
  34. scene.background = new THREE.Color( 0x0066FF );
  35. // textured mesh
  36. const loader = new THREE.TextureLoader();
  37. const uvTexture = loader.load( './textures/uv_grid_opengl.jpg' );
  38. const geometryBox = new THREE.BoxGeometry();
  39. const materialBox = new THREE.MeshBasicNodeMaterial();
  40. materialBox.colorNode = texture( uvTexture );
  41. //
  42. box = new THREE.Mesh( geometryBox, materialBox );
  43. scene.add( box );
  44. //
  45. renderer = new THREE.WebGPURenderer( { antialias: true } );
  46. renderer.setPixelRatio( dpr );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. renderer.setAnimationLoop( animate );
  49. document.body.appendChild( renderer.domElement );
  50. renderTarget = new THREE.RenderTarget( window.innerWidth * dpr, window.innerHeight * dpr );
  51. window.addEventListener( 'mousemove', onWindowMouseMove );
  52. window.addEventListener( 'resize', onWindowResize );
  53. // FX
  54. // modulate the final color based on the mouse position
  55. const screenFXNode = uniform( mouse );
  56. const materialFX = new THREE.MeshBasicNodeMaterial();
  57. materialFX.colorNode = hue( saturation( texture( renderTarget.texture ).rgb, screenFXNode.x.oneMinus() ), screenFXNode.y );
  58. quadMesh = new THREE.QuadMesh( materialFX );
  59. }
  60. function onWindowMouseMove( e ) {
  61. mouse.x = e.offsetX / window.innerWidth;
  62. mouse.y = e.offsetY / window.innerHeight;
  63. }
  64. function onWindowResize() {
  65. camera.aspect = window.innerWidth / window.innerHeight;
  66. camera.updateProjectionMatrix();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. renderTarget.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
  69. }
  70. function animate() {
  71. box.rotation.x += 0.01;
  72. box.rotation.y += 0.02;
  73. renderer.setRenderTarget( renderTarget );
  74. renderer.render( scene, camera );
  75. renderer.setRenderTarget( null );
  76. quadMesh.render( renderer );
  77. }
  78. </script>
  79. </body>
  80. </html>