-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update & optimize #3
Changes from 6 commits
83f908d
01d69cf
63ddda9
1ad939e
f821180
92f0701
a941ad5
13297c5
c86d111
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,113 @@ | ||
var create = require('webgl-context') | ||
var getPixels = require('canvas-pixels').get3d | ||
var triangle = require('a-big-triangle') | ||
var xtend = require('xtend') | ||
var assign = require('xtend/mutable') | ||
var glExt = require('webglew') | ||
var Framebuffer = require('gl-fbo') | ||
var Shader = require('gl-shader') | ||
|
||
module.exports = function(opt) { | ||
opt = xtend({ | ||
width: 1, | ||
|
||
module.exports = function (shader, opt) { | ||
if (!opt) { | ||
//just options | ||
if (typeof shader === 'object' && !shader.fragShader) { | ||
opt = shader | ||
} | ||
//just a shader | ||
else { | ||
opt = { | ||
shader: shader | ||
} | ||
} | ||
} | ||
else { | ||
opt.shader = shader | ||
} | ||
|
||
opt = xtend({ | ||
width: 1, | ||
height: 1, | ||
preserveDrawingBuffer: true | ||
preserveDrawingBuffer: true, | ||
float: true | ||
}, opt) | ||
var gl = opt.gl || create(opt) | ||
|
||
var gl = opt.shader && opt.shader.gl || opt.gl || create(opt) | ||
if (!opt.shader) | ||
throw new Error('no shader supplied to gl-shader-output') | ||
|
||
|
||
//set gl context dims | ||
gl.canvas.width = opt.width | ||
gl.canvas.height = opt.height | ||
|
||
var shader = typeof opt.shader === 'function' | ||
? opt.shader(gl) | ||
: opt.shader | ||
|
||
//create gl-shader, if only fragment shader source | ||
if (typeof shader === 'string') { | ||
shader = Shader(gl, '\ | ||
attribute vec2 position;\ | ||
void main() {\ | ||
gl_Position = vec4(position, 1.0, 1.0);\ | ||
}\ | ||
' , shader) | ||
} | ||
|
||
//micro optimizations | ||
gl.disable(gl.DEPTH_TEST) | ||
gl.disable(gl.BLEND) | ||
gl.disable(gl.CULL_FACE) | ||
gl.disable(gl.DITHER) | ||
gl.disable(gl.POLYGON_OFFSET_FILL) | ||
gl.disable(gl.SAMPLE_COVERAGE) | ||
gl.disable(gl.SCISSOR_TEST) | ||
gl.disable(gl.STENCIL_TEST) | ||
|
||
var buffer = gl.createBuffer() | ||
gl.bindBuffer(gl.ARRAY_BUFFER, buffer) | ||
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, -1, 3, 3, -1]), gl.STATIC_DRAW) | ||
shader.attributes.position.pointer() | ||
|
||
//try to use floats | ||
var float = glExt(gl).OES_texture_float && opt.float | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can avoid some complexity of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another note – maybe |
||
|
||
//set framebuffer as a main target | ||
var framebuffer = new Framebuffer(gl, [opt.width, opt.height], { | ||
float: float, | ||
depth: false, | ||
color: 1 | ||
}) | ||
framebuffer.bind() | ||
shader.bind() | ||
|
||
|
||
function process(uniforms) { | ||
var w = gl.drawingBufferWidth, h = gl.drawingBufferHeight | ||
|
||
gl.clearColor(0, 0, 0, 0) | ||
gl.clear(gl.COLOR_BUFFER_BIT) | ||
|
||
shader.bind() | ||
|
||
//if user specifies some uniforms | ||
if (uniforms) | ||
if (uniforms) { | ||
shader.bind() | ||
assign(shader.uniforms, uniforms) | ||
} | ||
|
||
//full-screen quad | ||
triangle(gl) | ||
gl.drawArrays(gl.TRIANGLES, 0, 3); | ||
|
||
if (float) { | ||
var pixels = new Float32Array(w * h * 4) | ||
gl.readPixels(0, 0, w, h, gl.RGBA, gl.FLOAT, pixels) | ||
} | ||
else { | ||
var pixels = new Uint8Array(w * h * 4) | ||
gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels) | ||
pixels = new Float32Array(pixels).map(function(p) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return p / 255 | ||
}) | ||
} | ||
|
||
var pixels = Array.prototype.slice.call(getPixels(gl)) | ||
return pixels.map(function(p) { | ||
return p / 255 | ||
}) | ||
return pixels | ||
} | ||
return process | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small thing here – I think it's best to reduce the peer dependency on
fragShader
andgl-shader
and just assume a more generic interface that is basically{ gl, bind() }
which is what we had before. 😄 But I think this part will go away if we keep the options simple as they were before.