2
0

webgpu_tsl_editor.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. bottom: 15px;
  27. right: calc( 50% + 15px );
  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 src="https://cdn.jsdelivr.net/npm/monaco-editor@latest/min/vs/loader.min.js"></script>
  38. <script type="importmap">
  39. {
  40. "imports": {
  41. "three": "../build/three.webgpu.js",
  42. "three/tsl": "../build/three.webgpu.js",
  43. "three/addons/": "./jsm/"
  44. }
  45. }
  46. </script>
  47. <script type="module">
  48. import * as THREE from 'three';
  49. import WebGPURenderer from '../src/renderers/webgpu/WebGPURenderer.js';
  50. import WGSLNodeBuilder from '../src/renderers/webgpu/nodes/WGSLNodeBuilder.js';
  51. import GLSLNodeBuilder from '../src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js';
  52. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  53. init();
  54. function init() {
  55. // add the depedencies
  56. const width = 200;
  57. const height = 200;
  58. const camera = new THREE.PerspectiveCamera( 70, width / height, 0.1, 10 );
  59. camera.position.z = .72;
  60. camera.lookAt( 0, 0, 0 );
  61. const scene = new THREE.Scene();
  62. scene.background = new THREE.Color( 0x222222 );
  63. const rendererDOM = document.getElementById( 'renderer' );
  64. const renderer = new WebGPURenderer( { antialias: true } );
  65. renderer.outputColorSpace = THREE.LinearSRGBColorSpace;
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( 200, 200 );
  68. rendererDOM.appendChild( renderer.domElement );
  69. const material = new THREE.NodeMaterial();
  70. material.fragmentNode = THREE.vec4( 0, 0, 0, 1 );
  71. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  72. scene.add( mesh );
  73. renderer.setAnimationLoop( () => {
  74. renderer.render( scene, camera );
  75. } );
  76. // editor
  77. window.require.config( { paths: { 'vs': 'https://cdn.jsdelivr.net/npm/[email protected]/min/vs' } } );
  78. require( [ 'vs/editor/editor.main' ], () => {
  79. const options = {
  80. shader: 'fragment',
  81. outputColorSpace: THREE.LinearSRGBColorSpace,
  82. output: 'WGSL',
  83. preview: true
  84. };
  85. let timeout = null;
  86. let nodeBuilder = null;
  87. const editorDOM = document.getElementById( 'source' );
  88. const resultDOM = document.getElementById( 'result' );
  89. const tslCode = `// Simple uv.x animation
  90. const { texture, uniform, vec2, vec4, uv, oscSine, timerLocal } = THREE;
  91. //const samplerTexture = new THREE.Texture();
  92. const samplerTexture = new THREE.TextureLoader().load( './textures/uv_grid_opengl.jpg' );
  93. samplerTexture.wrapS = THREE.RepeatWrapping;
  94. //samplerTexture.wrapT = THREE.RepeatWrapping;
  95. //samplerTexture.colorSpace = THREE.SRGBColorSpace;
  96. const timer = timerLocal( .5 ); // .5 is speed
  97. const uv0 = uv();
  98. const animateUv = vec2( uv0.x.add( oscSine( timer ) ), uv0.y );
  99. // label is optional
  100. const myMap = texture( samplerTexture, animateUv ).rgb.label( 'myTexture' );
  101. const myColor = uniform( new THREE.Color( 0x0066ff ) ).label( 'myColor' );
  102. const opacity = .7;
  103. const desaturatedMap = myMap.rgb.saturation( 0 ); // try add .temp( 'myVar' ) after saturation()
  104. const finalColor = desaturatedMap.add( myColor );
  105. output = vec4( finalColor, opacity );
  106. `;
  107. const editor = window.monaco.editor.create( editorDOM, {
  108. value: tslCode,
  109. language: 'javascript',
  110. theme: 'vs-dark',
  111. automaticLayout: true,
  112. minimap: { enabled: false }
  113. } );
  114. const result = window.monaco.editor.create( resultDOM, {
  115. value: '',
  116. language: 'wgsl',
  117. theme: 'vs-dark',
  118. automaticLayout: true,
  119. readOnly: true,
  120. minimap: { enabled: false }
  121. } );
  122. const showCode = () => {
  123. result.setValue( nodeBuilder[ options.shader + 'Shader' ] );
  124. result.revealLine( 1 );
  125. };
  126. const build = () => {
  127. try {
  128. const tslCode = `let output = null;\n${ editor.getValue() }\nreturn { output };`;
  129. const nodes = new Function( 'THREE', tslCode )( THREE );
  130. mesh.material.fragmentNode = nodes.output;
  131. mesh.material.needsUpdate = true;
  132. let NodeBuilder;
  133. if ( options.output === 'WGSL' ) {
  134. NodeBuilder = WGSLNodeBuilder;
  135. } else if ( options.output === 'GLSL ES 3.0' ) {
  136. NodeBuilder = GLSLNodeBuilder;
  137. }
  138. nodeBuilder = new NodeBuilder( mesh, renderer );
  139. nodeBuilder.build();
  140. showCode();
  141. // extra debug info
  142. /*const style = 'background-color: #333; color: white; font-style: italic; border: 2px solid #777; font-size: 22px;';
  143. console.log( '%c [ WGSL ] Vertex Shader ', style );
  144. console.log( nodeBuilder.vertexShader );
  145. console.log( '%c [ WGSL ] Fragment Shader ', style );
  146. console.log( nodeBuilder.fragmentShader );*/
  147. } catch ( e ) {
  148. result.setValue( 'Error: ' + e.message );
  149. }
  150. };
  151. build();
  152. editor.getModel().onDidChangeContent( () => {
  153. if ( timeout ) clearTimeout( timeout );
  154. timeout = setTimeout( build, 1000 );
  155. } );
  156. // gui
  157. const gui = new GUI();
  158. gui.add( options, 'output', [ 'WGSL', 'GLSL ES 3.0' ] ).onChange( build );
  159. gui.add( options, 'shader', [ 'vertex', 'fragment' ] ).onChange( showCode );
  160. gui.add( options, 'outputColorSpace', [ THREE.LinearSRGBColorSpace, THREE.SRGBColorSpace ] ).onChange( ( value ) => {
  161. renderer.outputColorSpace = value;
  162. build();
  163. } );
  164. gui.add( options, 'preview' ).onChange( ( value ) => {
  165. rendererDOM.style.display = value ? '' : 'none';
  166. } );
  167. } );
  168. }
  169. </script>
  170. </body>
  171. </html>