webgpu_loader_materialx.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 './jsm/controls/OrbitControls.js';
  31. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  32. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  33. import { MaterialXLoader } from './jsm/loaders/MaterialXLoader.js';
  34. const SAMPLE_PATH = 'https://raw.githubusercontent.com/materialx/MaterialX/main/resources/Materials/Examples/StandardSurface/';
  35. const LOCAL_SAMPLE_PATH = 'materialx/';
  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. const localSamples = [
  60. 'heightnormal.mtlx',
  61. 'conditional_if_float.mtlx',
  62. 'image_transform.mtlx',
  63. 'color3_vec3_cm_test.mtlx',
  64. 'rotate2d_test.mtlx',
  65. 'rotate3d_test.mtlx',
  66. 'heighttonormal_normal_input.mtlx',
  67. 'roughness_test.mtlx',
  68. 'opacity_test.mtlx',
  69. 'opacity_only_test.mtlx',
  70. 'specular_test.mtlx',
  71. 'ior_test.mtlx',
  72. 'combined_test.mtlx',
  73. 'texture_opacity_test.mtlx',
  74. 'transmission_test.mtlx',
  75. 'transmission_only_test.mtlx',
  76. 'transmission_rough.mtlx',
  77. 'thin_film_rainbow_test.mtlx',
  78. 'thin_film_ior_clamp_test.mtlx',
  79. 'sheen_test.mtlx',
  80. ];
  81. let camera, scene, renderer, prefab;
  82. const models = [];
  83. init();
  84. function init() {
  85. const container = document.createElement( 'div' );
  86. document.body.appendChild( container );
  87. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 50 );
  88. camera.position.set( 0, 3, 20 );
  89. scene = new THREE.Scene();
  90. renderer = new THREE.WebGPURenderer( { antialias: true, alpha: true } );
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. renderer.toneMapping = THREE.LinearToneMapping;
  94. renderer.toneMappingExposure = .5;
  95. renderer.setAnimationLoop( render );
  96. container.appendChild( renderer.domElement );
  97. //
  98. const controls = new OrbitControls( camera, renderer.domElement );
  99. controls.minDistance = 2;
  100. controls.maxDistance = 40;
  101. //
  102. new RGBELoader()
  103. .setPath( 'textures/equirectangular/' )
  104. .load( 'san_giuseppe_bridge_2k.hdr', async ( texture ) => {
  105. texture.mapping = THREE.EquirectangularReflectionMapping;
  106. scene.background = texture;
  107. scene.environment = texture;
  108. prefab = ( await new GLTFLoader().loadAsync( './models/gltf/ShaderBall.glb' ) ).scene;
  109. for ( const sample of samples ) {
  110. await addSample( sample, SAMPLE_PATH );
  111. }
  112. for ( const sample of localSamples ) {
  113. await addSample( sample, LOCAL_SAMPLE_PATH );
  114. }
  115. } );
  116. window.addEventListener( 'resize', onWindowResize );
  117. }
  118. function updateModelsAlign() {
  119. const COLUMN_COUNT = 6;
  120. const DIST_X = 3;
  121. const DIST_Y = 4;
  122. const lineCount = Math.floor( models.length / COLUMN_COUNT ) - 1.5;
  123. const offsetX = ( DIST_X * ( COLUMN_COUNT - 1 ) ) * - .5;
  124. const offsetY = ( DIST_Y * lineCount ) * .5;
  125. for ( let i = 0; i < models.length; i ++ ) {
  126. const model = models[ i ];
  127. model.position.x = ( ( i % COLUMN_COUNT ) * DIST_X ) + offsetX;
  128. model.position.y = ( Math.floor( i / COLUMN_COUNT ) * - DIST_Y ) + offsetY;
  129. }
  130. }
  131. async function addSample( sample, path ) {
  132. const model = prefab.clone();
  133. models.push( model );
  134. scene.add( model );
  135. updateModelsAlign();
  136. //
  137. const material = await new MaterialXLoader()
  138. .setPath( path )
  139. .loadAsync( sample )
  140. .then( ( { materials } ) => Object.values( materials ).pop() );
  141. const calibrationMesh = model.getObjectByName( 'Calibration_Mesh' );
  142. calibrationMesh.material = material;
  143. const Preview_Mesh = model.getObjectByName( 'Preview_Mesh' );
  144. Preview_Mesh.material = material;
  145. }
  146. //
  147. function onWindowResize() {
  148. camera.aspect = window.innerWidth / window.innerHeight;
  149. camera.updateProjectionMatrix();
  150. renderer.setSize( window.innerWidth, window.innerHeight );
  151. }
  152. function render() {
  153. renderer.render( scene, camera );
  154. }
  155. </script>
  156. </body>
  157. </html>
粤ICP备19079148号