|
| 1 | +let sketch |
| 2 | +let fbo |
| 3 | +const format = document.getElementById('format') |
| 4 | +const alpha = document.getElementById('alpha') |
| 5 | +const antialias = document.getElementById('antialias') |
| 6 | +const webglVersion = document.getElementById('webglVersion') |
| 7 | +const errors = document.getElementById('errors') |
| 8 | + |
| 9 | +for (const input of [format, alpha, antialias, webglVersion]) { |
| 10 | + input.addEventListener('input', remakeSketch) |
| 11 | +} |
| 12 | + |
| 13 | +function remakeSketch() { |
| 14 | + errors.textContent = '' |
| 15 | + Framebuffer.forceWebGL1 = webglVersion.value === '1' |
| 16 | + if (fbo) fbo.remove() |
| 17 | + if (sketch) sketch.remove() |
| 18 | + sketch = new p5(makeSketch) |
| 19 | +} |
| 20 | +remakeSketch() |
| 21 | + |
| 22 | +function makeSketch(p5) { |
| 23 | + p5.setup = function() { |
| 24 | + try { |
| 25 | + p5.createCanvas(400, 400, p5.WEBGL).parent('sketch') |
| 26 | + if (alpha.value === 'alpha') { |
| 27 | + p5.setAttributes({ alpha: true }) |
| 28 | + } |
| 29 | + fbo = p5.createFramebuffer({ |
| 30 | + colorFormat: format.value, |
| 31 | + //depthFormat: format.value, |
| 32 | + antialias: antialias.value === 'antialias', |
| 33 | + }) |
| 34 | + } catch (e) { |
| 35 | + errors.textContent = e.message |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + p5.draw = function() { |
| 40 | + // Draw to the Framebuffer |
| 41 | + fbo.draw(() => { |
| 42 | + p5.clear() |
| 43 | + p5.background(255) |
| 44 | + p5.push() |
| 45 | + p5.noStroke() |
| 46 | + p5.fill(255, 0, 0) |
| 47 | + p5.translate(0, 100*p5.sin(p5.frameCount * 0.01), 0) |
| 48 | + p5.rotateX(p5.frameCount * 0.01) |
| 49 | + p5.rotateY(p5.frameCount * 0.01) |
| 50 | + p5.box(50) |
| 51 | + p5.pop() |
| 52 | + }) |
| 53 | + |
| 54 | + // Do something with fbo.color or dbo.depth |
| 55 | + p5.clear() |
| 56 | + p5.background(255) |
| 57 | + |
| 58 | + p5.push() |
| 59 | + p5.noStroke() |
| 60 | + p5.texture(fbo.color) |
| 61 | + p5.plane(p5.width, -p5.height) |
| 62 | + p5.pop() |
| 63 | + } |
| 64 | + |
| 65 | + return p5 |
| 66 | +} |
0 commit comments