webgpu_tsl_graph.html 7.6 KB

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