webgpu_loader_materialx.html 4.9 KB

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