Public
Edited
Sep 23, 2023
1 fork
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(shaderSourceDrawImage)
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}

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

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;
attribute vec2 position;
attribute vec4 fbo;
varying vec2 v_position;
varying vec4 v_fbo;
varying vec4 v4;

void main () {
v_position = position;
v_fbo = fbo;

v4 = texture2D(uSampler, vec2(fbo));

// v_position.y = (1.0 - v_position.y);

gl_Position = vec4(position, 0.0, 0.0);

gl_Position = (v4 - 0.5) * 2.0;
gl_PointSize = 2.0;
}
`,
frag = await glsl`
${customGlsl.useStandardDerivatives}
${customGlsl.precision}

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

varying vec2 v_position;
varying vec4 v_fbo;
varying vec4 v4;
uniform float alpha2;

void main () {
// float rnd = snoise(vec4(v_position * 5.0, 0, 0));
// // gl_FragColor = vec4(vec3(rnd), 1.0);
// gl_FragColor = vec4(v_position, 0.0, 1.0);
// gl_FragColor = vec4(0.5, 0.0, 0.0, 0.5);
gl_FragColor = vec4(vec3(v4), 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]),
position: new Int8Array([-1, 0, 1, 0, -1, 1, 1, 1]),
// position: [0, 1, 2, 3],
// position: new Int8Array([-0.1, 0.1, 1, 0, -1, 1, 0, 1]),
fbo: { buffer: buffer, divisor: 1 }
},
uniforms: {
uSampler: texture,
alpha2
},
blend,
primitive: "points", //"triangle strip",
depth: { enable: false },
instances: () => width * height,
count: 4
});

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
createTextureArray(10, 20, 4)
Insert cell
createTextureArray = (w, h, stride) => {
stride = stride || 2;

var n = w * h * stride;

var out = new Float32Array(n);

for (var i = 0, iStride = 0; iStride < n; i++, iStride += stride) {
out[iStride] = ((i % w) + 0.5) / w;
out[iStride + 1] = (((i / w) | 0) + 0.5) / h;
}

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 highp 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