webgpu_tsl_editor.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. <meta property="og:title" content="three.js webgpu - tsl editor">
  7. <meta property="og:type" content="website">
  8. <meta property="og:url" content="https://threejs.org/examples/webgpu_tsl_editor.html">
  9. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_tsl_editor.jpg">
  10. <link type="text/css" rel="stylesheet" href="main.css">
  11. </head>
  12. <body>
  13. <style>
  14. #source {
  15. position: absolute;
  16. top: 0;
  17. left: 0;
  18. width: 50%;
  19. height: 100%;
  20. }
  21. #result {
  22. position: absolute;
  23. top: 0;
  24. right: 0;
  25. width: 50%;
  26. height: 100%;
  27. }
  28. #renderer {
  29. position: absolute;
  30. bottom: 15px;
  31. right: calc( 50% + 15px );
  32. width: 200px;
  33. height: 200px;
  34. z-index: 100;
  35. pointer-events: none;
  36. }
  37. </style>
  38. <div id="source"></div>
  39. <div id="result"></div>
  40. <div id="renderer"></div>
  41. <script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.55.1/min/vs/loader.js"></script>
  42. <script type="importmap">
  43. {
  44. "imports": {
  45. "three": "../build/three.webgpu.js",
  46. "three/webgpu": "../build/three.webgpu.js",
  47. "three/tsl": "../build/three.tsl.js",
  48. "three/addons/": "./jsm/"
  49. }
  50. }
  51. </script>
  52. <script type="module">
  53. import * as THREE from 'three/webgpu';
  54. import { vec4 } from 'three/tsl';
  55. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  56. init();
  57. function init() {
  58. // add the dependencies
  59. const width = 200;
  60. const height = 200;
  61. const camera = new THREE.PerspectiveCamera( 70, width / height, 0.1, 10 );
  62. camera.position.z = .72;
  63. camera.lookAt( 0, 0, 0 );
  64. const scene = new THREE.Scene();
  65. scene.background = new THREE.Color( 0x222222 );
  66. const rendererDOM = document.getElementById( 'renderer' );
  67. const renderer = new THREE.WebGPURenderer( { antialias: true } );
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( 200, 200 );
  70. renderer.debug.diagnostics.keywords = true;
  71. rendererDOM.appendChild( renderer.domElement );
  72. const material = new THREE.NodeMaterial();
  73. material.fragmentNode = vec4( 0, 0, 0, 1 );
  74. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  75. scene.add( mesh );
  76. //
  77. let compiling = false;
  78. renderer.setAnimationLoop( () => {
  79. if ( compiling ) return;
  80. renderer.render( scene, camera );
  81. } );
  82. // editor
  83. window.require.config( { paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.55.1/min/vs' } } );
  84. require( [ 'vs/editor/editor.main' ], () => {
  85. const options = {
  86. shader: 'fragment',
  87. outputColorSpace: THREE.SRGBColorSpace,
  88. output: 'WGSL',
  89. preview: true
  90. };
  91. let timeout = null;
  92. let rawShader = null;
  93. const editorDOM = document.getElementById( 'source' );
  94. const resultDOM = document.getElementById( 'result' );
  95. const tslCode = `// Simple uv.x animation
  96. const { texture, uniform, vec2, vec4, uv, oscSine, time, grayscale } = await import( 'three/tsl' );
  97. const samplerTexture = new THREE.TextureLoader().load( './textures/uv_grid_opengl.jpg' );
  98. samplerTexture.wrapS = THREE.RepeatWrapping;
  99. samplerTexture.colorSpace = THREE.SRGBColorSpace;
  100. const scaledTime = time.mul( .5 ); // .5 is speed
  101. const uv0 = uv();
  102. const animateUv = vec2( uv0.x.add( oscSine( scaledTime ) ), uv0.y );
  103. // .setName() is optional
  104. const myMap = texture( samplerTexture, animateUv ).rgb.setName( 'myTexture' );
  105. const myColor = uniform( new THREE.Color( 0x0066ff ) ).setName( 'myColor' );
  106. const opacity = .7;
  107. const desaturatedMap = grayscale( myMap.rgb );
  108. const finalColor = desaturatedMap.add( myColor );
  109. output = vec4( finalColor, opacity );
  110. `;
  111. const editor = window.monaco.editor.create( editorDOM, {
  112. value: tslCode,
  113. language: 'javascript',
  114. theme: 'vs-dark',
  115. automaticLayout: true,
  116. minimap: { enabled: false }
  117. } );
  118. const result = window.monaco.editor.create( resultDOM, {
  119. value: '',
  120. language: 'wgsl',
  121. theme: 'vs-dark',
  122. automaticLayout: true,
  123. readOnly: true,
  124. minimap: { enabled: false }
  125. } );
  126. const showCode = () => {
  127. result.setValue( rawShader[ options.shader + 'Shader' ] );
  128. result.revealLine( 1 );
  129. };
  130. const webGLRenderer = new THREE.WebGPURenderer( { forceWebGL: true } );
  131. webGLRenderer.debug.diagnostics.keywords = true;
  132. const build = async () => {
  133. try {
  134. const AsyncFunction = async function () {}.constructor;
  135. const tslCode = `let output = null;\n${ editor.getValue() }\nreturn { output };`;
  136. const nodes = await ( new AsyncFunction( 'THREE', tslCode )( THREE ) );
  137. mesh.material.fragmentNode = nodes.output;
  138. mesh.material.needsUpdate = true;
  139. compiling = true;
  140. mesh.visible = false;
  141. if ( options.output === 'WGSL' ) {
  142. rawShader = await renderer.debug.getShaderAsync( scene, camera, mesh );
  143. } else if ( options.output === 'GLSL ES 3.0' ) {
  144. rawShader = await webGLRenderer.debug.getShaderAsync( scene, camera, mesh );
  145. }
  146. mesh.visible = true;
  147. compiling = false;
  148. showCode();
  149. // extra debug info
  150. /*const style = 'background-color: #333; color: white; font-style: italic; border: 2px solid #777; font-size: 22px;';
  151. console.log( '%c [ WGSL ] Vertex Shader ', style );
  152. console.log( rawShader.vertexShader );
  153. console.log( '%c [ WGSL ] Fragment Shader ', style );
  154. console.log( rawShader.fragmentShader );/**/
  155. } catch ( e ) {
  156. result.setValue( 'Error: ' + e.message );
  157. }
  158. };
  159. build();
  160. editor.getModel().onDidChangeContent( () => {
  161. if ( timeout ) clearTimeout( timeout );
  162. timeout = setTimeout( build, 1000 );
  163. } );
  164. // gui
  165. const gui = new GUI();
  166. gui.add( options, 'output', [ 'WGSL', 'GLSL ES 3.0' ] ).onChange( build );
  167. gui.add( options, 'shader', [ 'vertex', 'fragment' ] ).onChange( showCode );
  168. gui.add( options, 'outputColorSpace', [ THREE.LinearSRGBColorSpace, THREE.SRGBColorSpace ] ).onChange( ( value ) => {
  169. renderer.outputColorSpace = value;
  170. build();
  171. } );
  172. gui.add( options, 'preview' ).onChange( ( value ) => {
  173. rendererDOM.style.display = value ? '' : 'none';
  174. } );
  175. } );
  176. }
  177. </script>
  178. </body>
  179. </html>
粤ICP备19079148号