Public
Edited
Oct 13, 2023
Insert cell
Insert cell
renderer.domElement
Insert cell
// Continuously updates
{
while (true) {
plane.material.uniforms.iTime.value = clock.getDelta();
renderer.render(scene, camera);
yield null;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
plane = {
const w = 200;
const h = 200;
let data = new Uint8Array(w * h);
data = data.map(item => item = Math.random() * 256 | 0);
const texture = new THREE.DataTexture(data, w, h);
// const w = 47;
// const h = 37;
// const texture = new THREE.DataTexture(TestData, w, h);
texture.format = THREE.RedFormat;
texture.minFilter = texture.magFilter = THREE.LinearFilter;
texture.unpackAlignment = 1;
texture.needsUpdate = true;
const material = new THREE.ShaderMaterial({
uniforms: {
u_texture: { value: texture },
dx: { value: 1 / width },
dy: { value: 1 / height },
iTime: { value: 0 },
},
vertexShader: vertexShader,
fragmentShader: fragmentShader,
})
const geometry = new THREE.PlaneBufferGeometry(width, height);
const plane = new THREE.Mesh(geometry, material);
return plane;
}
Insert cell
fragmentShader = `
uniform sampler2D u_texture;
uniform float dx;
uniform float dy;
uniform float iTime;
varying vec2 v_uv;

int lod = 5;
float thresh = .5;

// #define S(v) smoothstep( -.8,.8,(v)/fwidth(v) ) // antialiasing draw

float map(vec2 uv) {
// ivec2 s = textureSize(u_texture, 0);
return texelFetch(u_texture, ivec2(uv), lod).r;
}

float map1(vec2 uv) {
return texture(u_texture, uv).r;
}

float S(float v) {
return smoothstep( -.8,.8,(v)/fwidth(v) );
}
void main() {
vec2 uv = v_uv;
vec2 v;

vec4 c = vec4( map1(uv) , map1(uv+vec2(dx,0)), // value at corners
map1(uv+vec2(0,dy)), map1(uv+vec2(dx,dy)) );

vec4 b = step(thresh,c); // is > threshold ?
int n = int(b.x+b.y+b.z+b.w); // number of yes
// U = fract(U); // tile local coordinates
if (n==0) gl_FragColor += .3; // empty ( for test )
else if (n==4) gl_FragColor += 1.; // full
else if (n==1 || n==3 || n==2 && b.x==b.w ) { // corner + checker case
if (n==3) b = 1.-b;
if(b.x>0.) gl_FragColor += S( (1.-uv.x-uv.y)*c.x + uv.x *c.y + uv.y *c.z - thresh );
if(b.y>0.) gl_FragColor += S( ( uv.x -uv.y)*c.y + (1.-uv.x)*c.x + uv.y *c.w - thresh );
if(b.z>0.) gl_FragColor += S( (-uv.x+ uv.y )*c.z + uv.x *c.w + (1.-uv.y)*c.x - thresh );
if(b.w>0.) gl_FragColor += S( (uv.x+uv.y-1.)*c.w + (1.-uv.x)*c.z + (1.-uv.y)*c.y - thresh );
} else { // n==2 // (checker treated above)
if (b.x==b.y) v = (thresh-c.xy)/(c.zw-c.xy), // horizontal
gl_FragColor += S( uv.y - mix(v.x,v.y,uv.x) );
else if (b.x==b.z) v = (thresh-c.xz)/(c.yw-c.xz), // vertical
gl_FragColor += S( uv.x - mix(v.x,v.y,uv.y) );
if (b.x>0.) gl_FragColor = 1.-gl_FragColor;
}

// gl_FragColor = c;
// gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more