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 /
  • Help Room /
avatar image
0
Question by brinkerink · Jun 05, 2018 at 08:09 AM · shadercubemapblend

Combining two shaders into one

I know there is already questions about this, but I can't seem to get this woking. Combining the functions fails for me every time. Some insights are greatly appreciated!

its for a VR app.

Shader 1(maps texture to inside of cube):

Shader "Unlit/CubeMappy" { Properties { _MainTex("MainTex", 2D) = "black" {} _MainTex2("MainTex2", 2D) = "black" {}
_SecondTex ("SecondTexture", 2D) = "white" {} _DisolveTexture("Disolve Texture", 2D) = "white" {} _Blend("Blend Amount", Range(0,1)) = 1

     _Yaw ("Yaw", Float ) = 0
     [MaterialToggle] _Stereoscopic ("Stereoscopic", Float ) = 0
 }
 SubShader {
     Tags { "RenderType"="Opaque" }
     LOD 100
     Pass {
         Name "FORWARD"
         Cull Front
         ZWrite Off
         
         CGPROGRAM
         
         #pragma vertex vert        
         #pragma fragment frag    
          
         #include "UnityCG.cginc"

          struct appdata
           {
               float4 vertex : POSITION;
               float2 uv : TEXCOORD0;
           };
  
           struct v2f
           {
               float2 uv : TEXCOORD0;
               float2 uv2 : TEXCOORD1;
               float4 vertex : SV_POSITION;
           };            
         
         struct VertexInput {
             float4 vertex : POSITION;
         };

         struct VertexOutput {
             float4 pos : SV_POSITION;
             float4 posWorld : TEXCOORD0;
         };
                     
             uniform sampler2D _MainTex; uniform float4 _MainTex_ST;        
             
             sampler2D _MainTex2;
             sampler2D _SecondTex;
             float4 _MainTex2_ST;
             float4 _SecondTex_ST;
                          
         uniform float _Yaw;
         uniform fixed _Stereoscopic;

         VertexOutput vert (VertexInput v) {
             VertexOutput o = (VertexOutput)0;
             o.posWorld = mul(unity_ObjectToWorld, v.vertex); 
             o.pos = UnityObjectToClipPos(v.vertex); 
             return o;
         }
         
         //
         
         v2f vert (appdata v)
           {
               v2f o;
               o.vertex = UnityObjectToClipPos(v.vertex);
               o.uv = TRANSFORM_TEX(v.uv, _MainTex2);
               o.uv2 = TRANSFORM_TEX(v.uv, _SecondTex);
               return o;
           }
           
           half _Blend;
           sampler2D _DisolveTexture;
           fixed4 frag (v2f i) : SV_Target
           {
               fixed4 texture1 = tex2D(_MainTex2,i.uv); 
               fixed4 texture2 = tex2D(_SecondTex,i.uv); 
               return lerp(texture1,texture2,saturate(sign(_Blend-tex2D(_DisolveTexture,i.uv).r)));
           }
           
           //
           
           float2 CubemapUV(float3 dir, float yaw, float stereoscopic)
         {
             yaw += 0.5;
             float gC = (acos(dir.g) / 3.141592654);
             float2 N = float2((((atan2(dir.r, dir.b) / 6.28318530718) + 0.5) + yaw), frac(gC));
             N.y *= -1;
             float2 offset = stereoscopic ? float2(0.0, lerp(0.0, 0.5, unity_StereoEyeIndex)) : float2(0, 0);
             float2 M = frac((offset + lerp(N, (N*float2(1, 0.5)), stereoscopic)) );
             return M;
         }  

         float4 frag(VertexOutput i) : COLOR {
             return tex2D(_MainTex, CubemapUV( normalize(i.posWorld.rgb) , _Yaw, _Stereoscopic));
         }    
         ENDCG
     }
 }
 FallBack "Diffuse"

}


Shader 2(Dissolves between two textures):

Shader "Unlit/Dissolve2Tex" { Properties { _MainTex ("Texture", 2D) = "white" {} _SecondTex ("SecondTexture", 2D) = "white" {} _DisolveTexture("Disolve Texture", 2D) = "white" {} _Blend("Blend Amount", Range(0,1)) = 1 }

   SubShader
   {
       Tags { "RenderType"="Opaque" }
       LOD 100        

       Pass
       {            
           CGPROGRAM
           #pragma vertex vert
           #pragma fragment frag
  
           #include "UnityCG.cginc"
  
           struct appdata
           {
               float4 vertex : POSITION;
               float2 uv : TEXCOORD0;
           };
  
           struct v2f
           {
               float2 uv : TEXCOORD0;
               float2 uv2 : TEXCOORD1;
               float4 vertex : SV_POSITION;
           };
 
           sampler2D _MainTex;
           sampler2D _SecondTex;
           float4 _MainTex_ST;
           float4 _SecondTex_ST;
           v2f vert (appdata v)
           {
               v2f o;
               o.vertex = UnityObjectToClipPos(v.vertex);
               o.uv = TRANSFORM_TEX(v.uv, _MainTex);
               o.uv2=TRANSFORM_TEX(v.uv, _SecondTex);
               return o;
           }
 
           half _Blend;
           sampler2D _DisolveTexture;
           fixed4 frag (v2f i) : SV_Target
           {
               fixed4 texture1 = tex2D(_MainTex,i.uv); 
               fixed4 texture2 = tex2D(_SecondTex,i.uv); 
               return lerp(texture1,texture2,saturate(sign(_Blend-tex2D(_DisolveTexture,i.uv).r)));
           }
           ENDCG
       }
   }

}

