|
|
|
|
|
|
|
|
|
|
#if _NORMALMAP |
|
|
|
half sign = v.tangent.w * unity_WorldTransformParams.w; |
|
|
|
half3 tangent = UnityObjectToWorldDir(v.tangent); |
|
|
|
half3 binormal = cross(normal, tangent) * sign; |
|
|
|
|
|
|
|
// 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); |
|
|
|
o.tangent = normalize(mul((float3x3)unity_ObjectToWorld, v.tangent.xyz)); |
|
|
|
o.binormal = cross(normal, o.tangent) * sign; |
|
|
|
o.normal = normal; |
|
|
|
#else |
|
|
|
o.normal = normal; |
|
|
|
#endif |
|
|
|
|
|
|
// NdotV, reflectVec (view tangent space) |
|
|
|
// NdotL, (light in tangent space) |
|
|
|
|
|
|
|
half4 LightweightFragmentPBR(LightweightVertexOutput IN, SurfaceData surfaceData) |
|
|
|
half4 LightweightFragmentPBR(SurfaceInput surfaceInput, SurfaceData surfaceData) |
|
|
|
// 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 viewDir = surfaceInput.viewDir; |
|
|
|
float3 posWS = surfaceInput.worldPos; |
|
|
|
half3 normalWorld = TangentToWorldNormal(surfaceData.normal, IN); |
|
|
|
#if _NORMALMAP |
|
|
|
half3 normalWorld = TangentToWorldNormal(surfaceData.normal, surfaceInput.tangent, surfaceInput.binormal, surfaceInput.normal); |
|
|
|
#else |
|
|
|
half3 normalWorld = normalize(surfaceInput.normal); |
|
|
|
#endif |
|
|
|
UnityIndirect indirectLight = LightweightGI(lightmapUV, SH_L2Coeff, normalWorld, reflectVec, surfaceData.occlusion, brdfData.perceptualRoughness); |
|
|
|
UnityIndirect indirectLight = LightweightGI(surfaceInput.lightmapUV, surfaceData.ambient, normalWorld, reflectVec, surfaceData.occlusion, brdfData.perceptualRoughness); |
|
|
|
|
|
|
|
// PBS |
|
|
|
half fresnelTerm = Pow4(1.0 - saturate(dot(normalWorld, viewDir))); |
|
|
|
|
|
|
LightInput light; |
|
|
|
INITIALIZE_MAIN_LIGHT(light); |
|
|
|
half lightAtten = ComputeMainLightAttenuation(light, normalWorld, posWS, lightDirection); |
|
|
|
lightAtten *= LIGHTWEIGHT_SHADOW_ATTENUATION(IN, _ShadowLightDirection.xyz); |
|
|
|
lightAtten *= LIGHTWEIGHT_SHADOW_ATTENUATION(posWS, normalize(surfaceInput.normal), _ShadowLightDirection.xyz); |
|
|
|
|
|
|
|
half NdotL = saturate(dot(normalWorld, lightDirection)); |
|
|
|
half3 radiance = light.color * (lightAtten * NdotL); |
|
|
|
|
|
|
color += surfaceData.emission; |
|
|
|
|
|
|
|
// Computes fog factor per-vertex |
|
|
|
ApplyFog(color, fogFactor); |
|
|
|
ApplyFog(color, surfaceInput.fogFactor); |
|
|
|
return OutputColor(color, surfaceData.alpha); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
InitializeSurfaceData(IN, surfaceData); |
|
|
|
|
|
|
|
return LightweightFragmentPBR(IN, surfaceData); |
|
|
|
SurfaceInput surfaceInput; |
|
|
|
InitializeSurfaceInput(IN, surfaceInput); |
|
|
|
return LightweightFragmentPBR(surfaceInput, surfaceData); |
|
|
|
half4 LitPassFragmentSimple(LightweightVertexOutput i) : SV_Target |
|
|
|
half4 LitPassFragmentSimple(LightweightVertexOutput IN) : SV_Target |
|
|
|
half4 diffuseAlpha = tex2D(_MainTex, i.uv01.xy); |
|
|
|
float2 uv = IN.uv01.xy; |
|
|
|
float2 lightmapUV = IN.uv01.zw; |
|
|
|
|
|
|
|
half4 diffuseAlpha = tex2D(_MainTex, uv); |
|
|
|
half3 diffuse = LIGHTWEIGHT_GAMMA_TO_LINEAR(diffuseAlpha.rgb) * _Color.rgb; |
|
|
|
|
|
|
|
#ifdef _GLOSSINESS_FROM_BASE_ALPHA |
|
|
|
|
|
|
clip(alpha - _Cutoff); |
|
|
|
#endif |
|
|
|
|
|
|
|
half3 normalTangent = Normal(i.uv01.xy); |
|
|
|
half3 normalWorld = TangentToWorldNormal(normalTangent, i); |
|
|
|
#if _NORMALMAP |
|
|
|
half3 normalTangent = Normal(uv); |
|
|
|
half3 normalWorld = TangentToWorldNormal(normalTangent, IN.tangent, IN.binormal, IN.normal); |
|
|
|
#else |
|
|
|
half3 normalWorld = normalize(IN.normal); |
|
|
|
#endif |
|
|
|
SpecularGloss(i.uv01.xy, alpha, specularGloss); |
|
|
|
SpecularGloss(uv, alpha, specularGloss); |
|
|
|
half3 viewDir = i.viewDir.xyz; |
|
|
|
float3 worldPos = i.posWS.xyz; |
|
|
|
half3 viewDir = IN.viewDir.xyz; |
|
|
|
float3 worldPos = IN.posWS.xyz; |
|
|
|
half3 color = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv01.zw)) * diffuse; |
|
|
|
half3 color = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, lightmapUV)) * diffuse; |
|
|
|
half3 color = (SHEvalLinearL0L1(half4(normalWorld, 1.0)) + i.fogCoord.yzw) * diffuse; |
|
|
|
half3 color = (SHEvalLinearL0L1(half4(normalWorld, 1.0)) + IN.fogCoord.yzw) * diffuse; |
|
|
|
#endif |
|
|
|
|
|
|
|
#ifdef _MAIN_LIGHT |
|
|
|
|
|
|
lightAtten *= LIGHTWEIGHT_SHADOW_ATTENUATION(i, _ShadowLightDirection.xyz); |
|
|
|
lightAtten *= LIGHTWEIGHT_SHADOW_ATTENUATION(worldPos, normalize(IN.normal), _ShadowLightDirection.xyz); |
|
|
|
|
|
|
|
#ifdef LIGHTWEIGHT_SPECULAR_HIGHLIGHTS |
|
|
|
color += LightingBlinnPhong(diffuse, specularGloss, lightDirection, normalWorld, viewDir, lightAtten) * lightInput.color; |
|
|
|
|
|
|
|
|
|
|
#endif // _ADDITIONAL_PIXEL_LIGHTS |
|
|
|
|
|
|
|
color += EmissionLW(i.uv01.xy); |
|
|
|
color += EmissionLW(uv); |
|
|
|
ApplyFog(color, i.fogCoord.x); |
|
|
|
ApplyFog(color, IN.fogCoord.x); |
|
|
|
return OutputColor(color, alpha); |
|
|
|
}; |
|
|
|
|