Last active
July 26, 2020 13:20
-
-
Save nkint/bc4ad632e7c57fc14ef3a2a9654acf96 to your computer and use it in GitHub Desktop.
umbrella webgl-to-pixel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
yarn add @thi.ng/hdom @thi.ng/pixel @thi.ng/hdom-canvas @thi.ng/geom @thi.ng/transducers @thi.ng/hdom-components @thi.ng/webgl @thi.ng/memoize @thi.ng/shader-ast @thi.ng/shader-ast-stdlib webgl-shadertoy | |
*/ | |
import { renderOnce } from "@thi.ng/hdom"; | |
import { PackedBuffer, canvas2d, ABGR8888, GRAY8 } from "@thi.ng/pixel"; | |
import { canvas as hdomCanvas } from "@thi.ng/hdom-canvas"; | |
import * as g from "@thi.ng/geom"; | |
import { range, map, normRange } from "@thi.ng/transducers"; | |
import { glCanvas } from "@thi.ng/webgl"; | |
import { | |
vec4, | |
Vec2Sym, | |
FloatSym, | |
sym, | |
vec2, | |
float, | |
ret, | |
vec3, | |
$xy, | |
div, | |
sin, | |
mul, | |
distance, | |
} from "@thi.ng/shader-ast"; | |
import { fit1101 } from "@thi.ng/shader-ast-stdlib"; | |
import { shaderToy } from "@thi.ng/webgl-shadertoy"; | |
const W = 200; | |
const H = 200; | |
const { canvas, gl } = glCanvas({ | |
width: W, | |
height: H, | |
autoScale: false, | |
parent: document.body, | |
version: 1, | |
}); | |
const heightMap = shaderToy({ | |
canvas, | |
gl, | |
main: (gl, unis) => { | |
let st: Vec2Sym; | |
let color: FloatSym; | |
return [ | |
(st = sym(div($xy(gl.gl_FragCoord), unis.resolution))), | |
(color = sym( | |
fit1101(sin(mul(float(16), distance(st, vec2(0.5))))) | |
)), | |
ret(vec4(vec3(color), 1)), | |
]; | |
}, | |
}); | |
heightMap.update(); | |
const buf = new PackedBuffer(W, H, ABGR8888); | |
gl.readPixels( | |
0, | |
0, | |
W, | |
H, | |
// don't use gl.RGB since incompatible w/ PackedBuffer | |
// RGB has a stride of only 3 bytes, there's no equivalent typed array for that size | |
// hence we need to read 4 channels | |
gl.RGBA, | |
gl.UNSIGNED_BYTE, | |
// read directly into pixel buffer's memory | |
new Uint8Array(buf.pixels.buffer) | |
); | |
const canvas2 = canvas2d(W, H, document.body); | |
buf.flipY().blitCanvas(canvas2.canvas); | |
const grayBuf = buf.as(GRAY8); | |
const app = () => { | |
return [ | |
"div.flex", | |
["div.ba.w5.h5", "here I like to show webgl-shadertoy heightmap"], | |
["div.ba.w5.h5", "here I like to show pixel packed buffer"], | |
[ | |
hdomCanvas, | |
{ | |
width: W, | |
height: H, | |
class: "ba b--moon-gray", | |
}, | |
g.ellipse([100, 100], 25, { stroke: "black" }), | |
g.ellipse([100, 100], 95, { stroke: "black" }), | |
...map( | |
(yNorm) => | |
g.group({}, [ | |
...map((x) => { | |
const y = yNorm * grayBuf.height; | |
const value = grayBuf?.getAt(x, y); | |
const fill = `rgb(${value}, ${value}, ${value})`; | |
return g.rect([x, y], [1, 2], { | |
fill, | |
}); | |
}, range(grayBuf.width)), | |
]), | |
normRange(10) | |
), | |
], | |
]; | |
}; | |
renderOnce(app()); | |
if (process.env.NODE_ENV !== "production") { | |
const hot = (<any>module).hot; | |
hot && hot.dispose(() => {}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an updated & animated version with new comments: