Skip to content

Examples: Added hashBlur and improve webgpu_backdrop_area example #29606

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

Merged
merged 4 commits into from
Oct 10, 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
26 changes: 26 additions & 0 deletions examples/jsm/tsl/display/hashBlur.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { float, Fn, vec2, sin, rand, degrees, cos, Loop, vec4 } from 'three/tsl';

// https://www.shadertoy.com/view/4lXXWn

export const hashBlur = /*#__PURE__*/ Fn( ( [ textureNode, bluramount = float( 0.1 ), repeats = float( 45 ) ] ) => {

const draw = ( uv ) => textureNode.uv( uv );

const targetUV = textureNode.uvNode;
Copy link
Collaborator

@Mugen87 Mugen87 Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be texture nodes without uvNode so maybe:

const targetUV = textureNode.uvNode || uv();

Without this fix, I can't use the blur with ssrPass.getTextureNode().

const blurred_image = vec4( 0. ).toVar();

//const map = textureNode.value;

Loop( { start: 0., end: repeats, type: 'float' }, ( { i } ) => {

const q = vec2( vec2( cos( degrees( i.div( repeats ).mul( 360. ) ) ), sin( degrees( i.div( repeats ).mul( 360. ) ) ) ).mul( rand( vec2( i, targetUV.x.add( targetUV.y ) ) ).add( bluramount ) ) );
const uv2 = vec2( targetUV.add( q.mul( bluramount ) ) );
blurred_image.addAssign( draw( uv2 ) );

} );

blurred_image.divAssign( repeats );

return blurred_image;

} );
Binary file modified examples/screenshots/webgpu_backdrop_area.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 23 additions & 28 deletions examples/webgpu_backdrop_area.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
<script type="module">

import * as THREE from 'three';
import { color, linearDepth, viewportLinearDepth, viewportSharedTexture, textureBicubic, viewportMipTexture, screenUV, checker, uv, modelScale } from 'three/tsl';
import { color, positionWorld, linearDepth, viewportLinearDepth, viewportSharedTexture, screenUV, hue, time, checker, uv, modelScale } from 'three/tsl';
import { hashBlur } from 'three/addons/tsl/display/hashBlur.js';

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

Expand All @@ -44,16 +45,12 @@
camera.position.set( 3, 2, 3 );

scene = new THREE.Scene();
scene.background = new THREE.Color( 0x777777 );
scene.backgroundNode = hue( screenUV.y.mix( color( 0x66bbff ), color( 0x4466ff ) ), time.mul( 0.1 ) );
camera.lookAt( 0, 1, 0 );

clock = new THREE.Clock();

const light = new THREE.PointLight( 0xffffff, 50 );
camera.add( light );
scene.add( camera );

const ambient = new THREE.AmbientLight( 0x4466ff, 1 );
const ambient = new THREE.AmbientLight( 0xffffff, 2 );
scene.add( ambient );

// model
Expand All @@ -79,43 +76,42 @@
const depthDistance = viewportLinearDepth.distance( linearDepth() );

const depthAlphaNode = depthDistance.oneMinus().smoothstep( .90, 2 ).mul( 10 ).saturate();
const depthBlurred = textureBicubic( viewportMipTexture(), depthDistance.smoothstep( 0, .6 ).mul( 40 * 5 ).clamp( 0, 5 ) );
const depthBlurred = hashBlur( viewportSharedTexture(), depthDistance.smoothstep( 0, .6 ).mul( 40 ).clamp().mul( .1 ) );

const blurredBlur = new THREE.MeshBasicNodeMaterial();
blurredBlur.backdropNode = depthBlurred.add( depthAlphaNode.mix( color( 0x0066ff ), 0 ) );
blurredBlur.backdropNode = depthBlurred.add( depthAlphaNode.mix( color( 0x003399 ).mul( .3 ), 0 ) );
blurredBlur.transparent = true;
blurredBlur.side = THREE.DoubleSide;

