浏览代码

Further refactors to support ShaderGraph

/Add-support-for-light-specular-color-tint
Felipe Lira 7 年前
当前提交
fb2ea3ef
共有 7 个文件被更改,包括 397 次插入406 次删除
  1. 224
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightCore.cginc
  2. 176
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightLighting.cginc
  3. 163
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPassLit.cginc
  4. 2
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightStandard.shader
  5. 2
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightStandardSimpleLighting.shader
  6. 9
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightInput.cginc.meta
  7. 227
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightInput.cginc

224
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightCore.cginc


#ifndef LIGHTWEIGHT_PIPELINE_CORE_INCLUDED
#define LIGHTWEIGHT_PIPELINE_CORE_INCLUDED
#include "LightweightInput.cginc"
#include "LightweightLighting.cginc"
#include "LightweightShadows.cginc"
#if defined(_SPECGLOSSMAP) || defined(_SPECULAR_COLOR)
#define LIGHTWEIGHT_SPECULAR_HIGHLIGHTS
#endif
#define kDieletricSpec half4(0.04, 0.04, 0.04, 1.0 - 0.04) // standard dielectric reflectivity coef at incident angle (= 4%)
half SpecularReflectivity(half3 specular)
{
#if (SHADER_TARGET < 30)
// SM2.0: instruction count limitation
// SM2.0: simplified SpecularStrength
return specular.r; // Red channel - because most metals are either monocrhome or with redish/yellowish tint
#else
return max(max(specular.r, specular.g), specular.b);
#endif
}
inline void InitializeSurfaceData(LightweightVertexOutput IN, out SurfaceData outSurfaceData)
{
float2 uv = IN.uv01.xy;
half4 albedoAlpha = tex2D(_MainTex, uv);
half4 specGloss = MetallicSpecGloss(uv, albedoAlpha);
outSurfaceData.albedo = LIGHTWEIGHT_GAMMA_TO_LINEAR(albedoAlpha.rgb) * _Color.rgb;
#if _METALLIC_SETUP
outSurfaceData.specular = half4(1.0h, 1.0h, 1.0h, 1.0h);
outSurfaceData.metallic = specGloss.r;
#else
outSurfaceData.specular = specGloss.rgb;
outSurfaceData.metallic = 1.0h;
#endif
#include "UnityCG.cginc"
#include "UnityStandardInput.cginc"
outSurfaceData.smoothness = specGloss.a;
outSurfaceData.normal = Normal(uv);
outSurfaceData.occlusion = OcclusionLW(uv);
outSurfaceData.emission = EmissionLW(uv);
outSurfaceData.ambient = IN.fogCoord.yzw;
outSurfaceData.alpha = Alpha(albedoAlpha.a);
}
#define MAX_VISIBLE_LIGHTS 16
void InitializeSurfaceInput(LightweightVertexOutput IN, out SurfaceInput outSurfaceInput)
{
#if LIGHTMAP_ON
outSurfaceInput.lightmapUV = float4(IN.uv01.zw, 0.0, 0.0);
#if defined(UNITY_COLORSPACE_GAMMA) && defined(_LIGHTWEIGHT_FORCE_LINEAR)
#define LIGHTWEIGHT_GAMMA_TO_LINEAR(gammaColor) gammaColor * gammaColor
#define LIGHTWEIGHT_LINEAR_TO_GAMMA(linColor) sqrt(color)
outSurfaceInput.lightmapUV = float4(0.0, 0.0, 0.0, 0.0);
#define LIGHTWEIGHT_GAMMA_TO_LINEAR(color) color
#define LIGHTWEIGHT_LINEAR_TO_GAMMA(color) color
#if _NORMALMAP
outSurfaceInput.tangent = IN.tangent;
outSurfaceInput.binormal = IN.binormal;
#ifdef _SPECULAR_SETUP
#define SAMPLE_METALLICSPECULAR(uv) tex2D(_SpecGlossMap, uv)
outSurfaceInput.tangent = half3(1.0h, 0.0h, 0.0h);
outSurfaceInput.binormal = half3(0.0h, 1.0h, 0.0h);
#define SAMPLE_METALLICSPECULAR(uv) tex2D(_MetallicGlossMap, uv)
outSurfaceInput.normal = IN.normal;
outSurfaceInput.worldPos = IN.posWS;
outSurfaceInput.viewDir = IN.viewDir;
outSurfaceInput.fogFactor = IN.fogCoord.x;
}
inline void InitializeBRDFData(SurfaceData surfaceData, out BRDFData outBRDFData)
{
// BRDF SETUP
#ifdef _METALLIC_SETUP
// We'll need oneMinusReflectivity, so
// 1-reflectivity = 1-lerp(dielectricSpec, 1, metallic) = lerp(1-dielectricSpec, 0, metallic)
// store (1-dielectricSpec) in kDieletricSpec.a, then
// 1-reflectivity = lerp(alpha, 0, metallic) = alpha + metallic*(0 - alpha) =
// = alpha - metallic * alpha
half oneMinusDielectricSpec = kDieletricSpec.a;
half oneMinusReflectivity = oneMinusDielectricSpec - surfaceData.metallic * oneMinusDielectricSpec;
half reflectivity = 1.0 - oneMinusReflectivity;
outBRDFData.diffuse = surfaceData.albedo * oneMinusReflectivity;
outBRDFData.specular = lerp(kDieletricSpec.rgb, surfaceData.albedo, surfaceData.metallic);
#else
half reflectivity = SpecularReflectivity(surfaceData.specular);
CBUFFER_START(_PerObject)
half4 unity_LightIndicesOffsetAndCount;
half4 unity_4LightIndices0;
half4 unity_4LightIndices1;
half _Shininess;
CBUFFER_END
outBRDFData.diffuse = surfaceData.albedo * (half3(1.0h, 1.0h, 1.0h) - surfaceData.specular);
outBRDFData.specular = surfaceData.specular;
#endif
CBUFFER_START(_PerCamera)
float4 _MainLightPosition;
half4 _MainLightColor;
float4 _MainLightAttenuationParams;
half4 _MainLightSpotDir;
outBRDFData.grazingTerm = saturate(surfaceData.smoothness + reflectivity);
outBRDFData.perceptualRoughness = 1.0h - surfaceData.smoothness;
outBRDFData.roughness = outBRDFData.perceptualRoughness * outBRDFData.perceptualRoughness;
half4 _AdditionalLightCount;
float4 _AdditionalLightPosition[MAX_VISIBLE_LIGHTS];
half4 _AdditionalLightColor[MAX_VISIBLE_LIGHTS];
float4 _AdditionalLightAttenuationParams[MAX_VISIBLE_LIGHTS];
half4 _AdditionalLightSpotDir[MAX_VISIBLE_LIGHTS];
CBUFFER_END
#ifdef _ALPHAPREMULTIPLY_ON
half alpha = surfaceData.alpha;
outBRDFData.diffuse *= alpha;
surfaceData.alpha = reflectivity + alpha * (1.0 - reflectivity);
#endif
}
CBUFFER_START(_PerFrame)
half4 _GlossyEnvironmentColor;
sampler2D _AttenuationTexture;
CBUFFER_END
half3 TangentToWorldNormal(half3 normalTangent, half3 tangent, half3 binormal, half3 normal)
{

#if defined(FOG_LINEAR)
// factor = (end-z)/(end-start) = z * (-1/(end-start)) + (end/(end-start))
float fogFactor = saturate(clipZ_01 * unity_FogParams.z + unity_FogParams.w);
return fogFactor;
return half(fogFactor);
return saturate(exp2(-unityFogFactor));
return half(saturate(exp2(-unityFogFactor)));
return saturate(exp2(-unityFogFactor*unityFogFactor));
return half(saturate(exp2(-unityFogFactor*unityFogFactor)));
return 0.0;
return 0.0h;
void ApplyFog(inout half3 color, float fogFactor)
void ApplyFog(inout half3 color, half fogFactor)
{
#if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
color = lerp(unity_FogColor, color, fogFactor);

return half4(LIGHTWEIGHT_LINEAR_TO_GAMMA(color), 1);
#endif
}
inline half Alpha(half albedoAlpha)
{
#if defined(_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A)
half alpha = _Color.a;
#else
half alpha = albedoAlpha * _Color.a;
#endif
#if defined(_ALPHATEST_ON)
clip(alpha - _Cutoff);
#endif
return alpha;
}
half3 Normal(float2 uv)
{
#if _NORMALMAP
return UnpackNormal(tex2D(_BumpMap, uv));
#else
return half3(0.0h, 0.0h, 1.0h);
#endif
}
inline void SpecularGloss(half2 uv, half alpha, out half4 specularGloss)
{
specularGloss = half4(0, 0, 0, 1);
#ifdef _SPECGLOSSMAP
specularGloss = tex2D(_SpecGlossMap, uv);
specularGloss.rgb = LIGHTWEIGHT_GAMMA_TO_LINEAR(specularGloss.rgb);
#elif defined(_SPECULAR_COLOR)
specularGloss = _SpecColor;
#endif
#ifdef _GLOSSINESS_FROM_BASE_ALPHA
specularGloss.a = alpha;
#endif
}
half4 MetallicSpecGloss(float2 uv, half albedoAlpha)
{
half4 specGloss;
#ifdef _METALLICSPECGLOSSMAP
specGloss = specGloss = SAMPLE_METALLICSPECULAR(uv);
#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
specGloss.a = albedoAlpha * _GlossMapScale;
#else
specGloss.a *= _GlossMapScale;
#endif
#else // _METALLICSPECGLOSSMAP
#if _METALLIC_SETUP
specGloss.rgb = _Metallic.rrr;
#else
specGloss.rgb = _SpecColor.rgb;
#endif
#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
specGloss.a = albedoAlpha * _GlossMapScale;
#else
specGloss.a = _Glossiness;
#endif
#endif
return specGloss;
}
half OcclusionLW(float2 uv)
{
#ifdef _OCCLUSIONMAP
#if (SHADER_TARGET < 30)
// SM20: instruction count limitation
// SM20: simpler occlusion
return tex2D(_OcclusionMap, uv).g;
#else
half occ = tex2D(_OcclusionMap, uv).g;
return LerpOneTo(occ, _OcclusionStrength);
#endif
#else
return 1.0;
#endif
}
half3 EmissionLW(float2 uv)
{
#ifndef _EMISSION
return 0;
#else
return LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_EmissionMap, uv).rgb) * _EmissionColor.rgb;
#endif
}
#endif

