Skip to content

Commit

Permalink
Bug fix and added cheap stochastic blur.
Browse files Browse the repository at this point in the history
Blur is subject to change so that it will look better, but it'll work for now.
  • Loading branch information
AkaiMage authored Mar 29, 2019
1 parent 6590add commit 10865bb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
76 changes: 54 additions & 22 deletions Cancerspace.shader
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

_MaxFalloff ("Falloff Range", Float) = 30

_BlurRadius ("Blur Radius", Range(0, 50)) = 0
_BlurSampling ("Blur Sampling", Range(1, 5)) = 1
[Toggle(_)] _AnimatedSampling ("Animated Sampling", Float) = 1

_Zoom ("Zoom", Float) = 1
[PowerSlider(2.0)] _Pixelation ("Pixelation", Range(0, 1)) = 0

Expand Down Expand Up @@ -184,6 +188,19 @@

float _ScreenBoundaryHandling;

// blurring method is based on https://www.shadertoy.com/view/XsVBDR

int _BlurSampling;
float _AnimatedSampling;
float _BlurRadius;

float2 hash23(float3 p) {
if (_AnimatedSampling) p.z += frac(_Time.z) * 4;
p = frac(p * float3(400, 450, .1));
p += dot(p, p.yzx + 20);
return frac((p.xx + p.yz) * p.zy);
}

float3 hsv2rgb(float3 c) {
return ((clamp(abs(frac(c.x+float3(0,.666,.333))*6-3)-1,0,1)-1)*c.y+1)*c.z;
}
Expand Down Expand Up @@ -267,31 +284,46 @@

float4 grabCol = float4(0, 0, 0, 1);

UNITY_UNROLL for (int j = 0; j < 3; ++j) {
float2 multiplier = float2(_ScreenXMultiplier[j] * _ScreenXMultiplier.a, _ScreenYMultiplier[j] * _ScreenYMultiplier.a);
float2 shift = float2(_ScreenXOffset[j] + _ScreenXOffset.a, _ScreenYOffset[j] + _ScreenYOffset.a);
#if defined(USING_STEREO_MATRICES)
shift.x *= .5;
#endif
float rotationAngle = _RotationAngle[j] + _RotationAngle.a;
float2 rotationOrigin = float2(_ScreenRotationOriginX[j] + _ScreenRotationOriginX.a, _ScreenRotationOriginY[j] + _ScreenRotationOriginY.a);
UNITY_LOOP for (int blurPass = 0; blurPass < _BlurSampling; ++blurPass) {
float2 blurNoiseRand = hash23(float3(grabUV, (float) blurPass));

float2 uv = multiplier * (rotate(grabUV + shift - rotationOrigin, rotationAngle) + rotationOrigin);
blurNoiseRand.x *= UNITY_TWO_PI;

switch (_ScreenBoundaryHandling) {
case BOUNDARYMODE_CLAMP:
/*
* technically not necessary since this should happen automatically,
* but I feel better about it by explicitly making sure it happens.
*/
uv = saturate(uv);
break;
case BOUNDARYMODE_REPEAT:
uv = frac(uv);
break;
float s, c;
sincos(blurNoiseRand.x, s, c);

float2 sampleUV = grabUV + (blurNoiseRand.y * _BlurRadius * float2(s, c)) / (_Garb_TexelSize.zw);

float4 col;

UNITY_UNROLL for (int j = 0; j < 3; ++j) {
float2 multiplier = float2(_ScreenXMultiplier[j] * _ScreenXMultiplier.a, _ScreenYMultiplier[j] * _ScreenYMultiplier.a);
float2 shift = float2(_ScreenXOffset[j] + _ScreenXOffset.a, _ScreenYOffset[j] + _ScreenYOffset.a);
#if defined(USING_STEREO_MATRICES)
shift.x *= .5;
#endif
float rotationAngle = _RotationAngle[j] + _RotationAngle.a;
float2 rotationOrigin = float2(_ScreenRotationOriginX[j] + _ScreenRotationOriginX.a, _ScreenRotationOriginY[j] + _ScreenRotationOriginY.a);

float2 uv = multiplier * (rotate(sampleUV + shift - rotationOrigin, rotationAngle) + rotationOrigin);

switch (_ScreenBoundaryHandling) {
case BOUNDARYMODE_CLAMP:
/*
* technically not necessary since this should happen automatically,
* but I feel better about it by explicitly making sure it happens.
*/
uv = saturate(uv);
break;
case BOUNDARYMODE_REPEAT:
uv = frac(uv);
break;
}

col[j] = tex2D(_Garb, uv)[j];
}

grabCol[j] = tex2D(_Garb, uv)[j];
grabCol = lerp(grabCol, col, 1 / (float) (blurPass + 1));
}

float3 hsv = rgb2hsv(grabCol.rgb) * _HSVMultiply + _HSVAdd;
Expand All @@ -316,7 +348,7 @@
break;
case BLENDMODE_OVERLAY:
UNITY_UNROLL for (int k = 0; k < 3; ++k) {
if (finalScreenColor[k] < .5) blendedColor[k] = 2 * finalScreenColor[k] * color[j];
if (finalScreenColor[k] < .5) blendedColor[k] = 2 * finalScreenColor[k] * color[k];
else blendedColor[k] = 1 - 2 * (1 - finalScreenColor[k]) * (1 - color[k]);
}
break;
Expand Down
19 changes: 19 additions & 0 deletions Editor/CancerspaceInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static class Styles {
public static string targetObjectSettingsTitle = "Target Object Settings";
public static string falloffSettingsTitle = "Falloff Settings";
public static string wobbleSettingsTitle = "Wave Distortion";
public static string blurSettingsTitle = "Blur";
public static string screenShakeSettingsTitle = "Screen Shake";
public static string overlaySettingsTitle = "Overlay";
public static string screenColorAdjustmentsTitle = "Screen Color Adjustment";
Expand Down Expand Up @@ -76,6 +77,10 @@ public CSCategory(string nname, GUIStyle sstyle, CSCategorySetup ssetupDelegate)

protected MaterialProperty falloffMaxDistance;

protected MaterialProperty blurRadius;
protected MaterialProperty blurSampling;
protected MaterialProperty blurSamplingAnimated;

protected MaterialProperty zoomAmount;
protected MaterialProperty pixelationAmount;

Expand Down Expand Up @@ -138,6 +143,10 @@ public void FindProperties(MaterialProperty[] props) {

falloffMaxDistance = FindProperty("_MaxFalloff", props);

blurRadius = FindProperty("_BlurRadius", props);
blurSampling = FindProperty("_BlurSampling", props);
blurSamplingAnimated = FindProperty("_AnimatedSampling", props);

zoomAmount = FindProperty("_Zoom", props);
pixelationAmount = FindProperty("_Pixelation", props);

Expand Down Expand Up @@ -227,6 +236,16 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
}
}),

new CSCategory(Styles.blurSettingsTitle, defaultStyle, me => {
if (sliderMode) {
me.ShaderProperty(blurRadius, blurRadius.displayName);
} else {
me.FloatProperty(blurRadius, blurRadius.displayName);
}
DisplayIntSlider(me, blurSampling, 1, 5);
me.ShaderProperty(blurSamplingAnimated, blurSamplingAnimated.displayName);
}),

new CSCategory(Styles.screenShakeSettingsTitle, defaultStyle, me => {
if (sliderMode) {
me.ShaderProperty(shakeXAmount, shakeXAmount.displayName);
Expand Down

0 comments on commit 10865bb

Please sign in to comment.