webgpu_loader_materialx.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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="example.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" class="logo-link"></a>
  17. <div class="title-wrapper">
  18. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>MaterialX Loader</span>
  19. </div>
  20. <small>
  21. MaterialX Standard Surface loader.
  22. </small>
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/webgpu": "../build/three.webgpu.js",
  29. "three/tsl": "../build/three.tsl.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three/webgpu';
  36. import { Fn, abs, fract, fwidth, length, max, saturate, smoothstep, vec4, positionWorld, float } from 'three/tsl';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  39. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  40. import { MaterialXLoader } from 'three/addons/loaders/MaterialXLoader.js';
  41. import { Inspector } from 'three/addons/inspector/Inspector.js';
  42. const SAMPLE_PATH = 'https://raw.githubusercontent.com/materialx/MaterialX/main/resources/Materials/Examples/StandardSurface/';
  43. const LOCAL_SAMPLE_PATH = 'materialx/';
  44. const samples = [
  45. 'standard_surface_brass_tiled.mtlx',
  46. 'standard_surface_brick_procedural.mtlx',
  47. 'standard_surface_carpaint.mtlx',
  48. //'standard_surface_chess_set.mtlx',
  49. 'standard_surface_chrome.mtlx',
  50. 'standard_surface_copper.mtlx',
  51. //'standard_surface_default.mtlx',
  52. //'standard_surface_glass.mtlx',
  53. //'standard_surface_glass_tinted.mtlx',
  54. 'standard_surface_gold.mtlx',
  55. //'standard_surface_greysphere.mtlx',
  56. //'standard_surface_greysphere_calibration.mtlx',
  57. 'standard_surface_jade.mtlx',
  58. //'standard_surface_look_brass_tiled.mtlx',
  59. //'standard_surface_look_wood_tiled.mtlx',
  60. 'standard_surface_marble_solid.mtlx',
  61. 'standard_surface_metal_brushed.mtlx',
  62. 'standard_surface_plastic.mtlx',
  63. //'standard_surface_thin_film.mtlx',
  64. 'standard_surface_velvet.mtlx',
  65. 'standard_surface_wood_tiled.mtlx'
  66. ];
  67. const localSamples = [
  68. 'heightnormal.mtlx',
  69. 'conditional_if_float.mtlx',
  70. 'image_transform.mtlx',
  71. 'color3_vec3_cm_test.mtlx',
  72. 'rotate2d_test.mtlx',
  73. 'rotate3d_test.mtlx',
  74. 'heighttonormal_normal_input.mtlx',
  75. 'roughness_test.mtlx',
  76. 'opacity_test.mtlx',
  77. 'opacity_only_test.mtlx',
  78. 'specular_test.mtlx',
  79. 'ior_test.mtlx',
  80. 'combined_test.mtlx',
  81. 'texture_opacity_test.mtlx',
  82. 'transmission_test.mtlx',
  83. 'transmission_only_test.mtlx',
  84. 'transmission_rough.mtlx',
  85. 'thin_film_rainbow_test.mtlx',
  86. 'thin_film_ior_clamp_test.mtlx',
  87. 'sheen_test.mtlx',
  88. ];
  89. let camera, scene, renderer;
  90. let controls, prefab;
  91. const models = [];
  92. init();
  93. function init() {
  94. const container = document.createElement( 'div' );
  95. document.body.appendChild( container );
  96. renderer = new THREE.WebGPURenderer( { antialias: true } );
  97. renderer.setPixelRatio( window.devicePixelRatio );
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. renderer.toneMapping = THREE.LinearToneMapping;
  100. renderer.toneMappingExposure = .5;
  101. renderer.inspector = new Inspector();
  102. renderer.setAnimationLoop( render );
  103. container.appendChild( renderer.domElement );
  104. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 200 );
  105. camera.position.set( 10, 10, 20 );
  106. scene = new THREE.Scene();
  107. scene.background = new THREE.Color( 0xffffff );
  108. // Ground plane
  109. const material = new THREE.MeshBasicNodeMaterial();
  110. const grid = Fn( ( [ coord, lineWidth = float( 0.01 ), dotSize = float( 0.03 ) ] ) => {
  111. // https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8
  112. const g = fract( coord );
  113. const fw = fwidth( coord );
  114. const gx = abs( g.x.sub( 0.5 ) );
  115. const gy = abs( g.y.sub( 0.5 ) );
  116. const lineX = saturate( lineWidth.sub( gx ).div( fw.x ).add( 0.5 ) );
  117. const lineY = saturate( lineWidth.sub( gy ).div( fw.y ).add( 0.5 ) );
  118. const lines = max( lineX, lineY );
  119. const squareDist = max( gx, gy );
  120. const aa = max( fw.x, fw.y );
  121. const dots = smoothstep( dotSize.add( aa ), dotSize.sub( aa ), squareDist );
  122. return max( dots, lines );
  123. } );
  124. const fade = Fn( ( [ radius = float( 10.0 ), falloff = float( 1.0 ) ] ) => {
  125. return smoothstep( radius, radius.sub( falloff ), length( positionWorld ) );
  126. } );
  127. const gridColor = vec4( 0.5, 0.5, 0.5, 1.0 );
  128. const baseColor = vec4( 1.0, 1.0, 1.0, 0.0 );
  129. material.colorNode = grid( positionWorld.xz, 0.007, 0.03 ).mix( baseColor, gridColor ).mul( fade( 30.0, 20.0 ) );
  130. material.transparent = true;
  131. const plane = new THREE.Mesh( new THREE.CircleGeometry( 40 ), material );
  132. plane.rotation.x = - Math.PI / 2;
  133. plane.renderOrder = - 1;
  134. scene.add( plane );
  135. //
  136. controls = new OrbitControls( camera );
  137. controls.connect( renderer.domElement );
  138. controls.enableDamping = true;
  139. controls.minDistance = 2;
  140. controls.maxDistance = 40;
  141. //
  142. new HDRLoader()
  143. .setPath( 'textures/equirectangular/' )
  144. .load( 'san_giuseppe_bridge_2k.hdr', async ( texture ) => {
  145. texture.mapping = THREE.EquirectangularReflectionMapping;
  146. scene.environment = texture;
  147. prefab = ( await new GLTFLoader().loadAsync( './models/gltf/ShaderBall.glb' ) ).scene;
  148. for ( const sample of samples ) {
  149. await addSample( sample, SAMPLE_PATH );
  150. }
  151. for ( const sample of localSamples ) {
  152. await addSample( sample, LOCAL_SAMPLE_PATH );
  153. }
  154. addGUI();
  155. } );
  156. window.addEventListener( 'resize', onWindowResize );
  157. }
  158. function updateModelsAlign() {
  159. const COLUMN_COUNT = 6;
  160. const DIST_X = 3;
  161. const DIST_Z = 3;
  162. const lineCount = Math.floor( models.length / COLUMN_COUNT ) - 1.5;
  163. const offsetX = ( DIST_X * ( COLUMN_COUNT - 1 ) ) * - .5;
  164. const offsetZ = ( DIST_Z * lineCount ) * .5;
  165. for ( let i = 0; i < models.length; i ++ ) {
  166. const model = models[ i ];
  167. model.position.x = ( ( i % COLUMN_COUNT ) * DIST_X ) + offsetX;
  168. model.position.z = ( Math.floor( i / COLUMN_COUNT ) * - DIST_Z ) + offsetZ;
  169. }
  170. }
  171. async function addSample( sample, path ) {
  172. const model = prefab.clone();
  173. models.push( model );
  174. scene.add( model );
  175. updateModelsAlign();
  176. //
  177. const material = await new MaterialXLoader()
  178. .setPath( path )
  179. .loadAsync( sample )
  180. .then( ( { materials } ) => Object.values( materials ).pop() );
  181. const calibrationMesh = model.getObjectByName( 'Calibration_Mesh' );
  182. calibrationMesh.material = material;
  183. const previewMesh = model.getObjectByName( 'Preview_Mesh' );
  184. previewMesh.material = material;
  185. if ( material.transparent ) {
  186. calibrationMesh.renderOrder = 1;
  187. previewMesh.renderOrder = 2;
  188. }
  189. }
  190. function addGUI() {
  191. const gui = renderer.inspector.createParameters( 'MaterialX Loader' );
  192. const API = {
  193. showCalibrationMesh: true,
  194. showPreviewMesh: true
  195. };
  196. gui.add( API, 'showCalibrationMesh' )
  197. .name( 'Calibration Mesh' )
  198. .onChange( function ( value ) {
  199. setVisibility( 'Calibration_Mesh', value );
  200. } );
  201. gui.add( API, 'showPreviewMesh' )
  202. .name( 'Preview Mesh' )
  203. .onChange( function ( value ) {
  204. setVisibility( 'Preview_Mesh', value );
  205. } );
  206. }
  207. function setVisibility( name, visible ) {
  208. scene.traverse( function ( node ) {
  209. if ( node.isMesh ) {
  210. if ( node.name == name ) node.visible = visible;
  211. }
  212. } );
  213. }
  214. //
  215. function onWindowResize() {
  216. camera.aspect = window.innerWidth / window.innerHeight;
  217. camera.updateProjectionMatrix();
  218. renderer.setSize( window.innerWidth, window.innerHeight );
  219. }
  220. function render() {
  221. controls.update();
  222. renderer.render( scene, camera );
  223. }
  224. </script>
  225. </body>
  226. </html>
粤ICP备19079148号