176
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightLighting.cginc


#ifndef LIGHTWEIGHT_LIGHTING_INCLUDED
#define LIGHTWEIGHT_LIGHTING_INCLUDED
#include "LightweightCore.cginc"
#include "LightweightShadows.cginc"
#define kDieletricSpec half4(0.04, 0.04, 0.04, 1.0 - 0.04) // standard dielectric reflectivity coef at incident angle (= 4%)
// Main light initialized without indexing
#define INITIALIZE_MAIN_LIGHT(light) \
light.pos = _MainLightPosition; \
light.color = _MainLightColor; \
light.atten = _MainLightAttenuationParams; \
light.spotDir = _MainLightSpotDir;
// Indexing might have a performance hit for old mobile hardware
#define INITIALIZE_LIGHT(light, i) \
half4 indices = (i < 4) ? unity_4LightIndices0 : unity_4LightIndices1; \
int index = (i < 4) ? i : i - 4; \
int lightIndex = indices[index]; \
light.pos = _AdditionalLightPosition[lightIndex]; \
light.color = _AdditionalLightColor[lightIndex]; \
light.atten = _AdditionalLightAttenuationParams[lightIndex]; \
light.spotDir = _AdditionalLightSpotDir[lightIndex]
#if (defined(_MAIN_DIRECTIONAL_LIGHT) || defined(_MAIN_SPOT_LIGHT) || defined(_MAIN_POINT_LIGHT))
#define _MAIN_LIGHT
#endif
struct LightInput
{
float4 pos;
half4 color;
float4 atten;
half4 spotDir;
};
struct SurfaceData
{
half3 albedo;
half3 specular;
half metallic;
half smoothness;
half3 normal;
half3 emission;
half occlusion;
half alpha;
half3 ambient;
};
struct SurfaceInput
{
float4 lightmapUV;
half3 normalWS;
half3 tangentWS;
half3 bitangentWS;
float3 positionWS;
half3 viewDirectionWS;
float fogFactor;
};
struct BRDFData
{
half3 diffuse;
half3 specular;
half perceptualRoughness;
half roughness;
half grazingTerm;
};
inline void InitializeSurfaceData(out SurfaceData outSurfaceData)
{
outSurfaceData.albedo = half3(1.0h, 1.0h, 1.0h);
outSurfaceData.specular = half3(0.0h, 0.0h, 0.0h);
outSurfaceData.metallic = 1.0h;
outSurfaceData.smoothness = 0.5h;
outSurfaceData.normal = half3(0.0h, 0.0h, 1.0h);
outSurfaceData.occlusion = 1.0h;
outSurfaceData.emission = half3(0.0h, 0.0h, 0.0h);
outSurfaceData.alpha = 1.0h;
outSurfaceData.ambient = half3(0.0h, 0.0h, 0.0h);
}
half SpecularReflectivity(half3 specular)
{
#if (SHADER_TARGET < 30)
// SM2.0: instruction count limitation
// SM2.0: simplified SpecularStrength
return specular.r; // Red channel - because most metals are either monocrhome or with redish/yellowish tint
#else
return max(max(specular.r, specular.g), specular.b);
#endif
}
inline void InitializeBRDFData(half3 albedo, half metallic, half3 specular, half smoothness, half alpha, out BRDFData outBRDFData)
{
// BRDF SETUP
#ifdef _METALLIC_SETUP
// We'll need oneMinusReflectivity, so
// 1-reflectivity = 1-lerp(dielectricSpec, 1, metallic) = lerp(1-dielectricSpec, 0, metallic)
// store (1-dielectricSpec) in kDieletricSpec.a, then
// 1-reflectivity = lerp(alpha, 0, metallic) = alpha + metallic*(0 - alpha) =
// = alpha - metallic * alpha
half oneMinusDielectricSpec = kDieletricSpec.a;
half oneMinusReflectivity = oneMinusDielectricSpec - metallic * oneMinusDielectricSpec;
half reflectivity = 1.0 - oneMinusReflectivity;
outBRDFData.diffuse = albedo * oneMinusReflectivity;
outBRDFData.specular = lerp(kDieletricSpec.rgb, albedo, metallic);
#else
half reflectivity = SpecularReflectivity(specular);
outBRDFData.diffuse = albedo * (half3(1.0h, 1.0h, 1.0h) - specular);
outBRDFData.specular = specular;
#endif
outBRDFData.grazingTerm = saturate(smoothness + reflectivity);
outBRDFData.perceptualRoughness = 1.0h - smoothness;
outBRDFData.roughness = outBRDFData.perceptualRoughness * outBRDFData.perceptualRoughness;
#ifdef _ALPHAPREMULTIPLY_ON
outBRDFData.diffuse *= alpha;
alpha = reflectivity + alpha * (1.0 - reflectivity);
#endif
}
// Based on Minimalist CookTorrance BRDF
// Implementation is slightly different from original derivation: http://www.thetenthplanet.de/archives/255

