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