- Home /
Adding alpha to shader to control opacity of VideoPlayer for 360 video?
Hello!
I am working on a project focused around 360 video and the new VideoPlayer API. Right now I am implementing a crossfade between videos, but the first step is to simply fade in an out a sphere with a VideoPlayer on it.
I wrote a test script to make sure my fade code is working correctly:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Video;
using UnityEngine;
public class TestFade : MonoBehaviour {
[SerializeField] private GameObject m_VideoObject;
float fadeSpeed = 30f;
CanvasGroup sphereCanvas;
void Start () {
}
void Update () {
if (Input.GetKey("up")) {
StartCoroutine(FadeIn(m_VideoObject));
}
if (Input.GetKey("down"))
{
StartCoroutine(FadeOut(m_VideoObject));
}
}
public IEnumerator FadeIn(GameObject currentSphere)
{
Renderer currentRenderer = currentSphere.GetComponent<Renderer>();
while (currentRenderer.material.color.a < 1f)
{
currentRenderer.material.color = new Color(currentRenderer.material.color.r, currentRenderer.material.color.g, currentRenderer.material.color.b, currentRenderer.material.color.a + (Time.deltaTime / fadeSpeed));
yield return null;
}
yield return null;
}
public IEnumerator FadeOut(GameObject currentSphere)
{
Renderer currentRenderer = currentSphere.GetComponent<Renderer>();
while (currentRenderer.material.color.a > 0.0f)
{
currentRenderer.material.color = new Color(currentRenderer.material.color.r, currentRenderer.material.color.g, currentRenderer.material.color.b, currentRenderer.material.color.a - (Time.deltaTime / fadeSpeed));
yield return null;
}
yield return null;
}
}
This works with any shader that has a Color property with alpha.
Now, I have a shader that correctly plays the video on the inside of the sphere and flips the image. Unfortunately the shader does not have the _Color property it needs.
Here is my current Equirectangular shader:
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "custom/Equirectangular"
{
Properties{
_MainTex("Base (RGB)", 2D) = "white" {}
}
SubShader{
Tags{ "RenderType" = "Opaque" }
Cull front // ADDED BY BERNIE, TO FLIP THE SURFACES
LOD 100
//#pragma surface surf Lambert
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#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;
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(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);
return col;
}
ENDCG
}
}
}
I have done lots of research and I know that I need to add the _Color property as well as changing the RenderType to transparent but I cannot make sense of most shader code. Even though individual parts are becoming clear the structure is still confusing.
Please help me add the needed properties to this shader. Any help is much appreciated!
Answer by christiaanbrant · Sep 07, 2018 at 03:10 PM
You can use this shader:
Shader "Custom/InverseSphereShader" {
Properties
{
_Blend("Blend", Range(0, 1)) = 0.5
_MainTex("Frist texture", 2D) = "black" { }
_BlendTex("Second texture", 2D) = "black" { }
}
Category
{
Lighting Off
ZWrite Off
Cull Front
Blend SrcAlpha OneMinusSrcAlpha
SubShader
{
Pass
{
SetTexture[_MainTex]
SetTexture[_BlendTex]
{
constantColor(0, 0, 0, [_Blend])
combine texture lerp(constant) previous
}
}
}
}
}
With this shader you can blend between 2 textures or just keep the second blank to have an alpha slider.
You can change the slider in c# using the following code:
this.gameObject.GetComponent<Renderer>().material.SetFloat("_Blend", [THE VALUE YOU WANT]);
.