- Home /
Question by
Magmaw · Nov 15, 2019 at 05:55 AM ·
scripting problemshadershaderssprites
How to make transparent shader (2D)?
Hi, I'm trying to write a 2D shader that pixelates a sprite and makes it transparent. I'm having trouble getting it to appear transparent. I set the RenderType to transparent, but I don't know how to actually make the sprite appear somewhat transparent.
Here's my code:
Shader "Unlit/UITestPix"
{
Properties
{
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_Pixels ("Pixels", Float) = 512
_Ratio("Ratio", Vector) = (9,16,0,0)
}
SubShader
{
Tags { "RenderType"="Transparent" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Pixels;
float4 _Ratio;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float dx = _Ratio.x * (1 / _Pixels);
float dy = _Ratio.y * (1 / _Pixels);
float2 Coord = float2(dx * floor(i.uv.x / dx), dy * floor(i.uv.y / dy));
// sample the texture
fixed4 col = tex2D(_MainTex, Coord);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Thanks.
Comment
Answer by revolute · Nov 15, 2019 at 07:55 AM
Adding
Blend SrcAlpha OneMinusSrcAlpha
after line
LOD 100
should be enough.
Your answer
![](https://koobas.hobune.stream/wayback/20220612214726im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Grass shader sprite 2D 3 Answers
Shader Color 1 Answer
perform action on shader compilation/update 0 Answers
Shader not changing according to scene light in real time. 1 Answer