forked from hydra-synth/hydra-synth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader-generator.js
37 lines (31 loc) · 1.17 KB
/
shader-generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const Generator = require('./src/generator-factory.js')
const Sandbox = require('./src/eval-sandbox.js')
const baseUniforms = ['s0', 's1', 's2', 's3', 'o0', 'o1', 'o2'] // names of uniforms usually used in hydra. These can be customized
class ShaderGenerator {
constructor({ defaultUniforms = {time: 0, resolution: [1280, 720]}, customUniforms = baseUniforms, extendTransforms = []} = {}) {
var self = this
self.renderer = {}
var generatorOpts = { defaultUniforms, extendTransforms }
generatorOpts.changeListener = ({type, method, synth}) => {
if (type === 'add') {
self.renderer[method] = synth.generators[method]
} else if (type === 'remove') {
}
}
generatorOpts.defaultOutput = {
render: (pass) => self.generatedCode = pass[0]
}
this.generator = new Generator(generatorOpts)
this.sandbox = new Sandbox(this.renderer, false)
this.initialCode = `
${customUniforms.map((name) => `const ${name} = () => {}`).join(';')}
`
console.log(this.initialCode)
}
eval(code) {
this.sandbox.eval(`${this.initialCode}
${code}`)
return this.generatedCode
}
}
module.exports = ShaderGenerator