webgpu_tsl_transpiler.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <html lang="en">
  2. <head>
  3. <title>three.js - webgpu - tsl transpiler</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. </style>
  25. <div id="source"></div>
  26. <div id="result"></div>
  27. <script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.52.2/min/vs/loader.js"></script>
  28. <script type="importmap">
  29. {
  30. "imports": {
  31. "three": "../build/three.webgpu.js",
  32. "three/webgpu": "../build/three.webgpu.js",
  33. "three/tsl": "../build/three.tsl.js",
  34. "three/addons/": "./jsm/"
  35. }
  36. }
  37. </script>
  38. <script type="module">
  39. import Transpiler from 'three/addons/transpiler/Transpiler.js';
  40. import GLSLDecoder from 'three/addons/transpiler/GLSLDecoder.js';
  41. import WGSLEncoder from 'three/addons/transpiler/WGSLEncoder.js';
  42. import TSLEncoder from 'three/addons/transpiler/TSLEncoder.js';
  43. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  44. init();
  45. function init() {
  46. // editor
  47. window.require.config( { paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.52.2/min/vs' } } );
  48. require( [ 'vs/editor/editor.main' ], () => {
  49. const options = {
  50. decoder: 'GLSL',
  51. encoder: 'TSL'
  52. };
  53. const encoderLanguages = {
  54. 'TSL': 'javascript',
  55. 'WGSL': 'wgsl'
  56. };
  57. let timeout = null;
  58. const editorDOM = document.getElementById( 'source' );
  59. const resultDOM = document.getElementById( 'result' );
  60. const glslCode = `/*
  61. * Perlin noise
  62. * https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
  63. */
  64. const float PI = 3.141592653589793;
  65. float rand(vec2 c){
  66. return fract(sin(dot(c.xy ,vec2(12.9898,78.233))) * 43758.5453);
  67. }
  68. float noise(vec2 p, float freq ){
  69. float unit = 1./freq;
  70. vec2 ij = floor(p/unit);
  71. vec2 xy = mod(p,unit)/unit;
  72. //xy = 3.*xy*xy-2.*xy*xy*xy;
  73. xy = .5*(1.-cos(PI*xy));
  74. float a = rand((ij+vec2(0.,0.)));
  75. float b = rand((ij+vec2(1.,0.)));
  76. float c = rand((ij+vec2(0.,1.)));
  77. float d = rand((ij+vec2(1.,1.)));
  78. float x1 = mix(a, b, xy.x);
  79. float x2 = mix(c, d, xy.x);
  80. return mix(x1, x2, xy.y);
  81. }
  82. float pNoise(vec2 p, int res){
  83. float persistance = .5;
  84. float n = 0.;
  85. float normK = 0.;
  86. float f = 4.;
  87. float amp = 1.;
  88. int iCount = 0;
  89. for (int i = 0; i<50; i++){
  90. n+=amp*noise(p, f);
  91. f*=2.;
  92. normK+=amp;
  93. amp*=persistance;
  94. if (iCount == res) break;
  95. iCount++;
  96. }
  97. float nf = n/normK;
  98. return nf*nf*nf*nf;
  99. }
  100. `;
  101. const editor = window.monaco.editor.create( editorDOM, {
  102. value: glslCode,
  103. language: 'c',
  104. theme: 'vs-dark',
  105. automaticLayout: true,
  106. minimap: { enabled: false }
  107. } );
  108. const result = window.monaco.editor.create( resultDOM, {
  109. value: '',
  110. language: 'javascript',
  111. theme: 'vs-dark',
  112. automaticLayout: true,
  113. readOnly: true,
  114. minimap: { enabled: false }
  115. } );
  116. const showCode = ( code ) => {
  117. result.setValue( code );
  118. result.revealLine( 1 );
  119. };
  120. const build = () => {
  121. try {
  122. let encoder;
  123. if ( options.encoder === 'TSL' ) {
  124. encoder = new TSLEncoder();
  125. } else if ( options.encoder === 'WGSL' ) {
  126. encoder = new WGSLEncoder();
  127. } else {
  128. throw new Error( 'Unknown encoder: ' + options.encoder );
  129. }
  130. //
  131. const glsl = editor.getValue();
  132. const decoder = new GLSLDecoder();
  133. const transpiler = new Transpiler( decoder, encoder );
  134. const tsl = transpiler.parse( glsl );
  135. showCode( tsl );
  136. } catch ( e ) {
  137. result.setValue( 'Error: ' + e.message );
  138. }
  139. };
  140. build();
  141. editor.getModel().onDidChangeContent( () => {
  142. if ( timeout ) clearTimeout( timeout );
  143. timeout = setTimeout( build, 1000 );
  144. } );
  145. // gui
  146. const gui = new GUI();
  147. gui.add( options, 'decoder', [ 'GLSL' ] );
  148. gui.add( options, 'encoder', [ 'TSL', 'WGSL' ] ).onChange( ( encoder => {
  149. const language = encoderLanguages[ encoder ];
  150. window.monaco.editor.setModelLanguage( result.getModel(), language );
  151. build();
  152. } ) );
  153. } );
  154. }
  155. </script>
  156. </body>
  157. </html>
粤ICP备19079148号