larry babby and threejs for glsl

This commit is contained in:
Sam
2024-06-24 21:24:00 +12:00
parent 87d5dc634d
commit 907ebae4c0
6474 changed files with 1279596 additions and 8 deletions

View File

@@ -0,0 +1,12 @@
The MIT License (MIT)
=====================
Copyright (c) 2014 [stackgl](http://github.com/stackgl/) contributors
*stackgl contributors listed at <https://github.com/stackgl/contributing/blob/master/CONTRIBUTING.md#contributors>*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,42 @@
# glslify-bundle
[![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)
Bundle a [glslify-deps](http://github.com/stackgl/glslify-deps) dependency tree into
a GLSL source string.
This has been separated from *glslify-deps* such that you can prebundle a dependency
tree server-side, but then still modify shader file contents in a browser.
## Usage
[![NPM](https://nodei.co/npm/glslify-bundle.png)](https://nodei.co/npm/glslify-bundle/)
### `source = bundle(deps)`
Takes the output object from [glslify-deps](http://github.com/stackgl/glslify-deps)
and returns a bundled GLSL string.
``` javascript
var bundle = require('glslify-bundle')
var deps = require('glslify-deps')
var path = require('path')
var file = path.join(__dirname, 'index.glsl')
deps().add(file, function(err, tree) {
if (err) throw err
var glsl = bundle(tree)
console.log(glsl)
})
```
## Contributing
See [stackgl/contributing](https://github.com/stackgl/contributing) for details.
## License
MIT. See [LICENSE.md](http://github.com/stackgl/glslify-bundle/blob/master/LICENSE.md) for details.

View File

@@ -0,0 +1,16 @@
#pragma glslify: LightStruct = require(./struct)
#pragma glslify: export(another)
float alongside() {
return 0.0;
}
float another(float n) {
LightStruct b = LightStruct(vec3(0), vec4(0));
return n * n + b.position + alongside() + t + u;
}
float another(float n, float c) {
LightStruct b = LightStruct(vec3(0), vec4(0));
return n * n + b.position + another(c) + t + u - v;
}

View File

@@ -0,0 +1,9 @@
precision mediump float;
uniform vec2 uVec;
#pragma glslify: a = require( "./sibling" )
void main() {
gl_FragColor = vec4(a(1.0));
}

View File

@@ -0,0 +1,32 @@
#pragma glslify: Ray = require('./complex-ray.glsl')
#pragma glslify: RayResult = require('./complex-ray-result.glsl')
RayResult rayBail() {
return RayResult(0.0, 0.0, false);
}
RayResult march(Ray source, float maxd, float precis) {
float latest = precis * 2.0;
float dist = +0.0;
float type = -1.0;
RayResult res = rayBail();
for (int i = 0; i < 99; i++) {
if (latest < precis || dist > maxd) break;
res = map(source.ro + source.rd * dist);
dist += res.d;
}
if(dist >= maxd) {
return rayBail();
}
return res;
}
RayResult march(Ray ray) {
return march(ray, 20.0, 0.001);
}
#pragma glslify: export(march)

View File

@@ -0,0 +1,7 @@
struct Result {
float d;
float type;
bool hit;
};
#pragma glslify: export(Result)

View File

@@ -0,0 +1,6 @@
struct Ray {
vec3 ro;
vec3 rd;
};
#pragma glslify: export(Ray)

View File

@@ -0,0 +1,29 @@
precision mediump float;
#pragma glslify: RayResult = require('./complex-ray-result')
#pragma glslify: Ray = require('./complex-ray')
RayResult doModel(vec3 p);
#pragma glslify: march = require('./complex-march', map = doModel)
RayResult doModel(vec3 p) {
float d = length(p);
float id = 1.0;
return RayResult(d, id, true);
}
void main() {
vec3 color = vec3(0);
vec3 rd = normalize(vec3(1));
vec3 ro = vec3(0);
RayResult t = march(Ray(ro, rd));
if (t.hit) {
color = vec3(t.d);
}
gl_FragColor = vec4(color, 1);
}

View File

@@ -0,0 +1,5 @@
float mappedChild() {
return map(vec3(0));
}
#pragma glslify: export(mappedChild)

View File

@@ -0,0 +1,20 @@
precision mediump float;
float source1(vec3 p);
float source2(vec3 p);
#pragma glslify: map1 = require('./multiple-mapped-child', map = source1)
#pragma glslify: map2 = require('./multiple-mapped-child', map = source2)
#pragma glslify: map3 = require('./multiple-mapped-child', map = source1)
float source1(vec3 p) {
return length(p) - 1.0;
}
float source2(vec3 p) {
return length(p) - 2.0;
}
void main() {
gl_FragColor = vec4(map1(), map2(), map3(), 1);
}

View File

@@ -0,0 +1,9 @@
#pragma glslify: another_thing = require(./another, t = v = 4.0, u = uVec.x)
#pragma glslify: LightStruct = require(./struct)
float sibling(float a) {
LightStruct b = LightStruct(vec3(0), vec4(0));
return a * another_thing(b.color + 2.0);
}
#pragma glslify: export(sibling)

View File

@@ -0,0 +1,7 @@
precision mediump float;
attribute vec2 position;
void main() {
gl_Position = vec4(position, 1, 1);
}

View File

@@ -0,0 +1,6 @@
#pragma glslify: export(Light)
struct Light {
vec3 position;
vec4 color;
};

View File

@@ -0,0 +1,5 @@
vec3 child(vec2 coord) {
return vec3(g.zx - a * coord + b - c + float(e) * f, t);
}
#pragma glslify: export(child)

View File

@@ -0,0 +1,17 @@
struct Ray {
vec3 origin;
vec3 direction;
} ray1;
const Ray ray2 = Ray(vec3(0), vec3(0, 1, 0));
const vec2 vec = vec2(0.0);
const float pi = 6.28;
#pragma glslify: n = require('./unsuffixable-child', a = vec.x, b = 5.0, c = vec, e = 2, f = ray1.origin.xy, t = pi, g = ray2.direction)
void runner(in vec2 fragCoord) {
gl_FragColor = vec4(n(fragCoord + vec2(d + h)), 1.0);
}
#pragma glslify: export(runner)

View File

@@ -0,0 +1,10 @@
precision mediump float;
const float pi = 3.14;
uniform vec2 P;
#pragma glslify: run = require('./unsuffixable-source.glsl', d = pi, h = P.x)
void main() {
run(gl_FragCoord.xy);
}

View File

@@ -0,0 +1,3 @@
#version 150
float PI = 3.14159265359;

View File

@@ -0,0 +1,232 @@
/* eslint-disable no-redeclare */
var hash = require('murmurhash-js/murmurhash3_gc')
var trim = require('glsl-token-whitespace-trim')
var tokenize = require('glsl-tokenizer/string')
var inject = require('glsl-inject-defines')
var defines = require('glsl-token-defines')
var descope = require('glsl-token-descope')
var clean = require('./lib/clean-suffixes')
var string = require('glsl-token-string')
var scope = require('glsl-token-scope')
var depth = require('glsl-token-depth')
var topoSort = require('./lib/topo-sort')
var copy = require('shallow-copy')
module.exports = function (deps) {
return inject(Bundle(deps).src, {
GLSLIFY: 1
})
}
function Bundle (deps) {
if (!(this instanceof Bundle)) return new Bundle(deps)
// Reorder dependencies topologically
deps = topoSort(deps)
this.depList = deps
this.depIndex = indexBy(deps, 'id')
this.exported = {}
this.cache = {}
this.varCounter = 0
this.src = []
for (var i = 0; i < deps.length; i++) {
this.preprocess(deps[i])
}
for (var i = 0; i < deps.length; i++) {
if (deps[i].entry) {
this.src = this.src.concat(this.bundle(deps[i]))
}
}
this.src = string(this.src)
this.src = string(clean(trim(tokenize(this.src))))
}
var proto = Bundle.prototype
proto.preprocess = function (dep) {
var tokens = tokenize(dep.source)
var imports = []
var exports = null
depth(tokens)
scope(tokens)
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]
if (token.type !== 'preprocessor') continue
if (!glslifyPreprocessor(token.data)) continue
var exported = glslifyExport(token.data)
var imported = glslifyImport(token.data)
if (exported) {
exports = exported[1]
tokens.splice(i--, 1)
} else if (imported) {
var name = imported[1]
var maps = imported[2].split(/\s?,\s?/g)
var path = maps.shift()
.trim()
.replace(/^'|'$/g, '')
.replace(/^"|"$/g, '')
var target = this.depIndex[dep.deps[path]]
imports.push({
name: name,
path: path,
target: target,
maps: toMapping(maps),
index: i
})
tokens.splice(i--, 1)
}
}
var eof = tokens[tokens.length - 1]
if (eof && eof.type === 'eof') {
tokens.pop()
}
if (dep.entry) {
exports = exports || 'main'
}
if (!exports) {
throw new Error(dep.file + ' does not export any symbols')
}
dep.parsed = {
tokens: tokens,
imports: imports,
exports: exports
}
}
proto.bundle = function (entry) {
var resolved = {}
var result = resolve(entry, [])[1]
return result
function resolve (dep, bindings) {
// Compute suffix for module
bindings.sort()
var ident = bindings.join(':') + ':' + dep.id
var suffix = '_' + hash(ident)
if (dep.entry) {
suffix = ''
}
// Test if export is already resolved
var exportName = dep.parsed.exports + suffix
if (resolved[exportName]) {
return [exportName, []]
}
// Initialize map for variable renamings based on bindings
var rename = {}
for (var i = 0; i < bindings.length; ++i) {
var binding = bindings[i]
rename[binding[0]] = binding[1]
}
// Resolve all dependencies
var imports = dep.parsed.imports
var edits = []
for (var i = 0; i < imports.length; ++i) {
var data = imports[i]
var importMaps = data.maps
var importName = data.name
var importTarget = data.target
var importBindings = Object.keys(importMaps).map(function (id) {
var value = importMaps[id]
// floats/ints should not be renamed
if (value.match(/^\d+(?:\.\d+?)?$/g)) {
return [id, value]
}
// properties (uVec.x, ray.origin, ray.origin.xy etc.) should
// have their host identifiers renamed
var parent = value.match(/^([^.]+)\.(.+)$/)
if (parent) {
return [id, (rename[parent[1]] || (parent[1] + suffix)) + '.' + parent[2]]
}
return [id, rename[value] || (value + suffix)]
})
var importTokens = resolve(importTarget, importBindings)
rename[importName] = importTokens[0]
edits.push([data.index, importTokens[1]])
}
// Rename tokens
var parsedTokens = dep.parsed.tokens.map(copy)
var parsedDefs = defines(parsedTokens)
var tokens = descope(parsedTokens, function (local, token) {
if (parsedDefs[local]) return local
if (rename[local]) return rename[local]
return local + suffix
})
// Insert edits
edits.sort(function (a, b) {
return b[0] - a[0]
})
for (var i = 0; i < edits.length; ++i) {
var edit = edits[i]
tokens = tokens.slice(0, edit[0])
.concat(edit[1])
.concat(tokens.slice(edit[0]))
}
resolved[exportName] = true
return [exportName, tokens]
}
}
function glslifyPreprocessor (data) {
return /#pragma glslify:/.test(data)
}
function glslifyExport (data) {
return /#pragma glslify:\s*export\(([^)]+)\)/.exec(data)
}
function glslifyImport (data) {
return /#pragma glslify:\s*([^=\s]+)\s*=\s*require\(([^)]+)\)/.exec(data)
}
function indexBy (deps, key) {
return deps.reduce(function (deps, entry) {
deps[entry[key]] = entry
return deps
}, {})
}
function toMapping (maps) {
if (!maps) return false
return maps.reduce(function (mapping, defn) {
defn = defn.split(/\s?=\s?/g)
var expr = defn.pop()
defn.forEach(function (key) {
mapping[key] = expr
})
return mapping
}, {})
}

View File

@@ -0,0 +1,48 @@
module.exports = clean
var suffix = /^([^_]+)_(\d{4,})$/
function clean (tokens) {
var blacklist = {}
var index = {}
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]
if (token.type !== 'ident') continue
var match = token.data.match(suffix)
if (!match) {
blacklist[token.data] = true
continue
}
var pre = match[1]
var suf = match[2]
index[pre] = index[pre] || {}
index[pre][suf] = index[pre][suf] || []
index[pre][suf].push(token)
}
Object.keys(index).forEach(function (prefix) {
var suffixes = Object.keys(index[prefix])
if (suffixes.length === 1 && !blacklist[prefix]) {
var tokens = index[prefix][suffixes[0]]
for (var i = 0; i < tokens.length; i++) {
tokens[i].data = prefix
}
return
}
suffixes.forEach(function (suffix, i) {
var token = index[prefix][suffix]
var rename = prefix + '_' + i
if (blacklist[rename]) return
for (var j = 0; j < token.length; j++) {
token[j].data = rename
}
})
})
return tokens
}

View File

@@ -0,0 +1,69 @@
module.exports = topoSort
// Permutes the dependencies into topological order
function topoSort (deps) {
// Build reversed adjacency list
var adj = {}
var inDegree = {}
var index = {}
deps.forEach(function (dep) {
var v = dep.id
var nbhd = Object.keys(dep.deps)
index[dep.id] = dep
inDegree[v] = nbhd.length
nbhd.forEach(function (filename) {
var u = dep.deps[filename]
if (adj[u]) {
adj[u].push(v)
} else {
adj[u] = [v]
}
})
})
// Initialize toVisit queue
var result = []
var inverse = {}
deps.forEach(function (dep) {
var v = dep.id
if (!adj[v]) {
adj[v] = []
}
if (inDegree[v] === 0) {
inverse[v] = result.length
result.push(v)
}
})
// Run BFS
for (var ptr = 0; ptr < result.length; ptr++) {
var v = result[ptr]
adj[v].forEach(function (u) {
if (--inDegree[u] === 0) {
inverse[u] = result.length
result.push(u)
}
})
}
if (result.length !== deps.length) {
throw new Error('cyclic dependency')
}
// Relabel dependencies
return result.map(function (v) {
var dep = index[v]
var deps = dep.deps
var ndeps = {}
Object.keys(deps).forEach(function (filename) {
ndeps[filename] = inverse[deps[filename]] | 0
})
return {
id: inverse[v] | 0,
deps: ndeps,
file: dep.file,
source: dep.source,
entry: dep.entry
}
})
}

View File

@@ -0,0 +1,58 @@
{
"name": "glslify-bundle",
"version": "5.1.1",
"description": "Bundle a glslify-deps dependency tree into a GLSL source string",
"main": "index.js",
"license": "MIT",
"scripts": {
"pretest": "standard; true",
"test": "node test/_check-headless",
"test:linux": "xvfb-run -s '-ac -screen 0 1280x1024x24' npm run test:basic",
"test:basic": "node test | tap-spec"
},
"author": {
"name": "Hugh Kennedy",
"email": "hughskennedy@gmail.com",
"url": "http://hughsk.io/"
},
"dependencies": {
"glsl-inject-defines": "^1.0.1",
"glsl-token-defines": "^1.0.0",
"glsl-token-depth": "^1.1.1",
"glsl-token-descope": "^1.0.2",
"glsl-token-scope": "^1.1.1",
"glsl-token-string": "^1.0.1",
"glsl-token-whitespace-trim": "^1.0.0",
"glsl-tokenizer": "^2.0.2",
"murmurhash-js": "^1.0.0",
"shallow-copy": "0.0.1"
},
"devDependencies": {
"gl": "^4.1.1",
"gl-shader": "^4.0.6",
"glslify-deps": "^1.2.1",
"standard": "^12.0.1",
"tap-spec": "^5.0.0",
"tape": "^4.9.1",
"which": "^1.3.1"
},
"repository": {
"type": "git",
"url": "git://github.com/stackgl/glslify-bundle.git"
},
"keywords": [
"ecosystem:stackgl",
"glslify",
"glsl",
"shader",
"bundle",
"module",
"system",
"tool",
"webgl"
],
"homepage": "https://github.com/stackgl/glslify-bundle",
"bugs": {
"url": "https://github.com/stackgl/glslify-bundle/issues"
}
}