- Home /
Terrain Tree Billboard have bright outline against dark background
This is a question I have the answer for, and it's more fitting to Unity Answers than the forums.
When placing trees on the terrain using the terrain engine, the trees, when becoming billboards, have a bright 'glow' outline to them.
This is something that bothered my team for a long time, and we haven't found any information about it online. To illustrate the problem:
Answer by asafsitner · Oct 22, 2012 at 09:58 AM
We figured out the problem was with the alpha-blending part of the shader, and it was extremely easy to fix. We just downloaded the built-in terrain billboard shader and added the following line of code: `AlphaTest Greater 0.9` after the blending mode definition `Blend SrcAlpha OneMinusSrcAlpha`
You can find the Fixed shader here: Just copy and paste this into a shader file and you're done.
FINALLY! Thank you so much for sharing this fix. I've been searchin for hours and hours, and this has finally resolved my issue. Thanks!
@$$anonymous$$no - That's great to hear! We're very happy that this actually helped someone :D
Unfortunately for me, this shader did worse. I lush trees, and after applying this shader they have almost no leaves. How to fix it?
Answer by BTables · Oct 21, 2015 at 04:46 AM
I just ran into this problem when switching to linear lighting in unity 5.
The above solution didn't solve it, after much digging it turns out the background color of the render texture unity uses for tree billboards isn't black (it's (0.025,0.025,0.025)).
In gamma space this doesn't present itself as a problem. However in linear space, this is converted to gamma space at the end of the render pipeline bringing it to a much more visible grey (0.19,0.19,0.19)
To solve this I modified the TreeCreatorLeavesRendertex.shader to convert the billboard render texture to an approximation of gamma color space with
c.rgb = sqrt(c.rgb);
c.rgb = max(c.rgb,0.025); //Ensure our tree is never rendered darker than the background color
towards the end of the fragment shader
Then do the opposite operation in the BillboardTree.shader
col.rgb = col.rgb * col.rgb;
Not a perfect solution, but will work for us until speedtree stops performing so poorly
Where to exactly paste this?!?!?! can you please paste the full code sir?
Answer by mitaywalle · Jul 04, 2016 at 01:30 PM
[UPDATED 22.03.17] I've used this shader, plus:
TOD_TreeBillboardState. Means need to Colorize or not (Night or Day), set through Shader.SetGlobalFloat
TOD_TreeBillboardColor. Which Color to use for fill. Uses white-to-black Gradient, based on Night-time. Set through Shader.SetGlobalColor
Gradient t instructions:
if (Cycle.Hour > 17f)
t = Mathf.Lerp(0f, .5f, (Cycle.Hour - 17f) / 7f);
else
t = Mathf.Lerp(.5f, 1f, Cycle.Hour / 7f);
!All is Updating every frame, with day-night-cycle script. No performance problems.
Shader "Hidden/TerrainEngine/BillboardTree" {
Properties {
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
}
SubShader {
Tags { "Queue" = "Transparent-1" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
Pass {
Tags { "RequireOption" = "SoftVegetation" }
// Dont write to the depth buffer
ZWrite off
// Set up alpha blending
Blend SrcAlpha OneMinusSrcAlpha
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
#include "TerrainEngine.cginc"
Float TOD_TreeBillboardState;
fixed4 TOD_TreeBillboardColor;
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR0;
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
};
v2f vert (appdata_tree_billboard v) {
v2f o;
TerrainBillboardTree(v.vertex, v.texcoord1.xy, v.texcoord.y);
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv.x = v.texcoord.x;
o.uv.y = v.texcoord.y > 0;
o.color = v.color;
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
sampler2D _MainTex;
fixed4 frag(v2f input) : SV_Target
{
fixed4 col = tex2D( _MainTex, input.uv);
if (TOD_TreeBillboardState > .5)
if (col.r < .18 && col.a < .9)
col.rgb *= TOD_TreeBillboardColor.rgb;
col.rgb *= input.color.rgb;
clip(col.a);
UNITY_APPLY_FOG(input.fogCoord, col);
return col;
}
ENDCG
}
Pass {
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
#include "TerrainEngine.cginc"
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR0;
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
};
v2f vert (appdata_tree_billboard v) {
v2f o;
TerrainBillboardTree(v.vertex, v.texcoord1.xy, v.texcoord.y);
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv.x = v.texcoord.x;
o.uv.y = v.texcoord.y > 0;
o.color = v.color;
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
sampler2D _MainTex;
fixed4 frag(v2f input) : SV_Target
{
fixed4 col = tex2D( _MainTex, input.uv);
col.rgb *= input.color.rgb;
clip(col.a - .99);
UNITY_APPLY_FOG(input.fogCoord, col);
return col;
}
ENDCG
}
}
Fallback Off
}
Well it does actually improve a little bit the billboard problem but it doesn't fix it at all. I mean the outline alpha issue persist it just become dark rather than white wich is not a definitive fix. I'm start to think i'm going to change between the two shaders acording the day/night cycle. I'm not a shader writer at all so i don't know if it's actually possible to pick the light color or something like that and use that to fade the alpha issue.
Thanks man, i take ur code and modify that part:
if (col.a < .99f && col.r <.2f) col.rgb *= 0.0020;
$$anonymous$$y trees ok now)
Answer by TimNick151297 · Apr 17, 2017 at 01:16 PM
Another pretty simple solution for that is changing the alpha cutout directly in the shader. Simply go to "clip(col.a)" and add -0.9 to it. This will increase the cutout and removes the grey outline.
Answer by Brandon_Lindberg · Dec 11, 2014 at 07:31 PM
An easy way to fix this is to go to terrain settings and change the billboard to about halfway, and if it still appears, put it up all the way.
You mean increase the billboard start distance? That's not a fix, that's just moving the problem further away, and your performance will take a serious knock
Your answer
Follow this Question
Related Questions
How to hide Unity terrain trees when they are blocking the camera view? 0 Answers
Why doesn't built-in Nature->SoftOcclusion->Leaves/Bark support spot lighting? 0 Answers
Terrain system trees slightly sharpen and blur depending on camera rotation 2 Answers
Terrain Trees remain lit up when no light is cast 1 Answer
Can't use tree brush tool 0 Answers