浏览代码

Refactor lightweight shaders Part 1

- Add surface model to PBR
- Abstract everything to cgincs
/main
Matt Dean 7 年前
当前提交
f17a21ca
共有 6 个文件被更改,包括 426 次插入402 次删除
  1. 228
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipeline.shader
  2. 274
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineCore.cginc
  3. 25
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineInput.cginc
  4. 239
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightStandardShader.shader
  5. 53
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelinePass.cginc
  6. 9
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelinePass.cginc.meta

228
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipeline.shader


CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON
#pragma shader_feature _ _SPECGLOSSMAP _SPECGLOSSMAP_BASE_ALPHA _SPECULAR_COLOR
#pragma shader_feature _NORMALMAP

#include "UnityCG.cginc"
#include "UnityStandardInput.cginc"
#include "LightweightPipelineCore.cginc"
#include "LightweightPipelineLighting.cginc"
LightweightVertexOutput vert(LightweightVertexInput v)
{
LightweightVertexOutput o = (LightweightVertexOutput)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.uv01.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
#ifdef LIGHTMAP_ON
o.uv01.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
o.hpos = UnityObjectToClipPos(v.vertex);
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.posWS.xyz = worldPos;
o.viewDir.xyz = normalize(_WorldSpaceCameraPos - worldPos);
half3 normal = normalize(UnityObjectToWorldNormal(v.normal));
#if _NORMALMAP
half sign = v.tangent.w * unity_WorldTransformParams.w;
half3 tangent = normalize(UnityObjectToWorldDir(v.tangent));
half3 binormal = cross(normal, tangent) * v.tangent.w;
// Initialize tangetToWorld in column-major to benefit from better glsl matrix multiplication code
o.tangentToWorld0 = half3(tangent.x, binormal.x, normal.x);
o.tangentToWorld1 = half3(tangent.y, binormal.y, normal.y);
o.tangentToWorld2 = half3(tangent.z, binormal.z, normal.z);
#else
o.normal = normal;
#endif
// TODO: change to only support point lights per vertex. This will greatly simplify shader ALU
#if defined(_VERTEX_LIGHTS) && defined(_MULTIPLE_LIGHTS)
half3 diffuse = half3(1.0, 1.0, 1.0);
// pixel lights shaded = min(pixelLights, perObjectLights)
// vertex lights shaded = min(vertexLights, perObjectLights) - pixel lights shaded
// Therefore vertexStartIndex = pixelLightCount; vertexEndIndex = min(vertexLights, perObjectLights)
int vertexLightStart = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
int vertexLightEnd = min(globalLightCount.y, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = vertexLightStart; lightIter < vertexLightEnd; ++lightIter)
{
int lightIndex = unity_4LightIndices0[lightIter];
LightInput lightInput;
INITIALIZE_LIGHT(lightInput, lightIndex);
half3 lightDirection;
half atten = ComputeLightAttenuationVertex(lightInput, normal, worldPos, lightDirection);
o.fogCoord.yzw += LightingLambert(diffuse, lightDirection, normal, atten);
}
#endif
#if defined(_LIGHT_PROBES_ON) && !defined(LIGHTMAP_ON)
o.fogCoord.yzw += max(half3(0, 0, 0), ShadeSH9(half4(normal, 1)));
#endif
UNITY_TRANSFER_FOG(o, o.hpos);
return o;
}
half4 frag(LightweightVertexOutput i) : SV_Target
{
half4 diffuseAlpha = tex2D(_MainTex, i.uv01.xy);
half3 diffuse = LIGHTWEIGHT_GAMMA_TO_LINEAR(diffuseAlpha.rgb) * _Color.rgb;
half alpha = diffuseAlpha.a * _Color.a;
// Keep for compatibility reasons. Shader Inpector throws a warning when using cutoff
// due overdraw performance impact.
#ifdef _ALPHATEST_ON
clip(alpha - _Cutoff);
#endif
half3 normal;
NormalMap(i, normal);
half4 specularGloss;
SpecularGloss(i.uv01.xy, alpha, specularGloss);
half3 viewDir = i.viewDir.xyz;
float3 worldPos = i.posWS.xyz;
half3 lightDirection;
#ifndef _MULTIPLE_LIGHTS
LightInput lightInput;
INITIALIZE_MAIN_LIGHT(lightInput);
half lightAtten = ComputeLightAttenuation(lightInput, normal, worldPos, lightDirection);
#ifdef _SHADOWS
lightAtten *= ComputeShadowAttenuation(i, _ShadowLightDirection.xyz);
#endif
#ifdef LIGHTWEIGHT_SPECULAR_HIGHLIGHTS
half3 color = LightingBlinnPhong(diffuse, specularGloss, lightDirection, normal, viewDir, lightAtten) * lightInput.color;
#else
half3 color = LightingLambert(diffuse, lightDirection, normal, lightAtten) * lightInput.color;
#endif
#else
half3 color = half3(0, 0, 0);
#ifdef _SHADOWS
half shadowAttenuation = ComputeShadowAttenuation(i, _ShadowLightDirection.xyz);
#endif
int pixelLightCount = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = 0; lightIter < pixelLightCount; ++lightIter)
{
LightInput lightData;
int lightIndex = unity_4LightIndices0[lightIter];
INITIALIZE_LIGHT(lightData, lightIndex);
half lightAtten = ComputeLightAttenuation(lightData, normal, worldPos, lightDirection);
#ifdef _SHADOWS
lightAtten *= max(shadowAttenuation, half(lightIndex != _ShadowData.x));
#endif
//#include "LightweightPipelineLighting.cginc"
#ifdef LIGHTWEIGHT_SPECULAR_HIGHLIGHTS
color += LightingBlinnPhong(diffuse, specularGloss, lightDirection, normal, viewDir, lightAtten) * lightData.color;
#else
color += LightingLambert(diffuse, lightDirection, normal, lightAtten) * lightData.color;
#endif
}
#endif // _MULTIPLE_LIGHTS
#pragma vertex LightweightVertex
#pragma fragment LightweightFragmentLegacy
#ifdef _EMISSION
color += LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_EmissionMap, i.uv01.xy).rgb) * _EmissionColor;
#else
color += _EmissionColor;
#endif
#if defined(LIGHTMAP_ON)
color += (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv01.zw)) + i.fogCoord.yzw) * diffuse;
#elif defined(_VERTEX_LIGHTS) || defined(_LIGHT_PROBES_ON)
color += i.fogCoord.yzw * diffuse;
#endif
#if _REFLECTION_CUBEMAP
// TODO: we can use reflect vec to compute specular instead of half when computing cubemap reflection
half3 reflectVec = reflect(-i.viewDir.xyz, normal);
color += texCUBE(_Cube, reflectVec).rgb * specularGloss.rgb;
#elif defined(_REFLECTION_PROBE)
half3 reflectVec = reflect(-i.viewDir.xyz, normal);
half4 reflectionProbe = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflectVec);
color += reflectionProbe.rgb * (reflectionProbe.a * unity_SpecCube0_HDR.x) * specularGloss.rgb;
#endif
UNITY_APPLY_FOG(i.fogCoord, color);
return OutputColor(color, alpha);
};
ENDCG
}

