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
}
}
}
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)));
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"
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612164128im_/https://answers.unity.com/themes/thub/images/avi.jpg)