webgpu_loader_materialx.html 8.7 KB

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