#pragma vertex vert
#pragma fragment frag
float4 vert(float4 pos : POSITION) : SV_POSITION
{
float4 clipPos = UnityObjectToClipPos(pos);
#if defined(UNITY_REVERSED_Z)
clipPos.z = min(clipPos.z, UNITY_NEAR_CLIP_VALUE);
#else
clipPos.z = max(clipPos.z, UNITY_NEAR_CLIP_VALUE);
#endif
return clipPos;
}
half4 frag() : SV_TARGET
{
return 0;
}
#include "LightweightPipelinePass.cginc"
#pragma vertex shadowVert
#pragma fragment shadowFrag
ENDCG
}

#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 vert(float4 pos : POSITION) : SV_POSITION
{
return UnityObjectToClipPos(pos);
}
half4 frag() : SV_TARGET
{
return 0;
}
#include "LightweightPipelinePass.cginc"
#pragma vertex depthVert
#pragma fragment depthFrag
ENDCG
}

CGPROGRAM
#define UNITY_SETUP_BRDF_INPUT SpecularSetup
#pragma vertex vert_meta
#pragma fragment frag_meta_ld
#pragma shader_feature _EMISSION
#pragma shader_feature _EMISSION
#include "UnityStandardMeta.cginc"
#include "LightweightPipelineCore.cginc"
fixed4 frag_meta_ld(v2f_meta i) : SV_Target
{
UnityMetaInput o;
UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o);
o.Albedo = Albedo(i.uv);
half4 specularColor;
SpecularGloss(i.uv.xy, 1.0, specularColor);
o.SpecularColor = specularColor;
#ifdef _EMISSION
o.Emission += LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_EmissionMap, i.uv).rgb) * _EmissionColor;
#else
o.Emission += _EmissionColor;
#endif
return UnityMetaFragment(o);
}
#include "UnityCG.cginc"
#include "LightweightPipelinePass.cginc"
#pragma vertex vert_meta
#pragma fragment frag_meta_ld
ENDCG
}
}

