123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <html lang="en">
- <head>
- <title>three.js webgpu - rtt</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - rtt
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/tsl": "../build/three.webgpu.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { texture, uniform, saturation, hue } from 'three/tsl';
- let camera, scene, renderer;
- const mouse = new THREE.Vector2();
- let quadMesh, renderTarget;
- let box;
- const dpr = window.devicePixelRatio;
- init();
- function init() {
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
- camera.position.z = 3;
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x0066FF );
- // textured mesh
- const loader = new THREE.TextureLoader();
- const uvTexture = loader.load( './textures/uv_grid_opengl.jpg' );
- const geometryBox = new THREE.BoxGeometry();
- const materialBox = new THREE.MeshBasicNodeMaterial();
- materialBox.colorNode = texture( uvTexture );
- //
- box = new THREE.Mesh( geometryBox, materialBox );
- scene.add( box );
- //
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.setPixelRatio( dpr );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- renderTarget = new THREE.RenderTarget( window.innerWidth * dpr, window.innerHeight * dpr );
- window.addEventListener( 'mousemove', onWindowMouseMove );
- window.addEventListener( 'resize', onWindowResize );
- // FX
- // modulate the final color based on the mouse position
- const screenFXNode = uniform( mouse );
- const materialFX = new THREE.MeshBasicNodeMaterial();
- materialFX.colorNode = hue( saturation( texture( renderTarget.texture ).rgb, screenFXNode.x.oneMinus() ), screenFXNode.y );
- quadMesh = new THREE.QuadMesh( materialFX );
- }
- function onWindowMouseMove( e ) {
- mouse.x = e.offsetX / window.innerWidth;
- mouse.y = e.offsetY / window.innerHeight;
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderTarget.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
- }
- function animate() {
- box.rotation.x += 0.01;
- box.rotation.y += 0.02;
- renderer.setRenderTarget( renderTarget );
- renderer.render( scene, camera );
- renderer.setRenderTarget( null );
- quadMesh.render( renderer );
- }
- </script>
- </body>
- </html>
|