webgpu_tsl_editor.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <html lang="en">
  2. <head>
  3. <title>three.js - webgpu - tsl editor</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. </head>
  8. <body>
  9. <style>
  10. #source {
  11. position: absolute;
  12. top: 0;
  13. left: 0;
  14. width: 50%;
  15. height: 100%;
  16. }
  17. #result {
  18. position: absolute;
  19. top: 0;
  20. right: 0;
  21. width: 50%;
  22. height: 100%;
  23. }
  24. #renderer {
  25. position: absolute;
  26. top: 0;
  27. right: 50%;
  28. width: 200px;
  29. height: 200px;
  30. z-index: 100;
  31. pointer-events: none;
  32. }
  33. </style>
  34. <div id="source"></div>
  35. <div id="result"></div>
  36. <div id="renderer"></div>
  37. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  38. <script src="https://cdn.jsdelivr.net/npm/monaco-editor@latest/min/vs/loader.min.js"></script>
  39. <script type="importmap">
  40. {
  41. "imports": {
  42. "three": "../build/three.module.js",
  43. "three/addons/": "./jsm/",
  44. "three/nodes": "./jsm/nodes/Nodes.js"
  45. }
  46. }
  47. </script>
  48. <script type="module">
  49. import * as THREE from 'three';
  50. import * as Nodes from 'three/nodes';
  51. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  52. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  53. import WGSLNodeBuilder from 'three/addons/renderers/webgpu/nodes/WGSLNodeBuilder.js';
  54. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  55. init();
  56. function init() {
  57. if ( WebGPU.isAvailable() === false ) {
  58. document.body.appendChild( WebGPU.getErrorMessage() );
  59. throw new Error( 'No WebGPU support' );
  60. }
  61. // add the depedencies
  62. const width = 200;
  63. const height = 200;
  64. const camera = new THREE.PerspectiveCamera( 70, width / height, 0.1, 10 );
  65. camera.position.z = .72;
  66. camera.lookAt( 0, 0, 0 );
  67. const scene = new THREE.Scene();
  68. scene.background = new THREE.Color( 0x222222 );
  69. const rendererDOM = document.getElementById( 'renderer' );
  70. const renderer = new WebGPURenderer();
  71. renderer.outputColorSpace = THREE.LinearSRGBColorSpace;
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( 200, 200 );
  74. rendererDOM.appendChild( renderer.domElement );
  75. const material = new Nodes.NodeMaterial();
  76. material.outputNode = Nodes.vec4( 0, 0, 0, 1 );
  77. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  78. scene.add( mesh );
  79. renderer.setAnimationLoop( () => {
  80. renderer.render( scene, camera );
  81. } );
  82. // editor
  83. window.require.config( { paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@latest/min/vs' } } );
  84. require( [ 'vs/editor/editor.main' ], () => {
  85. const options = { shader: 'fragment', outputColorSpace: THREE.LinearSRGBColorSpace };
  86. let timeout = null;
  87. let nodeBuilder = null;
  88. const editorDOM = document.getElementById( 'source' );
  89. const resultDOM = document.getElementById( 'result' );
  90. const tslCode = `// Example: Desaturate texture
  91. const { texture, uniform, vec4 } = TSL;
  92. //const samplerTexture = new THREE.Texture();
  93. const samplerTexture = new THREE.TextureLoader().load( './textures/uv_grid_opengl.jpg' );
  94. // label is optional
  95. const myMap = texture( samplerTexture ).rgb.label( 'myTexture' );
  96. const myColor = uniform( new THREE.Color( 0x0066ff ) ).label( 'myColor' );
  97. const opacity = .7;
  98. const desaturatedMap = myMap.rgb.saturation( 0 ); // try add .temp( 'myVar' ) after saturation()
  99. const finalColor = desaturatedMap.add( myColor );
  100. output = vec4( finalColor, opacity );
  101. `;
  102. const editor = window.monaco.editor.create( editorDOM, {
  103. value: tslCode,
  104. language: 'javascript',
  105. theme: 'vs-dark',
  106. automaticLayout: true
  107. } );
  108. const result = window.monaco.editor.create( resultDOM, {
  109. value: '',
  110. language: 'wgsl',
  111. theme: 'vs-dark',
  112. automaticLayout: true,
  113. readOnly: true
  114. } );
  115. const showCode = () => {
  116. result.setValue( nodeBuilder[ options.shader + 'Shader' ] );
  117. result.revealLine( 1 );
  118. };
  119. const build = () => {
  120. try {
  121. const tslCode = `let output = null;\n${ editor.getValue() }\nreturn { output };`;
  122. const nodes = new Function( 'THREE', 'TSL', tslCode )( THREE, Nodes );
  123. mesh.material.outputNode = nodes.output;
  124. mesh.material.needsUpdate = true;
  125. nodeBuilder = new WGSLNodeBuilder( mesh, renderer );
  126. nodeBuilder.build();
  127. showCode();
  128. // extra debug info
  129. /*const style = 'background-color: #333; color: white; font-style: italic; border: 2px solid #777; font-size: 22px;';
  130. console.log( '%c [ WGSL ] Vertex Shader ', style );
  131. console.log( nodeBuilder.vertexShader );
  132. console.log( '%c [ WGSL ] Fragment Shader ', style );
  133. console.log( nodeBuilder.fragmentShader );*/
  134. } catch ( e ) {
  135. result.setValue( 'Error: ' + e.message );
  136. }
  137. };
  138. build();
  139. editor.getModel().onDidChangeContent( () => {
  140. if ( timeout ) clearTimeout( timeout );
  141. timeout = setTimeout( build, 1000 );
  142. } );
  143. // gui
  144. const gui = new GUI();
  145. gui.add( options, 'shader', [ 'vertex', 'fragment' ] ).onChange( showCode );
  146. gui.add( options, 'outputColorSpace', [ THREE.LinearSRGBColorSpace, THREE.SRGBColorSpace ] ).onChange( ( value ) => {
  147. renderer.outputColorSpace = value;
  148. build();
  149. } );
  150. } );
  151. }
  152. </script>
  153. </body>
  154. </html>