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

Examples: Add TSL angular slicing #29007

Merged
merged 5 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -401,6 +401,7 @@
"webgpu_textures_2d-array",
"webgpu_textures_anisotropy",
"webgpu_textures_partialupdate",
"webgpu_tsl_angular_slicing",
"webgpu_tsl_coffee_smoke",
"webgpu_tsl_compute_attractors_particles",
"webgpu_tsl_earth",
Expand Down
Binary file added examples/models/gltf/gears.glb
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
230 changes: 230 additions & 0 deletions examples/webgpu_tsl_angular_slicing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - angular slicing</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 webgpu</a> - angular slicing
<br>
Based on <a href="https://threejs-journey.com/lessons/sliced-model-shader" target="_blank" rel="noopener">Three.js Journey</a> lesson
</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 { If, PI2, atan2, color, frontFacing, output, positionLocal, tslFn, uniform, vec4 } from 'three/tsl';

import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

let camera, scene, renderer, controls;

init();

function init() {

camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.set( - 5, 5, 12 );

scene = new THREE.Scene();

const gui = new GUI();

// environment

const rgbeLoader = new RGBELoader();
rgbeLoader.load( './textures/equirectangular/royal_esplanade_1k.hdr', ( environmentMap ) => {

environmentMap.mapping = THREE.EquirectangularReflectionMapping;

scene.background = environmentMap;
scene.environment = environmentMap;

} );

// lights

const directionalLight = new THREE.DirectionalLight( '#ffffff', 4 );
directionalLight.position.set( 6.25, 3, 4 );
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.set( 1024, 1024 );
directionalLight.shadow.camera.near = 0.1;
directionalLight.shadow.camera.far = 30;
directionalLight.shadow.camera.top = 8;
directionalLight.shadow.camera.right = 8;
directionalLight.shadow.camera.bottom = - 8;
directionalLight.shadow.camera.left = - 8;
directionalLight.shadow.normalBias = 0.05;
scene.add( directionalLight );

// TSL functions

const inAngle = tslFn( ( [ position, angleStart, angleArc ] ) => {

const angle = atan2( position.y, position.x ).sub( angleStart ).mod( PI2 ).toVar();
return angle.greaterThan( 0 ).and( angle.lessThan( angleArc ) );

} );

// materials

const defaultMaterial = new THREE.MeshPhysicalNodeMaterial( {
metalness: 0.5,
roughness: 0.25,
envMapIntensity: 0.5,
color: '#858080'
} );

const slicedMaterial = new THREE.MeshPhysicalNodeMaterial( {
metalness: 0.5,
roughness: 0.25,
envMapIntensity: 0.5,
color: '#858080',
side: THREE.DoubleSide
} );

// uniforms

const sliceStart = uniform( 1.75 );
const sliceArc = uniform( 1.25 );
const sliceColor = uniform( color( '#b62f58' ) );

// debug

gui.add( sliceStart, 'value', - Math.PI, Math.PI, 0.001 ).name( 'sliceStart' );
gui.add( sliceArc, 'value', 0, Math.PI * 2, 0.001 ).name( 'sliceArc' );
gui.addColor( { color: sliceColor.value.getHexString( THREE.SRGBColorSpace ) }, 'color' ).onChange( value => sliceColor.value.set( value ) );

// output

slicedMaterial.outputNode = tslFn( () => {

// discard

inAngle( positionLocal.xy, sliceStart, sliceArc ).discard();

// backface color

const finalOutput = output;
If( frontFacing.not(), () => {

finalOutput.assign( vec4( sliceColor, 1 ) );

} );

return finalOutput;

} )();

// shadow

slicedMaterial.shadowNode = tslFn( () => {

// discard

inAngle( positionLocal.xy, sliceStart, sliceArc ).discard();

return vec4( 0, 0, 0, 1 );

} )();

// model

const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath( 'jsm/libs/draco/' );

const gltfLoader = new GLTFLoader();
gltfLoader.setDRACOLoader( dracoLoader );

gltfLoader.load( './models/gltf/gears.glb', ( gltf ) => {

const model = gltf.scene;

model.traverse( ( child ) => {

if ( child.isMesh ) {

if ( child.name === 'outerHull' )
child.material = slicedMaterial;
else
child.material = defaultMaterial;

child.castShadow = true;
child.receiveShadow = true;

}

} );

scene.add( model );

} );

// plane

const plane = new THREE.Mesh(
new THREE.PlaneGeometry( 10, 10, 10 ),
new THREE.MeshStandardMaterial( { color: '#aaaaaa' } )
);
plane.receiveShadow = true;
plane.position.set( - 4, - 3, - 4 );
plane.lookAt( new THREE.Vector3( 0, 0, 0 ) );
scene.add( plane );

// renderer

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

sunag marked this conversation as resolved.
Show resolved Hide resolved
// controls

controls = new OrbitControls( camera, renderer.domElement );
controls.enableDamping = true;
controls.minDistance = 0.1;
controls.maxDistance = 50;

window.addEventListener( 'resize', onWindowResize );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

}

async function animate() {

controls.update();

renderer.render( scene, camera );

}

</script>
</body>
</html>
1 change: 1 addition & 0 deletions test/e2e/puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const exceptionList = [
'webgpu_tsl_compute_attractors_particles',
'webgpu_tsl_raging_sea',
'webgpu_tsl_halftone',
'webgpu_tsl_angular_slicing',
'webgpu_tsl_earth',

// WebGPU idleTime and parseTime too low
Expand Down