| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - materialx loader</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>MaterialX Loader</span>
- </div>
- <small>
- MaterialX Standard Surface loader.
- </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, length, fract, vec4, positionWorld, smoothstep, max, abs, float, fwidth } 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 { MaterialXLoader } from 'three/addons/loaders/MaterialXLoader.js';
- import { Inspector } from 'three/addons/inspector/Inspector.js';
- const SAMPLE_PATH = 'https://raw.githubusercontent.com/materialx/MaterialX/main/resources/Materials/Examples/StandardSurface/';
- const LOCAL_SAMPLE_PATH = 'materialx/';
- const samples = [
- 'standard_surface_brass_tiled.mtlx',
- 'standard_surface_brick_procedural.mtlx',
- 'standard_surface_carpaint.mtlx',
- //'standard_surface_chess_set.mtlx',
- 'standard_surface_chrome.mtlx',
- 'standard_surface_copper.mtlx',
- //'standard_surface_default.mtlx',
- //'standard_surface_glass.mtlx',
- //'standard_surface_glass_tinted.mtlx',
- 'standard_surface_gold.mtlx',
- //'standard_surface_greysphere.mtlx',
- //'standard_surface_greysphere_calibration.mtlx',
- 'standard_surface_jade.mtlx',
- //'standard_surface_look_brass_tiled.mtlx',
- //'standard_surface_look_wood_tiled.mtlx',
- 'standard_surface_marble_solid.mtlx',
- 'standard_surface_metal_brushed.mtlx',
- 'standard_surface_plastic.mtlx',
- //'standard_surface_thin_film.mtlx',
- 'standard_surface_velvet.mtlx',
- 'standard_surface_wood_tiled.mtlx'
- ];
- const localSamples = [
- 'heightnormal.mtlx',
- 'conditional_if_float.mtlx',
- 'image_transform.mtlx',
- 'color3_vec3_cm_test.mtlx',
- 'rotate2d_test.mtlx',
- 'rotate3d_test.mtlx',
- 'heighttonormal_normal_input.mtlx',
- 'roughness_test.mtlx',
- 'opacity_test.mtlx',
- 'opacity_only_test.mtlx',
- 'specular_test.mtlx',
- 'ior_test.mtlx',
- 'combined_test.mtlx',
- 'texture_opacity_test.mtlx',
- 'transmission_test.mtlx',
- 'transmission_only_test.mtlx',
- 'transmission_rough.mtlx',
- 'thin_film_rainbow_test.mtlx',
- 'thin_film_ior_clamp_test.mtlx',
- 'sheen_test.mtlx',
- ];
- let camera, scene, renderer;
- let controls, prefab;
- const models = [];
- init();
- function init() {
- const container = document.createElement( 'div' );
- document.body.appendChild( container );
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.toneMapping = THREE.LinearToneMapping;
- renderer.toneMappingExposure = .5;
- renderer.inspector = new Inspector();
- renderer.setAnimationLoop( render );
- container.appendChild( renderer.domElement );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 200 );
- camera.position.set( 10, 10, 20 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xffffff );
- // Ground plane
- const material = new THREE.MeshBasicNodeMaterial();
- const gridXZ = Fn( ( [ gridSize = float( 1.0 ), dotWidth = float( 0.1 ), lineWidth = float( 0.02 ) ] ) => {
- const coord = positionWorld.xz.div( gridSize );
- const grid = fract( coord );
- // Screen-space derivative for automatic antialiasing
- const fw = fwidth( coord );
- const smoothing = max( fw.x, fw.y ).mul( 0.5 );
- // Create squares at cell centers
- const squareDist = max( abs( grid.x.sub( 0.5 ) ), abs( grid.y.sub( 0.5 ) ) );
- const dots = smoothstep( dotWidth.add( smoothing ), dotWidth.sub( smoothing ), squareDist );
- // Create grid lines
- const lineX = smoothstep( lineWidth.add( smoothing ), lineWidth.sub( smoothing ), abs( grid.x.sub( 0.5 ) ) );
- const lineZ = smoothstep( lineWidth.add( smoothing ), lineWidth.sub( smoothing ), abs( grid.y.sub( 0.5 ) ) );
- const lines = max( lineX, lineZ );
- return max( dots, lines );
- } );
- const radialGradient = Fn( ( [ radius = float( 10.0 ), falloff = float( 1.0 ) ] ) => {
- return smoothstep( radius, radius.sub( falloff ), length( positionWorld ) );
- } );
- // Create grid pattern
- const gridPattern = gridXZ( 1.0, 0.03, 0.005 );
- const baseColor = vec4( 1.0, 1.0, 1.0, 0.0 );
- const gridColor = vec4( 0.5, 0.5, 0.5, 1.0 );
- // Mix base color with grid lines
- material.colorNode = gridPattern.mix( baseColor, gridColor ).mul( radialGradient( 30.0, 20.0 ) );
- material.transparent = true;
- const plane = new THREE.Mesh( new THREE.CircleGeometry( 40 ), material );
- 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;
- //
- new HDRLoader()
- .setPath( 'textures/equirectangular/' )
- .load( 'san_giuseppe_bridge_2k.hdr', async ( texture ) => {
- texture.mapping = THREE.EquirectangularReflectionMapping;
- scene.environment = texture;
- prefab = ( await new GLTFLoader().loadAsync( './models/gltf/ShaderBall.glb' ) ).scene;
- for ( const sample of samples ) {
- await addSample( sample, SAMPLE_PATH );
- }
- for ( const sample of localSamples ) {
- await addSample( sample, LOCAL_SAMPLE_PATH );
- }
- addGUI();
- } );
- window.addEventListener( 'resize', onWindowResize );
- }
- function updateModelsAlign() {
- const COLUMN_COUNT = 6;
- const DIST_X = 3;
- const DIST_Z = 3;
- const lineCount = Math.floor( models.length / COLUMN_COUNT ) - 1.5;
- const offsetX = ( DIST_X * ( COLUMN_COUNT - 1 ) ) * - .5;
- const offsetZ = ( DIST_Z * lineCount ) * .5;
- for ( let i = 0; i < models.length; i ++ ) {
- const model = models[ i ];
- model.position.x = ( ( i % COLUMN_COUNT ) * DIST_X ) + offsetX;
- model.position.z = ( Math.floor( i / COLUMN_COUNT ) * - DIST_Z ) + offsetZ;
- }
- }
- async function addSample( sample, path ) {
- const model = prefab.clone();
- models.push( model );
- scene.add( model );
- updateModelsAlign();
- //
- const material = await new MaterialXLoader()
- .setPath( path )
- .loadAsync( sample )
- .then( ( { materials } ) => Object.values( materials ).pop() );
- const calibrationMesh = model.getObjectByName( 'Calibration_Mesh' );
- calibrationMesh.material = material;
- const previewMesh = model.getObjectByName( 'Preview_Mesh' );
- previewMesh.material = material;
- if ( material.transparent ) {
- calibrationMesh.renderOrder = 1;
- previewMesh.renderOrder = 2;
- }
- }
- function addGUI() {
- const gui = renderer.inspector.createParameters( 'MaterialX Loader' );
- 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 );
- } );
- }
- function setVisibility( name, visible ) {
- scene.traverse( function ( node ) {
- if ( node.isMesh ) {
- if ( node.name == name ) node.visible = visible;
- }
- } );
- }
- //
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function render() {
- controls.update();
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|