Public
Edited
Nov 16, 2023
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
reglDrawer.canvas
Insert cell
<div style='max-height: ${height+20}px; overflow-y: scroll; display: flex; flex-flow: wrap'>
<div id='vertDivForREGL' style='padding: 5px'>
[Vert div for regl...]
</div>
<div id='fragDivForREGL' style='padding: 5px'>
[Frag div for regl...]
</div>
</div>
Insert cell
shaderSourceTriangle = {
const vert = `
${customGlsl.precision}

attribute vec2 position;

varying vec2 v_position;

void main () {
v_position = position*0.5+0.5;

gl_Position = vec4(position, 0.0, 1.0);
}
`,
frag = await glsl`
${customGlsl.useStandardDerivatives}
${customGlsl.precision}

#pragma glslify: snoise = require('glsl-noise/simplex/3d')
#pragma glslify: colormap = require('glsl-colormap/${colorMapName}')

varying vec2 v_position;
uniform float secs, spatialZoom, speedUp;
uniform sampler2D src;

void main () {
float v = 0.5 + 0.5 * snoise(vec3(v_position * spatialZoom, secs * speedUp));
vec4 s = texture2D(src, v_position);
gl_FragColor = colormap(v) + s;
}
`;

return { vert, frag };
}
Insert cell
shaderSourceCompute = {
const vert = `
${customGlsl.precision}

attribute vec2 position;
varying vec2 v_position;

void main () {
v_position = position * 0.5 + 0.5;

gl_Position = vec4(position, 0.0, 1.0);
}
`,
frag = await glsl`
${customGlsl.useStandardDerivatives}
${customGlsl.precision}

#pragma glslify: snoise = require('glsl-noise/simplex/3d')

varying vec2 v_position;
uniform float secs, spatialZoom, speedUp;

uniform sampler2D src;

void main () {
// vec4 v4 = texture2D(src, v_position);
float v = 0.5 + 0.5 * snoise(vec3(v_position * spatialZoom, secs * speedUp));

if (v > 0.8){
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
} else {
gl_FragColor = vec4(0.0);
}

// gl_FragColor = vec4(v_position, 0.0, 1.0);
}
`;

return { vert, frag };
}
Insert cell
/**
* Draw the triangle using regl
*/
{
const { regl, blend } = reglDrawer;

const position = [
[-1, -1],
[3, -1],
[-1, 3]
];

function drawFrame(secs = 0, src, dst) {
const { vert, frag } = shaderSourceCompute,
draw = regl({
vert,
frag,
attributes: { position },
uniforms: {
secs,
spatialZoom,
speedUp,
src
},
// framebuffer: dst,
blend,
count: position.length,
depth: { enable: false }
});

draw();
}

function drawImageByTriangle(secs = 0, src) {
const { vert, frag } = shaderSourceTriangle,
draw = regl({
vert,
frag,
attributes: { position },
uniforms: {
secs,
spatialZoom,
speedUp,
src
},
blend,
count: position.length,
depth: { enable: false }
});

draw();
}

var secs = 0,
secsNext = 0;

function draw(secs) {
regl.clear({ color: [1, 0, 1, 0.1] });

stateFbos[1].fbo.use(() => {
if (secs > secsNext) {
regl.clear({ color: [0, 0, 0, 0.8] });
secsNext = secs + 10;
}
drawFrame(secs, stateFbos[0].color);
});

drawImageByTriangle(secs, stateFbos[1].fbo);

statsMonitor.update();
}

draw(secs);

for (;;) {
secs = parseFloat((performance.now() / 1000) % 10000);
draw(secs);
yield secs;
}
}
Insert cell
stateFbos = {
const { regl } = reglDrawer;

return [1, 2].map((d) => {
const color = regl.texture({
type: "float",
width,
height
});
return Object.assign(
{},
{
color,
fbo: regl.framebuffer({ color })
}
);
});
}
Insert cell
textureBuffer = reglDrawer.regl.buffer({ data: texture })
Insert cell
texture = createTexture(width, height, 4)
Insert cell
Insert cell
updateDivForREGL(shaderSourceTriangle);
Insert cell
Insert cell
Insert cell
/**
* Create w x h x stride sized texture array
*/
createTexture = (w, h, stride) => {
// stride = stride || 2;

const rnd = d3.randomUniform(),
n = w * h * stride,
out = new Float32Array(n);

for (var i = 0; i < n; i += stride) {
// out[i] = (i % (w * stride)) / (w * stride);
// out[i + 1] = (i - (i % (w * stride)) + 0.5) / n;
out[i] = 1.0; // rnd() > 0.9 ? 1.0 : 0.0;
}

return out;
}
Insert cell
width = 600
Insert cell
height = 600
Insert cell
reglDrawer = mkReglDrawer(width, height)
Insert cell
/**
* Make canvas, regl and blend for WebGL
*/
mkReglDrawer = (width, height) => {
const canvas = DOM.canvas(width, height),
pixelRatio = 2.0,
extensions = [
"oes_standard_derivatives",
"oes_texture_float",
"angle_instanced_arrays"
],
regl = wrapREGL({
canvas,
pixelRatio,
extensions
}),
blend = {
enable: true,
func: {
srcRGB: "src alpha",
srcAlpha: 1,
dstRGB: "one minus src alpha",
dstAlpha: 1
},
equation: {
rgb: "add",
alpha: "add"
}
};

return { canvas, regl, blend };
}
Insert cell
Insert cell
PR = require("https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js")
Insert cell
statsMonitor = {
const stats = new Stats(),
{ dom } = stats;
dom.style.position = "relative";

return stats;
}
Insert cell
Stats = require("https://cdn.jsdelivr.net/npm/stats-js@1.0.1/build/stats.min.js")
Insert cell
wrapREGL = require("regl")
Insert cell
import { glsl, glslify } from "@listenzcc/glslify"
Insert cell
d3 = require("d3")
Insert cell
customGlsl = ({
useStandardDerivatives: `
#extension GL_OES_standard_derivatives : enable
`,
constants: `
#define PI 3.141592653589793238
#define HALF_PI 1.57079632679
#define HALF_PI_INV 0.15915494309
#define LOG_2 0.69314718056
#define C_ONE (vec2(1.0, 0.0))
#define C_I (vec2(0.0, 1.0))
#define TO_RADIANS 0.01745329251
`,
precision: `
precision lowp float;
`,
dist: `
float dist(vec2 d) {
return sqrt(pow(d.x, 2.0) + pow(d.y, 2.0));
}

float dist(vec2 a, vec2 b) {
vec2 d = a - b;
return dist(d);
}
`,
hsv2rgb: `
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
`,
wireframe: `
float wireframe (vec3 parameter, float width, float feather) {
float w1 = width - feather * 0.5;
vec3 d = fwidth(parameter);
vec3 looped = 0.5 - abs(mod(parameter, 1.0) - 0.5);
vec3 a3 = smoothstep(d * w1, d * (w1 + feather), looped);
return min(min(a3.x, a3.y), a3.z);
}
`
})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more