-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2374fe9
commit dc60c38
Showing
5 changed files
with
210 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
struct AsciiUniforms { | ||
uSize: f32, | ||
uColor: vec3<f32>, | ||
uReplaceColor: f32, | ||
}; | ||
|
||
struct GlobalFilterUniforms { | ||
uInputSize:vec4<f32>, | ||
uInputPixel:vec4<f32>, | ||
uuInputClamp:vec4<f32>, | ||
uOutputFrame:vec4<f32>, | ||
uGlobalFrame:vec4<f32>, | ||
uOutputTexture:vec4<f32>, | ||
}; | ||
|
||
@group(0) @binding(0) var<uniform> gfu: GlobalFilterUniforms; | ||
|
||
@group(0) @binding(1) var uSampler: texture_2d<f32>; | ||
@group(1) @binding(0) var<uniform> asciiUniforms : AsciiUniforms; | ||
|
||
@fragment | ||
fn mainFragment( | ||
@location(0) uv: vec2<f32>, | ||
@builtin(position) position: vec4<f32> | ||
) -> @location(0) vec4<f32> { | ||
let pixelSize: f32 = asciiUniforms.uSize; | ||
let coord: vec2<f32> = mapCoord(uv); | ||
|
||
// get the rounded color.. | ||
var pixCoord: vec2<f32> = pixelate(coord, vec2<f32>(pixelSize)); | ||
pixCoord = unmapCoord(pixCoord); | ||
|
||
var color = textureSample(uSampler, uSampler, pixCoord); | ||
|
||
// determine the character to use | ||
let gray: f32 = 0.3 * color.r + 0.59 * color.g + 0.11 * color.b; | ||
|
||
var n: f32 = 65536.0; // . | ||
if (gray > 0.2) { | ||
n = 65600.0; // : | ||
} | ||
if (gray > 0.3) { | ||
n = 332772.0; // * | ||
} | ||
if (gray > 0.4) { | ||
n = 15255086.0; // o | ||
} | ||
if (gray > 0.5) { | ||
n = 23385164.0; // & | ||
} | ||
if (gray > 0.6) { | ||
n = 15252014.0; // 8 | ||
} | ||
if (gray > 0.7) { | ||
n = 13199452.0; // @ | ||
} | ||
if (gray > 0.8) { | ||
n = 11512810.0; // # | ||
} | ||
|
||
// get the mod.. | ||
let modd: vec2<f32> = getMod(coord, vec2<f32>(pixelSize)); | ||
return select(color, vec4<f32>(asciiUniforms.uColor, 1.), asciiUniforms.uReplaceColor > 0.5) * character(n, vec2<f32>(-1.0) + modd * 2.0); | ||
} | ||
|
||
fn pixelate(coord: vec2<f32>, size: vec2<f32>) -> vec2<f32> | ||
{ | ||
return floor( coord / size ) * size; | ||
} | ||
|
||
fn getMod(coord: vec2<f32>, size: vec2<f32>) -> vec2<f32> | ||
{ | ||
return moduloVec2( coord , size) / size; | ||
} | ||
|
||
fn character(n: f32, p: vec2<f32>) -> f32 | ||
{ | ||
var q: vec2<f32> = floor(p*vec2<f32>(4.0, 4.0) + 2.5); | ||
|
||
if (clamp(q.x, 0.0, 4.0) == q.x) | ||
{ | ||
if (clamp(q.y, 0.0, 4.0) == q.y) | ||
{ | ||
if (i32(modulo(n/exp2(q.x + 5.0*q.y), 2.0)) == 1) | ||
{ | ||
return 1.0; | ||
} | ||
} | ||
} | ||
|
||
return 0.0; | ||
} | ||
|
||
fn mapCoord(coord: vec2<f32> ) -> vec2<f32> | ||
{ | ||
var mappedCoord: vec2<f32> = coord; | ||
mappedCoord *= gfu.inputSize.xy; | ||
mappedCoord += gfu.outputFrame.xy; | ||
return mappedCoord; | ||
} | ||
|
||
fn unmapCoord(coord: vec2<f32> ) -> vec2<f32> | ||
{ | ||
var mappedCoord: vec2<f32> = coord; | ||
mappedCoord -= gfu.outputFrame.xy; | ||
mappedCoord /= gfu.inputSize.xy; | ||
return mappedCoord; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters