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,9 @@
node_modules
*.log
.DS_Store
bundle.js
test
test.js
demo
example
!.gitignore

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,98 @@
# glsl-token-descope
[![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)
"Descope" an array of GLSL tokens such that they can be safely inlined alongside
within another shader without causing any global variable conflicts.
Useful for modularising GLSL source files, e.g. as is done in
[glslify](http://github.com/stackgl/glslify), but should be useful in other
tools too.
Written with WebGL's GLSL syntax in mind all the same, pull requests to
support other variants would be much appreciated :)
## Usage
[![NPM](https://nodei.co/npm/glsl-token-descope.png)](https://nodei.co/npm/glsl-token-descope/)
### `descope(tokens, [rename(name)])`
Takes an array of GLSL `tokens` produced by
[glsl-tokenizer](http://github.com/stackgl/glsl-tokenizer) and renames variables
to avoid global conflicts by modifying their "data" property in-place.
For example:
``` javascript
var tokenize = require('glsl-tokenizer/string')
var descope = require('glsl-token-descope')
var stringify = require('glsl-token-string')
var src = `
precision mediump float;
uniform mat4 top1;
uniform float top2;
void main() {
float x = 1.0;
gl_FragColor = vec4(vec3(x), top2);
}
`.trim()
var tokens = tokenize(src)
console.log(stringify(descope(tokens)))
```
Which should rename `main`, `top1` and `top2` to result in this output:
``` glsl
precision mediump float;
uniform mat4 top1_0;
uniform float top2_1;
void main_2() {
float x = 1.0;
gl_FragColor = vec4(vec3(x), top2_1);
}
```
Optionally, you may pass in a custom `rename` function as `descope`'s second
argument to choose how you rename your variables. For example, adding a custom
`rename` function to the previous function:
``` javascript
descope(tokens, function(name) {
return 'a_' + name
})
```
Would result in the following shader:
``` glsl
precision mediump float;
uniform mat4 a_top1;
uniform float a_top2;
void a_main() {
float x = 1.0;
gl_FragColor = vec4(vec3(x), a_top2);
}
```
## See Also
* [glslify](http://github.com/stackgl/glslify)
* [glsl-token-scope](http://github.com/stackgl/glsl-token-scope)
* [glsl-token-depth](http://github.com/stackgl/glsl-token-depth)
* [glsl-token-properties](http://github.com/stackgl/glsl-token-properties)
* [glsl-token-assignments](http://github.com/stackgl/glsl-token-assignments)
* [glsl-token-string](http://github.com/stackgl/glsl-token-string)
## License
MIT. See [LICENSE.md](http://github.com/stackgl/glsl-token-descope/blob/master/LICENSE.md) for details.

View File

@@ -0,0 +1,19 @@
var tokenize = require('glsl-tokenizer/string')
var descope = require('./')
var stringify = require('glsl-token-string')
var src =
[ 'precision mediump float;'
,
, 'uniform mat4 top1;'
, 'uniform float top2;'
,
, 'void main() {'
, ' float x = 1.0;'
, ' gl_FragColor = vec4(vec3(x), top2);'
, '}'
].join('\n')
var tokens = tokenize(src)
console.log(stringify(descope(tokens)))

View File

@@ -0,0 +1,47 @@
precision mediump float;
uniform mat4 uProj;
uniform mat4 uView;
float x = 0.0;
float y = 0.0;
struct Thing {
float prop1;
float prop2;
float prop3;
};
uniform Thing uniformThing;
uniform struct Another {
float prop1;
} uniformAnother;
float _dim(float x, float y);
float _dim(float x, float y) {
return x + y;
}
void main() {
Thing y = Thing(sin(.0), .5, 1.);
y.prop1;
y.prop2;
y.prop3;
uniformThing.prop1;
uniformThing.prop2;
uniformThing.prop3 = 2.0;
uniformAnother.prop1;
for (int i = 0; i < 3; i++) {
vec[i];
float x = 10.0;
Thing uniformThing;
uniformThing.prop1;
}
uProj[0].x;
gl_FragColor = vec4(x, vec3(1));
}

View File

@@ -0,0 +1,71 @@
module.exports = glslTokenDescope
function glslTokenDescope(tokens, rename) {
require('glsl-token-depth')(tokens)
require('glsl-token-scope')(tokens)
require('glsl-token-properties')(tokens)
require('glsl-token-assignments')(tokens)
var scope = getScope(tokens)
var renamer = rename || defaultRenamer()
var map = {}
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]
var stack = token.stack
var name = token.data
token.descoped = false
if (token.type !== 'ident') continue
if (token.property) continue
if (token.structMember) continue
var bound = false
for (var j = stack.length - 1; j >= 0; j--) {
var s = scope[stack[j]]
if (!s) continue
if (!s[name]) continue
bound = true
// exit if declaration not in top-level scope
if (j) break
token.descoped = token.data
token.data = map[name] = map[name] || renamer(name, token) || token.data
}
// Handle unbound variables, i.e. ones not defined anywhere
// in the shader source but still used.
if (!bound) {
token.descoped = token.data
token.data = map[name] = map[name] || renamer(name, token) || token.data
}
}
return tokens
}
function defaultRenamer() {
var k = 0
return function rename(name) {
return name + '_' + (k++).toString(36)
}
}
function getScope(tokens) {
var scope = {}
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]
if (token.declaration) {
scope[token.scope] = scope[token.scope] || {}
scope[token.scope][token.data] = token
}
}
return scope
}

View File

@@ -0,0 +1,41 @@
{
"name": "glsl-token-descope",
"version": "1.0.2",
"description": "\"Descope\" an array of GLSL tokens such that they can be safely inlined alongside within another shader without causing any global variable conflicts.",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "node test/index.js | tap-spec",
"eyeball": "node test/eyeball.js"
},
"author": {
"name": "Hugh Kennedy",
"email": "hughskennedy@gmail.com",
"url": "http://hughsk.io/"
},
"dependencies": {
"glsl-token-assignments": "^2.0.0",
"glsl-token-depth": "^1.1.0",
"glsl-token-properties": "^1.0.0",
"glsl-token-scope": "^1.1.0"
},
"devDependencies": {
"6to5": "^2.5.0",
"chalk": "^0.5.1",
"glsl-token-string": "^1.0.0",
"glsl-tokenizer": "^2.0.0",
"tap-spec": "^2.1.2",
"tape": "^3.0.3"
},
"repository": {
"type": "git",
"url": "git://github.com/stackgl/glsl-token-descope.git"
},
"keywords": [
"ecosystem:stackgl"
],
"homepage": "https://github.com/stackgl/glsl-token-descope",
"bugs": {
"url": "https://github.com/stackgl/glsl-token-descope/issues"
}
}