return (diffuse + specular) * atten;
}
half4 LightweightFragmentPBR(half4 lightmapUV, float3 positionWS, half3 normalWS, half3 tangentWS, half3 bitangentWS,
half3 viewDirectionWS, half fogFactor, half3 albedo, half metallic, half3 specular, half smoothness,
half3 normalTS, half ambientOcclusion, half3 emission, half alpha, half3 ambient)
{
BRDFData brdfData;
InitializeBRDFData(albedo, metallic, specular, smoothness, alpha, brdfData);
half3 vertexNormal = normalWS;
#if _NORMALMAP
normalWS = TangentToWorldNormal(normalTS, tangentWS, bitangentWS, normalWS);
#else
normalWS = normalize(normalWS);
#endif
half3 reflectVec = reflect(-viewDirectionWS, normalWS);
half roughness2 = brdfData.roughness * brdfData.roughness;
UnityIndirect indirectLight = LightweightGI(lightmapUV, ambient, normalWS, reflectVec, ambientOcclusion, brdfData.perceptualRoughness);
// PBS
half fresnelTerm = Pow4(1.0 - saturate(dot(normalWS, viewDirectionWS)));
half3 color = LightweightBRDFIndirect(brdfData, indirectLight, roughness2, fresnelTerm);
half3 lightDirectionWS;
#ifdef _MAIN_LIGHT
LightInput light;
INITIALIZE_MAIN_LIGHT(light);
half lightAtten = ComputeMainLightAttenuation(light, normalWS, positionWS, lightDirectionWS);
lightAtten *= LIGHTWEIGHT_SHADOW_ATTENUATION(positionWS, normalize(vertexNormal), _ShadowLightDirection.xyz);
half NdotL = saturate(dot(normalWS, lightDirectionWS));
half3 radiance = light.color * (lightAtten * NdotL);
color += LightweightBDRF(brdfData, roughness2, normalWS, lightDirectionWS, viewDirectionWS) * radiance;
#endif
#ifdef _ADDITIONAL_PIXEL_LIGHTS
int pixelLightCount = min(_AdditionalLightCount.x, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = 0; lightIter < pixelLightCount; ++lightIter)
{
LightInput light;
INITIALIZE_LIGHT(light, lightIter);
half lightAtten = ComputeLightAttenuation(light, normalWS, positionWS, lightDirectionWS);
half NdotL = saturate(dot(normalWS, lightDirectionWS));
half3 radiance = light.color * (lightAtten * NdotL);
color += LightweightBDRF(brdfData, roughness2, normalWS, lightDirectionWS, viewDirectionWS) * radiance;
}
#endif
color += emission;
// Computes fog factor per-vertex
ApplyFog(color, fogFactor);
return OutputColor(color, alpha);
}
#endif

