webgpu_loader_materialx.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/webgpu": "../build/three.webgpu.js",
  23. "three/tsl": "../build/three.tsl.js",
  24. "three/addons/": "./jsm/"
  25. }
  26. }
  27. </script>
  28. <script type="module">
  29. import * as THREE from 'three/webgpu';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  32. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  33. import { MaterialXLoader } from 'three/addons/loaders/MaterialXLoader.js';
  34. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  35. const SAMPLE_PATH = 'https://raw.githubusercontent.com/materialx/MaterialX/main/resources/Materials/Examples/StandardSurface/';
  36. const LOCAL_SAMPLE_PATH = 'materialx/';
  37. const samples = [
  38. 'standard_surface_brass_tiled.mtlx',
  39. 'standard_surface_brick_procedural.mtlx',
  40. 'standard_surface_carpaint.mtlx',
  41. //'standard_surface_chess_set.mtlx',
  42. 'standard_surface_chrome.mtlx',
  43. 'standard_surface_copper.mtlx',
  44. //'standard_surface_default.mtlx',
  45. //'standard_surface_glass.mtlx',
  46. //'standard_surface_glass_tinted.mtlx',
  47. 'standard_surface_gold.mtlx',
  48. //'standard_surface_greysphere.mtlx',
  49. //'standard_surface_greysphere_calibration.mtlx',
  50. 'standard_surface_jade.mtlx',
  51. //'standard_surface_look_brass_tiled.mtlx',
  52. //'standard_surface_look_wood_tiled.mtlx',
  53. 'standard_surface_marble_solid.mtlx',
  54. 'standard_surface_metal_brushed.mtlx',
  55. 'standard_surface_plastic.mtlx',
  56. //'standard_surface_thin_film.mtlx',
  57. 'standard_surface_velvet.mtlx',
  58. 'standard_surface_wood_tiled.mtlx'
  59. ];
  60. const localSamples = [
  61. 'heightnormal.mtlx',
  62. 'conditional_if_float.mtlx',
  63. 'image_transform.mtlx',
  64. 'color3_vec3_cm_test.mtlx',
  65. 'rotate2d_test.mtlx',
  66. 'rotate3d_test.mtlx',
  67. 'heighttonormal_normal_input.mtlx',
  68. 'roughness_test.mtlx',
  69. 'opacity_test.mtlx',
  70. 'opacity_only_test.mtlx',
  71. 'specular_test.mtlx',
  72. 'ior_test.mtlx',
  73. 'combined_test.mtlx',
  74. 'texture_opacity_test.mtlx',
  75. 'transmission_test.mtlx',
  76. 'transmission_only_test.mtlx',
  77. 'transmission_rough.mtlx',
  78. 'thin_film_rainbow_test.mtlx',
  79. 'thin_film_ior_clamp_test.mtlx',
  80. 'sheen_test.mtlx',
  81. ];
  82. let camera, scene, renderer, prefab;
  83. const models = [];
  84. init();
  85. function init() {
  86. const container = document.createElement( 'div' );
  87. document.body.appendChild( container );
  88. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 50 );
  89. camera.position.set( 0, 3, 20 );
  90. scene = new THREE.Scene();
  91. renderer = new THREE.WebGPURenderer( { antialias: true, alpha: true } );
  92. renderer.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. renderer.toneMapping = THREE.LinearToneMapping;
  95. renderer.toneMappingExposure = .5;
  96. renderer.setAnimationLoop( render );
  97. container.appendChild( renderer.domElement );
  98. //
  99. const controls = new OrbitControls( camera, renderer.domElement );
  100. controls.minDistance = 2;
  101. controls.maxDistance = 40;
  102. //
  103. new RGBELoader()
  104. .setPath( 'textures/equirectangular/' )
  105. .load( 'san_giuseppe_bridge_2k.hdr', async ( texture ) => {
  106. texture.mapping = THREE.EquirectangularReflectionMapping;
  107. scene.background = texture;
  108. scene.environment = texture;
  109. prefab = ( await new GLTFLoader().loadAsync( './models/gltf/ShaderBall.glb' ) ).scene;
  110. for ( const sample of samples ) {
  111. await addSample( sample, SAMPLE_PATH );
  112. }
  113. for ( const sample of localSamples ) {
  114. await addSample( sample, LOCAL_SAMPLE_PATH );
  115. }
  116. addGUI();
  117. } );
  118. window.addEventListener( 'resize', onWindowResize );
  119. }
  120. function updateModelsAlign() {
  121. const COLUMN_COUNT = 8;
  122. const DIST_X = 3;
  123. const DIST_Y = 4;
  124. const lineCount = Math.floor( models.length / COLUMN_COUNT ) - 1.5;
  125. const offsetX = ( DIST_X * ( COLUMN_COUNT - 1 ) ) * - .5;
  126. const offsetY = ( DIST_Y * lineCount ) * .5;
  127. for ( let i = 0; i < models.length; i ++ ) {
  128. const model = models[ i ];
  129. model.position.x = ( ( i % COLUMN_COUNT ) * DIST_X ) + offsetX;
  130. model.position.y = ( Math.floor( i / COLUMN_COUNT ) * - DIST_Y ) + offsetY;
  131. }
  132. }
  133. async function addSample( sample, path ) {
  134. const model = prefab.clone();
  135. models.push( model );
  136. scene.add( model );
  137. updateModelsAlign();
  138. //
  139. const material = await new MaterialXLoader()
  140. .setPath( path )
  141. .loadAsync( sample )
  142. .then( ( { materials } ) => Object.values( materials ).pop() );
  143. const calibrationMesh = model.getObjectByName( 'Calibration_Mesh' );
  144. calibrationMesh.material = material;
  145. const previewMesh = model.getObjectByName( 'Preview_Mesh' );
  146. previewMesh.material = material;
  147. }
  148. function addGUI() {
  149. const gui = new GUI();
  150. const API = {
  151. showCalibrationMesh: true,
  152. showPreviewMesh: true
  153. };
  154. const folder = gui.addFolder( 'SHOW' );
  155. folder.add( API, 'showCalibrationMesh' )
  156. .name( 'Calibration Mesh')
  157. .onChange( function( value ) { setVisibility( 'Calibration_Mesh', value ) } );
  158. folder.add( API, 'showPreviewMesh' )
  159. .name( 'Preview Mesh')
  160. .onChange( function( value ) { setVisibility( 'Preview_Mesh', value ) } );
  161. }
  162. function setVisibility( name, visible ) {
  163. scene.traverse( function( node ) {
  164. if ( node.isMesh ) {
  165. if ( node.name == name ) node.visible = visible;
  166. }
  167. } );
  168. }
  169. //
  170. function onWindowResize() {
  171. camera.aspect = window.innerWidth / window.innerHeight;
  172. camera.updateProjectionMatrix();
  173. renderer.setSize( window.innerWidth, window.innerHeight );
  174. }
  175. function render() {
  176. renderer.render( scene, camera );
  177. }
  178. </script>
  179. </body>
  180. </html>
粤ICP备19079148号