- Home /
Transparency and blending issues with custom-made shader
Hi Unity Community!
I am facing an issue involving transparency and blending. I know the theory on transparency and the rendering pipeline, but I am not an expert. The problem:
I am drawing some geometry shader custom made cubes over a textured quad in Unity:
When I change the color alpha value in the fragment shader, it looks like this: It behaves like blending color is the camera's background color (white)
So I've tried playing with the z buffer and disabling simply occlude my cubes.
Here is my [simplified] shader code:
Properties { _SpriteTex ("RGBA Texture Image", 2D) = "white" {} _Size ("Size", Range(0, 30)) = 0.5 _IndexPos ("IndexPos", Vector) = (15.0,15.0,15.0,15.0) _X("X",Float) = 15.0 _Y("Y",Float) = 15.0 _Z("Z",Float) = 15.0 _BrushSize("BrushSize",Float) = 0.05 _MinD0("MinD0",Float) = 0 _MaxD0("MaxD0",Float) = 0 _MinD1("MinD1",Float) = 0 _MaxD1("MaxD1",Float) = 0 _Alpha("Alpha", Float) = 0 }
SubShader
{
Pass
{
LOD 400
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Blend SrcAlpha OneMinusSrcAlpha
Lighting Off
//ZWrite Off
Cull Off
AlphaTest Greater 0
CGPROGRAM
#pragma target 5.0
#pragma vertex VS_Main
#pragma fragment FS_Main
#pragma geometry GS_Main
#include "UnityCG.cginc"
// **************************************************************
// Data structures *
// **************************************************************
struct VS_INPUT {
float4 position : POSITION;
float4 color: COLOR;
float4 normal: NORMAL;
};
struct GS_INPUT
{
float4 pos : POSITION;
float3 normal : NORMAL;
float2 tex0 : TEXCOORD0;
float4 col : COLOR;
float isBrushed : FLOAT;
};
struct FS_INPUT
{
float4 pos : POSITION;
float2 tex0 : TEXCOORD0;
float4 col : COLOR;
float isBrushed : FLOAT;
};
// **************************************************************
// Vars *
// **************************************************************
float _Size;
float4 _IndexPos;
float _X;
float _Y;
float _Z;
float _BrushSize;
//*******************
// DIMENSIONS
//*******************
float _MinD0;
float _MaxD0;
float _MinD1;
float _MaxD1;
float _Alpha;
float4x4 _VP;
Texture2D _SpriteTex;
SamplerState sampler_SpriteTex;
//*********************************
// helper functions
//*********************************
float normaliseValue(float value, float i0, float i1, float j0, float j1)
{
float L = (j0 - j1) / (i0 - i1);
return (j0 - (L * i0) + (L * value));
}
// Vertex Shader ------------------------------------------------
GS_INPUT VS_Main(VS_INPUT v)
{
[my geometry shader code...]
}
// Fragment Shader -----------------------------------------------
float4 FS_Main(FS_INPUT input) : COLOR
{
if (input.isBrushed == 1.0)
_Alpha=1.0;
float4 colorReturn = float4(input.col.x*100.0, 0.0, 0.0, _Alpha);
return colorReturn; //ambient+diffuse*saturate(dot(Light,Norm));
}
ENDCG
}
}
I read somewhere to change the rendering queue should be set to a number higher than 3000 (e.g, 3001). I did that and did not see a difference.
Any help is more than welcome!
Thanks for your help Unity Community :-)
Answer by MaxMonahs · May 24, 2016 at 04:17 AM
Ok I solved the problem: if you want to draw custom semi-opaque meshes with your own shaders, just disable the z-buffer on the other objects.