| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - tsl graph</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="example.css">
- <style>
- .dg .property-name {
- width: 20% !important;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
- <div class="title-wrapper">
- <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>TSL Graph</span>
- </div>
- <small>
- TSL Graph Addons - <a href="https://www.tsl-graph.xyz/" target="_blank" rel="noopener">www.tsl-graph.xyz</a>
- </small>
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/webgpu": "../build/three.webgpu.js",
- "three/tsl": "../build/three.tsl.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three/webgpu';
- import { Fn, abs, fract, fwidth, length, max, saturate, smoothstep, vec3, vec4, positionWorld, float } from 'three/tsl';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- import { Inspector } from 'three/addons/inspector/Inspector.js';
- import { TSLGraphLoader } from 'three/addons/inspector/addons/tsl-graph/TSLGraphLoader.js';
- import { TSLGraphEditor } from 'three/addons/inspector/addons/tsl-graph/TSLGraphEditor.js';
- let camera, scene, renderer;
- let controls;
- let prefab;
- init();
- async function initTSLGraph() {
- // TSL Graph Editor
- const tslGraph = new TSLGraphEditor();
- renderer.inspector.addTab( tslGraph );
- renderer.inspector.setActiveTab( tslGraph );
- // Create Materials
- const m1 = new THREE.MeshPhysicalNodeMaterial();
- m1.userData.graphId = 'mat_1-physical';
- const m2 = new THREE.MeshStandardNodeMaterial();
- m2.userData.graphId = 'mat_2-standard';
- const m3 = new THREE.MeshPhongNodeMaterial();
- m3.userData.graphId = 'mat_3-phong';
- const m4 = new THREE.MeshBasicNodeMaterial();
- m4.userData.graphId = 'mat_4-basic';
- const materials = [ m1, m2, m3, m4 ];
- for ( let i = 0; i < materials.length; i ++ ) {
- createShaderBall( materials[ i ], new THREE.Vector3( ( i & 1 ) * 4 - 2, 0, ( i & 2 ) * 2 - 2 ) );
- }
- // Initialize
- // Load and apply TSL Graph from a file or from Local Storage if exists
- // Every time a TSL Graph is changed, it will be stored in the local storage
- if ( tslGraph.hasGraphs ) {
- tslGraph.apply( scene );
- } else {
- // Load a TSL Graph from a file
- const tslLoader = new TSLGraphLoader();
- const applier = await tslLoader.setPath( './shaders/' ).loadAsync( 'tsl-graphs.json' );
- applier.apply( scene );
- }
- // Picker a Material
- let boundingBox = null;
- const raycaster = new THREE.Raycaster();
- const pointer = new THREE.Vector2();
- function removeBoundingBox() {
- scene.remove( boundingBox );
- boundingBox.dispose();
- }
- tslGraph.addEventListener( 'change', ( { material } ) => {
- if ( material === null && boundingBox ) {
- removeBoundingBox();
- boundingBox = null;
- }
- } );
- tslGraph.addEventListener( 'remove', ( { graphId } ) => {
- scene.traverse( ( object ) => {
- if ( object.material && object.material.userData && object.material.userData.graphId === graphId ) {
- tslGraph.restoreMaterial( object.material );
- }
- } );
- } );
- const pointerDownPosition = new THREE.Vector2();
- renderer.domElement.addEventListener( 'pointerdown', ( e ) => {
- pointerDownPosition.set( e.clientX, e.clientY );
- } );
- renderer.domElement.addEventListener( 'pointerup', ( e ) => {
- if ( pointerDownPosition.distanceTo( pointer.set( e.clientX, e.clientY ) ) > 2 ) return;
- const rect = renderer.domElement.getBoundingClientRect();
- pointer.x = ( ( e.clientX - rect.left ) / rect.width ) * 2 - 1;
- pointer.y = - ( ( e.clientY - rect.top ) / rect.height ) * 2 + 1;
- raycaster.setFromCamera( pointer, camera );
- const intersects = raycaster.intersectObjects( scene.children, true );
- let graphMaterial = null;
- if ( intersects.length > 0 ) {
- for ( const intersect of intersects ) {
- const object = intersect.object;
- const material = object.material;
- if ( material.userData && material.userData.graphId ) {
- if ( boundingBox ) {
- removeBoundingBox();
- }
- boundingBox = new THREE.BoxHelper( object, 0xffff00 );
- scene.add( boundingBox );
- graphMaterial = material;
- }
- if ( object.isMesh || object.isSprite ) {
- break;
- }
- }
- }
- tslGraph.setMaterial( graphMaterial );
- } );
- }
- async function init() {
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.toneMapping = THREE.NeutralToneMapping;
- renderer.toneMappingExposure = .9;
- renderer.inspector = new Inspector();
- renderer.setAnimationLoop( render );
- document.body.appendChild( renderer.domElement );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 200 );
- camera.position.set( 3, 5, 8 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x444444 );
- await renderer.init();
- // Ground plane
- const plane = new THREE.Mesh( new THREE.CircleGeometry( 40 ), createGroudMaterial() );
- plane.rotation.x = - Math.PI / 2;
- plane.renderOrder = - 1;
- scene.add( plane );
- //
- controls = new OrbitControls( camera );
- controls.connect( renderer.domElement );
- controls.enableDamping = true;
- controls.minDistance = 2;
- controls.maxDistance = 40;
- //
- const environment = await new HDRLoader().setPath( 'textures/equirectangular/' ).loadAsync( 'monochrome_studio_02_1k.hdr' );
- environment.mapping = THREE.EquirectangularReflectionMapping;
- const light = new THREE.DirectionalLight( 0xffffff, 1 );
- light.position.set( 1, 1, 1 );
- scene.add( light );
- scene.environment = environment;
- prefab = ( await new GLTFLoader().loadAsync( './models/gltf/ShaderBall.glb' ) ).scene;
- await initTSLGraph();
- initGUI();
- // Events
- resize();
- window.addEventListener( 'resize', resize );
- renderer.inspector.addEventListener( 'resize', resize );
- }
- async function createShaderBall( material, position = new THREE.Vector3() ) {
- const model = prefab.clone();
- model.position.copy( position );
- scene.add( model );
- //
- const calibrationMesh = model.getObjectByName( 'Calibration_Mesh' );
- calibrationMesh.material = material;
- const previewMesh = model.getObjectByName( 'Preview_Mesh' );
- previewMesh.material = material;
- calibrationMesh.renderOrder = 1;
- previewMesh.renderOrder = 2;
- }
- function initGUI() {
- const gui = renderer.inspector.createParameters( 'Shader Ball' );
- const API = {
- showCalibrationMesh: true,
- showPreviewMesh: true
- };
- gui.add( API, 'showCalibrationMesh' )
- .name( 'Calibration Mesh' )
- .onChange( function ( value ) {
- setVisibility( 'Calibration_Mesh', value );
- } );
- gui.add( API, 'showPreviewMesh' )
- .name( 'Preview Mesh' )
- .onChange( function ( value ) {
- setVisibility( 'Preview_Mesh', value );
- } );
- renderer.inspector.hide();
- }
- function resize() {
- const size = renderer.inspector.getSize();
- const width = window.innerWidth - size.width;
- const height = window.innerHeight - size.height;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- function render() {
- if ( controls ) controls.update();
- renderer.render( scene, camera );
- }
- //
- function setVisibility( name, visible ) {
- scene.traverse( function ( node ) {
- if ( node.isMesh ) {
- if ( node.name == name ) node.visible = visible;
- }
- } );
- }
- function createGroudMaterial() {
- const material = new THREE.MeshBasicNodeMaterial();
- const grid = Fn( ( [ coord, lineWidth = float( 0.01 ), dotSize = float( 0.03 ) ] ) => {
- // https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8
- const g = fract( coord );
- const fw = fwidth( coord );
- const gx = abs( g.x.sub( 0.5 ) );
- const gy = abs( g.y.sub( 0.5 ) );
- const lineX = saturate( lineWidth.sub( gx ).div( fw.x ).add( 0.5 ) );
- const lineY = saturate( lineWidth.sub( gy ).div( fw.y ).add( 0.5 ) );
- const lines = max( lineX, lineY );
- const squareDist = max( gx, gy );
- const aa = max( fw.x, fw.y );
- const dots = smoothstep( dotSize.add( aa ), dotSize.sub( aa ), squareDist );
- return max( dots, lines );
- } );
- const fade = Fn( ( [ radius = float( 10.0 ), falloff = float( 1.0 ) ] ) => {
- return smoothstep( radius, radius.sub( falloff ), length( positionWorld ) );
- } );
- const gridColor = vec4( vec3( 0.2 ), 1.0 );
- const baseColor = vec4( vec3( 0.4 ), 0.0 );
- material.colorNode = grid( positionWorld.xz, 0.007, 0.03 ).mix( baseColor, gridColor ).mul( fade( 30.0, 20.0 ) );
- material.transparent = true;
- return material;
- }
- </script>
- </body>
- </html>
|