Comment
Add comment · Show 1
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
avatar image brinkerink · Jun 05, 2018 at 08:20 AM 0
Share

Currently I have it working like this:

The cube on the right is able to dissolve, but the texture is not mapped like it is in the left cube.

I think I need to find a way to combine the returns of both functions:

return tex2D(_$$anonymous$$ainTex, CubemapUV( normalize(i.posWorld.rgb) , _Yaw, _Stereoscopic));

and

return lerp(texture1,texture2,saturate(sign(_Blend-tex2D(_DisolveTexture,i.uv).r)));

alt text

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by brinkerink · Jun 05, 2018 at 01:38 PM

Got it working! heres the code:

Shader "Unlit/Dissolve2Tex" { Properties { _MainTex ("Texture", 2D) = "white" {} _SecondTex ("SecondTexture", 2D) = "white" {} _DisolveTexture("Disolve Texture", 2D) = "white" {} _Blend("Blend Amount", Range(0,1)) = 1

       _Yaw ("Yaw", Float ) = 0

     [MaterialToggle] _Stereoscopic ("Stereoscopic", Float ) = 0
   }
   SubShader
   {
       Tags { "RenderType"="Opaque" }
       LOD 100
  
       Pass
       {
           Cull Front
           ZWrite Off

           CGPROGRAM
           #pragma vertex vert
           #pragma fragment frag
  
           #include "UnityCG.cginc"
  
           struct appdata
           {
               float4 vertex : POSITION;
               float2 uv : TEXCOORD0;
           };
  
           struct v2f
           {
               float2 uv : TEXCOORD0;
               float2 uv2 : TEXCOORD1;
               float4 vertex : SV_POSITION;
               float4 posWorld : TEXCOORD2;
           };
 
           sampler2D _MainTex;
           sampler2D _SecondTex;
           float4 _MainTex_ST;
           float4 _SecondTex_ST;

           uniform float _Yaw;
         uniform fixed _Stereoscopic;

           float2 CubemapUV(float3 dir, float yaw, float stereoscopic)
         {
             yaw += 0.5;
             float gC = (acos(dir.g) / 3.141592654);
             float2 N = float2((((atan2(dir.r, dir.b) / 6.28318530718) + 0.5) + yaw), frac(gC));
             N.y *= -1;
             float2 offset = stereoscopic ? float2(0.0, lerp(0.0, 0.5, unity_StereoEyeIndex)) : float2(0, 0);
             float2 M = frac((offset + lerp(N, (N*float2(1, 0.5)), stereoscopic)) );
             return M;
         }

           v2f vert (appdata v)
           {
               v2f o;
               o.vertex = UnityObjectToClipPos(v.vertex);
               o.posWorld = mul(unity_ObjectToWorld, v.vertex); 
               o.uv = TRANSFORM_TEX(v.uv, _MainTex);
               o.uv2=TRANSFORM_TEX(v.uv, _SecondTex);
               return o;
           }
 
           half _Blend;
           sampler2D _DisolveTexture;
           fixed4 frag (v2f i) : SV_Target
           {
               fixed4 texture1 = tex2D(_MainTex,CubemapUV( normalize(i.posWorld.rgb) , _Yaw, _Stereoscopic)); 
               fixed4 texture2 = tex2D(_SecondTex,CubemapUV( normalize(i.posWorld.rgb) , _Yaw, _Stereoscopic)); 
               return lerp(texture1,texture2,saturate(sign(_Blend-tex2D(_DisolveTexture,CubemapUV( normalize(i.posWorld.rgb) , _Yaw, _Stereoscopic)).r)));
           }
           ENDCG
       }
   }
   FallBack "Diffuse"

}

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

195 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to merge video images to display on the cube, and the video images can flow from one side of the cube to another. 0 Answers

how to create a cubemap with latitude-longitude layout texture by script in runtime 0 Answers

How to make scroll or offset "Cubemap Reflection" in car paint shader? 0 Answers

Reflection (Cubemap) for shader 0 Answers

Why tilling property for Costume Shaders is not working ? 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