Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Transpile GLSL to JS

NotificationsYou must be signed in to change notification settings

stackgl/glsl-transpiler

Repository files navigation

Transformsglsl source to optimized js code. It converts vectors and matrices to arrays, expands swizzles, applies expressions optimizations and provides stdlib for environment compatibility.

Usage

npm install glsl-transpiler

importGLSLfrom'glsl-transpiler'varcompile=GLSL({uniform:function(name){return`uniforms.${name}`},attribute:function(name){return`attributes.${name}`}})compile(`precision mediump floatattribute vec2 uvattribute vec4 colorvarying vec4 fColoruniform vec2 uScreenSizevoid main (void) {fColor = colorvec2 position = vec2(uv.x, -uv.y) * 1.0position.x *= uScreenSize.y / uScreenSize.xgl_Position = vec4(position, 0, 1)}`)// result:`var uv = attributes.uvvar color = attributes.colorvar fColor = new Float32Array([0, 0, 0, 0])var uScreenSize = uniforms.uScreenSizefunction main () {fColor = colorvar position = new Float32Array([uv[0], -uv[1]])position[0] *= uScreenSize[1] / uScreenSize[0]gl_Position = new Float32Array([position[0], position[1], 0, 1])}`

API

glsl-transpiler

To apply compilation to glsl AST or string, requireglsl-transpiler:

importGLSLfrom'glsl-transpiler'letcompile=GLSL({// Enable expressions optimizations.optimize:true,// Apply preprocessing. Pass custom preprocessor function `(srcString) => resultString;` to set own preprocessing.preprocess:true,// A function replacing each uniform declaration. Eg: ``(name, node) => `uniforms["${name}"]`;`` will render each uniform declaration as `var <name> = uniforms["<name>"]`.uniform:false,// Same as `uniform`, but for attribute declarations.attribute:false,// Same as `uniform`, but for varying declarations.varying:false,// GLSL shader version, one of `'300 es'` or `'100 es'`.version:'100 es',// Append stdlib includes for the result. Can be bool or an object with defined stdlib functions to include, eg. `{normalize: false, min: false}`.includes:true,// Enable debugging facilities: `print(anything)` will log to console a string of transpiled code with it’s type separated by colon, `show(anything)` will print the rendered descriptor of passed fragment of code. Note also that you can safely use `console.log(value)` to debug shader runtime.debug:false})//compile source codeletresult=compile('...source.glsl')//get collected infolet{attributes,uniforms,varyings,structs,functions,scopes}=compile.compiler//clean collected infocompiler.reset()

Note thattexture2D function expects whether ndarray instance or definedwidth andheight parameters on passed array.

glsl-transpiler/stream

glsl-transpiler can also be used as a stream. For each node from theglsl-parser it will return compiled js chunk:

importcompilefrom'glsl-transpiler/stream.js'importparsefrom'glsl-parser/stream.js'importtokenizefrom'glsl-tokenizer/stream.js'fs.createReadStream('./source.glsl').pipe(tokenize()).pipe(parse()).pipe(compile(options?)).once('end',function(){//this.source contains the actual version of the compiled code//and gets updated on each input chunk of data.console.log(this.source)})

Dependencies

Used by

  • nogl-shader-output — evaluate fragment shader on rectangular vertex input, gl-less.
  • GLSLRun – debug shader via addingprint() function.

Similar

About

Transpile GLSL to JS

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors6


[8]ページ先頭

©2009-2025 Movatter.jp