I have never made a shader before, and so I have no clue how to fix this issue. These shaders were given to me, they were made with Amplify and I have them in HLSL.
It is 2 shaders, both with the same problem. The first shader blurs the screen, and does so correctly, by sampling the screen multiple times and them offsetting it inwards (I think this is what happens). However, if i move the gameobject that i’m looking at away from the origin point (0,0,0) the blur “shifts” towards (0,0,0). The further away from 0 i am, the harder it tries to “Reach” that point.
The same happens for the other shader which “Warps” the surroundings. It warps them towards (0,0,0)
In a game where my character is moving around, this makes the effect very dependant on where my camera is looking and where my character is.
Is it possible to have the shaders instead try “reach” their own current position?
Code for blur:
// Made with Amplify Shader Editor
Shader "Blur"
{
Properties
{
_Mul("Mul", Float) = 1
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent+1" }
LOD 100
CGINCLUDE
#pragma target 3.0
ENDCG
Blend SrcAlpha OneMinusSrcAlpha
AlphaToMask Off
Cull Off
ColorMask RGBA
ZWrite Off
ZTest Always
Offset 0 , 0
GrabPass{ }
Pass
{
Name "Unlit"
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
#define ASE_DECLARE_SCREENSPACE_TEXTURE(tex) UNITY_DECLARE_SCREENSPACE_TEXTURE(tex);
#else
#define ASE_DECLARE_SCREENSPACE_TEXTURE(tex) UNITY_DECLARE_SCREENSPACE_TEXTURE(tex)
#endif
#ifndef UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX
//only defining to not throw compilation error over Unity 5.5
#define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
#endif
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
float4 ase_texcoord1 : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
float3 worldPos : TEXCOORD0;
#endif
float4 ase_texcoord1 : TEXCOORD1;
float4 ase_texcoord2 : TEXCOORD2;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
ASE_DECLARE_SCREENSPACE_TEXTURE( _GrabTexture )
uniform half3 Pos2;
uniform half _Mul;
v2f vert ( appdata v )
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
UNITY_TRANSFER_INSTANCE_ID(v, o);
float4 ase_clipPos = UnityObjectToClipPos(v.vertex);
float4 screenPos = ComputeScreenPos(ase_clipPos);
o.ase_texcoord1 = screenPos;
o.ase_texcoord2 = v.ase_texcoord1;
float3 vertexValue = float3(0, 0, 0);
#if ASE_ABSOLUTE_VERTEX_POS
vertexValue = v.vertex.xyz;
#endif
vertexValue = vertexValue;
#if ASE_ABSOLUTE_VERTEX_POS
v.vertex.xyz = vertexValue;
#else
v.vertex.xyz += vertexValue;
#endif
o.vertex = UnityObjectToClipPos(v.vertex);
#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
#endif
return o;
}
fixed4 frag (v2f i ) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
fixed4 finalColor;
#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
float3 WorldPosition = i.worldPos;
#endif
float4 screenPos = i.ase_texcoord1;
float4 ase_screenPosNorm = screenPos / screenPos.w;
ase_screenPosNorm.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? ase_screenPosNorm.z : ase_screenPosNorm.z * 0.5 + 0.5;
half2 temp_output_10_0 = (ase_screenPosNorm).xy;
half4 unityObjectToClipPos69 = UnityObjectToClipPos( Pos2 );
half4 computeGrabScreenPos58 = ComputeGrabScreenPos( unityObjectToClipPos69 );
half4 texCoord34 = i.ase_texcoord2;
texCoord34.xy = i.ase_texcoord2.xy * float2( 1,1 ) + float2( 0,0 );
half2 temp_output_4_0 = ( ( temp_output_10_0 - ( (computeGrabScreenPos58).xy / (computeGrabScreenPos58).w ) ) * ( _Mul * texCoord34.z ) );
half2 temp_output_6_0 = ( temp_output_10_0 + temp_output_4_0 );
half4 screenColor9 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,temp_output_6_0);
half2 temp_output_22_0 = ( temp_output_6_0 + temp_output_4_0 );
half4 screenColor15 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,temp_output_22_0);
half2 temp_output_23_0 = ( temp_output_22_0 + temp_output_4_0 );
half4 screenColor16 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,temp_output_23_0);
half2 temp_output_24_0 = ( temp_output_23_0 + temp_output_4_0 );
half4 screenColor17 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,temp_output_24_0);
half2 temp_output_25_0 = ( temp_output_24_0 + temp_output_4_0 );
half4 screenColor18 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,temp_output_25_0);
half2 temp_output_26_0 = ( temp_output_25_0 + temp_output_4_0 );
half4 screenColor19 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,temp_output_26_0);
half2 temp_output_27_0 = ( temp_output_26_0 + temp_output_4_0 );
half4 screenColor20 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,temp_output_27_0);
half2 temp_output_28_0 = ( temp_output_27_0 + temp_output_4_0 );
half4 screenColor21 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,temp_output_28_0);
half2 temp_output_72_0 = ( temp_output_28_0 + temp_output_4_0 );
half4 screenColor70 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,temp_output_72_0);
half4 screenColor71 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,( temp_output_72_0 + temp_output_4_0 ));
half4 appendResult32 = (half4(( ( screenColor9 + screenColor15 + screenColor16 + screenColor17 + screenColor18 + screenColor19 + screenColor20 + screenColor21 + screenColor70 + screenColor71 ) / 10.0 ).rgb , 1.0));
finalColor = ( float4( 0,0,0,0 ) + appendResult32 );
return finalColor;
}
ENDCG
}
}
CustomEditor "ASEMaterialInspector"
}
Code for “Warp”
// Made with Amplify Shader Editor
Shader "Sphere"
{
Properties
{
_mul("mul", Float) = 1.64
_Size("Size", Range( 0 , 1)) = 0.3723984
_SizeFactor("SizeFactor", Float) = 0
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
LOD 100
CGINCLUDE
#pragma target 3.0
ENDCG
Blend SrcAlpha OneMinusSrcAlpha
AlphaToMask Off
Cull Back
ColorMask RGBA
ZWrite Off
ZTest LEqual
Offset 0 , 0
GrabPass{ }
Pass
{
Name "Unlit"
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
#define ASE_DECLARE_SCREENSPACE_TEXTURE(tex) UNITY_DECLARE_SCREENSPACE_TEXTURE(tex);
#else
#define ASE_DECLARE_SCREENSPACE_TEXTURE(tex) UNITY_DECLARE_SCREENSPACE_TEXTURE(tex)
#endif
#ifndef UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX
//only defining to not throw compilation error over Unity 5.5
#define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
#endif
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"
#include "UnityShaderVariables.cginc"
#define ASE_NEEDS_FRAG_WORLD_POSITION
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
half3 ase_normal : NORMAL;
float4 ase_texcoord1 : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
float3 worldPos : TEXCOORD0;
#endif
float4 ase_texcoord1 : TEXCOORD1;
float4 ase_texcoord2 : TEXCOORD2;
float4 ase_texcoord3 : TEXCOORD3;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
ASE_DECLARE_SCREENSPACE_TEXTURE( _GrabTexture )
uniform half3 Pos;
uniform half _mul;
uniform half _Size;
uniform half _SizeFactor;
inline float4 ASE_ComputeGrabScreenPos( float4 pos )
{
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
float4 o = pos;
o.y = pos.w * 0.5f;
o.y = ( pos.y - o.y ) * _ProjectionParams.x * scale + o.y;
return o;
}
v2f vert ( appdata v )
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
UNITY_TRANSFER_INSTANCE_ID(v, o);
float4 ase_clipPos = UnityObjectToClipPos(v.vertex);
float4 screenPos = ComputeScreenPos(ase_clipPos);
o.ase_texcoord1 = screenPos;
half3 ase_worldNormal = UnityObjectToWorldNormal(v.ase_normal);
o.ase_texcoord2.xyz = ase_worldNormal;
o.ase_texcoord3 = v.ase_texcoord1;
//setting value to unused interpolator channels and avoid initialization warnings
o.ase_texcoord2.w = 0;
float3 vertexValue = float3(0, 0, 0);
#if ASE_ABSOLUTE_VERTEX_POS
vertexValue = v.vertex.xyz;
#endif
vertexValue = vertexValue;
#if ASE_ABSOLUTE_VERTEX_POS
v.vertex.xyz = vertexValue;
#else
v.vertex.xyz += vertexValue;
#endif
o.vertex = UnityObjectToClipPos(v.vertex);
#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
#endif
return o;
}
fixed4 frag (v2f i , half ase_vface : VFACE) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
fixed4 finalColor;
#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
float3 WorldPosition = i.worldPos;
#endif
float4 screenPos = i.ase_texcoord1;
float4 ase_grabScreenPos = ASE_ComputeGrabScreenPos( screenPos );
half4 ase_grabScreenPosNorm = ase_grabScreenPos / ase_grabScreenPos.w;
half2 temp_output_16_0 = (ase_grabScreenPosNorm).xy;
half4 unityObjectToClipPos92 = UnityObjectToClipPos( Pos );
half4 computeGrabScreenPos44 = ComputeGrabScreenPos( unityObjectToClipPos92 );
half2 temp_output_17_0 = ( temp_output_16_0 - (( (computeGrabScreenPos44).xy / (computeGrabScreenPos44).w )).xy );
half3 ase_worldNormal = i.ase_texcoord2.xyz;
half3 switchResult76 = (((ase_vface>0)?(ase_worldNormal):(-ase_worldNormal)));
float3 ase_worldViewDir = UnityWorldSpaceViewDir(WorldPosition);
ase_worldViewDir = normalize(ase_worldViewDir);
half dotResult40 = dot( switchResult76 , ase_worldViewDir );
half smoothstepResult23 = smoothstep( _Size , ( _Size + _SizeFactor ) , saturate( dotResult40 ));
half4 texCoord98 = i.ase_texcoord3;
texCoord98.xy = i.ase_texcoord3.xy * float2( 1,1 ) + float2( 0,0 );
half4 screenColor22 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,( temp_output_16_0 + ( temp_output_17_0 * _mul * smoothstepResult23 * saturate( (0.0 + (abs( distance( _WorldSpaceCameraPos , WorldPosition ) ) - 0.0) * (1.0 - 0.0) / (20.0 - 0.0)) ) * texCoord98.w ) ));
half4 Big34 = screenColor22;
finalColor = Big34;
return finalColor;
}
ENDCG
}
}
CustomEditor "ASEMaterialInspector"
}
The blur shader gets applied to the entire screen. The warp shader (“Sphere” in code) gets applied to a sphere.