Public
Edited
Sep 27, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
reglDrawer.canvas
Insert cell
Insert cell
updateDivForREGL(shaderSourceDrawWhat)
Insert cell
Insert cell
shaderSourceDrawImage = {
const vert = `
${customGlsl.precision}

attribute vec2 position;

varying vec2 v_position;

void main () {
v_position = position * 0.5 + 0.5;
v_position.y = (1.0 - v_position.y);

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

uniform sampler2D uSampler;

varying vec2 v_position;
uniform float alpha1;

void main () {
vec4 tex = texture2D(uSampler, v_position);
gl_FragColor = vec4(vec3(tex), alpha1);
}
`;

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

uniform sampler2D uSampler;
uniform float positionMixRate;
attribute vec2 position;
attribute vec4 bo;
varying vec2 v_position;
varying vec4 vColor4;
varying vec4 v_bo;

void main () {
v_position = position;
v_bo = bo;

vColor4 = texture2D(uSampler, vec2(bo));

v_bo -= 0.5;
v_bo.y *= -1.0;

v_bo.xy *= 1.5;

v_bo.xy += v_position * 0.01;

gl_Position = mix((vColor4 - 0.5) * 2.0, vec4(vec2(v_bo), 0.0, 1.0), positionMixRate);

gl_PointSize = 2.0;
}
`,
frag = await glsl`
${customGlsl.useStandardDerivatives}
${customGlsl.precision}

varying vec4 vColor4;
uniform float alpha2;

void main () {
gl_FragColor = vec4(vec3(vColor4), alpha2);
}
`;

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

const { texture, buffer, width, height } = myImage;

// Object.assign(reglDrawer.canvas, { width, height });

function drawTheImage(source) {
const { vert, frag } = source;

regl.clear({
color: [0, 0, 0, 1]
});

// Example of position parameter
const position = [
[-4, -1],
[1, -1],
[1, 4]
];

const drawTriangle = regl({
vert,
frag,
attributes: { position },
uniforms: {
uSampler: texture,
alpha1
},
count: position.length,
blend,
depth: { enable: false }
});

drawTriangle();

statsMonitor.update();
}

function drawWhat(source) {
const { vert, frag } = source;

const drawTriangle = regl({
vert,
frag,
attributes: {
position: new Int8Array([-1, 0, 1, 0, -1, 1, 1, 1]),
bo: { buffer: buffer, divisor: 1 }
},
uniforms: {
uSampler: texture,
alpha2,
positionMixRate
},
blend,
// primitive: "points",
// count: 1,
primitive: "triangle strip",
count: 4,
depth: { enable: false },
instances: () => width * height
});

drawTriangle();

statsMonitor.update();
}

drawTheImage(shaderSourceDrawImage);

drawWhat(shaderSourceDrawWhat);
}
Insert cell
Insert cell
reglDrawer = myImage.reglDrawer
Insert cell
myImage = await showMeTheImage()
Insert cell
showMeTheImage = () => {
return new Promise((resolve) => {
const src = select,
image = new Image();
image.src = src;
image.crossOrigin = "anonymous";

image.onload = () => {
const r = image.width / image.height;

if (r > 1.0) {
image.width = sizeLimit;
image.height = image.width / r;
} else {
image.height = sizeLimit;
image.width = image.height * r;
}

const reglDrawer = mkReglDrawer(image.width, image.height),
{ regl, blend } = reglDrawer,
texture = regl.texture(image),
buffer = regl.buffer(createTextureArray(width, height, 4));

Object.assign(image, { texture, buffer, reglDrawer });

resolve(image);
};

// resolve(image);
});
}
Insert cell
mkReglDrawer = (width, height) => {
const canvas = DOM.canvas(width || sizeLimit, height || sizeLimit),
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"
}
};

// Object.assign(canvas, { width, height });

return { canvas, regl, blend };
}
Insert cell
cta = createTextureArray(myImage.width, myImage.height, 4)
Insert cell
/**
* Create w x h x stride sized texture array, stride usually equals to 4.
*/
createTextureArray = (w, h, stride) => {
stride = stride || 2;

var n = w * h * stride;

var 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;
}

return out;
}
Insert cell
Insert cell
sizeLimit = 600
Insert cell
width = 600
Insert cell
height = 600
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
PR = require("https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js")
Insert cell
import { glsl, glslify } from "@aman-tiwari/glslify"
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