Skip to content

[DRAFT] Using original alpha for masking #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/hero/liquid-frag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ void main() {
float thin_strip_2_width = cycle_width * thin_strip_2_ratio;


opacity = smoothstep(1., 1. - 1e-4 - u_edgeBlur, edge);
opacity *= get_img_frame_alpha(img_uv, 1e-2);
opacity = img.a;
opacity *= get_img_frame_alpha(img_uv, 0.);


float noise = snoise(uv - t);
Expand Down
28 changes: 14 additions & 14 deletions src/app/hero/parse-logo-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,28 @@ export function parseLogoImage(file: File): Promise<{ imageData: ImageData; pngB
var g = data[idx4 + 1];
var b = data[idx4 + 2];
var a = data[idx4 + 3];
if ((r === 255 && g === 255 && b === 255 && a === 255) || a === 0) {
shapeMask[y * width + x] = false;
} else {
shapeMask[y * width + x] = true;

// keep original value
shapeMask[y * width + x] = a;

// or #ffffff transparent
if (r === 255 && g === 255 && b === 255) {
shapeMask[y * width + x] = 0;
}
}
}

function inside(x: number, y: number) {
if (x < 0 || x >= width || y < 0 || y >= height) return false;
return shapeMask[y * width + x];
return shapeMask[y * width + x] > 0;
}

// 2) Identify boundary (pixels that have at least one non-shape neighbor)
var boundaryMask = new Array(width * height).fill(false);
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
var idx = y * width + x;
if (!shapeMask[idx]) continue;
if (shapeMask[idx] > 0) continue;
var isBoundary = false;
for (var ny = y - 1; ny <= y + 1 && !isBoundary; ny++) {
for (var nx = x - 1; nx <= x + 1 && !isBoundary; nx++) {
Expand All @@ -110,15 +113,15 @@ export function parseLogoImage(file: File): Promise<{ imageData: ImageData; pngB

function getU(x: number, y: number, arr: Float32Array) {
if (x < 0 || x >= width || y < 0 || y >= height) return 0;
if (!shapeMask[y * width + x]) return 0;
if (shapeMask[y * width + x] === 0) return 0;
return arr[y * width + x];
}

for (var iter = 0; iter < ITERATIONS; iter++) {
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
var idx = y * width + x;
if (!shapeMask[idx] || boundaryMask[idx]) {
if (shapeMask[idx] === 0 || boundaryMask[idx]) {
newU[idx] = 0;
continue;
}
Expand All @@ -144,20 +147,17 @@ export function parseLogoImage(file: File): Promise<{ imageData: ImageData; pngB
for (var x = 0; x < width; x++) {
var idx = y * width + x;
var px = idx * 4;
if (!shapeMask[idx]) {
outImg.data[px] = 255;
outImg.data[px + 1] = 255;
outImg.data[px + 2] = 255;
outImg.data[px + 3] = 255;
if (shapeMask[idx] === 0) {

} else {
const raw = u[idx] / maxVal;
const remapped = Math.pow(raw, alpha);
const gray = 255 * (1 - remapped);
outImg.data[px] = gray;
outImg.data[px + 1] = gray;
outImg.data[px + 2] = gray;
outImg.data[px + 3] = 255;
}
outImg.data[px + 3] = shapeMask[idx];
}
}
ctx.putImageData(outImg, 0, 0);
Expand Down