Skip to content

Commit

Permalink
Large rewrite
Browse files Browse the repository at this point in the history
Updated and new shaders. Shaders folder path accessible from a global variable. Shaders create shader variable and use set_default function to set default parameters. Updated readme. This commit will break existing usage of the module, and layouts must be updated.
  • Loading branch information
keilmillerjr committed Jan 3, 2019
1 parent c20c71c commit 3dd73e6
Show file tree
Hide file tree
Showing 12 changed files with 1,255 additions and 207 deletions.
182 changes: 89 additions & 93 deletions module.nut
Original file line number Diff line number Diff line change
@@ -1,126 +1,122 @@
class CrtLottes {
::shadersDirectory <- fe.module_dir + "shaders/";
::shadersDir <- shadersDirectory;

class Bloom {
shader = null;

constructor(w=640, h=480, c=0, a=2.0, r=1.0, v=fe.module_dir+"CRT-lottes.vsh", f=fe.module_dir+"CRT-lottes_rgb32_dir.fsh") {
shader = fe.add_shader(Shader.VertexAndFragment, v, f);

curvature(c);
aperature(a);
distortion();
cornerSize();
cornerSmooth();
hardScan();
hardPix();
maskDark();
maskLight();
saturation();
tint();
backClip();
brightMult();
rotated(r);
setTextures(w, h);
constructor() {
shader = fe.add_shader(Shader.Fragment, shadersDir + "bloom.fsh");
set_default();
}

function curvature(val=0) {
// 0 = Flat
// 1.0 = Curved
shader.set_param("curvature", val);
function set_default() {
shader.set_param("bloom_spread", 0.000695);
shader.set_param("bloom_power", 0.228);
shader.set_texture_param("mpass_texture");
}
}

function aperature(val=2.0) {
// 0 = VGA style shadow mask.
// 1.0 = Very compressed TV style shadow mask.
// 2.0 = Aperture-grille.
shader.set_param("aperature_type", val);
}
class BloomMultipass {
shader = null;

function distortion(val=0.1) {
shader.set_param("distortion", val);
constructor() {
shader = fe.add_shader(Shader.VertexAndFragment, shadersDir + "bloomMultipass.vsh", shadersDir + "bloomMultipass.fsh");
set_default();
}

function cornerSize(val=0.038) {
shader.set_param("cornersize", val);
function set_default() {
shader.set_texture_param("source");
shader.set_param("sourceSize", 320, 240);
}
}

function cornerSmooth(val=400.0) {
// Accepts values 100-1000
shader.set_param("cornersmooth", val);
}
class CrtCgwg {
shader = null;

function hardScan(val=-10.0) {
// -8.0 = soft
// -16.0 = medium
shader.set_param("hardScan", val);
constructor() {
shader = fe.add_shader(Shader.VertexAndFragment, shadersDir + "crtCgwg.vsh", shadersDir + "crtCgwg.fsh");
set_default();
}

function hardPix(val=-2.3) {
// -2.0 = soft
// -4.0 = hard
shader.set_param("hardPix", val);
}
function set_default() {
shader.set_param("CRTgamma", 2.4); // gamma of simulated CRT
shader.set_param("monitorgamma", 2.2); // gamma of display monitor (typically 2.2 is correct)
shader.set_param("overscan", 0.99, 0.99); // overscan (e.g. 1.02 for 2% overscan)
shader.set_param("aspect", 1.0, 0.75); // aspect ratio
shader.set_param("d", 1.3); // distance from viewer
shader.set_param("R", 2.5); // radius of curvature - 2.0 to 3.0?
shader.set_param("cornersize", 0.02); // size of curved corners
shader.set_param("cornersmooth", 80); // border smoothness parameter, decrease if borders are too aliased
shader.set_texture_param("texture");
shader.set_param("inputSize", 320, 240); // size of input
shader.set_param("outputSize", 320, 240); // size of mask
shader.set_param("textureSize", 320, 240); // size drawing to

function maskDark(val=0.4) {
shader.set_param("maskDark", val);
}
}

function maskLight(val=1.3) {
shader.set_param("maskLight", val);
}
class CrtLottes {
shader = null;

function saturation(val=1.25) {
// 1.0 is normal saturation. Increase as needed.
shader.set_param("saturation", val);
constructor() {
shader = fe.add_shader(Shader.VertexAndFragment, shadersDir + "crtLottes.vsh", shadersDir + "crtLottes.fsh");
set_default();
}

function tint(val=0.1) {
// 0.0 is 0.0 degrees of Tint. Adjust as needed.
shader.set_param("tint", val);
function set_default() {
// APERATURE_TYPE
// 0.0 = VGA style shadow mask.
// 1.0 = Very compressed TV style shadow mask.
// 2.0 = Aperture-grille.
shader.set_param("aperature_type", 2.0);
shader.set_param("hardScan", -20.0); // Hardness of Scanline -8.0 = soft -16.0 = medium
shader.set_param("hardPix", -5.0); // Hardness of pixels in scanline -2.0 = soft, -4.0 = hard
shader.set_param("maskDark", 0.4); // Sets how dark a "dark subpixel" is in the aperture pattern.
shader.set_param("maskLight", 1.5); // Sets how dark a "bright subpixel" is in the aperture pattern
shader.set_param("saturation", 1.1); // 1.0 is normal saturation. Increase as needed.
shader.set_param("tint", 0.0); // 0.0 is 0.0 degrees of Tint. Adjust as needed.
shader.set_param("blackClip", 0.02); // Drops the final color value by this amount
shader.set_param("brightMult", 1.2); // Multiplies the color values by this amount if GAMMA_CONTRAST_BOOST is defined
shader.set_param("distortion", 0.15); // 0.0 to 0.2 seems right
shader.set_param("cornersize", 0.03); // 0.0 to 0.1
shader.set_param("cornersmooth", 80); // Reduce jagginess of corners
shader.set_texture_param("texture");
shader.set_param("color_texture_sz", 320, 240);
shader.set_param("color_texture_pow2_sz", 320, 240);
}
}

function backClip(val=0.08) {
// Drops the final color value by this amount if GAMMA_CONTRAST_BOOST is defined
shader.set_param("blackClip", val);
}
class CrtLottesMultipass {
shader = null;

function brightMult(val=1.25) {
// Multiplies the color settings by this amount if GAMMA_CONTRAST_BOOST is defined
shader.set_param("brightMult", val);
}

function rotated(val=1.0) {
// 0 = Scan line orientation vertical.
// 1.0 = Scan line orientation horizontal.
shader.set_param("rotated", val);
constructor() {
shader = fe.add_shader(Shader.VertexAndFragment, shadersDir + "crtLottesMultipass.vsh", shadersDir + "crtLottesMultipass.fsh");
set_default();
}

function setTextures(w=640, h=480) {
shader.set_param("color_texture_sz", w, h);
shader.set_param("color_texture_pow2_sz", w, h);
shader.set_texture_param("mpass_texture");
function set_default() {
shader.set_texture_param("source");
shader.set_param("sourceSize", 320, 240);
shader.set_param("outputSize", 320, 240);
}
}

class RoundCorners {
class RoundedCorners {
shader = null;

constructor(r, iw, ih, siw=null, sih=null, f=fe.module_dir+"RoundCorners.fsh") {
shader = fe.add_shader(Shader.Fragment, f);

setRadius(r);
setImgDimensions(iw, ih);
if (!siw || !sih ) setSubImgDimensions(iw, ih);
else setSubImgDimensions(siw, sih);
constructor(...) {
shader = fe.add_shader(Shader.Fragment, shadersDir + "roundedCorners.fsh");
if (vargv.len() == 3) set_default(vargv[0], vargv[1], vargv[2]);
if (vargv.len() == 5) set_default(vargv[0], vargv[1], vargv[2], vargv[3], vargv[4]);
}

function setRadius(val) {
shader.set_param("radius", val);
}

function setImgDimensions(val1, val2) {
shader.set_param("snap_dimensions", val1, val2);
}

function setSubImgDimensions(val1, val2) {
shader.set_param("subimg_dimensions", val1, val2);

function set_default(r, iw, ih, siw=null, sih=null) {
shader.set_param("radius", r);
shader.set_param("snap_dimensions", iw, ih);
if (!siw || !sih) {
siw = iw;
sih = ih;
}
shader.set_param("subimg_dimensions", siw, sih);
}
}
46 changes: 25 additions & 21 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,45 @@ You may need to change file paths as necessary as each platform (windows, mac, l

## Usage

From within your layout, you can load the module.
The shaders module provides a global variable ```shadersDirectory``` or ```shadersDir``` containing the shaders directory path. This enables you to use the shaders as you wish.

You may optionally use the provided classes to make usage easier, and in some cases, set with default parameters. All classes utilize a public shader variable. This enables you to set some parameters outside of the defaults provided from the class.

Example:

```Squirrel
// Create Artwork Variable
local snap = fe.add_artwork("snap", 0, 0, 640, 480);
// Load Shader Module
if (fe.load_module("shader")) {
// CrtLottes
local snapShader = CrtLottes();
// RoundCorners
if (snap.preserve_aspect_ratio) RoundCorners(100, snap.width, snap.height);
else RoundCorners(100, snap.width, snap.height, snap.subimg_width, snap.subimg_height);
// Apply the shader to your object
snap.shader = snapShader.shader;
}
// Load the Shader module
fe.load_module("shader");
// Create your shaders
local shaderBloom = Bloom();
// Create an object
local surf = fe.add_surface(640, 480);
// Apply shader
surf.shader = shaderBloom.shader;
```

#### Optional Params
#### Parameters

See [module.nut](https://github.com/keilmillerjr/shader-module/blob/master/module.nut) for notes on acceptable variables and public class functions to change other params.
See [module.nut](https://github.com/keilmillerjr/shader-module/blob/master/module.nut) for notes on parameters.

```Squirrel
CrtLottes(width, height, curvature, aperature, vertex, fragment);
RoundCorners(radius, snapWidth, snapHeight, snapSubImgWidth, snapSubImgHeight);
Bloom();
BloomMultipass();
crtCgwg();
CrtLottes();
crtLottesMultipass();
RoundCorners(radius, imageWidth, imageHeight, [subImgWidth], [subImgHeight]);
```

## Notes

Crt Lottes Shader is by Timothy Lottes. It was converted to MAME and AttractMode FE by Luke-Nukem found [here](https://github.com/Luke-Nukem/attract-extra/tree/master/layouts/lottes-crt). It has been slightly modified to allow for changes to some constants such as curvature.
Bloom shader from [here](http://wp.applesandoranges.eu/?p=14), modified by Chris Van Graas. Crt Lottes Shader and Bloom Multipass is by Timothy Lottes. Crt Cgwg is by Themaister and DOLLS. They were converted to work with AttractMode FE by Luke-Nukem, found in his [crt-shader-theme](https://gitlab.com/ljcode/crt-shader-theme).

Round Corders Shader is by Oomek, and was shared on the AttractMode forum [here](http://forum.attractmode.org/index.php?topic=1588).

All shaders were used here with permission. Thank you so much for your contribution. Including them in a module makes it easier to be included in a layout.

More functionality is expected as it meets my needs. If you have an idea of something to add that might benefit a wide range of layout developers, please join the AttractMode forum and send me a message.
40 changes: 40 additions & 0 deletions shaders/bloom.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Bloom shader from http://wp.applesandoranges.eu/?p=14
//
// Modified by Chris Van Graas
//
uniform float bloom_spread;
uniform float bloom_power;

uniform sampler2D mpass_texture;

void main()
{
vec4 sum = vec4(0);
vec2 texcoord = vec2(gl_TexCoord[0]);
int j;
int i;

for( i= -4 ;i < 4; i++)
{
for (j = -3; j < 3; j++)
{
sum += gl_Color * texture2D(mpass_texture, texcoord + vec2(j, i) * bloom_spread) * bloom_power;
}
}
if (texture2D(mpass_texture, texcoord).r < 0.3)
{
gl_FragColor = sum*sum*0.012 + gl_Color * texture2D(mpass_texture, texcoord);
}
else
{
if (texture2D(mpass_texture, texcoord).r < 0.5)
{
gl_FragColor = sum*sum*0.009 + gl_Color * texture2D(mpass_texture, texcoord);
}
else
{
gl_FragColor = sum*sum*0.0075 + gl_Color * texture2D(mpass_texture, texcoord);
}
}
}
Loading

0 comments on commit 3dd73e6

Please sign in to comment.