Monday, August 15, 2011

Blending Problems

So I got these indicators to know when a unit is selected or where he's going to move:


Its a projector, its not the fastest, but it looks good. Its blend equation is set right now to multiply. That's fine but:


Multiply darkens the blend, making it hard to see in dark places. So, I try an additive blend:


So that looks better. Only it does the opposite: in bright areas, its hard to see:


More so in very bright areas:


So in the end, I am forced to use a normal alpha-blend equation. It looks plain, but is sure to be visible in both bright and dark areas:



Unfortunately, you can't emulate the blend types in Photoshop like Overlay, because blending equations in videocards are fixed-function.

Here's the Unity shaderlab code for alpha-blended projectors:

Shader "Projector/Diffuse" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_ShadowTex ("Cookie", 2D) = "" { TexGen ObjectLinear }
	}
	Subshader {
		Tags { "RenderType"="Transparent"  "Queue"="Transparent"}
		Pass {

			ZWrite off
			Fog { Color (0, 0, 0) }
			Color [_Color]
			ColorMask RGB
			Offset -1, -1

			Blend SrcAlpha OneMinusSrcAlpha

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest

#include "UnityCG.cginc"

sampler2D _ShadowTex;
float4x4 _Projector;
fixed4 _Color;

struct v2f {
	float4 vertex : SV_POSITION;
	float4 texcoord : TEXCOORD0;
};

v2f vert (appdata_tan v)
{
	v2f o;
	o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
	o.texcoord = mul(_Projector, v.vertex);
	return o;
}

half4 frag (v2f i) : COLOR
{
	return tex2Dproj(_ShadowTex, UNITY_PROJ_COORD(i.texcoord)) * _Color;
}
ENDCG
		}
	}
}

No comments:

Post a Comment

Made me post. 0/10.