274
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineCore.cginc


#ifndef LIGHTWEIGHT_PIPELINE_CORE_INCLUDED
#define LIGHTWEIGHT_PIPELINE_CORE_INCLUDED
// -------------------------------------
#include "LightweightPipelineLighting.cginc"
#include "LightweightPipelineBRDF.cginc"
#if defined(_HARD_SHADOWS) || defined(_SOFT_SHADOWS) || defined(_HARD_SHADOWS_CASCADES) || defined(_SOFT_SHADOWS_CASCADES)
#define _SHADOWS

#endif
}
half3 MetallicSetup(float2 uv, half3 albedo, half albedoAlpha, out half3 specular, out half smoothness, out half oneMinusReflectivity)
half3 MetallicSetup(float2 uv, Surface s, /*half3 albedo, half albedoAlpha, */out half3 specular, out half smoothness, out half oneMinusReflectivity)
half2 metallicGloss = MetallicSpecGloss(uv, albedoAlpha).ra;
half metallic = metallicGloss.r;
smoothness = metallicGloss.g;
//half2 metallicGloss = MetallicSpecGloss(uv, albedoAlpha).ra;
//half metallic = metallicGloss.r;
smoothness = s.Smoothness;// metallicGloss.g;
// We'll need oneMinusReflectivity, so
// 1-reflectivity = 1-lerp(dielectricSpec, 1, metallic) = lerp(1-dielectricSpec, 0, metallic)

half oneMinusDielectricSpec = _DieletricSpec.a;
oneMinusReflectivity = oneMinusDielectricSpec - metallic * oneMinusDielectricSpec;
specular = lerp(_DieletricSpec.rgb, albedo, metallic);
oneMinusReflectivity = oneMinusDielectricSpec - s.Metallic * oneMinusDielectricSpec;
specular = lerp(_DieletricSpec.rgb, s.Albedo, s.Metallic);
return albedo * oneMinusReflectivity;
return s.Albedo * oneMinusReflectivity;
half3 SpecularSetup(float2 uv, half3 albedo, half albedoAlpha, out half3 specular, out half smoothness, out half oneMinusReflectivity)
half3 SpecularSetup(float2 uv, Surface s, /*half3 albedo, half albedoAlpha, */out half3 specular, out half smoothness, out half oneMinusReflectivity)
half4 specGloss = MetallicSpecGloss(uv, albedoAlpha);
half4 specGloss = float4(s.Specular, s.Smoothness);// MetallicSpecGloss(uv, albedoAlpha);
#if defined(UNITY_COLORSPACE_GAMMA) && defined(LIGHTWEIGHT_LINEAR)
specGloss.rgb = LIGHTWEIGHT_GAMMA_TO_LINEAR(specGloss.rgb);
#endif
return albedo * (half3(1, 1, 1) - specular);
return s.Albedo * (half3(1, 1, 1) - specular);
}
half4 OutputColor(half3 color, half alpha)

