- Home /
using two shaders together
Hi. I created my own vertex shader that manipulates the vertex position with a function of two variables. I attached it to a material and placed it as the second material on the object that already has the default-diffuse material. The result is this criss-cross of color that shifts as the camera move.
What should I do? can i "direct" the fragment part of my shader to be the diffuse one? is there a way i can "turn off" my fragment shader so there won't be two active ones?
Here is the shader:
Shader "Custom/ShockwaveShader" { Properties { _TimeFromExp("TimeSinceExplosion", float) = 1 _ExplosionPos("ExplosionPosition", Vector) = (0,0,0) }
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _TimeFromExp;
float4 _ExplosionPos;
struct v2f {
float4 pos: SV_POSITION;
float3 color: COLOR0;
};
float dist(float3 a, float3 b) {
return sqrt( (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z) );
}
v2f vert (appdata_full v)
{
v2f o;
float dis = dist(v.vertex, _ExplosionPos);
float4 norm = normalize(v.vertex - _ExplosionPos);
float funcVal = exp(-pow((dis/5 - _TimeFromExp),2));
float4 ver = v.vertex + norm*funcVal;
o.pos = mul(UNITY_MATRIX_MVP, ver);
o.color = v.color;
return o;
}
half4 frag (v2f i) : COLOR
{
return half4(i.color, 1);
}
ENDCG
}
}
Fallback "VertexLit"
}
Answer by electricsauce · Mar 04, 2013 at 02:50 PM
I think you should only be using one material at a time for the same uv mapped space. If you need a diffuse texture just add it to your shockwave shader and diable/remove the default diffuse shader.
Cool. How can I use a diffuse fragment shader without implementing one in my shader? I know there is the FallBack option, but that is for compatibility issues. Is there a "for this, go use that ins$$anonymous$$d" option?
Thanks.
I'm pretty sure I butchered your code, but try this:
Shader "Custom/ShockwaveShader" {
Properties {
_$$anonymous$$ainTex ("Base (RGB)", 2D) = "white" {}
_TimeFromExp("TimeSinceExplosion", float) = 1
_ExplosionPos("ExplosionPosition", Vector) = (0,0,0) }
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Pass {
Cull Back
Lighting On
CGPROGRA$$anonymous$$
// Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct v2f members uv)
#pragma exclude_renderers d3d11 xbox360
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _TimeFromExp;
float4 _ExplosionPos;
sampler2D _$$anonymous$$ainTex;
float4 _$$anonymous$$ainTex_ST;
struct v2f {
float4 pos: SV_POSITION;
float2 uv;
float3 color: COLOR0;
};
float dist(float3 a, float3 b) {
return sqrt( (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z) );
}
v2f vert (appdata_full v)
{
v2f o;
float dis = dist(v.vertex, _ExplosionPos);
float4 norm = normalize(v.vertex - _ExplosionPos);
float funcVal = exp(-pow((dis/5 - _TimeFromExp),2));
float4 ver = v.vertex + norm*funcVal;
o.uv = TRANSFOR$$anonymous$$_TEX (v.texcoord, _$$anonymous$$ainTex);
o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, ver);
o.color = v.color;
return o;
}
float4 frag(v2f i) : COLOR
{
float4 c = tex2D (_$$anonymous$$ainTex, i.uv);
c.rgb = c.rgb * i.color * 2;
return c;
}
ENDCG
}
}
Fallback "VertexLit"
}