浏览代码

More changes to support ShaderGraph + make it easier to support additional lit shaders.

/Add-support-for-light-specular-color-tint
Felipe Lira 7 年前
当前提交
e82227ea
共有 4 个文件被更改,包括 103 次插入47 次删除
  1. 42
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightCore.cginc
  2. 27
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightInput.cginc
  3. 69
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPassLit.cginc
  4. 12
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightStandard.shader

42
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightCore.cginc


#endif
outSurfaceData.smoothness = specGloss.a;
outSurfaceData.normalWorld = Normal(i);
outSurfaceData.normal = Normal(uv);
outSurfaceData.occlusion = OcclusionLW(uv);
outSurfaceData.emission = EmissionLW(uv);
outSurfaceData.alpha = Alpha(albedoAlpha.a);

half alpha = surfaceData.alpha;
outBRDFData.diffuse *= alpha;
surfaceData.alpha = reflectivity + alpha * (1.0 - reflectivity);
#endif
}
half3 TangentToWorldNormal(half3 normalTangent, LightweightVertexOutput IN)
{
#if _NORMALMAP
// 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)
return normalize(half3(dot(normalTangent, IN.tangentToWorld0), dot(normalTangent, IN.tangentToWorld1), dot(normalTangent, IN.tangentToWorld2)));
#else
return normalize(IN.normal);
#endif
}
float ComputeFogFactor(float z)
{
float clipZ_01 = UNITY_Z_0_FAR_FROM_CLIPSPACE(z);
#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;
#elif defined(FOG_EXP)
// factor = exp(-density*z)
float unityFogFactor = unity_FogParams.y * clipZ_01;
return saturate(exp2(-unityFogFactor));
#elif defined(FOG_EXP2)
// factor = exp(-(density*z)^2)
float unityFogFactor = unity_FogParams.x * clipZ_01;
return saturate(exp2(-unityFogFactor*unityFogFactor));
#else
return 0.0;
#endif
}
void ApplyFog(inout half3 color, float fogFactor)
{
#if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
color = lerp(unity_FogColor, color, fogFactor);
#endif
}

27
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightInput.cginc


half3 specular;
half metallic;
half smoothness;
half3 normalWorld;
half3 normal;
};
struct SurfaceInput
{
float3 tangentToWorld0; // tx, bx, nx
float3 tangentToWorld1; // ty, by, ny
float3 tangentToWorld2; // tz, bz, nz
float3 worldPos;
half3 viewDir;
float fogFactor;
};
struct BRDFData

return alpha;
}
inline half3 Normal(LightweightVertexOutput i)
half3 Normal(float2 uv)
#if _NORMALMAP
half3 normalTangent = UnpackNormal(tex2D(_BumpMap, i.uv01.xy));
// 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)
half3 normalWorld = normalize(half3(dot(normalTangent, i.tangentToWorld0), dot(normalTangent, i.tangentToWorld1), dot(normalTangent, i.tangentToWorld2)));
#if _NORMALMAP
return UnpackNormal(tex2D(_BumpMap, uv));
half3 normalWorld = normalize(i.normal);
return half3(0.0h, 0.0h, 1.0h);
return normalWorld;
}
inline void SpecularGloss(half2 uv, half alpha, out half4 specularGloss)

69
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPassLit.cginc


#endif
o.hpos = UnityObjectToClipPos(v.vertex);
UNITY_TRANSFER_FOG(o, o.hpos);
o.fogCoord.x = ComputeFogFactor(o.hpos.z);
half4 LightweightFragmentPBR(LightweightVertexOutput i, SurfaceData surfaceData)
// NdotV, reflectVec (view tangent space)
// NdotL, (light in tangent space)
half4 LightweightFragmentPBR(LightweightVertexOutput IN, SurfaceData surfaceData)
float2 lightmapUV = i.uv01.zw;
half3 normal = surfaceData.normalWorld;
half3 reflectVec = reflect(-i.viewDir.xyz, normal);
// Should we standardize/pass this as input
float2 lightmapUV = IN.uv01.zw;
float fogFactor = IN.fogCoord.x;
float3 SH_L2Coeff = IN.fogCoord.yzw;
half3 viewDir = IN.viewDir.xyz;
float3 posWS = IN.posWS.xyz;
half3 normalWorld = TangentToWorldNormal(surfaceData.normal, IN);
half3 reflectVec = reflect(-viewDir, normalWorld);
UnityIndirect indirectLight = LightweightGI(lightmapUV, i.fogCoord.yzw, normal, reflectVec, surfaceData.occlusion, brdfData.perceptualRoughness);
UnityIndirect indirectLight = LightweightGI(lightmapUV, SH_L2Coeff, normalWorld, reflectVec, surfaceData.occlusion, brdfData.perceptualRoughness);
half fresnelTerm = Pow4(1.0 - saturate(dot(normal, i.viewDir.xyz)));
half fresnelTerm = Pow4(1.0 - saturate(dot(normalWorld, viewDir)));
half3 color = LightweightBRDFIndirect(brdfData, indirectLight, roughness2, fresnelTerm);
half3 lightDirection;