return half4(LIGHTWEIGHT_LINEAR_TO_GAMMA(color), 1);
#endif
}
LightweightVertexOutput LightweightVertex(LightweightVertexInput v)
{
LightweightVertexOutput o = (LightweightVertexOutput)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.uv01.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
#ifdef LIGHTMAP_ON
o.uv01.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
o.hpos = UnityObjectToClipPos(v.vertex);
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.posWS.xyz = worldPos;
o.viewDir.xyz = normalize(_WorldSpaceCameraPos - worldPos);
half3 normal = normalize(UnityObjectToWorldNormal(v.normal));
#if _NORMALMAP
half sign = v.tangent.w * unity_WorldTransformParams.w;
half3 tangent = normalize(UnityObjectToWorldDir(v.tangent));
half3 binormal = cross(normal, tangent) * v.tangent.w;
// Initialize tangetToWorld in column-major to benefit from better glsl matrix multiplication code
o.tangentToWorld0 = half3(tangent.x, binormal.x, normal.x);
o.tangentToWorld1 = half3(tangent.y, binormal.y, normal.y);
o.tangentToWorld2 = half3(tangent.z, binormal.z, normal.z);
#else
o.normal = normal;
#endif
// TODO: change to only support point lights per vertex. This will greatly simplify shader ALU
#if defined(_VERTEX_LIGHTS) && defined(_MULTIPLE_LIGHTS)
half3 diffuse = half3(1.0, 1.0, 1.0);
// pixel lights shaded = min(pixelLights, perObjectLights)
// vertex lights shaded = min(vertexLights, perObjectLights) - pixel lights shaded
// Therefore vertexStartIndex = pixelLightCount; vertexEndIndex = min(vertexLights, perObjectLights)
int vertexLightStart = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
int vertexLightEnd = min(globalLightCount.y, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = vertexLightStart; lightIter < vertexLightEnd; ++lightIter)
{
int lightIndex = unity_4LightIndices0[lightIter];
LightInput lightInput;
INITIALIZE_LIGHT(lightInput, lightIndex);
half3 lightDirection;
half atten = ComputeLightAttenuationVertex(lightInput, normal, worldPos, lightDirection);
o.fogCoord.yzw += LightingLambert(diffuse, lightDirection, normal, atten);
}
#endif
#if defined(_LIGHT_PROBES_ON) && !defined(LIGHTMAP_ON)
o.fogCoord.yzw += max(half3(0, 0, 0), ShadeSH9(half4(normal, 1)));
#endif
UNITY_TRANSFER_FOG(o, o.hpos);
return o;
}
Surface InitializeSurface()
{
Surface s;
s.Albedo = float3(1, 1, 1);
s.Specular = float3(0, 0, 0);
s.Metallic = 0;
s.Normal = float3(.5, .5, 1);
s.Emission = 0;
s.Smoothness = 0;
s.Occlusion = 1;
s.Alpha = 1;
return s;
}
void DefineSurface(LightweightVertexOutput i, inout Surface s);
half4 LightweightFragmentPBR(LightweightVertexOutput i) : SV_Target
{
Surface s = InitializeSurface();
DefineSurface(i, s);
float2 uv = i.uv01.xy;
float2 lightmapUV = i.uv01.zw;
half3 specColor;
half smoothness;
half oneMinusReflectivity;
#ifdef _METALLIC_SETUP
half3 diffColor = MetallicSetup(uv, s, specColor, smoothness, oneMinusReflectivity);
#else
half3 diffColor = SpecularSetup(uv, s, specColor, smoothness, oneMinusReflectivity);
#endif
diffColor = PreMultiplyAlpha(diffColor, s.Alpha, oneMinusReflectivity, /*out*/ s.Alpha);
// Roughness is (1.0 - smoothness)�
half perceptualRoughness = 1.0h - smoothness;
// TODO - Actually handle normal
half3 normal;
CalculateNormal(s.Normal, i, normal);
// TODO: shader keyword for occlusion
// TODO: Reflection Probe blend support.
half3 reflectVec = reflect(-i.viewDir.xyz, normal);
UnityIndirect indirectLight = LightweightGI(lightmapUV, i.fogCoord.yzw, reflectVec, s.Occlusion, perceptualRoughness);
// PBS
// grazingTerm = F90
half grazingTerm = saturate(smoothness + (1 - oneMinusReflectivity));
half fresnelTerm = Pow4(1.0 - saturate(dot(normal, i.viewDir.xyz)));
half3 color = LightweightBRDFIndirect(diffColor, specColor, indirectLight, perceptualRoughness * perceptualRoughness, grazingTerm, fresnelTerm);
half3 lightDirection;
#ifndef _MULTIPLE_LIGHTS
LightInput light;
INITIALIZE_MAIN_LIGHT(light);
half lightAtten = ComputeLightAttenuation(light, normal, i.posWS.xyz, lightDirection);
#ifdef _SHADOWS
lightAtten *= ComputeShadowAttenuation(i, _ShadowLightDirection.xyz);
#endif
half NdotL = saturate(dot(normal, lightDirection));
half3 radiance = light.color * (lightAtten * NdotL);
color += LightweightBDRF(diffColor, specColor, oneMinusReflectivity, perceptualRoughness, normal, lightDirection, i.viewDir.xyz) * radiance;
#else
#ifdef _SHADOWS
half shadowAttenuation = ComputeShadowAttenuation(i, _ShadowLightDirection.xyz);
#endif
int pixelLightCount = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = 0; lightIter < pixelLightCount; ++lightIter)
{
LightInput light;
int lightIndex = unity_4LightIndices0[lightIter];
INITIALIZE_LIGHT(light, lightIndex);
half lightAtten = ComputeLightAttenuation(light, normal, i.posWS.xyz, lightDirection);
#ifdef _SHADOWS
lightAtten *= max(shadowAttenuation, half(lightIndex != _ShadowData.x));
#endif
half NdotL = saturate(dot(normal, lightDirection));
half3 radiance = light.color * (lightAtten * NdotL);
color += LightweightBDRF(diffColor, specColor, oneMinusReflectivity, perceptualRoughness, normal, lightDirection, i.viewDir.xyz) * radiance;
}
#endif
color += s.Emission;
UNITY_APPLY_FOG(i.fogCoord, color);
return OutputColor(color, s.Alpha);
}
half4 LightweightFragmentLegacy(LightweightVertexOutput i) : SV_Target
{
half4 diffuseAlpha = tex2D(_MainTex, i.uv01.xy);
half3 diffuse = LIGHTWEIGHT_GAMMA_TO_LINEAR(diffuseAlpha.rgb) * _Color.rgb;
half alpha = diffuseAlpha.a * _Color.a;
// Keep for compatibility reasons. Shader Inpector throws a warning when using cutoff
// due overdraw performance impact.
#ifdef _ALPHATEST_ON
clip(alpha - _Cutoff);
#endif
half3 normal;
half3 normalMap = UnpackNormal(tex2D(_BumpMap, i.uv01.xy));
CalculateNormal(normalMap, i, normal);
half4 specularGloss;
SpecularGloss(i.uv01.xy, alpha, specularGloss);
half3 viewDir = i.viewDir.xyz;
float3 worldPos = i.posWS.xyz;
half3 lightDirection;
#ifndef _MULTIPLE_LIGHTS
LightInput lightInput;
INITIALIZE_MAIN_LIGHT(lightInput);
half lightAtten = ComputeLightAttenuation(lightInput, normal, worldPos, lightDirection);
#ifdef _SHADOWS
lightAtten *= ComputeShadowAttenuation(i, _ShadowLightDirection.xyz);
#endif
#ifdef LIGHTWEIGHT_SPECULAR_HIGHLIGHTS
half3 color = LightingBlinnPhong(diffuse, specularGloss, lightDirection, normal, viewDir, lightAtten) * lightInput.color;
#else
half3 color = LightingLambert(diffuse, lightDirection, normal, lightAtten) * lightInput.color;
#endif
#else
half3 color = half3(0, 0, 0);
#ifdef _SHADOWS
half shadowAttenuation = ComputeShadowAttenuation(i, _ShadowLightDirection.xyz);
#endif
int pixelLightCount = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = 0; lightIter < pixelLightCount; ++lightIter)
{
LightInput lightData;
int lightIndex = unity_4LightIndices0[lightIter];
INITIALIZE_LIGHT(lightData, lightIndex);
half lightAtten = ComputeLightAttenuation(lightData, normal, worldPos, lightDirection);
#ifdef _SHADOWS
lightAtten *= max(shadowAttenuation, half(lightIndex != _ShadowData.x));
#endif
#ifdef LIGHTWEIGHT_SPECULAR_HIGHLIGHTS
color += LightingBlinnPhong(diffuse, specularGloss, lightDirection, normal, viewDir, lightAtten) * lightData.color;
#else
color += LightingLambert(diffuse, lightDirection, normal, lightAtten) * lightData.color;
#endif
}
#endif // _MULTIPLE_LIGHTS
#ifdef _EMISSION
color += LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_EmissionMap, i.uv01.xy).rgb) * _EmissionColor;
#else
color += _EmissionColor;
#endif
#if defined(LIGHTMAP_ON)
color += (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv01.zw)) + i.fogCoord.yzw) * diffuse;
#elif defined(_VERTEX_LIGHTS) || defined(_LIGHT_PROBES_ON)
color += i.fogCoord.yzw * diffuse;
#endif
#if _REFLECTION_CUBEMAP
// TODO: we can use reflect vec to compute specular instead of half when computing cubemap reflection
half3 reflectVec = reflect(-i.viewDir.xyz, normal);
color += texCUBE(_Cube, reflectVec).rgb * specularGloss.rgb;
#elif defined(_REFLECTION_PROBE)
half3 reflectVec = reflect(-i.viewDir.xyz, normal);
half4 reflectionProbe = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflectVec);
color += reflectionProbe.rgb * (reflectionProbe.a * unity_SpecCube0_HDR.x) * specularGloss.rgb;
#endif
UNITY_APPLY_FOG(i.fogCoord, color);
return OutputColor(color, alpha);
};
#endif

25
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelineInput.cginc


#ifndef LIGHTWEIGHT_INPUT_INCLUDED
#define LIGHTWEIGHT_INPUT_INCLUDED
#include "UnityCG.cginc"
#include "UnityStandardInput.cginc"
#define MAX_VISIBLE_LIGHTS 16
// Main light initialized without indexing

UNITY_VERTEX_OUTPUT_STEREO
};
inline void NormalMap(LightweightVertexOutput i, out half3 normal)
//TODO - Shouldnt be here
struct Surface
#if _NORMALMAP
half3 normalmap = UnpackNormal(tex2D(_BumpMap, i.uv01.xy));
float3 Albedo; // diffuse color
float3 Specular; // specular color
float Metallic; // metallic
float3 Normal; // tangent space normal, if written
half3 Emission;
half Smoothness; // 0=rough, 1=smooth
half Occlusion; // occlusion (default 1)
float Alpha; // alpha for transparencies
};
// glsl compiler will generate underperforming code by using a row-major pre multiplication matrix: mul(normalmap, i.tangentToWorld)
// i.tangetToWorld was initialized as column-major in vs and here dot'ing individual for better performance.
// The code below is similar to post multiply: mul(i.tangentToWorld, normalmap)
normal = normalize(half3(dot(normalmap, i.tangentToWorld0), dot(normalmap, i.tangentToWorld1), dot(normalmap, i.tangentToWorld2)));
inline void CalculateNormal(float3 normalMap, LightweightVertexOutput i, out half3 normal)
{
#if _NORMALMAP
normal = normalize(half3(dot(normalMap, i.tangentToWorld0), dot(normalMap, i.tangentToWorld1), dot(normalMap, i.tangentToWorld2)));
#else
normal = normalize(i.normal);
#endif

239
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightStandardShader.shader


CGPROGRAM
#pragma target 3.0
// -------------------------------------
#pragma shader_feature _METALLIC_SETUP _SPECULAR_SETUP
#pragma shader_feature _NORMALMAP
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#pragma shader_feature _EMISSION
#pragma shader_feature _METALLICSPECGLOSSMAP
#pragma shader_feature ___ _DETAIL_MULX2
#pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
#pragma shader_feature _ _SPECULARHIGHLIGHTS_ON
#pragma shader_feature _ _GLOSSYREFLECTIONS_ON
#pragma shader_feature _PARALLAXMAP
#pragma multi_compile _ _SINGLE_DIRECTIONAL_LIGHT _SINGLE_SPOT_LIGHT _SINGLE_POINT_LIGHT
#pragma multi_compile _ LIGHTWEIGHT_LINEAR
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#pragma multi_compile _ LIGHTMAP_ON
#pragma multi_compile _ _LIGHT_PROBES_ON
#pragma multi_compile _ _HARD_SHADOWS _SOFT_SHADOWS _HARD_SHADOWS_CASCADES _SOFT_SHADOWS_CASCADES
#pragma multi_compile _ _VERTEX_LIGHTS
#pragma multi_compile_fog
#pragma multi_compile_instancing
#pragma vertex LightweightVertex
#pragma fragment LightweightFragment
#include "UnityCG.cginc"
#include "UnityStandardInput.cginc"
#include "LightweightPipelineCore.cginc"
#include "LightweightPipelineLighting.cginc"
#include "LightweightPipelineBRDF.cginc"
LightweightVertexOutput LightweightVertex(LightweightVertexInput v)
{
LightweightVertexOutput o = (LightweightVertexOutput)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.uv01.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
#ifdef LIGHTMAP_ON
o.uv01.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
o.hpos = UnityObjectToClipPos(v.vertex);
#pragma shader_feature _METALLIC_SETUP _SPECULAR_SETUP
#pragma shader_feature _NORMALMAP
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#pragma shader_feature _EMISSION
#pragma shader_feature _METALLICSPECGLOSSMAP
#pragma shader_feature ___ _DETAIL_MULX2
#pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
#pragma shader_feature _ _SPECULARHIGHLIGHTS_ON
#pragma shader_feature _ _GLOSSYREFLECTIONS_ON
#pragma shader_feature _PARALLAXMAP
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.posWS.xyz = worldPos;
#pragma multi_compile _ _SINGLE_DIRECTIONAL_LIGHT _SINGLE_SPOT_LIGHT _SINGLE_POINT_LIGHT
#pragma multi_compile _ LIGHTWEIGHT_LINEAR
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#pragma multi_compile _ LIGHTMAP_ON
#pragma multi_compile _ _LIGHT_PROBES_ON
#pragma multi_compile _ _HARD_SHADOWS _SOFT_SHADOWS _HARD_SHADOWS_CASCADES _SOFT_SHADOWS_CASCADES
#pragma multi_compile _ _VERTEX_LIGHTS
#pragma multi_compile_fog
#pragma multi_compile_instancing
half3 viewDir = normalize(_WorldSpaceCameraPos - worldPos);
o.viewDir.xyz = viewDir;
#include "UnityCG.cginc"
#include "UnityStandardInput.cginc"
#include "LightweightPipelineCore.cginc"
half3 normal = normalize(UnityObjectToWorldNormal(v.normal));
#pragma vertex LightweightVertex
#pragma fragment LightweightFragmentPBR
#if _NORMALMAP
half sign = v.tangent.w * unity_WorldTransformParams.w;
half3 tangent = UnityObjectToWorldDir(v.tangent);
half3 binormal = cross(normal, tangent) * sign;
void DefineSurface(LightweightVertexOutput i, inout Surface s)
{
// Albedo
float4 c = tex2D(_MainTex, i.uv01.xy);
s.Albedo = LIGHTWEIGHT_GAMMA_TO_LINEAR(c.rgb) * _Color.rgb;
// Initialize tangetToWorld in column-major to benefit from better glsl matrix multiplication code
o.tangentToWorld0 = half3(tangent.x, binormal.x, normal.x);
o.tangentToWorld1 = half3(tangent.y, binormal.y, normal.y);
o.tangentToWorld2 = half3(tangent.z, binormal.z, normal.z);
// Metallic
#ifdef _METALLICSPECGLOSSMAP
float4 metallicMap = tex2D(_MetallicSpecGlossMap, i.uv01.xy).r;
s.Metallic = metallicMap.r;
#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
s.Smoothness = c.a;
o.normal = normal;
#endif
#if defined(_LIGHT_PROBES_ON) && !defined(LIGHTMAP_ON)
o.fogCoord.yzw += max(half3(0, 0, 0), ShadeSH9(half4(normal, 1)));
s.Smoothness = metallicMap.a;
UNITY_TRANSFER_FOG(o, o.hpos);
return o;
}
half4 LightweightFragment(LightweightVertexOutput i) : SV_Target
{
float2 uv = i.uv01.xy;
float2 lightmapUV = i.uv01.zw;
half4 albedoTex = tex2D(_MainTex, i.uv01.xy);
half3 albedo = LIGHTWEIGHT_GAMMA_TO_LINEAR(albedoTex.rgb) * _Color.rgb;
s.Smoothness *= _GlossMapScale;
#if defined(_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A)
half alpha = _Color.a;
#else //_METALLICSPECGLOSSMAP
s.Metallic = _Metallic;
#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
s.Smoothness = c.a * _GlossMapScale;
half alpha = albedoTex.a * _Color.a;
s.Smoothness = _Glossiness;
#if defined(_ALPHATEST_ON)
clip(alpha - _Cutoff);
half3 specColor;
half smoothness;
half oneMinusReflectivity;
#ifdef _METALLIC_SETUP
half3 diffColor = MetallicSetup(uv, albedo, alpha, specColor, smoothness, oneMinusReflectivity);
#else
half3 diffColor = SpecularSetup(uv, albedo, alpha, specColor, smoothness, oneMinusReflectivity);
// Normal
#if _NORMALMAP
s.Normal = UnpackNormal(tex2D(_BumpMap, i.uv01.xy));
diffColor = PreMultiplyAlpha(diffColor, alpha, oneMinusReflectivity, /*out*/ alpha);
// Roughness is (1.0 - smoothness)²
half perceptualRoughness = 1.0h - smoothness;
half3 normal;
NormalMap(i, normal);
// TODO: shader keyword for occlusion
// TODO: Reflection Probe blend support.
half3 reflectVec = reflect(-i.viewDir.xyz, normal);
half occlusion = Occlusion(uv);
UnityIndirect indirectLight = LightweightGI(lightmapUV, i.fogCoord.yzw, reflectVec, occlusion, perceptualRoughness);
// PBS
// grazingTerm = F90
half grazingTerm = saturate(smoothness + (1 - oneMinusReflectivity));
half fresnelTerm = Pow4(1.0 - saturate(dot(normal, i.viewDir.xyz)));
half3 color = LightweightBRDFIndirect(diffColor, specColor, indirectLight, perceptualRoughness * perceptualRoughness, grazingTerm, fresnelTerm);
half3 lightDirection;
// Occlusion
s.Occlusion = Occlusion(i.uv01.xy);
#ifndef _MULTIPLE_LIGHTS
LightInput light;
INITIALIZE_MAIN_LIGHT(light);
half lightAtten = ComputeLightAttenuation(light, normal, i.posWS.xyz, lightDirection);
#ifdef _SHADOWS
lightAtten *= ComputeShadowAttenuation(i, _ShadowLightDirection.xyz);
// Emission
#ifdef _EMISSION
s.Emission = Emission(i.uv01.xy);
half NdotL = saturate(dot(normal, lightDirection));
half3 radiance = light.color * (lightAtten * NdotL);
color += LightweightBDRF(diffColor, specColor, oneMinusReflectivity, perceptualRoughness, normal, lightDirection, i.viewDir.xyz) * radiance;
// Alpha
#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
s.Alpha = _Color.a;
#ifdef _SHADOWS
half shadowAttenuation = ComputeShadowAttenuation(i, _ShadowLightDirection.xyz);
s.Alpha = c.a * _Color.a;
int pixelLightCount = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = 0; lightIter < pixelLightCount; ++lightIter)
{
LightInput light;
int lightIndex = unity_4LightIndices0[lightIter];
INITIALIZE_LIGHT(light, lightIndex);
half lightAtten = ComputeLightAttenuation(light, normal, i.posWS.xyz, lightDirection);
#ifdef _SHADOWS
lightAtten *= max(shadowAttenuation, half(lightIndex != _ShadowData.x));
#endif
half NdotL = saturate(dot(normal, lightDirection));
half3 radiance = light.color * (lightAtten * NdotL);
color += LightweightBDRF(diffColor, specColor, oneMinusReflectivity, perceptualRoughness, normal, lightDirection, i.viewDir.xyz) * radiance;
}
#ifdef _ALPHATEST_ON
clip(s.Alpha - _Cutoff);
color += Emission(uv);
UNITY_APPLY_FOG(i.fogCoord, color);
return OutputColor(color, alpha);
}
}
ENDCG
}