163
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPassLit.cginc


#ifndef LIGHTWEIGHT_PASS_LIT_INCLUDED
#define LIGHTWEIGHT_PASS_LIT_INCLUDED
#include "LightweightCore.cginc"
#include "LightweightLighting.cginc"
struct LightweightVertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord : TEXCOORD0;
float2 lightmapUV : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct LightweightVertexOutput
{
float4 uv01 : TEXCOORD0; // uv01.xy: uv0, uv01.zw: uv1
float4 posWS : TEXCOORD1;
#if _NORMALMAP
half3 tangent : TEXCOORD2;
half3 binormal : TEXCOORD3;
half3 normal : TEXCOORD4;
#else
half3 normal : TEXCOORD2;
#endif
half4 viewDir : TEXCOORD5; // xyz: viewDir
half4 ambient : TEXCOORD6; // x: fogCoord, yzw: vertex/SH color
float4 clipPos : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
inline void InitializeStandardLitSurfaceData(LightweightVertexOutput IN, out SurfaceData outSurfaceData)
{
float2 uv = IN.uv01.xy;
half4 albedoAlpha = tex2D(_MainTex, uv);
half4 specGloss = MetallicSpecGloss(uv, albedoAlpha);
outSurfaceData.albedo = LIGHTWEIGHT_GAMMA_TO_LINEAR(albedoAlpha.rgb) * _Color.rgb;
#if _METALLIC_SETUP
outSurfaceData.metallic = specGloss.r;
outSurfaceData.specular = half3(0.0h, 0.0h, 0.0h);
#else
outSurfaceData.metallic = 1.0h;
outSurfaceData.specular = specGloss.rgb;
#endif
outSurfaceData.smoothness = specGloss.a;
outSurfaceData.normal = Normal(uv);
outSurfaceData.occlusion = OcclusionLW(uv);
outSurfaceData.emission = EmissionLW(uv);
outSurfaceData.alpha = Alpha(albedoAlpha.a);
outSurfaceData.ambient = IN.ambient.yzw;
}
void InitializeSurfaceInput(LightweightVertexOutput IN, out SurfaceInput outSurfaceInput)
{
#if LIGHTMAP_ON
outSurfaceInput.lightmapUV = float4(IN.uv01.zw, 0.0, 0.0);
#else
outSurfaceInput.lightmapUV = float4(0.0, 0.0, 0.0, 0.0);
#endif
#if _NORMALMAP
outSurfaceInput.tangentWS = IN.tangent;
outSurfaceInput.bitangentWS = IN.binormal;
#else
outSurfaceInput.tangentWS = half3(1.0h, 0.0h, 0.0h);
outSurfaceInput.bitangentWS = half3(0.0h, 1.0h, 0.0h);
#endif
outSurfaceInput.normalWS = IN.normal;
outSurfaceInput.positionWS = IN.posWS;
outSurfaceInput.viewDirectionWS = IN.viewDir;
outSurfaceInput.fogFactor = IN.ambient.x;
}
LightweightVertexOutput LitPassVertex(LightweightVertexInput v)
{

#if _NORMALMAP
half sign = v.tangent.w * unity_WorldTransformParams.w;
o.tangent = normalize(mul((float3x3)unity_ObjectToWorld, v.tangent.xyz));
o.tangent = normalize(mul((half3x3)unity_ObjectToWorld, v.tangent.xyz));
o.binormal = cross(normal, o.tangent) * sign;
o.normal = normal;
#else

#if !defined(LIGHTMAP_ON)
o.ambient.yzw = SHEvalLinearL2(half4(normal, 1.0));
#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);

//
// half3 lightDirection;
// half atten = ComputeLightAttenuationVertex(lightInput, normal, worldPos, lightDirection);
// o.fogCoord.yzw += LightingLambert(diffuse, lightDirection, normal, atten);
// o.ambient.yzw += LightingLambert(diffuse, lightDirection, normal, atten);
#if !defined(LIGHTMAP_ON)
o.fogCoord.yzw = SHEvalLinearL2(half4(normal, 1.0));
#endif
o.hpos = UnityObjectToClipPos(v.vertex);
o.fogCoord.x = ComputeFogFactor(o.hpos.z);
float4 clipPos = UnityObjectToClipPos(v.vertex);
o.ambient.x = ComputeFogFactor(clipPos.z);
o.clipPos = clipPos;
// NdotV, reflectVec (view tangent space)
// NdotL, (light in tangent space)
half4 LightweightFragmentPBR(SurfaceInput surfaceInput, SurfaceData surfaceData)
{
BRDFData brdfData;
InitializeBRDFData(surfaceData, brdfData);
half3 viewDir = surfaceInput.viewDir;
float3 posWS = surfaceInput.worldPos;
#if _NORMALMAP
half3 normalWorld = TangentToWorldNormal(surfaceData.normal, surfaceInput.tangent, surfaceInput.binormal, surfaceInput.normal);
#else
half3 normalWorld = normalize(surfaceInput.normal);
#endif
half3 reflectVec = reflect(-viewDir, normalWorld);
half roughness2 = brdfData.roughness * brdfData.roughness;
UnityIndirect indirectLight = LightweightGI(surfaceInput.lightmapUV, surfaceData.ambient, normalWorld, reflectVec, surfaceData.occlusion, brdfData.perceptualRoughness);
// PBS
half fresnelTerm = Pow4(1.0 - saturate(dot(normalWorld, viewDir)));
half3 color = LightweightBRDFIndirect(brdfData, indirectLight, roughness2, fresnelTerm);
half3 lightDirection;
#ifdef _MAIN_LIGHT
LightInput light;
INITIALIZE_MAIN_LIGHT(light);
half lightAtten = ComputeMainLightAttenuation(light, normalWorld, posWS, lightDirection);
lightAtten *= LIGHTWEIGHT_SHADOW_ATTENUATION(posWS, normalize(surfaceInput.normal), _ShadowLightDirection.xyz);
half NdotL = saturate(dot(normalWorld, lightDirection));
half3 radiance = light.color * (lightAtten * NdotL);
color += LightweightBDRF(brdfData, roughness2, normalWorld, lightDirection, viewDir) * radiance;
#endif
#ifdef _ADDITIONAL_PIXEL_LIGHTS
int pixelLightCount = min(_AdditionalLightCount.x, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = 0; lightIter < pixelLightCount; ++lightIter)
{
LightInput light;
INITIALIZE_LIGHT(light, lightIter);
half lightAtten = ComputeLightAttenuation(light, normalWorld, posWS, lightDirection);
half NdotL = saturate(dot(normalWorld, lightDirection));
half3 radiance = light.color * (lightAtten * NdotL);
color += LightweightBDRF(brdfData, roughness2, normalWorld, lightDirection, viewDir) * radiance;
}
#endif
color += surfaceData.emission;
// Computes fog factor per-vertex
ApplyFog(color, surfaceInput.fogFactor);
return OutputColor(color, surfaceData.alpha);
}
InitializeSurfaceData(IN, surfaceData);
InitializeStandardLitSurfaceData(IN, surfaceData);
return LightweightFragmentPBR(surfaceInput, surfaceData);
return LightweightFragmentPBR(surfaceInput.lightmapUV, surfaceInput.positionWS, surfaceInput.normalWS, surfaceInput.tangentWS, surfaceInput.bitangentWS, surfaceInput.viewDirectionWS, surfaceInput.fogFactor, surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.normal, surfaceData.occlusion, surfaceData.emission, surfaceData.alpha, surfaceData.ambient);
}
half4 LitPassFragmentSimple(LightweightVertexOutput IN) : SV_Target

#if defined(LIGHTMAP_ON)
half3 color = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, lightmapUV)) * diffuse;
#else
half3 color = (SHEvalLinearL0L1(half4(normalWorld, 1.0)) + IN.fogCoord.yzw) * diffuse;
half3 color = (SHEvalLinearL0L1(half4(normalWorld, 1.0)) + IN.ambient.yzw) * diffuse;
#endif
#ifdef _MAIN_LIGHT

lightAtten *= LIGHTWEIGHT_SHADOW_ATTENUATION(worldPos, normalize(IN.normal), _ShadowLightDirection.xyz);
#ifdef LIGHTWEIGHT_SPECULAR_HIGHLIGHTS
#if defined(_SPECGLOSSMAP) || defined(_SPECULAR_COLOR)
color += LightingBlinnPhong(diffuse, specularGloss, lightDirection, normalWorld, viewDir, lightAtten) * lightInput.color;
#else
color += LightingLambert(diffuse, lightDirection, normalWorld, lightAtten) * lightInput.color;

INITIALIZE_LIGHT(lightData, lightIter);
half lightAtten = ComputeLightAttenuation(lightData, normalWorld, worldPos, lightDirection);
#ifdef LIGHTWEIGHT_SPECULAR_HIGHLIGHTS
#if defined(_SPECGLOSSMAP) || defined(_SPECULAR_COLOR)
color += LightingBlinnPhong(diffuse, specularGloss, lightDirection, normalWorld, viewDir, lightAtten) * lightData.color;
#else
color += LightingLambert(diffuse, lightDirection, normalWorld, lightAtten) * lightData.color;

color += EmissionLW(uv);
// Computes Fog Factor per vextex
ApplyFog(color, IN.fogCoord.x);
ApplyFog(color, IN.ambient.x);
return OutputColor(color, alpha);
};

2
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightStandard.shader


#pragma vertex LitPassVertex
#pragma fragment LitPassFragment
#include "UnityCG.cginc"
#include "UnityStandardInput.cginc"
#include "LightweightPassLit.cginc"
ENDCG
}

2
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightStandardSimpleLighting.shader


#pragma vertex LitPassVertex
#pragma fragment LitPassFragmentSimple
#include "UnityCG.cginc"
#include "UnityStandardInput.cginc"
#include "LightweightPassLit.cginc"
ENDCG
}

9
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightInput.cginc.meta


fileFormatVersion: 2
guid: 926949d7f34e3e143a1acbf6270e42e0
timeCreated: 1488965025
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

227
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightInput.cginc


#ifndef LIGHTWEIGHT_INPUT_INCLUDED
#define LIGHTWEIGHT_INPUT_INCLUDED
#define MAX_VISIBLE_LIGHTS 16
// Main light initialized without indexing
#define INITIALIZE_MAIN_LIGHT(light) \
light.pos = _MainLightPosition; \
light.color = _MainLightColor; \
light.atten = _MainLightAttenuationParams; \
light.spotDir = _MainLightSpotDir;
// Indexing might have a performance hit for old mobile hardware
#define INITIALIZE_LIGHT(light, i) \
half4 indices = (i < 4) ? unity_4LightIndices0 : unity_4LightIndices1; \
int index = (i < 4) ? i : i - 4; \
int lightIndex = indices[index]; \
light.pos = _AdditionalLightPosition[lightIndex]; \
light.color = _AdditionalLightColor[lightIndex]; \
light.atten = _AdditionalLightAttenuationParams[lightIndex]; \
light.spotDir = _AdditionalLightSpotDir[lightIndex]
#if (defined(_MAIN_DIRECTIONAL_LIGHT) || defined(_MAIN_SPOT_LIGHT) || defined(_MAIN_POINT_LIGHT))
#define _MAIN_LIGHT
#endif
#ifdef _SPECULAR_SETUP
#define SAMPLE_METALLICSPECULAR(uv) tex2D(_SpecGlossMap, uv)
#else
#define SAMPLE_METALLICSPECULAR(uv) tex2D(_MetallicGlossMap, uv)
#endif
#if defined(UNITY_COLORSPACE_GAMMA) && defined(_LIGHTWEIGHT_FORCE_LINEAR)
#define LIGHTWEIGHT_GAMMA_TO_LINEAR(gammaColor) gammaColor * gammaColor
#define LIGHTWEIGHT_LINEAR_TO_GAMMA(linColor) sqrt(color)
#else
#define LIGHTWEIGHT_GAMMA_TO_LINEAR(color) color
#define LIGHTWEIGHT_LINEAR_TO_GAMMA(color) color
#endif
struct LightInput
{
float4 pos;
half4 color;
float4 atten;
half4 spotDir;
};
CBUFFER_START(_PerObject)
half4 unity_LightIndicesOffsetAndCount;
half4 unity_4LightIndices0;
half4 unity_4LightIndices1;
half _Shininess;
CBUFFER_END
CBUFFER_START(_PerCamera)
float4 _MainLightPosition;
half4 _MainLightColor;
float4 _MainLightAttenuationParams;
half4 _MainLightSpotDir;
half4 _AdditionalLightCount;
float4 _AdditionalLightPosition[MAX_VISIBLE_LIGHTS];
half4 _AdditionalLightColor[MAX_VISIBLE_LIGHTS];
float4 _AdditionalLightAttenuationParams[MAX_VISIBLE_LIGHTS];
half4 _AdditionalLightSpotDir[MAX_VISIBLE_LIGHTS];
CBUFFER_END
CBUFFER_START(_PerFrame)
half4 _GlossyEnvironmentColor;
sampler2D _AttenuationTexture;
CBUFFER_END
struct LightweightVertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord : TEXCOORD0;
float2 lightmapUV : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct LightweightVertexOutput
{
float4 uv01 : TEXCOORD0; // uv01.xy: uv0, uv01.zw: uv1
float4 posWS : TEXCOORD1;
#if _NORMALMAP
half3 tangent : TEXCOORD2;
half3 binormal : TEXCOORD3;
half3 normal : TEXCOORD4;
#else
half3 normal : TEXCOORD2;
#endif
half4 viewDir : TEXCOORD5; // xyz: viewDir
half4 fogCoord : TEXCOORD6; // x: fogCoord, yzw: vertexColor
float4 hpos : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
struct SurfaceData
{
half3 albedo;
half3 specular;
half metallic;
half smoothness;
half3 normal;
half3 emission;
half3 ambient;
half occlusion;
half alpha;
};
struct SurfaceInput
{
float4 lightmapUV;
half3 tangent;
half3 binormal;
half3 normal;
float3 worldPos;
half3 viewDir;
float fogFactor;
};
struct BRDFData
{
half3 diffuse;
half3 specular;
half perceptualRoughness;
half roughness;
half grazingTerm;
};
inline half Alpha(half albedoAlpha)
{
#if defined(_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A)
half alpha = _Color.a;
#else
half alpha = albedoAlpha * _Color.a;
#endif
#if defined(_ALPHATEST_ON)
clip(alpha - _Cutoff);
#endif
return alpha;
}
half3 Normal(float2 uv)
{
#if _NORMALMAP
return UnpackNormal(tex2D(_BumpMap, uv));
#else
return half3(0.0h, 0.0h, 1.0h);
#endif
}
inline void SpecularGloss(half2 uv, half alpha, out half4 specularGloss)
{
specularGloss = half4(0, 0, 0, 1);
#ifdef _SPECGLOSSMAP
specularGloss = tex2D(_SpecGlossMap, uv);
specularGloss.rgb = LIGHTWEIGHT_GAMMA_TO_LINEAR(specularGloss.rgb);
#elif defined(_SPECULAR_COLOR)
specularGloss = _SpecColor;
#endif
#ifdef _GLOSSINESS_FROM_BASE_ALPHA
specularGloss.a = alpha;
#endif
}
half4 MetallicSpecGloss(float2 uv, half albedoAlpha)
{
half4 specGloss;
#ifdef _METALLICSPECGLOSSMAP
specGloss = specGloss = SAMPLE_METALLICSPECULAR(uv);
#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
specGloss.a = albedoAlpha * _GlossMapScale;
#else
specGloss.a *= _GlossMapScale;
#endif
#else // _METALLICSPECGLOSSMAP
#if _METALLIC_SETUP
specGloss.rgb = _Metallic.rrr;
#else
specGloss.rgb = _SpecColor.rgb;
#endif
#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
specGloss.a = albedoAlpha * _GlossMapScale;
#else
specGloss.a = _Glossiness;
#endif
#endif
return specGloss;
}
half OcclusionLW(float2 uv)
{
#ifdef _OCCLUSIONMAP
#if (SHADER_TARGET < 30)
// SM20: instruction count limitation
// SM20: simpler occlusion
return tex2D(_OcclusionMap, uv).g;
#else
half occ = tex2D(_OcclusionMap, uv).g;
return LerpOneTo(occ, _OcclusionStrength);
#endif
#else
return 1.0;
#endif
}
half3 EmissionLW(float2 uv)
{
#ifndef _EMISSION
return 0;
#else
return LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_EmissionMap, uv).rgb) * _EmissionColor.rgb;
#endif
}
#endif
正在加载...
取消
保存