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