I'm trying to create a shader that will make the object invisible and then with a script a fade in/out effect between invisible and visible.
What I tried:
Shader "InsideVisible" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color ("Color (RGBA)", Color) = (1, 1, 1, 1) // add _Color property
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Cull front // ADDED BY BERNIE, TO FLIP THE SURFACES
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert alpha
#pragma fragment frag alpha
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Color;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
// ADDED BY BERNIE:
v.texcoord.x = 1 - v.texcoord.x;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord) * _Color; // multiply by _Color
return col;
}
ENDCG
}
}
}
I added this shader to my cube's material But it's not invisible at all not even close.
Sorry for the confusion.
The shader that I need is not to be based on distance from the camera but to be based on fade in/out effect so it will change between visible/invisible slowly depending on speed value.
Update:
This script seems to be working on Standard shader but for some reason it's not fading smooth it's fading a bit each time but not smooth flow. Why is that ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InsideVisible : MonoBehaviour
{
private void Start()
{
Coroutine _currentFade = StartCoroutine(FadeTo(0f, 10.0f));
}
IEnumerator FadeTo(float aValue, float aTime)
{
float alpha = transform.GetComponent<Renderer>().material.color.a;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
{
Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha, aValue, t));
transform.GetComponent<Renderer>().material.color = newColor;
yield return null;
}
}
}