Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodes: Add SSAAPassNode. #29106

Merged
merged 7 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@
"webgpu_postprocessing_masking",
"webgpu_postprocessing_motion_blur",
"webgpu_postprocessing_sobel",
"webgpu_postprocessing_ssaa",
"webgpu_postprocessing_transition",
"webgpu_postprocessing",
"webgpu_procedural_texture",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
215 changes: 215 additions & 0 deletions examples/webgpu_postprocessing_ssaa.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webpu - postprocessing manual ssaa</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> - Unbiased Manual Supersamling Anti-Aliasing (SSAA) pass by <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a>
</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 { ssaaPass } from 'three/tsl';

import { Timer } from 'three/addons/misc/Timer.js';
import Stats from 'three/addons/libs/stats.module.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

let scene, mesh, renderer, postProcessing;
let camera, ssaaRenderPass;
let gui, stats, timer;

const params = {
sampleLevel: 3,
camera: 'perspective',
clearColor: 'black',
clearAlpha: 1.0,
viewOffsetX: 0,
autoRotate: true

};

init();

clearGui();

function clearGui() {

if ( gui ) gui.destroy();

gui = new GUI();

gui.add( params, 'sampleLevel', {
'Level 0: 1 Sample': 0,
'Level 1: 2 Samples': 1,
'Level 2: 4 Samples': 2,
'Level 3: 8 Samples': 3,
'Level 4: 16 Samples': 4,
'Level 5: 32 Samples': 5
} );
gui.add( params, 'clearColor', [ 'black', 'white', 'blue', 'green', 'red' ] );
gui.add( params, 'clearAlpha', 0, 1 );
gui.add( params, 'viewOffsetX', - 100, 100 );
gui.add( params, 'autoRotate' );

gui.open();

}

function init() {

const width = window.innerWidth;
const height = window.innerHeight;

renderer = new THREE.WebGPURenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
document.body.appendChild( renderer.domElement );

stats = new Stats();
document.body.appendChild( stats.dom );

timer = new Timer();

camera = new THREE.PerspectiveCamera( 65, width / height, 3, 10 );
camera.position.z = 7;
camera.setViewOffset( width, height, params.viewOffsetX, 0, width, height );

scene = new THREE.Scene();

const group = new THREE.Group();
scene.add( group );

const light = new THREE.PointLight( 0xefffef, 500 );
light.position.z = 10;
light.position.y = - 10;
light.position.x = - 10;
scene.add( light );

const light2 = new THREE.PointLight( 0xffefef, 500 );
light2.position.z = 10;
light2.position.x = - 10;
light2.position.y = 10;
scene.add( light2 );

const light3 = new THREE.PointLight( 0xefefff, 500 );
light3.position.z = 10;
light3.position.x = 10;
light3.position.y = - 10;
scene.add( light3 );

const light4 = new THREE.AmbientLight( 0xffffff, 0.2 );
scene.add( light4 );

const geometry = new THREE.SphereGeometry( 3, 48, 24 );
const material = new THREE.MeshStandardMaterial();

mesh = new THREE.InstancedMesh( geometry, material, 120 );

const dummy = new THREE.Mesh();
const color = new THREE.Color();

for ( let i = 0; i < mesh.count; i ++ ) {

dummy.position.x = Math.random() * 4 - 2;
dummy.position.y = Math.random() * 4 - 2;
dummy.position.z = Math.random() * 4 - 2;
dummy.rotation.x = Math.random();
dummy.rotation.y = Math.random();
dummy.rotation.z = Math.random();
dummy.scale.setScalar( Math.random() * 0.2 + 0.05 );

dummy.updateMatrix();

color.setHSL( Math.random(), 1.0, 0.3 );

mesh.setMatrixAt( i, dummy.matrix );
mesh.setColorAt( i, color );

}

scene.add( mesh );

// postprocessing

postProcessing = new THREE.PostProcessing( renderer );

ssaaRenderPass = ssaaPass( scene, camera );
const scenePassColor = ssaaRenderPass.getTextureNode();

postProcessing.outputNode = scenePassColor;

window.addEventListener( 'resize', onWindowResize );

}

function onWindowResize() {

const width = window.innerWidth;
const height = window.innerHeight;

camera.aspect = width / height;
camera.setViewOffset( width, height, params.viewOffsetX, 0, width, height );
camera.updateProjectionMatrix();

renderer.setSize( width, height );

}

function animate() {

timer.update();

if ( params.autoRotate ) {

const delta = timer.getDelta();

mesh.rotation.x += delta * 0.25;
mesh.rotation.y += delta * 0.5;

}

let newColor = ssaaRenderPass.clearColor;

switch ( params.clearColor ) {

case 'blue': newColor = 0x0000ff; break;
case 'red': newColor = 0xff0000; break;
case 'green': newColor = 0x00ff00; break;
case 'white': newColor = 0xffffff; break;
case 'black': newColor = 0x000000; break;

}

ssaaRenderPass.clearColor.set( newColor );
ssaaRenderPass.clearAlpha = params.clearAlpha;

ssaaRenderPass.sampleLevel = params.sampleLevel;

camera.view.offsetX = params.viewOffsetX;

postProcessing.render();

stats.update();

}

</script>
</body>
</html>
1 change: 1 addition & 0 deletions src/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export { default as BloomNode, bloom } from './display/BloomNode.js';
export { default as TransitionNode, transition } from './display/TransitionNode.js';
export { default as RenderOutputNode, renderOutput } from './display/RenderOutputNode.js';
export { default as PixelationPassNode, pixelationPass } from './display/PixelationPassNode.js';
export { default as SSAAPassNode, ssaaPass } from './display/SSAAPassNode.js';
export { bleach } from './display/BleachBypassNode.js';
export { sepia } from './display/SepiaNode.js';

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/core/MRTNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { addNodeClass } from './Node.js';
import OutputStructNode from './OutputStructNode.js';
import { nodeProxy, vec4 } from '../shadernode/ShaderNode.js';

function getTextureIndex( textures, name ) {
export function getTextureIndex( textures, name ) {

for ( let i = 0; i < textures.length; i ++ ) {

Expand Down
Loading