Skip to content

Commit

Permalink
Update Cross Hatch Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
bbazukun123 committed Jan 1, 2024
1 parent dc60c38 commit 3d79c59
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 16 deletions.
12 changes: 7 additions & 5 deletions filters/ascii/src/AsciiFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export class AsciiFilter extends Filter

options = { ...AsciiFilter.DEFAULT_OPTIONS, ...options } as AsciiFilterOptions;

const asciiUniforms = {
uSize: { value: options.size, type: 'f32' },
uColor: { value: new Float32Array(3), type: 'vec3<f32>' },
uReplaceColor: { value: Number(replaceColor), type: 'f32' },
};

const gpuProgram = new GpuProgram({
vertex: {
source: wgslVertex,
Expand All @@ -75,11 +81,7 @@ export class AsciiFilter extends Filter
gpuProgram,
glProgram,
resources: {
asciiUniforms: {
uSize: { value: options.size, type: 'f32' },
uColor: { value: new Float32Array(3), type: 'vec3<f32>' },
uReplaceColor: { value: Number(replaceColor), type: 'f32' },
}
asciiUniforms,
},
});

Expand Down
17 changes: 15 additions & 2 deletions filters/cross-hatch/src/CrossHatchFilter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { vertex } from '@tools/fragments';
import { vertex, wgslVertex } from '@tools/fragments';
import fragment from './crosshatch.frag';
import { Filter, GlProgram } from 'pixi.js';
import source from './crosshatch.wgsl';
import { Filter, GlProgram, GpuProgram } from 'pixi.js';

/**
* A Cross Hatch effect filter.<br>
Expand All @@ -15,13 +16,25 @@ export class CrossHatchFilter extends Filter
{
constructor()
{
const gpuProgram = new GpuProgram({
vertex: {
source: wgslVertex,
entryPoint: 'mainVertex',
},
fragment: {
source,
entryPoint: 'mainFragment',
},
});

const glProgram = new GlProgram({
vertex,
fragment,
name: 'cross-hatch-filter',
});

super({
gpuProgram,
glProgram,
resources: {},
});
Expand Down
17 changes: 8 additions & 9 deletions filters/cross-hatch/src/crosshatch.frag
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
precision mediump float;

varying vec2 vTextureCoord;
in vec2 vTextureCoord;
out vec4 finalColor;

uniform sampler2D uSampler;

void main(void)
{
float lum = length(texture2D(uSampler, vTextureCoord.xy).rgb);
float lum = length(texture(uSampler, vTextureCoord.xy).rgb);

gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
finalColor = vec4(1.0, 1.0, 1.0, 1.0);

if (lum < 1.00)
{
if (mod(gl_FragCoord.x + gl_FragCoord.y, 10.0) == 0.0)
{
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
finalColor = vec4(0.0, 0.0, 0.0, 1.0);
}
}

if (lum < 0.75)
{
if (mod(gl_FragCoord.x - gl_FragCoord.y, 10.0) == 0.0)
{
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
finalColor = vec4(0.0, 0.0, 0.0, 1.0);
}
}

if (lum < 0.50)
{
if (mod(gl_FragCoord.x + gl_FragCoord.y - 5.0, 10.0) == 0.0)
{
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
finalColor = vec4(0.0, 0.0, 0.0, 1.0);
}
}

if (lum < 0.3)
{
if (mod(gl_FragCoord.x - gl_FragCoord.y - 5.0, 10.0) == 0.0)
{
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
finalColor = vec4(0.0, 0.0, 0.0, 1.0);
}
}
}
43 changes: 43 additions & 0 deletions filters/cross-hatch/src/crosshatch.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@group(0) @binding(1) var uSampler: texture_2d<f32>;

@fragment
fn mainFragment(
@location(0) uv: vec2<f32>,
@builtin(position) position: vec4<f32>
) -> @location(0) vec4<f32> {
let lum: f32 = length(textureSample(uSampler, uSampler, uv).rgb);

if (lum < 1.00)
{
if (modulo(position.x + position.y, 10.0) == 0.0)
{
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
}

if (lum < 0.75)
{
if (modulo(position.x - position.y, 10.0) == 0.0)
{
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
}

if (lum < 0.50)
{
if (modulo(position.x + position.y - 5.0, 10.0) == 0.0)
{
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
}

if (lum < 0.3)
{
if (modulo(position.x - position.y - 5.0, 10.0) == 0.0)
{
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
}

return vec4<f32>(1.0);
}
1 change: 1 addition & 0 deletions tools/demo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const main = async () =>
filters.hslAdjustment.call(app);
filters.rgb.call(app);
filters.ascii.call(app);
filters.crossHatch.call(app);
// filters.kawaseBlur.call(app);

// TODO: Re-enable this in place of the above once v8 conversion is complete
Expand Down

0 comments on commit 3d79c59

Please sign in to comment.