webgpu_loader_materialx.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - materialx loader</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. .dg .property-name {
  10. width: 20% !important;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - materialx loader<br />
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.webgpu.js",
  22. "three/tsl": "../build/three.webgpu.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  30. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  31. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  32. import { MaterialXLoader } from './jsm/loaders/MaterialXLoader.js';
  33. const SAMPLE_PATH = 'https://raw.githubusercontent.com/materialx/MaterialX/main/resources/Materials/Examples/StandardSurface/';
  34. const samples = [
  35. 'standard_surface_brass_tiled.mtlx',
  36. 'standard_surface_brick_procedural.mtlx',
  37. 'standard_surface_carpaint.mtlx',
  38. //'standard_surface_chess_set.mtlx',
  39. 'standard_surface_chrome.mtlx',
  40. 'standard_surface_copper.mtlx',
  41. //'standard_surface_default.mtlx',
  42. //'standard_surface_glass.mtlx',
  43. //'standard_surface_glass_tinted.mtlx',
  44. 'standard_surface_gold.mtlx',
  45. //'standard_surface_greysphere.mtlx',
  46. //'standard_surface_greysphere_calibration.mtlx',
  47. 'standard_surface_jade.mtlx',
  48. //'standard_surface_look_brass_tiled.mtlx',
  49. //'standard_surface_look_wood_tiled.mtlx',
  50. 'standard_surface_marble_solid.mtlx',
  51. 'standard_surface_metal_brushed.mtlx',
  52. 'standard_surface_plastic.mtlx',
  53. //'standard_surface_thin_film.mtlx',
  54. 'standard_surface_velvet.mtlx',
  55. 'standard_surface_wood_tiled.mtlx'
  56. ];
  57. let camera, scene, renderer, prefab;
  58. const models = [];
  59. init();
  60. function init() {
  61. const container = document.createElement( 'div' );
  62. document.body.appendChild( container );
  63. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 50 );
  64. camera.position.set( 0, 3, 20 );
  65. scene = new THREE.Scene();
  66. renderer = new THREE.WebGPURenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.toneMapping = THREE.LinearToneMapping;
  70. renderer.toneMappingExposure = .5;
  71. renderer.setAnimationLoop( render );
  72. container.appendChild( renderer.domElement );
  73. //
  74. const controls = new OrbitControls( camera, renderer.domElement );
  75. controls.minDistance = 2;
  76. controls.maxDistance = 40;
  77. //
  78. new RGBELoader()
  79. .setPath( 'textures/equirectangular/' )
  80. .load( 'san_giuseppe_bridge_2k.hdr', async ( texture ) => {
  81. texture.mapping = THREE.EquirectangularReflectionMapping;
  82. scene.background = texture;
  83. scene.environment = texture;
  84. prefab = ( await new GLTFLoader().loadAsync( './models/gltf/ShaderBall.glb' ) ).scene;
  85. for ( const sample of samples ) {
  86. addSample( sample );
  87. }
  88. } );
  89. window.addEventListener( 'resize', onWindowResize );
  90. }
  91. function updateModelsAlign() {
  92. const COLUMN_COUNT = 6;
  93. const DIST_X = 3;
  94. const DIST_Y = 4;
  95. const lineCount = Math.floor( models.length / COLUMN_COUNT ) - 1.5;
  96. const offsetX = ( DIST_X * ( COLUMN_COUNT - 1 ) ) * - .5;
  97. const offsetY = ( DIST_Y * lineCount ) * .5;
  98. for ( let i = 0; i < models.length; i ++ ) {
  99. const model = models[ i ];
  100. model.position.x = ( ( i % COLUMN_COUNT ) * DIST_X ) + offsetX;
  101. model.position.y = ( Math.floor( i / COLUMN_COUNT ) * - DIST_Y ) + offsetY;
  102. }
  103. }
  104. async function addSample( sample ) {
  105. const model = prefab.clone();
  106. models.push( model );
  107. scene.add( model );
  108. updateModelsAlign();
  109. //
  110. const material = await new MaterialXLoader()
  111. .setPath( SAMPLE_PATH )
  112. .loadAsync( sample )
  113. .then( ( { materials } ) => Object.values( materials ).pop() );
  114. const calibrationMesh = model.getObjectByName( 'Calibration_Mesh' );
  115. calibrationMesh.material = material;
  116. const Preview_Mesh = model.getObjectByName( 'Preview_Mesh' );
  117. Preview_Mesh.material = material;
  118. }
  119. //
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. }
  125. function render() {
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>