Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

glslify

Package Overview
Dependencies
Maintainers
2
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

glslify - npm Package Compare versions

Comparing version 0.0.1 to 1.0.0

adapter.js

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc