Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Telexister · Jun 10, 2017 at 07:58 AM · vrshadersvideovideotexture

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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]);

.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using video render texture as input in shader graph. Texture frames not updating. 0 Answers

Multiple 360 VR video player in Unity 0 Answers

Video textures appearing white in Samsung Gear Vr Android unity 0 Answers

Is it feasible to play 4K video in Unity? 8 Answers

6k video player 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges