- Home /
Mesh Renderer Fade in and fade out.[Mobile Platform]
Is there any way to make the mesh appear with fade in and fade out effect?
Goal: Mesh Fade in slowly and fade out in mobile platform.
Solution I tried: Am pretty sure in normal material I can use the alpha to achieve this effect directly in material by lerping between alpha value in color.
How is it possible to do this in mobile? I want it to be less memory consuming as I need to do achieve this effect in Mobile platform. Is there any efficient way to achieve this effect? In Mobile material there is no separate color value(possibly am i missing something?) also I have a texture[Diffuse] applied in the material
Thanks. Any input would be appreciated.
Answer by ethestel · Apr 07, 2015 at 08:09 AM
Yes, you can do this by animating a custom material's color or opacity values. And it can be optimized for mobile, depending on the shader you are using.
So is that mean do I need to write a shader? I want to achieve it through Unity $$anonymous$$obile shader. Is it possible? I don't see any color or opacity properties in mobile shader [Unity Default] Can you provide an example? or do any links possibly? Thanks.
Yes, unfortunately Unity's bundled mobile shaders doesn't have transparency option.
I can share a sample shader if you give more details about the properties of this mesh. How does this mesh looks like? Does it have a texture on it, or just color? Does it affected by lighting? Is it casting shadows?
I have a simple diffuse and specular texture. I have lights in the scene (Is it possible to render the lights in mobile? If so I will use it other wise I can remove the light and bake it in the map directly). Can you send the material, that would be very helpful. Am not much in to shader. Thanks.
Hello again,
Here's a shader a quickly put together. It supports 1 directional light which should work without issues on mobile.
Shader "Diffuse Specular with Opacity w Directional Lights" {
Properties {
_DiffuseTex ("Diffuse (RGBA)", 2D) = "white" {}
_SpecularTex ("Specular (RGB)", 2D) = "white" {}
_SpecularPower ("Specular Power", float) = 10
_Opacity ("Opacity", Range(0,1)) = 1
}
SubShader {
Tags { "RenderType"="Transparent" "Queue"="Transparent" "Light$$anonymous$$ode"="ForwardBase" }
ZWrite Off
Cull Back
Blend SrcAlpha One$$anonymous$$inusSrcAlpha
Pass
{
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
uniform sampler2D _DiffuseTex;
uniform sampler2D _SpecularTex;
uniform float4 _DiffuseTex_ST;
uniform float4 _SpecularTex_ST;
uniform float4 _LightColor0;
uniform float _SpecularPower;
uniform float _Opacity;
struct vIn
{
float4 vertex : POSITION;
half4 normal : NOR$$anonymous$$AL;
half2 coord0 : TEXCOORD0;
half2 coord1 : TEXCOORD1;
};
struct vOut
{
float4 pos : SV_POSITION;
half3 worldPos : COLOR;
fixed3 normal : NOR$$anonymous$$AL;
half2 diffuseCoord : TEXCOORD0;
half2 specularCoord : TEXCOORD1;
};
vOut vert(vIn v)
{
vOut o;
o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
o.worldPos = mul(_Object2World, v.vertex);
o.normal = normalize(mul( v.normal, _World2Object).xyz);
o.diffuseCoord = v.coord0 * _DiffuseTex_ST.xy + _DiffuseTex_ST.zw;
o.specularCoord = v.coord1 * _SpecularTex_ST.xy + _SpecularTex_ST.zw;
return o;
}
float4 frag(vOut i) : COLOR
{
float3 diffuseColor = tex2D(_DiffuseTex, i.diffuseCoord);
float3 specularColor = tex2D(_SpecularTex, i.specularCoord);
fixed3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
fixed3 viewDirection = normalize(_WorldSpaceCameraPos-i.worldPos).xyz;
float3 ambientLight = UNITY_LIGHT$$anonymous$$O$$anonymous$$_A$$anonymous$$BIENT.rgb * diffuseColor;
float3 diffuseRef = _LightColor0.rgb * diffuseColor * max(dot(i.normal, lightDirection), 0.0);
float3 specularRef = _LightColor0.rgb * specularColor * pow(max(dot(viewDirection,reflect(-lightDirection, i.normal)), 0.0),_SpecularPower);
return float4(ambientLight+diffuseRef+specularRef,_Opacity);
}
ENDCG
}
}
FallBack "Diffuse"
}
Wow! Super Awesome. Thanks. Am away from my workstation. Once I get back to work, I will check this one. Thanks.
Your answer
