webgpu_loader_materialx.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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, length, fract, vec4, positionWorld, smoothstep, max, abs, float, fwidth } 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 gridXZ = Fn( ( [ gridSize = float( 1.0 ), dotWidth = float( 0.1 ), lineWidth = float( 0.02 ) ] ) => {
  111. const coord = positionWorld.xz.div( gridSize );
  112. const grid = fract( coord );
  113. // Screen-space derivative for automatic antialiasing
  114. const fw = fwidth( coord );
  115. const smoothing = max( fw.x, fw.y ).mul( 0.5 );
  116. // Create squares at cell centers
  117. const squareDist = max( abs( grid.x.sub( 0.5 ) ), abs( grid.y.sub( 0.5 ) ) );
  118. const dots = smoothstep( dotWidth.add( smoothing ), dotWidth.sub( smoothing ), squareDist );
  119. // Create grid lines
  120. const lineX = smoothstep( lineWidth.add( smoothing ), lineWidth.sub( smoothing ), abs( grid.x.sub( 0.5 ) ) );
  121. const lineZ = smoothstep( lineWidth.add( smoothing ), lineWidth.sub( smoothing ), abs( grid.y.sub( 0.5 ) ) );
  122. const lines = max( lineX, lineZ );
  123. return max( dots, lines );
  124. } );
  125. const radialGradient = Fn( ( [ radius = float( 10.0 ), falloff = float( 1.0 ) ] ) => {
  126. return smoothstep( radius, radius.sub( falloff ), length( positionWorld ) );
  127. } );
  128. // Create grid pattern
  129. const gridPattern = gridXZ( 1.0, 0.03, 0.005 );
  130. const baseColor = vec4( 1.0, 1.0, 1.0, 0.0 );
  131. const gridColor = vec4( 0.5, 0.5, 0.5, 1.0 );
  132. // Mix base color with grid lines
  133. material.colorNode = gridPattern.mix( baseColor, gridColor ).mul( radialGradient( 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. }
  194. function addGUI() {
  195. const gui = renderer.inspector.createParameters( 'MaterialX Loader' );
  196. const API = {
  197. showCalibrationMesh: true,
  198. showPreviewMesh: true
  199. };
  200. gui.add( API, 'showCalibrationMesh' )
  201. .name( 'Calibration Mesh' )
  202. .onChange( function ( value ) {
  203. setVisibility( 'Calibration_Mesh', value );
  204. } );
  205. gui.add( API, 'showPreviewMesh' )
  206. .name( 'Preview Mesh' )
  207. .onChange( function ( value ) {
  208. setVisibility( 'Preview_Mesh', value );
  209. } );
  210. }
  211. function setVisibility( name, visible ) {
  212. scene.traverse( function ( node ) {
  213. if ( node.isMesh ) {
  214. if ( node.name == name ) node.visible = visible;
  215. }
  216. } );
  217. }
  218. //
  219. function onWindowResize() {
  220. camera.aspect = window.innerWidth / window.innerHeight;
  221. camera.updateProjectionMatrix();
  222. renderer.setSize( window.innerWidth, window.innerHeight );
  223. }
  224. function render() {
  225. controls.update();
  226. renderer.render( scene, camera );
  227. }
  228. </script>
  229. </body>
  230. </html>
粤ICP备19079148号