提交 dc6403ea 编写于 作者: timchen1002's avatar timchen1002

udpate

上级 85339df0
......@@ -12,6 +12,8 @@ Shader "Custom RP/Lit"
// 阴影模式
[KeywordEnum(On, Clip, Dither, Off)] _Shadows ("Shadows", Float) = 0
// 是否使用遮罩纹理
[Toggle(_MASK_MAP)] _MaskMapToggle ("Mask Map", Float) = 0
// 遮罩纹理
[NoScaleOffset] _MaskMap("Mask (MODS)", 2D) = "white" {}
// 金属度
......@@ -23,6 +25,8 @@ Shader "Custom RP/Lit"
// 菲涅尔反射强度
_Fresnel ("Fresnel", Range(0, 1)) = 1
// 是否使用法线纹理
[Toggle(_NORMAL_MAP)] _NormalMapToggle ("Normal Map", Float) = 0
// 法线贴图
[NoScaleOffset] _NormalMap("Normals", 2D) = "bump" {}
_NormalScale("Normal Scale", Range(0, 1)) = 1
......@@ -31,6 +35,8 @@ Shader "Custom RP/Lit"
[NoScaleOffset] _EmissionMap("Emission", 2D) = "white" {}
[HDR] _EmissionColor("Emission", Color) = (0.0, 0.0, 0.0, 0.0)
// 是否使用细节纹理
[Toggle(_DETAIL_MAP)] _DetailMapToggle ("Detail Maps", Float) = 0
// 细节纹理
_DetailMap("Details", 2D) = "linearGrey" {}
// 细节法线纹理
......@@ -79,6 +85,9 @@ Shader "Custom RP/Lit"
#pragma shader_feature _RECEIVE_SHADOWS
// 是否透明通道预乘
#pragma shader_feature _PREMULTIPLY_ALPHA
#pragma shader_feature _MASK_MAP
#pragma shader_feature _NORMAL_MAP
#pragma shader_feature _DETAIL_MAP
#pragma multi_compile _ _DIRECTIONAL_PCF3 _DIRECTIONAL_PCF5 _DIRECTIONAL_PCF7
#pragma multi_compile _ _CASCADE_BLEND_SOFT _CASCADE_BLEND_DITHER
#pragma multi_compile _ _SHADOW_MASK_ALWAYS _SHADOW_MASK_DISTANCE
......
......@@ -24,15 +24,29 @@ UNITY_INSTANCING_BUFFER_START(UnityPerMaterial)
UNITY_DEFINE_INSTANCED_PROP(float, _Fresnel)
UNITY_DEFINE_INSTANCED_PROP(float, _DetailAlbedo)
UNITY_DEFINE_INSTANCED_PROP(float, _DetailSmoothness)
UNITY_DEFINE_INSTANCED_PROP(float, _DetailNormalScale)
UNITY_DEFINE_INSTANCED_PROP(float, _DetailNormalScale)
UNITY_DEFINE_INSTANCED_PROP(float, _NormalScale)
UNITY_INSTANCING_BUFFER_END(UnityPerMaterial)
// 通过UNITY_ACCESS_INSTANCED_PROP访问material属性
#define INPUT_PROP(name) UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, name)
float4 GetMask(float2 baseUV)
struct InputConfig
{
return SAMPLE_TEXTURE2D(_MaskMap, sampler_BaseMap, baseUV);
float2 baseUV;
float2 detailUV;
bool useMask;
bool useDetail;
};
InputConfig GetInputConfig(float2 baseUV, float2 detailUV = 0.0)
{
InputConfig c;
c.baseUV = baseUV;
c.detailUV = detailUV;
c.useMask = false;
c.useDetail = false;
return c;
}
// 基本纹理UV变换
......@@ -50,79 +64,102 @@ float2 TransformDetailUV(float2 detailUV)
return detailUV * detailST.xy + detailST.zw;
}
float4 GetDetail(float2 detailUV)
float4 GetMask(InputConfig c)
{
float map = SAMPLE_TEXTURE2D(_DetailMap, sampler_DetailMap, detailUV);
return map * 2.0 - 1.0;
if (c.useMask)
{
return SAMPLE_TEXTURE2D(_MaskMap, sampler_BaseMap, c.baseUV);
}
return 1.0;
}
float4 GetBase(float2 baseUV, float2 detailUV = 0.0)
float4 GetDetail(InputConfig c)
{
float4 map = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, baseUV);
// 通过UNITY_ACCESS_INSTANCED_PROP访问material属性
float4 color = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _BaseColor);
float4 detail = GetDetail(detailUV).r * INPUT_PROP(_DetailAlbedo);
float mask = GetMask(baseUV).b;
map.rgb = lerp(sqrt(map.rgb), detail < 0.0 ? 0.0 : 1.0, abs(detail) * mask);
map.rgb *= map.rgb;
if (c.useDetail)
{
float map = SAMPLE_TEXTURE2D(_DetailMap, sampler_DetailMap, c.detailUV);
return map * 2.0 - 1.0;
}
return 0.0;
}
float4 GetBase(InputConfig c)
{
float4 map = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, c.baseUV);
float4 color = INPUT_PROP(_BaseColor);
if (c.useDetail)
{
float4 detail = GetDetail(c).r * INPUT_PROP(_DetailAlbedo);
float mask = GetMask(c).b;
map.rgb = lerp(sqrt(map.rgb), detail < 0.0 ? 0.0 : 1.0, abs(detail) * mask);
map.rgb *= map.rgb;
}
return map * color;
}
float3 GetEmission(float2 baseUV)
float3 GetEmission(InputConfig c)
{
float4 map = SAMPLE_TEXTURE2D(_EmissionMap, sampler_BaseMap, baseUV);
// 通过UNITY_ACCESS_INSTANCED_PROP访问material属性
float4 color = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _EmissionColor);
float4 map = SAMPLE_TEXTURE2D(_EmissionMap, sampler_BaseMap, c.baseUV);
float4 color = INPUT_PROP(_EmissionColor);
return map.rgb * color.rgb;
}
float GetCutoff(float2 baseUV)
float GetCutoff(InputConfig c)
{
// 透明度低于阈值的片元进行舍弃
return UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _Cutoff);
return INPUT_PROP(_Cutoff);
}
float GetMetallic(float2 baseUV)
float GetMetallic(InputConfig c)
{
float metallic = INPUT_PROP(_Metallic);
metallic *= GetMask(baseUV).r;
metallic *= GetMask(c).r;
return metallic;
}
float GetSmoothness(float2 baseUV, float2 detailUV = 0.0)
float GetSmoothness(InputConfig c)
{
float smoothness = INPUT_PROP(_Smoothness);
smoothness *= GetMask(baseUV).a;
float detail = GetDetail(detailUV).b * INPUT_PROP(_DetailSmoothness);
float mask = GetMask(baseUV).b;
smoothness = lerp(smoothness, detail < 0.0 ? 0.0 : 1.0, abs(detail) * mask);
smoothness *= GetMask(c).a;
if (c.useDetail)
{
float detail = GetDetail(c).b * INPUT_PROP(_DetailSmoothness);
float mask = GetMask(c).b;
smoothness = lerp(smoothness, detail < 0.0 ? 0.0 : 1.0, abs(detail) * mask);
}
return smoothness;
}
float GetFresnel(float2 baseUV)
float GetFresnel(InputConfig c)
{
return UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _Fresnel);
return INPUT_PROP(_Fresnel);
}
float GetOcclusion(float2 baseUV)
float GetOcclusion(InputConfig c)
{
float strength = INPUT_PROP(_Occlusion);
float occlusion = GetMask(baseUV).g;
float occlusion = GetMask(c).g;
occlusion = lerp(occlusion, 1.0, strength);
return occlusion;
}
// 采样法线并解码法线向量a
float3 GetNormalTS(float2 baseUV, float2 detailUV = 0.0)
float3 GetNormalTS(InputConfig c)
{
float4 map = SAMPLE_TEXTURE2D(_NormalMap, sampler_BaseMap, baseUV);
float4 map = SAMPLE_TEXTURE2D(_NormalMap, sampler_BaseMap, c.baseUV);
float scale = INPUT_PROP(_NormalScale);
float3 normal = DecodeNormal(map, scale);
map = SAMPLE_TEXTURE2D(_DetailNormalMap, sampler_DetailMap, detailUV);
scale = INPUT_PROP(_DetailNormalScale) * GetMask(baseUV).b;
float3 detail = DecodeNormal(map, scale);
normal = BlendNormalRNM(normal, detail);
if (c.useDetail)
{
map = SAMPLE_TEXTURE2D(_DetailNormalMap, sampler_DetailMap, c.detailUV);
scale = INPUT_PROP(_DetailNormalScale) * GetMask(c).b;
float3 detail = DecodeNormal(map, scale);
normal = BlendNormalRNM(normal, detail);
}
return normal;
}
......
......@@ -27,11 +27,15 @@ struct Varyings
float4 positionCS : SV_POSITION;
float3 positionWS: VAR_POSITION;
float2 baseUV : VAR_BASE_UV;
float2 detailUV : VAR_DETAIL_UV;
#if defined(_DETAIL_MAP)
float2 detailUV : VAR_DETAIL_UV;
#endif
// 世界法线
float3 normalWS : VAR_NORMAL;
// 世界空间的切线
float4 tangentWS : VAR_TANGENT;
#if defined(_NORMAL_MAP)
// 世界空间的切线
float4 tangentWS : VAR_TANGENT;
#endif
GI_ATTRIBUTE_DATA
UNITY_VERTEX_INPUT_INSTANCE_ID
};
......@@ -48,11 +52,15 @@ Varyings LitPassVertex(Attributes input)
output.positionCS = TransformWorldToHClip(output.positionWS);
// 计算世界空间的法线
output.normalWS = TransformObjectToWorldNormal(input.normalOS);
// 计算世界空间的切线
output.tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
#if defined(_NORMAL_MAP)
// 计算世界空间的切线
output.tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
#endif
// 计算缩放和偏移后的UV坐标
output.baseUV = TransformBaseUV(input.baseUV);
output.detailUV = TransformDetailUV(input.baseUV);
#if defined(_DETAIL_MAP)
output.detailUV = TransformDetailUV(input.baseUV);
#endif
return output;
}
......@@ -62,25 +70,39 @@ float4 LitPassFragment(Varyings input) : SV_TARGET
UNITY_SETUP_INSTANCE_ID(input);
ClipLOD(input.positionCS.xy, unity_LODFade.x);
float4 base = GetBase(input.baseUV, input.detailUV);
InputConfig config = GetInputConfig(input.baseUV);
#if defined(_MASK_MAP)
config.useMask = true;
#endif
#if defined(_DETAIL_MAP)
config.detailUV = input.detailUV;
config.useDetail = true;
#endif
float4 base = GetBase(config);
#if defined(_CLIPPING)
// 透明度低于阈值的片元进行舍弃
clip(base.a - GetCutoff(input.baseUV));
clip(base.a - GetCutoff(config));
#endif
// 定义一个surface并填充属性
Surface surface;
surface.position = input.positionWS;
surface.normal = NormalTangentToWorld(GetNormalTS(input.baseUV, input.detailUV), input.normalWS, input.tangentWS);
surface.interpolatedNormal = input.normalWS;
#if defined(_NORMAL_MAP)
surface.normal = NormalTangentToWorld(GetNormalTS(config), input.normalWS, input.tangentWS);
surface.interpolatedNormal = input.normalWS;
#else
surface.normal = normalize(input.normalWS);
surface.interpolatedNormal = surface.normal;
#endif
// 得到视角方向
surface.viewDirection = normalize(_WorldSpaceCameraPos - input.positionWS);
surface.depth = -TransformWorldToView(input.positionWS).z;
surface.color = base.rgb;
surface.alpha = base.a;
surface.metallic = GetMetallic(input.baseUV);
surface.smoothness = GetSmoothness(input.baseUV, input.detailUV);
surface.fresnelStrength = GetFresnel(input.baseUV);
surface.occlusion = GetOcclusion(input.baseUV);
surface.metallic = GetMetallic(config);
surface.smoothness = GetSmoothness(config);
surface.fresnelStrength = GetFresnel(config);
surface.occlusion = GetOcclusion(config);
// 计算抖动值
surface.dither = InterleavedGradientNoise(input.positionCS.xy, 0);
// 通过表面属性和BRDF计算最终光照结果
......@@ -92,7 +114,7 @@ float4 LitPassFragment(Varyings input) : SV_TARGET
// 获取全局照明数据
GI gi = GetGI(GI_FRAGMENT_DATA(input), surface, brdf);
float3 color = GetLighting(surface, brdf, gi);
color += GetEmission(input.baseUV);
color += GetEmission(config);
return float4(color, surface.alpha);
}
......
......@@ -33,12 +33,13 @@ Varyings MetaPassVertex(Attributes input)
float4 MetaPassFragment(Varyings input) : SV_TARGET
{
float4 base = GetBase(input.baseUV);
InputConfig config = GetInputConfig(input.baseUV);
float4 base = GetBase(config);
Surface surface;
ZERO_INITIALIZE(Surface, surface);
surface.color = base.rgb;
surface.metallic = GetMetallic(input.baseUV);
surface.smoothness = GetSmoothness(input.baseUV);
surface.metallic = GetMetallic(config);
surface.smoothness = GetSmoothness(config);
BRDF brdf = GetBRDF(surface);
float4 meta = 0.0;
if (unity_MetaFragmentControl.x)
......@@ -51,7 +52,7 @@ float4 MetaPassFragment(Varyings input) : SV_TARGET
}
else if (unity_MetaFragmentControl.y)
{
meta = float4(GetEmission(input.baseUV), 1.0);
meta = float4(GetEmission(config), 1.0);
}
return meta;
}
......
......@@ -42,10 +42,13 @@ Varyings ShadowCasterPassVertex(Attributes input)
void ShadowCasterPassFragment(Varyings input)
{
UNITY_SETUP_INSTANCE_ID(input);
float4 base = GetBase(input.baseUV);
ClipLOD(input.positionCS.xy, unity_LODFade.x);
InputConfig config = GetInputConfig(input.baseUV);
float4 base = GetBase(config);
#if defined(_SHADOWS_CLIP)
// 透明度低于阈值的片元进行舍弃
clip(base.a - GetCutoff(input.baseUV));
clip(base.a - GetCutoff(config));
#elif defined(_SHADOWS_DITHER)
// 计算抖动值
float dither = InterleavedGradientNoise(input.positionCS.xy, 0);
......
......@@ -10,39 +10,78 @@ UNITY_INSTANCING_BUFFER_START(UnityPerMaterial)
UNITY_DEFINE_INSTANCED_PROP(float, _Cutoff)
UNITY_INSTANCING_BUFFER_END(UnityPerMaterial)
// 通过UNITY_ACCESS_INSTANCED_PROP访问material属性
#define INPUT_PROP(name) UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, name)
struct InputConfig
{
float2 baseUV;
};
InputConfig GetInputConfig(float2 baseUV)
{
InputConfig c;
c.baseUV = baseUV;
return c;
}
float2 TransformBaseUV(float2 baseUV)
{
// 计算缩放和偏移后的UV坐标
float4 baseST = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _BaseMap_ST);
float4 baseST = INPUT_PROP(_BaseMap_ST);
return baseUV * baseST.xy + baseST.zw;
}
float4 GetBase(float2 baseUV)
float2 TransformDetailUV(float2 baseUV)
{
return 0.0;
}
float4 GetMask(InputConfig c)
{
return 1.0;
}
float4 GetDetail(InputConfig c)
{
return 0.0;
}
float4 GetBase(InputConfig c)
{
float4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, baseUV);
// 通过UNITY_ACCESS_INSTANCED_PROP访问material属性
float4 baseColor = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _BaseColor);
float4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, c.baseUV);
float4 baseColor = INPUT_PROP(_BaseColor);
return baseMap * baseColor;
}
float3 GetEmission(float2 baseUV)
float3 GetEmission(InputConfig c)
{
return GetBase(c).rgb;
}
float GetCutoff(InputConfig c)
{
return GetBase(baseUV).rgb;
return INPUT_PROP(_Cutoff);
}
float GetCutoff(float2 baseUV)
float GetMetallic(InputConfig c)
{
return UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _Cutoff);
return 0.0;
}
float GetMetallic(float2 baseUV)
float GetSmoothness(InputConfig c)
{
return 0.0;
}
float GetSmoothness(float2 baseUV)
float GetFresnel(InputConfig c)
{
return 0.0;
}
float3 GetNormalTS(InputConfig c)
{
return float3(0.0, 0.0, 1.0);
}
#endif
\ No newline at end of file
......@@ -34,11 +34,12 @@ Varyings UnlitPassVertex(Attributes input)
float4 UnlitPassFragment(Varyings input) : SV_TARGET
{
UNITY_SETUP_INSTANCE_ID(input);
float4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.baseUV);
float4 base = GetBase(input.baseUV);
InputConfig config = GetInputConfig(input.baseUV);
float4 base = GetBase(config);
#if defined(_CLIPPING)
// 透明度低于阈值的片元进行舍弃
clip(base.a - GetCutoff(input.baseUV));
clip(base.a - GetCutoff(config));
#endif
return base;
}
......
......@@ -9,7 +9,7 @@ Material:
m_PrefabAsset: {fileID: 0}
m_Name: Circuitry
m_Shader: {fileID: 4800000, guid: 6848ac66b88696c4ea2f1389e0877b8e, type: 3}
m_ShaderKeywords: _DETAIL_MAP _MASK_MAP _NORMAL_MAP _RECEIVE_SHADOWS _SHADOWS_ON
m_ShaderKeywords: _MASK_MAP _RECEIVE_SHADOWS _SHADOWS_ON
m_LightmapFlags: 4
m_EnableInstancingVariants: 1
m_DoubleSidedGI: 0
......@@ -78,7 +78,7 @@ Material:
- _Cutoff: 0.5
- _DetailAlbedo: 0.2
- _DetailAlbedoStrength: 1
- _DetailMapToggle: 1
- _DetailMapToggle: 0
- _DetailNormalMapScale: 1
- _DetailNormalScale: 0.5
- _DetailSmoothness: 0.2
......@@ -90,7 +90,7 @@ Material:
- _MaskMapToggle: 1
- _Metallic: 1
- _Mode: 0
- _NormalMapToggle: 1
- _NormalMapToggle: 0
- _NormalScale: 1
- _Occlusion: 0.5
- _OcclusionStrength: 1
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册