我在Unity中花费了很多时间将两个着色器组合为一个,但没有任何效果。从未遇到过Cg / HLSL
在Unity项目中,我想将两个着色器组合到一个着色器中,以获取它们的全部功能(第二个着色器的雾化除外)。 第一个着色器用于消除透明的照明和纹理颜色,另一个着色器用于水下苛性和雾。我该如何结合?
第一个着色器
Shader "Nature/Tree Soft Occlusion Leaves" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Main Texture", 2D) = "white" { }
_Cutoff ("Alpha cutoff", Range(0.25,0.9)) = 0.5
_BaseLight ("Base Light", Range(0, 1)) = 0.35
_AO ("Amb. Occlusion", Range(0, 10)) = 2.4
_Occlusion ("Dir Occlusion", Range(0, 20)) = 7.5
// These are here only to provide default values
[HideInInspector] _TreeInstanceColor ("TreeInstanceColor", Vector) = (1,1,1,1)
[HideInInspector] _TreeInstanceScale ("TreeInstanceScale", Vector) = (1,1,1,1)
[HideInInspector] _SquashAmount ("Squash", Float) = 1
}
SubShader {
Tags {
"Queue" = "AlphaTest"
"IgnoreProjector"="True"
"RenderType" = "TreeTransparentCutout"
"DisableBatching"="True"
}
Cull Off
ColorMask RGB
Pass {
Lighting On
CGPROGRAM
#pragma vertex leaves
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityBuiltin2xTreeLibrary.cginc"
sampler2D _MainTex;
fixed _Cutoff;
fixed4 frag(v2f input) : SV_Target
{
fixed4 c = tex2D( _MainTex, input.uv.xy);
c.rgb *= input.color.rgb;
clip (c.a - _Cutoff);
UNITY_APPLY_FOG(input.fogCoord, c);
return c;
}
ENDCG
}
Pass {
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#include "UnityCG.cginc"
#include "TerrainEngine.cginc"
struct v2f {
V2F_SHADOW_CASTER;
float2 uv : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO
};
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
v2f vert( appdata v )
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
TerrainAnimateTree(v.vertex, v.color.w);
TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
o.uv = v.texcoord;
return o;
}
sampler2D _MainTex;
fixed _Cutoff;
float4 frag( v2f i ) : SV_Target
{
fixed4 texcol = tex2D( _MainTex, i.uv );
clip( texcol.a - _Cutoff );
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
// This subshader is never actually used, but is only kept so
// that the tree mesh still assumes that normals are needed
// at build time (due to Lighting On in the pass). The subshader
// above does not actually use normals, so they are stripped out.
// We want to keep normals for backwards compatibility with Unity 4.2
// and earlier.
SubShader {
Tags {
"Queue" = "AlphaTest"
"IgnoreProjector"="True"
"RenderType" = "TransparentCutout"
}
Cull Off
ColorMask RGB
Pass {
Tags { "LightMode" = "Vertex" }
AlphaTest GEqual [_Cutoff]
Lighting On
Material {
Diffuse [_Color]
Ambient [_Color]
}
SetTexture [_MainTex] { combine primary * texture DOUBLE, texture }
}
}
Dependency "BillboardShader" = "Hidden/Nature/Tree Soft Occlusion Leaves Rendertex"
Fallback Off}
第二个着色器
Shader "Underwater/Mobile"{
Properties
{
_MainTex("Main Texture", 2D) = "white" {}
[Header(Caustics)]
_Caustics("Caustics Texture (RGBA)", 2D) = "white" {}
_CausticsCoord("Tiling(XY) Offset(ZW) - Overrides texture coords", Vector) = (0.5,0.5,0,0)
_CausticsSpeed("Speed", Float) = 1
_CausticsBoost("Boost", Range(0,1)) = 0
_CausticsIntensity0("Intensity A", Range(0, 1)) = 1
_CausticsIntensity1("Intensity B", Range(0, 1)) = 0
_CausticsPosition0("Position A (World Y)", Float) = 2
_CausticsPosition1("Position B (World Y)", Float) = 4
[Header(Fog)]
_FogColor0("Color A", Color) = (0.004, 0.271, 0.302, 1)
_FogColor1("Color B", Color) = (0.004, 0.271, 0.302, 1)
_FogIntensity0("Intensity A", Range(0, 1)) = 1
_FogIntensity1("Intensity B", Range(0, 1)) = 1
_FogPosition0("Position A (World Y)", Float) = 0
_FogPosition1("Position B (World Y)", Float) = 6
_FogStart("Start", Float) = 0
_FogEnd("End", Float) = 15
[HideInInspector]
_Animation("Animation", Float) = 0
}
SubShader
{
Tags{ "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf CustomLambert nofog noambient noforwardadd exclude_path:deferred exclude_path:prepass
#pragma multi_compile SHADER_CONTROL SCRIPT_CONTROL
#pragma target 3.0
sampler2D _MainTex;
sampler2D _Caustics;
float4 _CausticsCoord;
fixed _CausticsSpeed;
fixed _CausticsBoost;
fixed _CausticsIntensity0;
fixed _CausticsIntensity1;
fixed _CausticsPosition0;
fixed _CausticsPosition1;
fixed3 _FogColor0;
fixed3 _FogColor1;
uniform fixed _FogIntensity0;
uniform fixed _FogIntensity1;
uniform fixed _FogPosition0;
uniform fixed _FogPosition1;
uniform half _FogStart;
uniform half _FogEnd;
#ifdef SCRIPT_CONTROL
fixed _Animation;
#endif
struct SurfaceOutputCustom
{
fixed3 Albedo;
fixed3 Normal;
fixed3 Emission;
half Specular;
fixed Gloss;
fixed Alpha;
fixed3 worldPos;
fixed3 worldNormal;
};
half4 LightingCustomLambert( SurfaceOutputCustom s, half3 lightDir, half atten )
{
half diffuseReflection = dot(s.Normal, lightDir);
float dist = distance(_WorldSpaceCameraPos, s.worldPos);
fixed mixed = clamp((s.worldPos.y - _FogPosition0) / (_FogPosition1 - _FogPosition0), 0, 1);
fixed3 fogColor = lerp(_FogColor0 * _FogIntensity0, _FogColor1 * _FogIntensity1, mixed);
mixed = lerp(_FogIntensity0, _FogIntensity1, mixed);
mixed = saturate((_FogStart - dist) / (_FogStart - _FogEnd)) * mixed;
#ifdef DIRECTIONAL
fogColor = fogColor * mixed;
#else
fogColor = fogColor * mixed * atten;
#endif
fixed lightColor = diffuseReflection * atten * (1 - mixed);
#ifdef SCRIPT_CONTROL
fixed4 caustics0 = tex2D(_Caustics, s.worldPos.zx * _CausticsCoord.xy + float2(_Animation, 0));
fixed4 caustics1 = tex2D(_Caustics, s.worldPos.zx * _CausticsCoord.xy + float2(-_Animation, 0));
fixed4 caustics2 = tex2D(_Caustics, s.worldPos.zx * _CausticsCoord.xy + float2(0, _Animation));
fixed4 caustics3 = tex2D(_Caustics, s.worldPos.zx * _CausticsCoord.xy + float2(0, -_Animation));
#else
fixed4 caustics0 = tex2D(_Caustics, s.worldPos.zx * _CausticsCoord.xy + float2(_CausticsCoord.z + (_Time.x * _CausticsSpeed), _CausticsCoord.w));
fixed4 caustics1 = tex2D(_Caustics, s.worldPos.zx * _CausticsCoord.xy + float2(_CausticsCoord.z - (_Time.x * _CausticsSpeed), _CausticsCoord.w));
fixed4 caustics2 = tex2D(_Caustics, s.worldPos.zx * _CausticsCoord.xy + float2(_CausticsCoord.z, _CausticsCoord.w + (_Time.x * _CausticsSpeed)));
fixed4 caustics3 = tex2D(_Caustics, s.worldPos.zx * _CausticsCoord.xy + float2(_CausticsCoord.z, _CausticsCoord.w - (_Time.x * _CausticsSpeed)));
#endif
fixed3 caustics = ((saturate(caustics0.r + caustics1.g + caustics2.b + caustics3.a) * (_CausticsBoost + 1)) + _CausticsBoost);
fixed causticsIntensity = clamp((s.worldPos.y - _CausticsPosition0) / (_CausticsPosition1 - _CausticsPosition0), 0, 1);
causticsIntensity = lerp(_CausticsIntensity0, _CausticsIntensity1, causticsIntensity);
fixed3 light = lightColor * _LightColor0;
#ifdef DIRECTIONAL
fixed3 ambient = ShadeSH9(half4(s.worldNormal, 1)) * (1 - mixed);
light = lerp(light, light * caustics, causticsIntensity);
#else
fixed3 ambient = (ShadeSH9(half4(s.worldNormal, 1)) * (1 - mixed)) * atten;
#endif
ambient = lerp(ambient, ambient * caustics, causticsIntensity);
half4 c;
c.rgb = s.Albedo * ( light + ambient ) + fogColor;
c.a = s.Alpha;
return c;
}
struct Input
{
float2 uv_MainTex;
float3 worldPos;
float3 worldNormal;
};
void surf( Input IN, inout SurfaceOutputCustom o)
{
o.worldPos = IN.worldPos;
o.worldNormal = IN.worldNormal;
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
FallBack "Underwater/Mobile Fast"}
答案 0 :(得分:0)
它可能提供了另一种选择:如果要将两个材质球都放在同一个对象上,而不必合并它们,可以做的一件事就是将2种材质分配给同一渲染器。
为此,进入“ MeshRenderer”组件,将材料数量增加到2,然后应用第二个材料。
不太确定性能成本是多少,但是仍然可以帮上忙。