Friday, August 3, 2018

unity - How can I make a shader that will make a cube or any object invisible?


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.


Invi


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().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().material.color = newColor;
yield return null;
}
}

}

Answer



I've verified that the shader in the question works just fine for creating transparency.


For the fade script, it looks like you want something more like this:


// Local storage for the material instance we're modifying.
Material _materialCopy;

IEnumerator FadeTo(float endAlpha, float durationSeconds)
{
// Cache the material so we don't need to re-get it all the time,

// and can clean it up when we're done with it.
if(_materialCopy == null)
_materialCopy = GetComponent().material;

// Record the starting colour so we can preserve it.
Color color = _materialCopy.color;
// Record the starting alpha so we know where we're fading from.
float startAlpha = color.a;

for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / durationSeconds)

{
// Lerp between start and end, not between current and end.
color.a = Mathf.Lerp(startAlpha, endAlpha, t);
_materialCopy.color = color;
yield return null;
}

// Ensure we end exactly where we want, not off by any fraction.
color.a = endAlpha;
_materialCopy.color = color;

}

// Add a method to clean up our local material copy when we're done with it,
// so it doesn't clutter up memory until the next scene change.
void OnDestroy()
{
if(_materialCopy != null)
Destroy(_materialCopy);
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...