webgpu_loader_materialx.html 8.4 KB

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