Comparing version 0.0.1 to 1.0.0
150
index.js
@@ -1,68 +0,126 @@ | ||
var tty = require('tty') | ||
, nopt = require('nopt') | ||
, path = require('path') | ||
, fs = require('fs') | ||
, shorthand | ||
, options | ||
module.exports = transform | ||
var glslify = require('glslify-stream') | ||
, deparser = require('glsl-deparser') | ||
, minify = require('glsl-min-stream') | ||
, replace = require('replace-method') | ||
, concat = require('concat-stream') | ||
, evaluate = require('static-eval') | ||
, extract = require('glsl-extract') | ||
, through = require('through') | ||
, resolve = require('resolve') | ||
, path = require('path') | ||
options = { | ||
'minify': Boolean | ||
, 'output': path | ||
, 'help': Boolean | ||
} | ||
function transform(filename) { | ||
var stream = through(write, end) | ||
, accum = [] | ||
, len = 0 | ||
shorthand = { | ||
'h': ['--help'] | ||
, 'm': ['--minify'] | ||
, 'o': ['--output'] | ||
} | ||
return stream | ||
module.exports = run | ||
function write(buf) { | ||
accum[accum.length] = buf | ||
len += buf.length | ||
} | ||
function help() { | ||
/* | ||
glslify [-o|--output file] [-h|--help] file | ||
function end() { | ||
var src = replace(Buffer.concat(accum).toString('utf8')) | ||
, loading = 0 | ||
, map = {} | ||
, id = 0 | ||
compile multiple glsl modules with #pragma: glslify directives into a single | ||
glsl output. | ||
src.replace(['glslify'], function(node) { | ||
var fragment | ||
, current | ||
, vertex | ||
, config | ||
if no output option is defined, output will be written to stdout. | ||
current = ++id | ||
arguments: | ||
if(!node.arguments.length) { | ||
return | ||
} | ||
--help, -h this help message. | ||
config = evaluate(node.arguments[0], { | ||
__filename: filename | ||
, __dirname: path.dirname(filename) | ||
}) | ||
--output path, -o path output result of minification to file represented by `path`. | ||
by default, output will be written to stdout. | ||
if(typeof config !== 'object') { | ||
return | ||
} | ||
*/ | ||
++loading | ||
glslify(config.vertex) | ||
.pipe(deparser()) | ||
.pipe(concat(onvertex)) | ||
var str = help+'' | ||
glslify(config.fragment) | ||
.pipe(deparser()) | ||
.pipe(concat(onfragment)) | ||
process.stdout.write(str.slice(str.indexOf('/*')+3, str.indexOf('*/'))) | ||
} | ||
return { | ||
type: 'CallExpression' | ||
, callee: { | ||
type: 'CallExpression' | ||
, callee: { | ||
type: 'Identifier' | ||
, name: 'require' | ||
} | ||
, arguments: [ | ||
{type: 'Literal', value: 'glslify/adapter.js'} | ||
] | ||
} | ||
, arguments: [ | ||
{type: 'Identifier', name: '__glslify_' + current + '_vert'} | ||
, {type: 'Identifier', name: '__glslify_' + current + '_frag'} | ||
, {type: 'Identifier', name: '__glslify_' + current + '_unis'} | ||
, {type: 'Identifier', name: '__glslify_' + current + '_attrs'} | ||
] | ||
} | ||
function run() { | ||
var parsed = nopt(options, shorthand) | ||
function onvertex(data) { | ||
vertex = data | ||
vertex && fragment && done() | ||
} | ||
if(parsed.help || !parsed.argv.remain.length) { | ||
return help(), process.exit(1) | ||
} | ||
function onfragment(data) { | ||
fragment = data | ||
vertex && fragment && done() | ||
} | ||
var should_minify = parsed.minify | ||
, output = parsed.output ? fs.createWriteStream(parsed.output) : process.stdout | ||
, input = path.resolve(parsed.argv.remain[0]) | ||
, stream | ||
function done() { | ||
extract(vertex + '\n' + fragment)(function(err, info) { | ||
if(err) { | ||
return stream.emit('error', err) | ||
} | ||
stream = glslify(input) | ||
--loading | ||
if(should_minify) | ||
stream = stream.pipe(minify()) | ||
map[current] = [vertex, fragment, info.uniforms, info.attributes] | ||
stream.pipe(deparser(!should_minify)) | ||
.pipe(output) | ||
if(!loading) { | ||
finish() | ||
} | ||
}) | ||
} | ||
}) | ||
if(!loading) { | ||
finish() | ||
} | ||
function finish() { | ||
var code = src.code() | ||
, unmap | ||
unmap = {vert: 0, frag: 1, unis: 2, attrs: 3} | ||
code = code.replace(/__glslify_(\d+)_(vert|frag|unis|attrs)/g, function(all, num, type) { | ||
return JSON.stringify(map[num][unmap[type]]) | ||
}) | ||
stream.queue(code) | ||
stream.queue(null) | ||
} | ||
} | ||
} |
{ | ||
"name": "glslify", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "command line glsl module system builder", | ||
@@ -28,3 +28,3 @@ "main": "index.js", | ||
"glslify-stream": "0.0.1", | ||
"nopt": "~2.0.0", | ||
"nopt": "^2.0.0", | ||
"glsl-deparser": "0.0.2", | ||
@@ -35,4 +35,13 @@ "glsl-min-stream": "0.0.2", | ||
"cssauron": "0.0.2", | ||
"through": "~1.X.X" | ||
"through": "^2.3.4", | ||
"replace-method": "0.0.0", | ||
"static-eval": "^0.1.0", | ||
"resolve": "^0.6.1", | ||
"concat-stream": "^1.4.1", | ||
"glsl-extract": "0.0.2", | ||
"gl-shader-core": "^2.0.0" | ||
}, | ||
"browser": { | ||
"index.js": "browser.js" | ||
} | ||
} |
# glslify | ||
a module system? in my GLSL? unpossible! | ||
a module system for GLSL and a transform enabling easy access to GLSL Shaders in JavaScript. | ||
## As a Browserify transform: | ||
```bash | ||
$ npm install --save glslify | ||
$ browserify entry.js -t glslify > bundle.js | ||
``` | ||
glslify will find and replace all instances of `glslify({vertex: path, fragment: path})` | ||
with a function that takes a webgl context and returns a [shader instance](npm.im/gl-shader-core). | ||
Recommended usage: | ||
```javascript | ||
var glslify = require('glslify') // requiring `glslify` is safe in this context. | ||
// if the program is run without the transform, | ||
// it'll output a helpful error message. | ||
var shell = require('gl-now')() | ||
var createShader = glslify({ | ||
vertex: './vertex.glsl' | ||
, fragment: './fragment.glsl' | ||
}) | ||
var program | ||
shell.on('gl-init', function() { | ||
program = createShader(shell.gl) | ||
}) | ||
``` | ||
As part of the transform, the program will be analyzed for its **uniforms** and **attributes**, | ||
so once you have a `program` instance, the following will work: | ||
```javascript | ||
// given a glsl program containing: | ||
// | ||
// uniform vec2 color; | ||
// uniform mat4 view; | ||
program.bind() | ||
program.uniforms.color = [0.5, 1.0] | ||
program.uniforms.view = [ | ||
1, 0, 0, 0 | ||
, 0, 1, 0, 0 | ||
, 0, 0, 1, 0 | ||
, 0, 0, 0, 1 | ||
] | ||
``` | ||
## As a GLSL module system: | ||
glslify can be run as a standalone command as well: | ||
```bash | ||
$ glslify my-module.glsl > output.glsl | ||
``` | ||
glslify is [browserify](https://github.com/substack/node-browserify) for GLSL. | ||
it allows you to write GLSL modules that export a local function, variable, or type, | ||
glslify allows you to write GLSL modules that export a local function, variable, or type, | ||
and `require` those modules to bring that export into another module. | ||
lookups work like node's `require` -- that is, it'll work relatively to the file first, | ||
Lookups work like node's `require` -- that is, it'll work relatively to the file first, | ||
and then work from the file's directory on up to the root directory looking for a package | ||
in `node_modules/`. | ||
output is plain GLSL -- so you'll need to run [exportify](https://github.com/substack/exportify) | ||
on them to use them with browserify. | ||
## example files | ||
| [`main.glsl`](#mainglsl) | [`file1.glsl`](#file1glsl) | [`file2.glsl`](#file2glsl) | | ||
|---------------------------|-----------------------------|-----------------------------| | ||
### main.glsl | ||
[back to file list](#example-files) | ||
```c | ||
@@ -28,3 +88,8 @@ // main.glsl | ||
// require a function from another file! | ||
#pragma glslify: program_one = require(./file1) | ||
// require a function from another file, and replace | ||
// `local_value` in that file with `resolution.x` from | ||
// this scope. | ||
#pragma glslify: program_two = require(./file2, local_value=resolution.x) | ||
@@ -47,2 +112,7 @@ | ||
### file1.glsl | ||
[back to file list](#example-files) | ||
```c | ||
@@ -57,2 +127,6 @@ // file1.glsl | ||
### file2.glsl | ||
[back to file list](#example-files) | ||
```c | ||
@@ -59,0 +133,0 @@ // file2.glsl |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11102
10
167
1
176
0
14
2
2
+ Addedconcat-stream@^1.4.1
+ Addedgl-shader-core@^2.0.0
+ Addedglsl-extract@0.0.2
+ Addedreplace-method@0.0.0
+ Addedresolve@^0.6.1
+ Addedstatic-eval@^0.1.0
+ Addedamdefine@1.0.1(transitive)
+ Addedast-types@0.3.38(transitive)
+ Addedbuffer-from@1.1.2(transitive)
+ Addedcls@0.1.5(transitive)
+ Addedconcat-stream@1.6.2(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddup@1.0.0(transitive)
+ Addedgl-shader-core@2.2.0(transitive)
+ Addedglsl-extract@0.0.2(transitive)
+ Addedglsl-parser@0.0.5(transitive)
+ Addedglsl-tokenizer@0.0.8(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@0.0.11.0.0(transitive)
+ Addednopt@2.2.1(transitive)
+ Addedprivate@0.1.8(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@1.0.342.3.8(transitive)
+ Addedrecast@0.5.27(transitive)
+ Addedreplace-method@0.0.0(transitive)
+ Addedresolve@0.6.3(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedsource-map@0.1.32(transitive)
+ Addedstatic-eval@0.1.1(transitive)
+ Addedstring_decoder@0.10.311.1.1(transitive)
+ Addedthrough@2.3.8(transitive)
+ Addedtypedarray@0.0.6(transitive)
+ Addedutf8-stream@0.0.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedglsl-parser@0.0.9(transitive)
- Removedglsl-tokenizer@0.0.9(transitive)
- Removednopt@2.0.0(transitive)
Updatednopt@^2.0.0
Updatedthrough@^2.3.4