浏览代码

More refactor changes to support ShaderGraph

/Add-support-for-light-specular-color-tint
Felipe Lira 7 年前
当前提交
6c874f37
共有 5 个文件被更改,包括 79 次插入61 次删除
  1. 41
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightCore.cginc
  2. 14
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightInput.cginc
  3. 4
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightLighting.cginc
  4. 67
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPassLit.cginc
  5. 14
      ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightShadows.cginc

41
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightCore.cginc


#endif
}
inline void InitializeSurfaceData(LightweightVertexOutput i, out SurfaceData outSurfaceData)
inline void InitializeSurfaceData(LightweightVertexOutput IN, out SurfaceData outSurfaceData)
float2 uv = i.uv01.xy;
float2 uv = IN.uv01.xy;
half4 albedoAlpha = tex2D(_MainTex, uv);
half4 specGloss = MetallicSpecGloss(uv, albedoAlpha);

outSurfaceData.normal = Normal(uv);
outSurfaceData.occlusion = OcclusionLW(uv);
outSurfaceData.emission = EmissionLW(uv);
outSurfaceData.ambient = IN.fogCoord.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.tangent = IN.tangent;
outSurfaceInput.binormal = IN.binormal;
#else
outSurfaceInput.tangent = half3(1.0h, 0.0h, 0.0h);
outSurfaceInput.binormal = half3(0.0h, 1.0h, 0.0h);
#endif
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

#endif
}
half3 TangentToWorldNormal(half3 normalTangent, LightweightVertexOutput IN)
half3 TangentToWorldNormal(half3 normalTangent, half3 tangent, half3 binormal, half3 normal)
#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
half3x3 tangentToWorld = half3x3(tangent, binormal, normal);
return normalize(mul(normalTangent, tangentToWorld));
}
float ComputeFogFactor(float z)

half4 OutputColor(half3 color, half alpha)
{
#if defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_ON)
return LIGHTWEIGHT_LINEAR_TO_GAMMA(half4(color, alpha));
return half4(LIGHTWEIGHT_LINEAR_TO_GAMMA(color), alpha);
#else
return half4(LIGHTWEIGHT_LINEAR_TO_GAMMA(color), 1);
#endif

14
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightInput.cginc


float4 uv01 : TEXCOORD0; // uv01.xy: uv0, uv01.zw: uv1
float4 posWS : TEXCOORD1;
#if _NORMALMAP
half3 tangentToWorld0 : TEXCOORD2; // tangentToWorld matrix
half3 tangentToWorld1 : TEXCOORD3; // tangentToWorld matrix
half3 tangentToWorld2 : TEXCOORD4; // tangentToWorld matrix
half3 tangent : TEXCOORD2;
half3 binormal : TEXCOORD3;
half3 normal : TEXCOORD4;
#else
half3 normal : TEXCOORD2;
#endif

half smoothness;
half3 normal;
half3 emission;
half3 ambient;
half occlusion;
half alpha;
};

float3 tangentToWorld0; // tx, bx, nx
float3 tangentToWorld1; // ty, by, ny
float3 tangentToWorld2; // tz, bz, nz
float4 lightmapUV;
half3 tangent;
half3 binormal;
half3 normal;
float3 worldPos;
half3 viewDir;
float fogFactor;

4
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightLighting.cginc


return c;
}
UnityIndirect LightweightGI(float2 lightmapUV, half3 ambientColor, half3 normalWorld, half3 reflectVec, half occlusion, half perceptualRoughness)
UnityIndirect LightweightGI(float4 lightmapUV, half3 ambientColor, half3 normalWorld, half3 reflectVec, half occlusion, half perceptualRoughness)
ambientColor = (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, lightmapUV)));
ambientColor += (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, lightmapUV.xy)));
#else
ambientColor += SHEvalLinearL0L1(half4(normalWorld, 1.0));
ambientColor = max(half3(0.0, 0.0, 0.0), ambientColor);

67
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPassLit.cginc


#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);
};

14
ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightShadows.cginc


#endif
#ifdef _SHADOWS
#define LIGHTWEIGHT_SHADOW_ATTENUATION(vertexOutput, shadowDir) ComputeShadowAttenuation(vertexOutput, shadowDir)
#define LIGHTWEIGHT_SHADOW_ATTENUATION(posWorld, vertexNormal, shadowDir) ComputeShadowAttenuation(posWorld, vertexNormal, shadowDir)
#define LIGHTWEIGHT_SHADOW_ATTENUATION(vertexOutput, shadowDir) 1.0h
#define LIGHTWEIGHT_SHADOW_ATTENUATION(posWorld, vertexNormal, shadowDir) 1.0h
#endif
sampler2D_float _ShadowMap;

return attenuation * 0.25;
}
inline half ComputeShadowAttenuation(LightweightVertexOutput i, half3 shadowDir)
inline half ComputeShadowAttenuation(float3 posWorld, half3 vertexNormal, half3 shadowDir)
#if _NORMALMAP
half3 vertexNormal = half3(i.tangentToWorld0.z, i.tangentToWorld1.z, i.tangentToWorld2.z);
#else
half3 vertexNormal = i.normal;
#endif
float3 posWorldOffsetNormal = i.posWS + vertexNormal * bias;
float3 posWorldOffsetNormal = posWorld + vertexNormal * bias;
int cascadeIndex = 0;
#ifdef _SHADOW_CASCADES

正在加载...
取消
保存