Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【Unity】リムライティングエフェクト【Shader】
Shader "Unlit/RimLightEffect"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_MainColor("MainColor",Color)=(1,1,1,1)
_RimPower("RimPower",Float)=1
_RimAmplitude("RimAmplitude",Float)=1
_RimColor("RimColor",Color)=(1,1,1,1)
_Alpha("Alpha",Range(0.0,1.0))=1
}
SubShader
{
Tags {
"RenderType"="Transparent"
"Queue"="Transparent"
}
CGINCLUDE
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
float4 worldPos : TEXCOORD2;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _MainColor;
float _RimPower;
fixed4 _RimColor;
float _RimAmplitude;
float _Alpha;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.normal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
float4 frag (v2f i) : SV_Target
{
float4 col = tex2D(_MainTex, i.uv)*_MainColor;
float3 normalDir = normalize(i.normal);
float3 viewDir = normalize(_WorldSpaceCameraPos.xyz-i.worldPos.xyz);
float NVdot = 1 - dot(viewDir,normalDir);
float rim=pow(NVdot,_RimPower)*_RimAmplitude;
col.rgb =col.rgb+ rim*(1-abs(_Alpha*2-1))*_RimColor.rgb;
col.a = _Alpha * (1 + rim);
return col;
}
ENDCG
Pass{
ZWrite On
ColorMask 0
}
Pass{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment