glsl-transpiler
Transpile GLSL to JS
Last updated 2 years ago by dy .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install glsl-transpiler 
SYNC missed versions from official npm registry.

glsl-transpiler Build Status

Transforms glsl 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

var Compiler = require('glsl-transpiler')

var compile = Compiler({
	uniform: function (name) {
		return `uniforms.${name}`
	},
	attribute: function (name) {
		return `attributes.${name}`
	}
})

compile(`
	precision mediump float
	attribute vec2 uv
	attribute vec4 color
	varying vec4 fColor
	uniform vec2 uScreenSize

	void main (void) {
		fColor = color
		vec2 position = vec2(uv.x, -uv.y) * 1.0
		position.x *= uScreenSize.y / uScreenSize.x
		gl_Position = vec4(position, 0, 1)
	}
`)

// result:

`
var uv = attributes.uv
var color = attributes.color
var fColor = [0, 0, 0, 0]
var uScreenSize = uniforms.uScreenSize

function main () {
	fColor = color
	var position = [uv[0], -uv[1]]
	position[0] *= uScreenSize[1] / uScreenSize[0]
	gl_Position = [position[0], position[1], 0, 1]
}
`

API

glsl-transpiler

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

var GLSL = require('glsl-transpiler')

var source = glslify('./source.glsl')
var compile = GLSL(options?)

//compile source code
var result = compile(source)


//get collected info
var compiler = compile.compiler
compiler.attributes
compiler.uniforms
compiler.varyings
compiler.structs
compiler.functions
compiler.scopes


//clean collected info
compiler.reset()

options

Property Default Description
optimize true Enable expressions optimizations, eg. TODO
preprocess true Apply preprocessing. Pass custom preprocessor function function (srcString) { return resultString; } to set own preprocessing.
uniform false A function replacing each uniform declaration. Eg: function (name, node) { return 'uniforms["' + name + '"]' } will render each uniform declaration as var <name> = uniforms["<name>"].
attribute false Same as uniform, but for attribute declarations.
varying false Same as uniform, but for varying declarations.
version '100 es' GLSL shader version, one of '300 es' or '100 es'.
comments false TODO: preserve comments in source code.
sourceMap false TODO: include source map for the transpiled code.
includes true Append stdlib includes for the result. Can be bool or an object with defined stdlib functions to include, eg. {normalize: false, min: false}.
debug false 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.

Note that texture2D function expects whether ndarray instance or defined width and height parameters on passed array.

glsl-transpiler/stream

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

var compile = require('glsl-transpiler/stream')
var parse = require('glsl-parser/stream')
var tokenize = require('glsl-tokenizer/stream')

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)
})

Related

Current Tags

  • 1.8.5                                ...           latest (2 years ago)

26 Versions

  • 1.8.5                                ...           2 years ago
  • 1.8.4                                ...           2 years ago
  • 1.8.3                                ...           2 years ago
  • 1.8.2                                ...           2 years ago
  • 1.8.1                                ...           2 years ago
  • 1.8.0                                ...           2 years ago
  • 1.7.2                                ...           2 years ago
  • 1.7.1                                ...           2 years ago
  • 1.7.0                                ...           2 years ago
  • 1.6.1                                ...           2 years ago
  • 1.6.0                                ...           2 years ago
  • 1.5.11                                ...           2 years ago
  • 1.5.10                                ...           2 years ago
  • 1.5.9                                ...           2 years ago
  • 1.5.8                                ...           3 years ago
  • 1.5.7                                ...           4 years ago
  • 1.5.6                                ...           4 years ago
  • 1.5.5                                ...           5 years ago
  • 1.5.4                                ...           5 years ago
  • 1.5.3                                ...           5 years ago
  • 1.5.2                                ...           5 years ago
  • 1.5.1                                ...           5 years ago
  • 1.5.0                                ...           5 years ago
  • 1.4.0                                ...           5 years ago
  • 1.3.0                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 26
Last Month 40
Dependencies (8)
Dev Dependencies (12)

Copyright 2014 - 2016 © taobao.org |