const volumeMaterial = new THREE.MeshBasicNodeMaterial();
volumeMaterial.colorNode = color( 0x0066ff );
volumeMaterial.backdropNode = viewportSharedTexture();
volumeMaterial.backdropAlphaNode = depthAlphaNode;
volumeMaterial.transparent = true;
volumeMaterial.side = THREE.DoubleSide;

const depthMaterial = new THREE.MeshBasicNodeMaterial();
depthMaterial.backdropNode = depthAlphaNode;
depthMaterial.transparent = true;
depthMaterial.side = THREE.DoubleSide;

const bicubicMaterial = new THREE.MeshBasicNodeMaterial();
bicubicMaterial.backdropNode = textureBicubic( viewportMipTexture(), 5 ); // @TODO: Move to alpha value [ 0, 1 ]
bicubicMaterial.backdropAlphaNode = checker( uv().mul( 3 ).mul( modelScale.xy ) );
bicubicMaterial.opacityNode = bicubicMaterial.backdropAlphaNode;
bicubicMaterial.transparent = true;
bicubicMaterial.side = THREE.DoubleSide;
const checkerMaterial = new THREE.MeshBasicNodeMaterial();
checkerMaterial.backdropNode = hashBlur( viewportSharedTexture(), .05 );
checkerMaterial.backdropAlphaNode = checker( uv().mul( 3 ).mul( modelScale.xy ) );
checkerMaterial.opacityNode = checkerMaterial.backdropAlphaNode;
checkerMaterial.transparent = true;
checkerMaterial.side = THREE.DoubleSide;

const pixelMaterial = new THREE.MeshBasicNodeMaterial();
pixelMaterial.backdropNode = viewportSharedTexture( screenUV.mul( 100 ).floor().div( 100 ) );
pixelMaterial.transparent = true;

// box / floor

const box = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), volumeMaterial );
const box = new THREE.Mesh( new THREE.BoxGeometry( 2, 2, 2 ), blurredBlur );
box.position.set( 0, 1, 0 );
box.renderOrder = 1;
scene.add( box );

const floor = new THREE.Mesh( new THREE.BoxGeometry( 1.99, .01, 1.99 ), new THREE.MeshBasicNodeMaterial( { color: 0x333333 } ) );
const floor = new THREE.Mesh( new THREE.BoxGeometry( 5, .01, 5 ), new THREE.MeshBasicNodeMaterial( {
color: 0xff6600,
opacityNode: positionWorld.xz.distance( 0 ).oneMinus().clamp(),
transparent: true,
depthWrite: false
} ) );
floor.position.set( 0, 0, 0 );
scene.add( floor );

Expand All @@ -125,8 +121,8 @@
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.toneMapping = THREE.LinearToneMapping;
renderer.toneMappingExposure = 0.2;
renderer.toneMapping = THREE.NeutralToneMapping;
renderer.toneMappingExposure = .9;
document.body.appendChild( renderer.domElement );

const controls = new OrbitControls( camera, renderer.domElement );
Expand All @@ -139,9 +135,8 @@

const materials = {
'blurred': blurredBlur,
'volume': volumeMaterial,
'depth': depthMaterial,
'bicubic': bicubicMaterial,
'checker': checkerMaterial,
'pixel': pixelMaterial
};

Expand Down
4 changes: 2 additions & 2 deletions src/nodes/utils/LoopNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ class LoopNode extends Node {
condition = param.condition;
update = param.update;

if ( typeof start === 'number' ) start = start.toString();
if ( typeof start === 'number' ) start = builder.generateConst( type, start );
else if ( start && start.isNode ) start = start.build( builder, type );

if ( typeof end === 'number' ) end = end.toString();
if ( typeof end === 'number' ) end = builder.generateConst( type, end );
else if ( end && end.isNode ) end = end.build( builder, type );

if ( start !== undefined && end === undefined ) {
Expand Down