1
0

webgpu_tsl_editor.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. rendererDOM.appendChild( renderer.domElement );
  71. const material = new THREE.NodeMaterial();
  72. material.fragmentNode = vec4( 0, 0, 0, 1 );
  73. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  74. scene.add( mesh );
  75. //
  76. let compiling = false;
  77. renderer.setAnimationLoop( () => {
  78. if ( compiling ) return;
  79. renderer.render( scene, camera );
  80. } );
  81. // editor
  82. window.require.config( { paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.55.1/min/vs' } } );
  83. require( [ 'vs/editor/editor.main' ], () => {
  84. const options = {
  85. shader: 'fragment',
  86. outputColorSpace: THREE.SRGBColorSpace,
  87. output: 'WGSL',
  88. preview: true
  89. };
  90. let timeout = null;
  91. let rawShader = null;
  92. const editorDOM = document.getElementById( 'source' );
  93. const resultDOM = document.getElementById( 'result' );
  94. const tslCode = `// Simple uv.x animation
  95. const { texture, uniform, vec2, vec4, uv, oscSine, time, grayscale } = await import( 'three/tsl' );
  96. const samplerTexture = new THREE.TextureLoader().load( './textures/uv_grid_opengl.jpg' );
  97. samplerTexture.wrapS = THREE.RepeatWrapping;
  98. samplerTexture.colorSpace = THREE.SRGBColorSpace;
  99. const scaledTime = time.mul( .5 ); // .5 is speed
  100. const uv0 = uv();
  101. const animateUv = vec2( uv0.x.add( oscSine( scaledTime ) ), uv0.y );
  102. // .setName() is optional
  103. const myMap = texture( samplerTexture, animateUv ).rgb.setName( 'myTexture' );
  104. const myColor = uniform( new THREE.Color( 0x0066ff ) ).setName( 'myColor' );
  105. const opacity = .7;
  106. const desaturatedMap = grayscale( myMap.rgb );
  107. const finalColor = desaturatedMap.add( myColor );
  108. output = vec4( finalColor, opacity );
  109. `;
  110. const editor = window.monaco.editor.create( editorDOM, {
  111. value: tslCode,
  112. language: 'javascript',
  113. theme: 'vs-dark',
  114. automaticLayout: true,
  115. minimap: { enabled: false }
  116. } );
  117. const result = window.monaco.editor.create( resultDOM, {
  118. value: '',
  119. language: 'wgsl',
  120. theme: 'vs-dark',
  121. automaticLayout: true,
  122. readOnly: true,
  123. minimap: { enabled: false }
  124. } );
  125. const showCode = () => {
  126. result.setValue( rawShader[ options.shader + 'Shader' ] );
  127. result.revealLine( 1 );
  128. };
  129. const webGLRenderer = new THREE.WebGPURenderer( { forceWebGL: true } );
  130. const build = async () => {
  131. try {
  132. const AsyncFunction = async function () {}.constructor;
  133. const tslCode = `let output = null;\n${ editor.getValue() }\nreturn { output };`;
  134. const nodes = await ( new AsyncFunction( 'THREE', tslCode )( THREE ) );
  135. mesh.material.fragmentNode = nodes.output;
  136. mesh.material.needsUpdate = true;
  137. compiling = true;
  138. mesh.visible = false;
  139. if ( options.output === 'WGSL' ) {
  140. rawShader = await renderer.debug.getShaderAsync( scene, camera, mesh );
  141. } else if ( options.output === 'GLSL ES 3.0' ) {
  142. rawShader = await webGLRenderer.debug.getShaderAsync( scene, camera, mesh );
  143. }
  144. mesh.visible = true;
  145. compiling = false;
  146. showCode();
  147. // extra debug info
  148. /*const style = 'background-color: #333; color: white; font-style: italic; border: 2px solid #777; font-size: 22px;';
  149. console.log( '%c [ WGSL ] Vertex Shader ', style );
  150. console.log( rawShader.vertexShader );
  151. console.log( '%c [ WGSL ] Fragment Shader ', style );
  152. console.log( rawShader.fragmentShader );/**/
  153. } catch ( e ) {
  154. result.setValue( 'Error: ' + e.message );
  155. }
  156. };
  157. build();
  158. editor.getModel().onDidChangeContent( () => {
  159. if ( timeout ) clearTimeout( timeout );
  160. timeout = setTimeout( build, 1000 );
  161. } );
  162. // gui
  163. const gui = new GUI();
  164. gui.add( options, 'output', [ 'WGSL', 'GLSL ES 3.0' ] ).onChange( build );
  165. gui.add( options, 'shader', [ 'vertex', 'fragment' ] ).onChange( showCode );
  166. gui.add( options, 'outputColorSpace', [ THREE.LinearSRGBColorSpace, THREE.SRGBColorSpace ] ).onChange( ( value ) => {
  167. renderer.outputColorSpace = value;
  168. build();
  169. } );
  170. gui.add( options, 'preview' ).onChange( ( value ) => {
  171. rendererDOM.style.display = value ? '' : 'none';
  172. } );
  173. } );
  174. }
  175. </script>
  176. </body>
  177. </html>
粤ICP备19079148号