You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.7 KiB

2 years ago
<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 async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/",
"three/nodes": "./jsm/nodes/Nodes.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import * as Nodes from 'three/nodes';
import WebGPU from 'three/addons/capabilities/WebGPU.js';
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
import WebGPUTextureRenderer from 'three/addons/renderers/webgpu/WebGPUTextureRenderer.js';
let camera, scene, renderer;
const mouse = new THREE.Vector2();
let cameraFX, sceneFX, textureRenderer;
let box;
const dpr = window.devicePixelRatio;
init();
animate();
function init() {
if ( WebGPU.isAvailable() === false ) {
document.body.appendChild( WebGPU.getErrorMessage() );
throw new Error( 'No WebGPU support' );
}
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
camera.position.z = 4;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x222222 );
// textured mesh
const loader = new THREE.TextureLoader();
const texture = loader.load( './textures/uv_grid_opengl.jpg' );
const geometryBox = new THREE.BoxGeometry();
const materialBox = new Nodes.MeshBasicNodeMaterial();
materialBox.colorNode = new Nodes.TextureNode( texture );
//
box = new THREE.Mesh( geometryBox, materialBox );
scene.add( box );
//
renderer = new WebGPURenderer();
renderer.setPixelRatio( dpr );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
textureRenderer = new WebGPUTextureRenderer( renderer );
textureRenderer.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
window.addEventListener( 'mousemove', onWindowMouseMove );
window.addEventListener( 'resize', onWindowResize );
// FX
cameraFX = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
sceneFX = new THREE.Scene();
const geometryFX = new THREE.PlaneGeometry( 2, 2 );
// modulate the final color based on the mouse position
const screenFXNode = new Nodes.OperatorNode( '+', new Nodes.UniformNode( mouse ), new Nodes.ConstNode( new THREE.Vector2( 0.5, 0.5 ) ) );
const materialFX = new Nodes.MeshBasicNodeMaterial();
materialFX.colorNode = new Nodes.OperatorNode( '*', new Nodes.TextureNode( textureRenderer.getTexture() ), screenFXNode );
const quad = new THREE.Mesh( geometryFX, materialFX );
sceneFX.add( quad );
}
function onWindowMouseMove( e ) {
mouse.x = e.offsetX / screen.width;
mouse.y = e.offsetY / screen.height;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
textureRenderer.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
}
function animate() {
requestAnimationFrame( animate );
box.rotation.x += 0.01;
box.rotation.y += 0.02;
textureRenderer.render( scene, camera );
renderer.render( sceneFX, cameraFX );
}
</script>
</body>
</html>