half lightAtten = ComputeMainLightAttenuation(light, normal, i.posWS.xyz, lightDirection);
lightAtten *= LIGHTWEIGHT_SHADOW_ATTENUATION(i, _ShadowLightDirection.xyz);
half lightAtten = ComputeMainLightAttenuation(light, normalWorld, posWS, lightDirection);
lightAtten *= LIGHTWEIGHT_SHADOW_ATTENUATION(IN, _ShadowLightDirection.xyz);
half NdotL = saturate(dot(normal, lightDirection));
half NdotL = saturate(dot(normalWorld, lightDirection));
color += LightweightBDRF(brdfData, roughness2, normal, lightDirection, i.viewDir.xyz) * radiance;
color += LightweightBDRF(brdfData, roughness2, normalWorld, lightDirection, viewDir) * radiance;
#endif
#ifdef _ADDITIONAL_PIXEL_LIGHTS

LightInput light;
INITIALIZE_LIGHT(light, lightIter);
half lightAtten = ComputeLightAttenuation(light, normal, i.posWS.xyz, lightDirection);
half lightAtten = ComputeLightAttenuation(light, normalWorld, posWS, lightDirection);
half NdotL = saturate(dot(normal, lightDirection));
half NdotL = saturate(dot(normalWorld, lightDirection));
color += LightweightBDRF(brdfData, roughness2, normal, lightDirection, i.viewDir.xyz) * radiance;
color += LightweightBDRF(brdfData, roughness2, normalWorld, lightDirection, viewDir) * radiance;
UNITY_APPLY_FOG(i.fogCoord, color);
// Computes fog factor per-vertex
ApplyFog(color, fogFactor);
half4 LitPassFragment(LightweightVertexOutput i) : SV_Target
half4 LitPassFragment(LightweightVertexOutput IN) : SV_Target
InitializeSurfaceData(i, surfaceData);
InitializeSurfaceData(IN, surfaceData);
return LightweightFragmentPBR(i, surfaceData);
return LightweightFragmentPBR(IN, surfaceData);
}
half4 LitPassFragmentSimple(LightweightVertexOutput i) : SV_Target

clip(alpha - _Cutoff);
#endif
half3 normal = Normal(i);
half3 normalTangent = Normal(i.uv01.xy);
half3 normalWorld = TangentToWorldNormal(normalTangent, i);
half4 specularGloss;
SpecularGloss(i.uv01.xy, alpha, specularGloss);

#if defined(LIGHTMAP_ON)
half3 color = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv01.zw)) * diffuse;
#else
half3 color = (SHEvalLinearL0L1(half4(normal, 1.0)) + i.fogCoord.yzw) * diffuse;
half3 color = (SHEvalLinearL0L1(half4(normalWorld, 1.0)) + i.fogCoord.yzw) * diffuse;
half lightAtten = ComputeMainLightAttenuation(lightInput, normal, worldPos, lightDirection);
half lightAtten = ComputeMainLightAttenuation(lightInput, normalWorld, worldPos, lightDirection);
color += LightingBlinnPhong(diffuse, specularGloss, lightDirection, normal, viewDir, lightAtten) * lightInput.color;
color += LightingBlinnPhong(diffuse, specularGloss, lightDirection, normalWorld, viewDir, lightAtten) * lightInput.color;
color += LightingLambert(diffuse, lightDirection, normal, lightAtten) * lightInput.color;
color += LightingLambert(diffuse, lightDirection, normalWorld, lightAtten) * lightInput.color;
#endif
#endif

{
LightInput lightData;
INITIALIZE_LIGHT(lightData, lightIter);
half lightAtten = ComputeLightAttenuation(lightData, normal, worldPos, lightDirection);
half lightAtten = ComputeLightAttenuation(lightData, normalWorld, worldPos, lightDirection);
color += LightingBlinnPhong(diffuse, specularGloss, lightDirection, normal, viewDir, lightAtten) * lightData.color;
color += LightingBlinnPhong(diffuse, specularGloss, lightDirection, normalWorld, viewDir, lightAtten) * lightData.color;
color += LightingLambert(diffuse, lightDirection, normal, lightAtten) * lightData.color;
color += LightingLambert(diffuse, lightDirection, normalWorld, lightAtten) * lightData.color;
#endif
}

UNITY_APPLY_FOG(i.fogCoord, color);
// Computes Fog Factor per vextex
ApplyFog(color, i.fogCoord.x);
return OutputColor(color, alpha);
};

12
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightStandard.shader


#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_OFF
#pragma shader_feature _ _GLOSSYREFLECTIONS_OFF
#pragma shader_feature _ _OCCLUSIONMAP
#pragma shader_feature _PARALLAXMAP
#pragma shader_feature _METALLICSPECGLOSSMAP
#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
#pragma shader_feature _SPECULARHIGHLIGHTS_OFF
#pragma shader_feature _GLOSSYREFLECTIONS_OFF
#pragma shader_feature _OCCLUSIONMAP
#pragma multi_compile _ _MAIN_DIRECTIONAL_LIGHT _MAIN_SPOT_LIGHT _MAIN_POINT_LIGHT
#pragma multi_compile _ _ADDITIONAL_PIXEL_LIGHTS

正在加载...
取消
保存