具有自定义着色器的对象在光照贴图时会投射灰度阴影
Objects with custom shaders cast grayscale shadows when lightmapped
Unity 5 似乎能够计算来自标准着色器和传统着色器的彩色阴影。我应该怎么做才能让我的 自定义着色器 投射彩色阴影?
我的着色器代码:
Shader "Opaque/Lightmap"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Tags { "Queue" = "Geometry" "RenderType" = "Opaque" }
LOD 100
Pass
{
Name "FORWARD"
Tags { "LIGHTMODE"="ForwardBase" "SHADOWSUPPORT"="false" "RenderType"="Opaque" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma exclude_renderers d3d11_9x d3d11
#pragma multi_compile_fog
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#include "UnityCG.cginc"
sampler2D _MainTex;
struct appdata
{
float4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
half2 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
half2 lightmapuv : TEXCOORD1;
UNITY_FOG_COORDS(2)
};
v2f vert (appdata v)
{
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.uv = v.texcoord;
o.lightmapuv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
fixed4 frag (v2f i) : COLOR
{
fixed4 color = tex2D(_MainTex, i.uv);
#ifndef LIGHTMAP_OFF
fixed3 lightMap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lightmapuv));
color.rgb *= lightMap;
#endif
UNITY_APPLY_FOG(i.fogCoord, color);
return color;
}
ENDCG
}
}
}
解决方案是将 UsePass "Mobile/Diffuse/META"
添加到 光照贴图着色器的所有 LOD。
这是我的着色器的固定版本:
Shader "Opaque/Lightmap"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Tags { "Queue" = "Geometry" "RenderType" = "Opaque"}
LOD 100
Pass
{
Name "FORWARD"
Tags { "LIGHTMODE"="ForwardBase" "SHADOWSUPPORT"="false" "RenderType"="Opaque" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma exclude_renderers d3d11_9x d3d11
#pragma multi_compile_fog
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#include "UnityCG.cginc"
sampler2D _MainTex;
struct appdata
{
float4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
half2 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
half2 lightmapuv : TEXCOORD1;
UNITY_FOG_COORDS(2)
};
v2f vert (appdata v)
{
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.uv = v.texcoord;
o.lightmapuv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
fixed4 frag (v2f i) : COLOR
{
fixed4 color = tex2D(_MainTex, i.uv);
#ifndef LIGHTMAP_OFF
fixed3 lightMap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lightmapuv));
color.rgb *= lightMap;
#endif
UNITY_APPLY_FOG(i.fogCoord, color);
return color;
}
ENDCG
}
// to fix diffuse component bounce in the lightmaps
UsePass "Mobile/Diffuse/META"
}
}
感谢 Michael Steliaros 的帮助。
Unity 5 似乎能够计算来自标准着色器和传统着色器的彩色阴影。我应该怎么做才能让我的 自定义着色器 投射彩色阴影?
我的着色器代码:
Shader "Opaque/Lightmap"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Tags { "Queue" = "Geometry" "RenderType" = "Opaque" }
LOD 100
Pass
{
Name "FORWARD"
Tags { "LIGHTMODE"="ForwardBase" "SHADOWSUPPORT"="false" "RenderType"="Opaque" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma exclude_renderers d3d11_9x d3d11
#pragma multi_compile_fog
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#include "UnityCG.cginc"
sampler2D _MainTex;
struct appdata
{
float4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
half2 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
half2 lightmapuv : TEXCOORD1;
UNITY_FOG_COORDS(2)
};
v2f vert (appdata v)
{
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.uv = v.texcoord;
o.lightmapuv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
fixed4 frag (v2f i) : COLOR
{
fixed4 color = tex2D(_MainTex, i.uv);
#ifndef LIGHTMAP_OFF
fixed3 lightMap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lightmapuv));
color.rgb *= lightMap;
#endif
UNITY_APPLY_FOG(i.fogCoord, color);
return color;
}
ENDCG
}
}
}
解决方案是将 UsePass "Mobile/Diffuse/META"
添加到 光照贴图着色器的所有 LOD。
这是我的着色器的固定版本:
Shader "Opaque/Lightmap"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Tags { "Queue" = "Geometry" "RenderType" = "Opaque"}
LOD 100
Pass
{
Name "FORWARD"
Tags { "LIGHTMODE"="ForwardBase" "SHADOWSUPPORT"="false" "RenderType"="Opaque" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma exclude_renderers d3d11_9x d3d11
#pragma multi_compile_fog
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#include "UnityCG.cginc"
sampler2D _MainTex;
struct appdata
{
float4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
half2 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
half2 lightmapuv : TEXCOORD1;
UNITY_FOG_COORDS(2)
};
v2f vert (appdata v)
{
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.uv = v.texcoord;
o.lightmapuv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
fixed4 frag (v2f i) : COLOR
{
fixed4 color = tex2D(_MainTex, i.uv);
#ifndef LIGHTMAP_OFF
fixed3 lightMap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lightmapuv));
color.rgb *= lightMap;
#endif
UNITY_APPLY_FOG(i.fogCoord, color);
return color;
}
ENDCG
}
// to fix diffuse component bounce in the lightmaps
UsePass "Mobile/Diffuse/META"
}
}
感谢 Michael Steliaros 的帮助。