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.
 
 
 
 
 

146 lines
3.8 KiB

<html lang="en">
<head>
<title>three.js - WebGPU - Depth Texture</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 - Depth Texture
</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';
import { smoothstep, negate, positionView, invert } from 'three/nodes';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
let camera, scene, controls, renderer;
let cameraFX, sceneFX, textureRenderer;
const dpr = window.devicePixelRatio;
init();
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.01, 30 );
camera.position.z = 4;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x222222 );
// depth material
const material = new Nodes.MeshBasicNodeMaterial();
material.colorNode = invert( smoothstep( camera.near, camera.far, negate( positionView.z ) ) );
//
const geometry = new THREE.TorusKnotGeometry( 1, 0.3, 128, 64 );
const count = 50;
const scale = 5;
for ( let i = 0; i < count; i ++ ) {
const r = Math.random() * 2.0 * Math.PI;
const z = ( Math.random() * 2.0 ) - 1.0;
const zScale = Math.sqrt( 1.0 - z * z ) * scale;
const mesh = new THREE.Mesh( geometry, material );
mesh.position.set(
Math.cos( r ) * zScale,
Math.sin( r ) * zScale,
z * scale
);
mesh.rotation.set( Math.random(), Math.random(), Math.random() );
scene.add( mesh );
}
//
renderer = new WebGPURenderer();
renderer.setPixelRatio( dpr );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
document.body.appendChild( renderer.domElement );
textureRenderer = new WebGPUTextureRenderer( renderer );
textureRenderer.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
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 );
//
const materialFX = new Nodes.MeshBasicNodeMaterial();
materialFX.colorNode = new Nodes.TextureNode( textureRenderer.getTexture() );
const quad = new THREE.Mesh( geometryFX, materialFX );
sceneFX.add( quad );
//
controls = new OrbitControls( camera, renderer.domElement );
controls.enableDamping = true;
}
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() {
textureRenderer.render( scene, camera );
renderer.render( sceneFX, cameraFX );
}
</script>
</body>
</html>