webgpu_tsl_graph.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/addons/tsl-graph/TSLGraphLoader.js';
  42. import { TSLGraphEditor } from 'three/addons/inspector/addons/tsl-graph/TSLGraphEditor.js';
  43. let camera, scene, renderer;
  44. let controls;
  45. let prefab;
  46. init();
  47. async function initTSLGraph() {
  48. // TSL Graph Editor
  49. const tslGraph = new TSLGraphEditor();
  50. renderer.inspector.addTab( tslGraph );
  51. renderer.inspector.setActiveTab( tslGraph );
  52. // Create Materials
  53. const m1 = new THREE.MeshPhysicalNodeMaterial();
  54. m1.userData.graphId = 'mat_1-physical';
  55. const m2 = new THREE.MeshStandardNodeMaterial();
  56. m2.userData.graphId = 'mat_2-standard';
  57. const m3 = new THREE.MeshPhongNodeMaterial();
  58. m3.userData.graphId = 'mat_3-phong';
  59. const m4 = new THREE.MeshBasicNodeMaterial();
  60. m4.userData.graphId = 'mat_4-basic';
  61. const materials = [ m1, m2, m3, m4 ];
  62. for ( let i = 0; i < materials.length; i ++ ) {
  63. createShaderBall( materials[ i ], new THREE.Vector3( ( i & 1 ) * 4 - 2, 0, ( i & 2 ) * 2 - 2 ) );
  64. }
  65. // Initialize
  66. // Load and apply TSL Graph from a file or from Local Storage if exists
  67. // Every time a TSL Graph is changed, it will be stored in the local storage
  68. if ( tslGraph.hasGraphs ) {
  69. tslGraph.apply( scene );
  70. } else {
  71. // Load a TSL Graph from a file
  72. const tslLoader = new TSLGraphLoader();
  73. const applier = await tslLoader.setPath( './shaders/' ).loadAsync( 'tsl-graphs.json' );
  74. applier.apply( scene );
  75. }
  76. // Picker a Material
  77. let boundingBox = null;
  78. const raycaster = new THREE.Raycaster();
  79. const pointer = new THREE.Vector2();
  80. function removeBoundingBox() {
  81. scene.remove( boundingBox );
  82. boundingBox.dispose();
  83. }
  84. tslGraph.addEventListener( 'change', ( { material } ) => {
  85. if ( material === null && boundingBox ) {
  86. removeBoundingBox();
  87. boundingBox = null;
  88. }
  89. } );
  90. tslGraph.addEventListener( 'remove', ( { graphId } ) => {
  91. scene.traverse( ( object ) => {
  92. if ( object.material && object.material.userData && object.material.userData.graphId === graphId ) {
  93. tslGraph.restoreMaterial( object.material );
  94. }
  95. } );
  96. } );
  97. const pointerDownPosition = new THREE.Vector2();
  98. renderer.domElement.addEventListener( 'pointerdown', ( e ) => {
  99. pointerDownPosition.set( e.clientX, e.clientY );
  100. } );
  101. renderer.domElement.addEventListener( 'pointerup', ( e ) => {
  102. if ( pointerDownPosition.distanceTo( pointer.set( e.clientX, e.clientY ) ) > 2 ) return;
  103. const rect = renderer.domElement.getBoundingClientRect();
  104. pointer.x = ( ( e.clientX - rect.left ) / rect.width ) * 2 - 1;
  105. pointer.y = - ( ( e.clientY - rect.top ) / rect.height ) * 2 + 1;
  106. raycaster.setFromCamera( pointer, camera );
  107. const intersects = raycaster.intersectObjects( scene.children, true );
  108. let graphMaterial = null;
  109. if ( intersects.length > 0 ) {
  110. for ( const intersect of intersects ) {
  111. const object = intersect.object;
  112. const material = object.material;
  113. if ( material.userData && material.userData.graphId ) {
  114. if ( boundingBox ) {
  115. removeBoundingBox();
  116. }
  117. boundingBox = new THREE.BoxHelper( object, 0xffff00 );
  118. scene.add( boundingBox );
  119. graphMaterial = material;
  120. }
  121. if ( object.isMesh || object.isSprite ) {
  122. break;
  123. }
  124. }
  125. }
  126. tslGraph.setMaterial( graphMaterial );
  127. } );
  128. }
  129. async function init() {
  130. renderer = new THREE.WebGPURenderer( { antialias: true } );
  131. renderer.setPixelRatio( window.devicePixelRatio );
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. renderer.toneMapping = THREE.NeutralToneMapping;
  134. renderer.toneMappingExposure = .9;
  135. renderer.inspector = new Inspector();
  136. renderer.setAnimationLoop( render );
  137. document.body.appendChild( renderer.domElement );
  138. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 200 );
  139. camera.position.set( 3, 5, 8 );
  140. scene = new THREE.Scene();
  141. scene.background = new THREE.Color( 0x444444 );
  142. await renderer.init();
  143. // Ground plane
  144. const plane = new THREE.Mesh( new THREE.CircleGeometry( 40 ), createGroudMaterial() );
  145. plane.rotation.x = - Math.PI / 2;
  146. plane.renderOrder = - 1;
  147. scene.add( plane );
  148. //
  149. controls = new OrbitControls( camera );
  150. controls.connect( renderer.domElement );
  151. controls.enableDamping = true;
  152. controls.minDistance = 2;
  153. controls.maxDistance = 40;
  154. //
  155. const environment = await new HDRLoader().setPath( 'textures/equirectangular/' ).loadAsync( 'monochrome_studio_02_1k.hdr' );
  156. environment.mapping = THREE.EquirectangularReflectionMapping;
  157. const light = new THREE.DirectionalLight( 0xffffff, 1 );
  158. light.position.set( 1, 1, 1 );
  159. scene.add( light );
  160. scene.environment = environment;
  161. prefab = ( await new GLTFLoader().loadAsync( './models/gltf/ShaderBall.glb' ) ).scene;
  162. await initTSLGraph();
  163. initGUI();
  164. // Events
  165. resize();
  166. window.addEventListener( 'resize', resize );
  167. renderer.inspector.addEventListener( 'resize', resize );
  168. }
  169. async function createShaderBall( material, position = new THREE.Vector3() ) {
  170. const model = prefab.clone();
  171. model.position.copy( position );
  172. scene.add( model );
  173. //
  174. const calibrationMesh = model.getObjectByName( 'Calibration_Mesh' );
  175. calibrationMesh.material = material;
  176. const previewMesh = model.getObjectByName( 'Preview_Mesh' );
  177. previewMesh.material = material;
  178. calibrationMesh.renderOrder = 1;
  179. previewMesh.renderOrder = 2;
  180. }
  181. function initGUI() {
  182. const gui = renderer.inspector.createParameters( 'Shader Ball' );
  183. const API = {
  184. showCalibrationMesh: true,
  185. showPreviewMesh: true
  186. };
  187. gui.add( API, 'showCalibrationMesh' )
  188. .name( 'Calibration Mesh' )
  189. .onChange( function ( value ) {
  190. setVisibility( 'Calibration_Mesh', value );
  191. } );
  192. gui.add( API, 'showPreviewMesh' )
  193. .name( 'Preview Mesh' )
  194. .onChange( function ( value ) {
  195. setVisibility( 'Preview_Mesh', value );
  196. } );
  197. renderer.inspector.hide();
  198. }
  199. function resize() {
  200. const size = renderer.inspector.getSize();
  201. const width = window.innerWidth - size.width;
  202. const height = window.innerHeight - size.height;
  203. camera.aspect = width / height;
  204. camera.updateProjectionMatrix();
  205. renderer.setSize( width, height );
  206. }
  207. function render() {
  208. if ( controls ) controls.update();
  209. renderer.render( scene, camera );
  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. function createGroudMaterial() {
  220. const material = new THREE.MeshBasicNodeMaterial();
  221. const grid = Fn( ( [ coord, lineWidth = float( 0.01 ), dotSize = float( 0.03 ) ] ) => {
  222. // https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8
  223. const g = fract( coord );
  224. const fw = fwidth( coord );
  225. const gx = abs( g.x.sub( 0.5 ) );
  226. const gy = abs( g.y.sub( 0.5 ) );
  227. const lineX = saturate( lineWidth.sub( gx ).div( fw.x ).add( 0.5 ) );
  228. const lineY = saturate( lineWidth.sub( gy ).div( fw.y ).add( 0.5 ) );
  229. const lines = max( lineX, lineY );
  230. const squareDist = max( gx, gy );
  231. const aa = max( fw.x, fw.y );
  232. const dots = smoothstep( dotSize.add( aa ), dotSize.sub( aa ), squareDist );
  233. return max( dots, lines );
  234. } );
  235. const fade = Fn( ( [ radius = float( 10.0 ), falloff = float( 1.0 ) ] ) => {
  236. return smoothstep( radius, radius.sub( falloff ), length( positionWorld ) );
  237. } );
  238. const gridColor = vec4( vec3( 0.2 ), 1.0 );
  239. const baseColor = vec4( vec3( 0.4 ), 0.0 );
  240. material.colorNode = grid( positionWorld.xz, 0.007, 0.03 ).mix( baseColor, gridColor ).mul( fade( 30.0, 20.0 ) );
  241. material.transparent = true;
  242. return material;
  243. }
  244. </script>
  245. </body>
  246. </html>
粤ICP备19079148号