Tags{"Lightmode" = "ShadowCaster"}
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 vert(float4 pos : POSITION) : SV_POSITION
{
float4 clipPos = UnityObjectToClipPos(pos);
#if defined(UNITY_REVERSED_Z)
clipPos.z = min(clipPos.z, UNITY_NEAR_CLIP_VALUE);
#else
clipPos.z = max(clipPos.z, UNITY_NEAR_CLIP_VALUE);
#endif
return clipPos;
}
half4 frag() : SV_TARGET
{
return 0;
}
#include "UnityCG.cginc"
#include "LightweightPipelinePass.cginc"
#pragma vertex shadowVert
#pragma fragment shadowFrag
ENDCG
}

#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 vert(float4 pos : POSITION) : SV_POSITION
{
return UnityObjectToClipPos(pos);
}
half4 frag() : SV_TARGET
{
return 0;
}
#include "UnityCG.cginc"
#include "LightweightPipelinePass.cginc"
#pragma vertex depthVert
#pragma fragment depthFrag
FallBack "Standard"
CustomEditor "LightweightStandardShaderGUI"
}

53
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelinePass.cginc


#ifndef LIGHTWEIGHT_PASS_INCLUDED
#define LIGHTWEIGHT_PASS_INCLUDED
float4 shadowVert(float4 pos : POSITION) : SV_POSITION
{
float4 clipPos = UnityObjectToClipPos(pos);
#if defined(UNITY_REVERSED_Z)
clipPos.z = min(clipPos.z, UNITY_NEAR_CLIP_VALUE);
#else
clipPos.z = max(clipPos.z, UNITY_NEAR_CLIP_VALUE);
#endif
return clipPos;
}
half4 shadowFrag() : SV_TARGET
{
return 0;
}
float4 depthVert(float4 pos : POSITION) : SV_POSITION
{
return UnityObjectToClipPos(pos);
}
half4 depthFrag() : SV_TARGET
{
return 0;
}
#include "UnityStandardMeta.cginc"
#include "LightweightPipelineInput.cginc"
fixed4 frag_meta_ld(v2f_meta i) : SV_Target
{
UnityMetaInput o;
UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o);
o.Albedo = Albedo(i.uv);
half4 specularColor;
SpecularGloss(i.uv.xy, 1.0, specularColor);
o.SpecularColor = specularColor;
#ifdef _EMISSION
o.Emission += LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_EmissionMap, i.uv).rgb) * _EmissionColor;
#else
o.Emission += _EmissionColor;
#endif
return UnityMetaFragment(o);
}
#endif

9
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPipelinePass.cginc.meta


fileFormatVersion: 2
guid: 83e29b5f678b4c847bb1d9677843726d
timeCreated: 1488965025
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存