Skip to content

Commit

Permalink
✨ Displace transition
Browse files Browse the repository at this point in the history
  • Loading branch information
xurei committed Oct 24, 2023
1 parent 648d988 commit 0d15df7
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Added Parameter type `color`
- Added support for `.shadertastic` bundled effects
- Ink drop transition
- Displacement transition
- Displacement Map filter

### Changed
- Dev mode setting, disabling the "Reload" and "Auto Reload" options by default
Expand Down
10 changes: 5 additions & 5 deletions data/effects/filters/displacement_map_source/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{"label": "RGB", "value": 0},
{"label": "YUV", "value": 1}
],
"default": 0
"default": 1
},
{
"name": "displacement_strength_x",
Expand All @@ -20,8 +20,8 @@
"slider": true,
"min": 0,
"max": 2.0,
"default": 0.05,
"step": 0.001
"default": 0.35,
"step": 0.005
},
{
"name": "displacement_strength_y",
Expand All @@ -30,8 +30,8 @@
"slider": true,
"min": 0,
"max": 2.0,
"default": 0.05,
"step": 0.001
"default": 0.35,
"step": 0.005
},
{
"name": "displacement_map",
Expand Down
112 changes: 112 additions & 0 deletions data/effects/transitions/displace/main.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Common parameters for all shaders, as reference. Do not uncomment this (but you can remove it safely).
/*
uniform float time; // Time since the shader is running. Goes from 0 to 1 for transition effects; goes from 0 to infinity for filter effects
uniform texture2d tex_a; // Texture of the previous frame (transitions only)
uniform texture2d tex_b; // Texture of the next frame (transitions only)
uniform texture2d tex_interm; // Intermediate texture where the previous step will be rendered (for multistep effects)
uniform float upixel; // Width of a pixel in the UV space
uniform float vpixel; // Height of a pixel in the UV space
uniform float rand_seed; // Seed for random functions
uniform int current_step; // index of current step (for multistep effects)
*/

// Specific parameters of the shader. They must be defined in the meta.json file next to this one.
uniform float displacement_strength;
uniform int color_space;
//----------------------------------------------------------------------------------------------------------------------

// These are required objects for the shader to work.
// You don't need to change anything here, unless you know what you are doing
sampler_state textureSampler {
Filter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};

struct VertData {
float2 uv : TEXCOORD0;
float4 pos : POSITION;
};

struct FragData {
float2 uv : TEXCOORD0;
};

VertData VSDefault(VertData v_in)
{
VertData vert_out;
vert_out.uv = v_in.uv;
vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
return vert_out;
}
//----------------------------------------------------------------------------------------------------------------------

float3 rgbToYuv(float3 rgb)
{
return float3(
( 0.257 * rgb.r + 0.504 * rgb.g + 0.098 * rgb.b) / 0.859,
(-0.148 * rgb.r - 0.291 * rgb.g + 0.439 * rgb.b),
( 0.439 * rgb.r - 0.368 * rgb.g - 0.071 * rgb.b)
);
}
//----------------------------------------------------------------------------------------------------------------------

float4 EffectLinear(float2 uv)
{
float4 mask = tex_a.Sample(textureSampler, uv);
float2 displacement = float2(0.0, 0.0);

float lerp_breakpoint = 0.2;

float t = smoothstep(lerp_breakpoint, 1.0, 1.0-time);

if (color_space == 0) { // RGB
displacement = t*displacement_strength * (mask.xy - 0.5);
}
else if (color_space == 1) { // YUV
float3 mask_yuv = rgbToYuv(mask.rgb);
displacement = t*displacement_strength * float2(mask_yuv.y, mask_yuv.z);
}
float2 uv2 = uv + displacement;
float4 px = tex_b.Sample(textureSampler, uv2);

if (time <= lerp_breakpoint) {
return lerp(mask, px, smoothstep(0.05, 0.95, time / lerp_breakpoint));
}

return px;
}
//----------------------------------------------------------------------------------------------------------------------

// You probably don't want to change anything from this point.

float4 PSEffect(FragData f_in) : TARGET
{
float4 rgba = EffectLinear(f_in.uv);
rgba.rgb = srgb_nonlinear_to_linear(rgba.rgb);
return rgba;
}

float4 PSEffectLinear(FragData f_in) : TARGET
{
float4 rgba = EffectLinear(f_in.uv);
return rgba;
}

technique Draw
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSEffect(f_in);
}
}

technique DrawLinear
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSEffectLinear(f_in);
}
}
25 changes: 25 additions & 0 deletions data/effects/transitions/displace/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"label": "Displace",
"parameters": [
{
"name": "color_space",
"label": "Color Space",
"type": "list_int",
"values": [
{"label": "RGB", "value": 0},
{"label": "YUV", "value": 1}
],
"default": 1
},
{
"name": "displacement_strength",
"label": "Strength",
"type": "double",
"slider": true,
"min": 0,
"max": 2.0,
"default": 0.35,
"step": 0.001
}
]
}

0 comments on commit 0d15df7

Please sign in to comment.