Need help to combine shaders, is this possible?
I am horrible at shaders and need some help.
I am making a golf game that uses a grid shader and height color shader. I can switch between them with no problems, but... IS THERE ANY WAY TO COMBINE THEM? I have tried, with no success... Any help / guidance would be great.
Thanks and here are the shaders:
Shader "Custom/HeightDependentTint"
{
Properties
{
_MainTex("Base (RGB)", 2D) = "white" {}
_HeightMin("Height Min", Float) = -1
_HeightMax("Height Max", Float) = 1
_ColorMin("Tint Color At Min", Color) = (0,0,0,1)
_ColorMax("Tint Color At Max", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _ColorMin;
fixed4 _ColorMax;
float _HeightMin;
float _HeightMax;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
void surf(Input IN, inout SurfaceOutput o)
{
half4 c = tex2D(_MainTex, IN.uv_MainTex);
float h = (_HeightMax - IN.worldPos.y) / (_HeightMax - _HeightMin);
fixed4 tintColor = lerp(_ColorMax.rgba, _ColorMin.rgba, h);
o.Albedo = c.rgb * tintColor.rgb;
o.Alpha = c.a * tintColor.a;
}
ENDCG
}
Fallback "Diffuse"
}
AND
Shader "Custom/Grid_Global"
{
Properties
{
_BaseColor("BaseColor", Color) = (0.06228374,0.8320726,0.9411765,0)
_Grid("Grid", 2D) = "white" {}
_GridScale("GridScale", Float) = 5
_Falloff("Falloff", Float) = 50
_OverlayAmount("OverlayAmount", Range( 0 , 1)) = 1
[HideInInspector] __dirty( "", Int ) = 1
}
SubShader
{
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry+0" }
Cull Back
CGINCLUDE
#include "UnityPBSLighting.cginc"
#include "Lighting.cginc"
#pragma target 3.0
#ifdef UNITY_PASS_SHADOWCASTER
#undef INTERNAL_DATA
#undef WorldReflectionVector
#undef WorldNormalVector
#define INTERNAL_DATA half3 internalSurfaceTtoW0; half3 internalSurfaceTtoW1; half3 internalSurfaceTtoW2;
#define WorldReflectionVector(data,normal) reflect (data.worldRefl, half3(dot(data.internalSurfaceTtoW0,normal), dot(data.internalSurfaceTtoW1,normal), dot(data.internalSurfaceTtoW2,normal)))
#define WorldNormalVector(data,normal) half3(dot(data.internalSurfaceTtoW0,normal), dot(data.internalSurfaceTtoW1,normal), dot(data.internalSurfaceTtoW2,normal))
#endif
struct Input
{
float3 worldPos;
float3 worldNormal;
INTERNAL_DATA
};
uniform float4 _BaseColor;
uniform sampler2D _Grid;
uniform float _GridScale;
uniform float _Falloff;
uniform float _OverlayAmount;
inline float4 TriplanarSamplingCF( sampler2D topTexMap, sampler2D midTexMap, sampler2D botTexMap, float3 worldPos, float3 worldNormal, float falloff, float tilling, float3 normalScale, float3 index )
{
float3 projNormal = ( pow( abs( worldNormal ), falloff ) );
projNormal /= projNormal.x + projNormal.y + projNormal.z;
float3 nsign = sign( worldNormal );
float negProjNormalY = max( 0, projNormal.y * -nsign.y );
projNormal.y = max( 0, projNormal.y * nsign.y );
half4 xNorm; half4 yNorm; half4 yNormN; half4 zNorm;
xNorm = ( tex2D( midTexMap, tilling * worldPos.zy * float2( nsign.x, 1.0 ) ) );
yNorm = ( tex2D( topTexMap, tilling * worldPos.xz * float2( nsign.y, 1.0 ) ) );
yNormN = ( tex2D( botTexMap, tilling * worldPos.xz * float2( nsign.y, 1.0 ) ) );
zNorm = ( tex2D( midTexMap, tilling * worldPos.xy * float2( -nsign.z, 1.0 ) ) );
return xNorm * projNormal.x + yNorm * projNormal.y + yNormN * negProjNormalY + zNorm * projNormal.z;
}
void surf( Input i , inout SurfaceOutputStandard o )
{
o.Normal = float3(0,0,1);
float3 ase_worldPos = i.worldPos;
float3 ase_worldNormal = WorldNormalVector( i, float3( 0, 0, 1 ) );
float4 triplanar6 = TriplanarSamplingCF( _Grid, _Grid, _Grid, ase_worldPos, ase_worldNormal, _Falloff, _GridScale, float3( 1,1,1 ), float3(0,0,0) );
float4 lerpResult15 = lerp( float4(1,1,1,0) , triplanar6 , _OverlayAmount);
o.Albedo = ( _BaseColor * lerpResult15 ).rgb;
o.Alpha = 1;
}
ENDCG
CGPROGRAM
#pragma surface surf Standard keepalpha fullforwardshadows
ENDCG
Pass
{
Name "ShadowCaster"
Tags{ "LightMode" = "ShadowCaster" }
ZWrite On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#pragma multi_compile_shadowcaster
#pragma multi_compile UNITY_PASS_SHADOWCASTER
#pragma skip_variants FOG_LINEAR FOG_EXP FOG_EXP2
#include "HLSLSupport.cginc"
#if ( SHADER_API_D3D11 || SHADER_API_GLCORE || SHADER_API_GLES3 || SHADER_API_METAL || SHADER_API_VULKAN )
#define CAN_SKIP_VPOS
#endif
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "UnityPBSLighting.cginc"
struct v2f
{
V2F_SHADOW_CASTER;
float4 tSpace0 : TEXCOORD1;
float4 tSpace1 : TEXCOORD2;
float4 tSpace2 : TEXCOORD3;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
v2f vert( appdata_full v )
{
v2f o;
UNITY_SETUP_INSTANCE_ID( v );
UNITY_INITIALIZE_OUTPUT( v2f, o );
UNITY_TRANSFER_INSTANCE_ID( v, o );
float3 worldPos = mul( unity_ObjectToWorld, v.vertex ).xyz;
half3 worldNormal = UnityObjectToWorldNormal( v.normal );
half3 worldTangent = UnityObjectToWorldDir( v.tangent.xyz );
half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
half3 worldBinormal = cross( worldNormal, worldTangent ) * tangentSign;
o.tSpace0 = float4( worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x );
o.tSpace1 = float4( worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y );
o.tSpace2 = float4( worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z );
TRANSFER_SHADOW_CASTER_NORMALOFFSET( o )
return o;
}
half4 frag( v2f IN
#if !defined( CAN_SKIP_VPOS )
, UNITY_VPOS_TYPE vpos : VPOS
#endif
) : SV_Target
{
UNITY_SETUP_INSTANCE_ID( IN );
Input surfIN;
UNITY_INITIALIZE_OUTPUT( Input, surfIN );
float3 worldPos = float3( IN.tSpace0.w, IN.tSpace1.w, IN.tSpace2.w );
half3 worldViewDir = normalize( UnityWorldSpaceViewDir( worldPos ) );
surfIN.worldPos = worldPos;
surfIN.worldNormal = float3( IN.tSpace0.z, IN.tSpace1.z, IN.tSpace2.z );
surfIN.internalSurfaceTtoW0 = IN.tSpace0.xyz;
surfIN.internalSurfaceTtoW1 = IN.tSpace1.xyz;
surfIN.internalSurfaceTtoW2 = IN.tSpace2.xyz;
SurfaceOutputStandard o;
UNITY_INITIALIZE_OUTPUT( SurfaceOutputStandard, o )
surf( surfIN, o );
#if defined( CAN_SKIP_VPOS )
float2 vpos = IN.pos;
#endif
SHADOW_CASTER_FRAGMENT( IN )
}
ENDCG
}
}
Fallback "Diffuse"
}
Comment
Your answer
Follow this Question
Related Questions
Unity2D: Remove Shader Colour 0 Answers
Using Texture as HDR color? 2 Answers
Solid color texture or adding a color property to shader? 1 Answer
Tint Multiple Textures Separately 1 Answer
shader graph messes up the texture (2D) 0 Answers