I'm trying to learn writing shaders. And I've pieced together something that is kinda working. I want to create a transparent glow around a sphere.
The problem I have is that I can't seem to alter the transparency of the glow.
I tried experimenting with ZWrite off
but that doesn't give me the desired results.
The red part around the sphere is the glow. But I'm unable to change its alpha. I'd would also like to change the "blurriness" of the glow. How would I do that?
EDIT
so I did some more tinkering. I've changed the blending to
Blend SrcAlpha OneMinusSrcAlpha
and enabled ZWrite off
. This allows me to change the alpha. The problem with this is that the inside of the object is also colored by the glow. I just want the glow around the circle. not inside.
Shader "Custom/HoleShader" {
Properties {
_holeColor ("Color", Color) = (1,1,1,1)
_glowColor ("Glow Color", Color) = (1,1,1,1)
_glowSize ("Glow Size", Range (0.0,5)) = 1.0
_glowOpacity ("Glow Opacity", Range(0,1)) = 1
}
SubShader {
Tags { "LightMode" = "ForwardBase" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform float4 _holeColor;
uniform float4 _glowColor;
struct appData {
float4 normal : NORMAL;
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
float4 col : COLOR;
};
v2f vert (appData v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
return o;
}
float4 frag(v2f i) : COLOR {
return _holeColor;
}
ENDCG
}
Tags { "LightMode" = "ForwardBase" "Queue" = "Transparent" "RenderType" = "Transparent"}
Pass {
ZWrite Off
//Blend One OneMinusSrcColor
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform float4 _holeColor;
uniform float4 _glowColor;
uniform float _strength;
uniform float _glowSize;
uniform float _glowOpacity;
struct appData {
float4 normal : NORMAL;
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
float4 col : COLOR;
float3 viewDirection;
float3 normalDirection;
};
v2f vert (appData v) {
v2f o;
float4 pos = v.vertex + (v.normal * _glowSize);
o.pos = mul(UNITY_MATRIX_MVP,pos);
return o;
}
float4 frag(v2f i) : COLOR {
return float4(_glowColor.rgb, _glowOpacity);
}
ENDCG
}
}
}