webgpu_tsl_graph.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - tsl graph</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 - tsl graph">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_tsl_graph.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_tsl_graph.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>TSL Graph</span>
  23. </div>
  24. <small>
  25. TSL Graph Addons - <a href="https://www.tsl-graph.xyz/" target="_blank" rel="noopener">www.tsl-graph.xyz</a>
  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, vec3, 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 { Inspector } from 'three/addons/inspector/Inspector.js';
  45. import { TSLGraphLoader } from 'three/addons/inspector/extensions/tsl-graph/TSLGraphLoader.js';
  46. let camera, scene, renderer;
  47. let controls;
  48. let prefab;
  49. init();
  50. async function initTSLGraph() {
  51. // Create Materials
  52. const m1 = new THREE.MeshPhysicalNodeMaterial();
  53. m1.userData.graphId = 'mat_1-physical';
  54. const m2 = new THREE.MeshStandardNodeMaterial();
  55. m2.userData.graphId = 'mat_2-standard';
  56. const m3 = new THREE.MeshPhongNodeMaterial();
  57. m3.userData.graphId = 'mat_3-phong';
  58. const m4 = new THREE.MeshBasicNodeMaterial();
  59. m4.userData.graphId = 'mat_4-basic';
  60. const materials = [ m1, m2, m3, m4 ];
  61. for ( let i = 0; i < materials.length; i ++ ) {
  62. createShaderBall( materials[ i ], new THREE.Vector3( ( i & 1 ) * 4 - 2, 0, ( i & 2 ) * 2 - 2 ) );
  63. }
  64. // TSL Graph Editor
  65. renderer.inspector.onExtension( 'TSL Graph', async ( tslGraph ) => {
  66. renderer.inspector.setActiveTab( tslGraph );
  67. // Apply TSL Graph from Local Storage if exists
  68. // Every time a TSL Graph is changed, it will be stored in the local storage
  69. if ( tslGraph.hasGraphs ) {
  70. tslGraph.apply( scene );
  71. } else {
  72. // Load a TSL Graph from a file
  73. // Use it for production
  74. const tslLoader = new TSLGraphLoader();
  75. const applier = await tslLoader.setPath( './shaders/' ).loadAsync( 'tsl-graphs.json' );
  76. applier.apply( scene );
  77. }
  78. } );
  79. // Active TSL Graph Editor
  80. // Only is needed if you don't activate it from the GUI
  81. renderer.inspector.setActiveExtension( 'TSL Graph', true );
  82. }
  83. async function init() {
  84. renderer = new THREE.WebGPURenderer( { antialias: true } );
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. renderer.toneMapping = THREE.NeutralToneMapping;
  88. renderer.toneMappingExposure = .9;
  89. renderer.inspector = new Inspector();
  90. renderer.setAnimationLoop( render );
  91. document.body.appendChild( renderer.domElement );
  92. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 200 );
  93. camera.position.set( 3, 5, 8 );
  94. scene = new THREE.Scene();
  95. scene.background = new THREE.Color( 0x444444 );
  96. await renderer.init();
  97. // Ground plane
  98. const plane = new THREE.Mesh( new THREE.CircleGeometry( 40 ), createGroudMaterial() );
  99. plane.rotation.x = - Math.PI / 2;
  100. plane.renderOrder = - 1;
  101. scene.add( plane );
  102. //
  103. controls = new OrbitControls( camera );
  104. controls.connect( renderer.domElement );
  105. controls.enableDamping = true;
  106. controls.minDistance = 2;
  107. controls.maxDistance = 40;
  108. //
  109. const environment = await new HDRLoader().setPath( 'textures/equirectangular/' ).loadAsync( 'monochrome_studio_02_1k.hdr' );
  110. environment.mapping = THREE.EquirectangularReflectionMapping;
  111. const light = new THREE.DirectionalLight( 0xffffff, 1 );
  112. light.position.set( 1, 1, 1 );
  113. scene.add( light );
  114. scene.environment = environment;
  115. prefab = ( await new GLTFLoader().loadAsync( './models/gltf/ShaderBall.glb' ) ).scene;
  116. await initTSLGraph();
  117. initGUI();
  118. // Events
  119. resize();
  120. window.addEventListener( 'resize', resize );
  121. renderer.inspector.addEventListener( 'resize', resize );
  122. }
  123. async function createShaderBall( material, position = new THREE.Vector3() ) {
  124. const model = prefab.clone();
  125. model.position.copy( position );
  126. scene.add( model );
  127. //
  128. const calibrationMesh = model.getObjectByName( 'Calibration_Mesh' );
  129. calibrationMesh.material = material;
  130. const previewMesh = model.getObjectByName( 'Preview_Mesh' );
  131. previewMesh.material = material;
  132. calibrationMesh.renderOrder = 1;
  133. previewMesh.renderOrder = 2;
  134. }
  135. function initGUI() {
  136. const gui = renderer.inspector.createParameters( 'Shader Ball' );
  137. const API = {
  138. showCalibrationMesh: true,
  139. showPreviewMesh: true
  140. };
  141. gui.add( API, 'showCalibrationMesh' )
  142. .name( 'Calibration Mesh' )
  143. .onChange( function ( value ) {
  144. setVisibility( 'Calibration_Mesh', value );
  145. } );
  146. gui.add( API, 'showPreviewMesh' )
  147. .name( 'Preview Mesh' )
  148. .onChange( function ( value ) {
  149. setVisibility( 'Preview_Mesh', value );
  150. } );
  151. renderer.inspector.hide();
  152. }
  153. function resize() {
  154. const size = renderer.inspector.getSize();
  155. const width = window.innerWidth - size.width;
  156. const height = window.innerHeight - size.height;
  157. camera.aspect = width / height;
  158. camera.updateProjectionMatrix();
  159. renderer.setSize( width, height );
  160. }
  161. function render() {
  162. if ( controls ) controls.update();
  163. renderer.render( scene, camera );
  164. }
  165. //
  166. function setVisibility( name, visible ) {
  167. scene.traverse( function ( node ) {
  168. if ( node.isMesh ) {
  169. if ( node.name == name ) node.visible = visible;
  170. }
  171. } );
  172. }
  173. function createGroudMaterial() {
  174. const material = new THREE.MeshBasicNodeMaterial();
  175. const grid = Fn( ( [ coord, lineWidth = float( 0.01 ), dotSize = float( 0.03 ) ] ) => {
  176. // https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8
  177. const g = fract( coord );
  178. const fw = fwidth( coord );
  179. const gx = abs( g.x.sub( 0.5 ) );
  180. const gy = abs( g.y.sub( 0.5 ) );
  181. const lineX = saturate( lineWidth.sub( gx ).div( fw.x ).add( 0.5 ) );
  182. const lineY = saturate( lineWidth.sub( gy ).div( fw.y ).add( 0.5 ) );
  183. const lines = max( lineX, lineY );
  184. const squareDist = max( gx, gy );
  185. const aa = max( fw.x, fw.y );
  186. const dots = smoothstep( dotSize.add( aa ), dotSize.sub( aa ), squareDist );
  187. return max( dots, lines );
  188. } );
  189. const fade = Fn( ( [ radius = float( 10.0 ), falloff = float( 1.0 ) ] ) => {
  190. return smoothstep( radius, radius.sub( falloff ), length( positionWorld ) );
  191. } );
  192. const gridColor = vec4( vec3( 0.2 ), 1.0 );
  193. const baseColor = vec4( vec3( 0.4 ), 0.0 );
  194. material.colorNode = grid( positionWorld.xz, 0.007, 0.03 ).mix( baseColor, gridColor ).mul( fade( 30.0, 20.0 ) );
  195. material.transparent = true;
  196. return material;
  197. }
  198. </script>
  199. </body>
  200. </html>
粤ICP备19079148号