Created
June 11, 2023 00:00
-
-
Save partybusiness/eb51732d6491726c76fdc53f29fd08a6 to your computer and use it in GitHub Desktop.
Gradient shaders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Unlit/ScreenSpaceGradient" | |
{ | |
Properties | |
{ | |
_TopColour("Top Colour", Color) = (0.5,0.5,0.5,1) | |
_BottomColour("Bottom Colour", Color) = (0.5,0.5,0.5,1) | |
_Height("Height", float) = 0.0 | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" } | |
LOD 100 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
}; | |
float4 _TopColour; | |
float4 _BottomColour; | |
float _Height; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
float4 clipPos = UnityObjectToClipPos(v.vertex); | |
o.vertex = clipPos; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float3 forward = mul((float3x3)unity_CameraToWorld, float3(0,0,1)); | |
float angle = atan2(forward.y, length(forward.xz)); | |
float t = clamp(i.vertex.y *0.5 / _ScreenParams.y + angle *0.5 - _Height, 0, 1);//can I adjust based on view angle? | |
return lerp (_BottomColour, _TopColour, t); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Unlit/ScreenSpaceWorldOffsetDirectionalGradient" | |
{ | |
Properties | |
{ | |
_TopColour("Top Colour", Color) = (0.5,0.5,0.5,1) | |
_BottomColour("Bottom Colour", Color) = (0.5,0.5,0.5,1) | |
_Centre("Centre", float) = (0,0,0,0) | |
_Direction("Direction", Vector) = (0,-1,0,0) | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" "DisableBatching" = "True" } | |
LOD 100 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
float3 worldPos : TEXCOORD0; | |
float3 centrePos : TEXCOORD1; | |
float3 dirPos : TEXCOORD2; | |
}; | |
float4 _TopColour; | |
float4 _BottomColour; | |
float3 _Centre; | |
float3 _Direction; | |
inline float3 projectOnPlane(float3 vec, float3 normal) | |
{ | |
return vec - normal * dot(vec, normal); | |
} | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
float4 clipPos = UnityObjectToClipPos(v.vertex); | |
o.vertex = clipPos; | |
float3 worldPos = mul(unity_ObjectToWorld, v.vertex); | |
float3 centrePos = mul(unity_ObjectToWorld, float4(_Centre, 1)); | |
float3 dirPos = mul(unity_ObjectToWorld, float4(_Centre + _Direction, 1)); | |
o.worldPos = worldPos; | |
o.centrePos = centrePos; | |
o.dirPos = dirPos; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float3 forward = normalize(i.worldPos - _WorldSpaceCameraPos); | |
float3 dir = projectOnPlane(i.dirPos - i.centrePos, forward); | |
float dirMag = length(dir); | |
dir = normalize(dir); | |
float t = clamp(dot(projectOnPlane(i.worldPos-i.centrePos, forward), dir) / dirMag,0,1); | |
return lerp (_TopColour, _BottomColour, t); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Unlit/ScreenSpaceWorldOffsetGradient" | |
{ | |
Properties | |
{ | |
_TopColour("Top Colour", Color) = (0.5,0.5,0.5,1) | |
_BottomColour("Bottom Colour", Color) = (0.5,0.5,0.5,1) | |
_Height("Height", float) = 0.0 | |
_Range("Range", float) = 1.0 | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" "DisableBatching" = "True" } | |
LOD 100 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
float3 screenPos : TEXCOORD0; | |
}; | |
float4 _TopColour; | |
float4 _BottomColour; | |
float _Height; | |
float _Range; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
float4 clipPos = UnityObjectToClipPos(v.vertex); | |
o.vertex = clipPos; | |
//o.uv = TRANSFORM_TEX(v.uv, _MainTex); | |
float4 screenPos = ComputeScreenPos(clipPos); | |
float4 worldCentre = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)); | |
worldCentre += float4(0, _Height, 0, 0); | |
float4 centrePos = ComputeScreenPos(UnityObjectToClipPos(mul(unity_WorldToObject, worldCentre))); | |
worldCentre += float4(0,_Range,0,0); | |
float4 scalePos = ComputeScreenPos(UnityObjectToClipPos(mul(unity_WorldToObject, worldCentre))); | |
o.screenPos = float3(centrePos.y / centrePos.w, screenPos.y / screenPos.w, scalePos.y / scalePos.w); | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
fixed diff = i.screenPos.x - i.screenPos.z; | |
float t = clamp((i.screenPos.x - i.screenPos.y) / diff,0,1); | |
return lerp (_BottomColour, _TopColour, t); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Unlit/WorldSpaceGradient" | |
{ | |
Properties | |
{ | |
_TopColour("Top Colour", Color) = (0.5,0.5,0.5,1) | |
_BottomColour("Bottom Colour", Color) = (0.5,0.5,0.5,1) | |
_Height("Height", float) = 0.0 | |
_Range("Range", float) = 1.0 | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" "DisableBatching" = "True" } | |
LOD 100 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
float2 posXY : TEXCOORD0; | |
}; | |
float4 _TopColour; | |
float4 _BottomColour; | |
float _Height; | |
float _Range; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
float worldY = mul(unity_ObjectToWorld, v.vertex).y; | |
float centreY = mul(unity_ObjectToWorld, float4(0,0,0,1)).y + _Height; | |
o.posXY = float2(worldY, centreY); | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float l = clamp((i.posXY.y - i.posXY.x) / _Range,0,1); | |
return lerp (_TopColour, _BottomColour, l); | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment