Skip to content

Commit

Permalink
Added normal-mapped distortion.
Browse files Browse the repository at this point in the history
  • Loading branch information
AkaiMage authored Mar 31, 2019
1 parent a4b5a6a commit d9d10e3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Cancerspace.shader
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
[PowerSlider(2.0)]_XWobbleSpeed ("X Speed", Range(0, 100)) = 100
[PowerSlider(2.0)]_YWobbleSpeed ("Y Speed", Range(0, 100)) = 100

_BumpMap ("Distortion Map (Normal)", 2D) = "bump" {}
_DistortionAmplitude ("Amplitude", Range(-1, 1)) = 0.1
_BumpMapScrollSpeedX ("Scroll Speed X", Range(-2, 2)) = 0
_BumpMapScrollSpeedY ("Scroll Speed Y", Range(-2, 2)) = 0

[PowerSlider(2.0)]_XShake ("X Shake", Range(0, 1)) = 0
[PowerSlider(2.0)]_YShake ("Y Shake", Range(0, 1)) = 0
_XShakeSpeed ("X Shake Speed", Range(0, 300)) = 200
Expand Down Expand Up @@ -193,6 +198,11 @@
float _AnimatedSampling;
float _BlurRadius;

sampler2D _BumpMap;
float4 _BumpMap_ST;
float _DistortionAmplitude;
float _BumpMapScrollSpeedX, _BumpMapScrollSpeedY;

float2 hash23(float3 p) {
if (_AnimatedSampling) p.z += frac(_Time.z) * 4;
p = frac(p * float3(400, 450, .1));
Expand Down Expand Up @@ -261,9 +271,12 @@
VRFix = .5;
#endif

float4 color = tex2D(_MainTex, TRANSFORM_TEX(computeScreenSpaceOverlayUV(i.posWorld), _MainTex)) * _OverlayColor;
float2 screenSpaceOverlayUV = computeScreenSpaceOverlayUV(i.posWorld);
float4 color = tex2D(_MainTex, TRANSFORM_TEX(screenSpaceOverlayUV, _MainTex)) * _OverlayColor;

float2 displace = float2(_XShake * VRFix, _YShake) * sin(_Time.yy * float2(_XShakeSpeed, _YShakeSpeed)) * _ShakeAmplitude;
float2 displace = float2(_XShake, _YShake) * sin(_Time.yy * float2(_XShakeSpeed, _YShakeSpeed)) * _ShakeAmplitude;
displace += UnpackNormal(tex2D(_BumpMap, TRANSFORM_TEX(screenSpaceOverlayUV + _Time.yy * float2(_BumpMapScrollSpeedX, _BumpMapScrollSpeedY), _BumpMap))).xy * _DistortionAmplitude;
displace.x *= VRFix;

float2 grabUV = i.projPos.xy / i.projPos.w;

Expand Down
24 changes: 24 additions & 0 deletions Editor/CancerspaceInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static class Styles {
public static string falloffSettingsTitle = "Falloff Settings";
public static string wobbleSettingsTitle = "Wave Distortion";
public static string blurSettingsTitle = "Blur";
public static string distortionMapSettingsTitle = "Distortion Mapping";
public static string screenShakeSettingsTitle = "Screen Shake";
public static string overlaySettingsTitle = "Overlay";
public static string screenColorAdjustmentsTitle = "Screen Color Adjustment";
Expand Down Expand Up @@ -123,6 +124,11 @@ public CSCategory(string nname, GUIStyle sstyle, CSCategorySetup ssetupDelegate)

protected MaterialProperty mirrorReflectionMode;

protected MaterialProperty distortionMap;
protected MaterialProperty distortionAmplitude;
protected MaterialProperty distortionScrollSpeedX;
protected MaterialProperty distortionScrollSpeedY;

protected int customRenderQueue;
protected bool initialized;

Expand Down Expand Up @@ -157,6 +163,11 @@ public void FindProperties(MaterialProperty[] props) {
wobbleYTiling = FindProperty("_YWobbleTiling", props);
wobbleXSpeed = FindProperty("_XWobbleSpeed", props);
wobbleYSpeed = FindProperty("_YWobbleSpeed", props);

distortionMap = FindProperty("_BumpMap", props);
distortionAmplitude = FindProperty("_DistortionAmplitude", props);
distortionScrollSpeedX = FindProperty("_BumpMapScrollSpeedX", props);
distortionScrollSpeedY = FindProperty("_BumpMapScrollSpeedY", props);

shakeXAmount = FindProperty("_XShake", props);
shakeYAmount = FindProperty("_YShake", props);
Expand Down Expand Up @@ -248,6 +259,19 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
me.ShaderProperty(blurSamplingAnimated, blurSamplingAnimated.displayName);
}),

new CSCategory(Styles.distortionMapSettingsTitle, defaultStyle, me => {
me.ShaderProperty(distortionMap, distortionMap.displayName);
if (sliderMode) {
me.ShaderProperty(distortionAmplitude, distortionAmplitude.displayName);
me.ShaderProperty(distortionScrollSpeedX, distortionScrollSpeedX.displayName);
me.ShaderProperty(distortionScrollSpeedY, distortionScrollSpeedY.displayName);
} else {
me.FloatProperty(distortionAmplitude, distortionAmplitude.displayName);
me.FloatProperty(distortionScrollSpeedX, distortionScrollSpeedX.displayName);
me.FloatProperty(distortionScrollSpeedY, distortionScrollSpeedY.displayName);
}
}),

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

0 comments on commit d9d10e3

Please sign in to comment.