StandardDSCoreForward.cginc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 
  2. #ifndef UNITY_STANDARD_CORE_FORWARD_INCLUDED
  3. #define UNITY_STANDARD_CORE_FORWARD_INCLUDED
  4. #if defined(UNITY_NO_FULL_STANDARD_SHADER)
  5. # define UNITY_STANDARD_SIMPLE 1
  6. #endif
  7. #include "UnityStandardConfig.cginc"
  8. #if UNITY_STANDARD_SIMPLE
  9. #include "UnityStandardCoreForwardSimple.cginc"
  10. half4 fragForwardBaseSimpleInternalDS(VertexOutputBaseSimple i, in float face : VFACE)
  11. {
  12. UNITY_APPLY_DITHER_CROSSFADE(i.pos.xy);
  13. FragmentCommonData s = FragmentSetupSimple(i);
  14. float _sign = sign(face);
  15. /// flip direction of normal based on sign of face
  16. float3 normal = s.normalWorld * _sign;
  17. s.normalWorld = normal;
  18. UnityLight mainLight = MainLightSimple(i, s);
  19. #if !defined(LIGHTMAP_ON) && defined(_NORMALMAP)
  20. half ndotl = saturate(dot(s.tangentSpaceNormal, i.tangentSpaceLightDir));
  21. #else
  22. half ndotl = saturate(dot(s.normalWorld, mainLight.dir));
  23. #endif
  24. //we can't have worldpos here (not enough interpolator on SM 2.0) so no shadow fade in that case.
  25. half shadowMaskAttenuation = UnitySampleBakedOcclusion(i.ambientOrLightmapUV, 0);
  26. half realtimeShadowAttenuation = SHADOW_ATTENUATION(i);
  27. half atten = UnityMixRealtimeAndBakedShadows(realtimeShadowAttenuation, shadowMaskAttenuation, 0);
  28. half occlusion = Occlusion(i.tex.xy);
  29. half rl = dot(REFLECTVEC_FOR_SPECULAR(i, s), LightDirForSpecular(i, mainLight));
  30. UnityGI gi = FragmentGI(s, occlusion, i.ambientOrLightmapUV, atten, mainLight);
  31. half3 attenuatedLightColor = gi.light.color * ndotl;
  32. half3 c = BRDF3_Indirect(s.diffColor, s.specColor, gi.indirect, PerVertexGrazingTerm(i, s), PerVertexFresnelTerm(i));
  33. c += BRDF3DirectSimple(s.diffColor, s.specColor, s.smoothness, rl) * attenuatedLightColor;
  34. c += Emission(i.tex.xy);
  35. UNITY_APPLY_FOG(i.fogCoord, c);
  36. return OutputForward(half4(c, 1), s.alpha);
  37. }
  38. half4 fragForwardAddSimpleInternalDS(VertexOutputForwardAddSimple i, in float face : VFACE)
  39. {
  40. UNITY_APPLY_DITHER_CROSSFADE(i.pos.xy);
  41. FragmentCommonData s = FragmentSetupSimpleAdd(i);
  42. float _sign = sign(face);
  43. /// flip direction of normal based on sign of face
  44. float3 normal = s.normalWorld * _sign;
  45. s.normalWorld = normal;
  46. half3 c = BRDF3DirectSimple(s.diffColor, s.specColor, s.smoothness, dot(REFLECTVEC_FOR_SPECULAR(i, s), i.lightDir));
  47. #if SPECULAR_HIGHLIGHTS // else diffColor has premultiplied light color
  48. c *= _LightColor0.rgb;
  49. #endif
  50. UNITY_LIGHT_ATTENUATION(atten, i, s.posWorld)
  51. c *= atten * saturate(dot(LightSpaceNormal(i, s), i.lightDir));
  52. UNITY_APPLY_FOG_COLOR(i.fogCoord, c.rgb, half4(0, 0, 0, 0)); // fog towards black in additive pass
  53. return OutputForward(half4(c, 1), s.alpha);
  54. }
  55. VertexOutputBaseSimple vertBase(VertexInput v) { return vertForwardBaseSimple(v); }
  56. VertexOutputForwardAddSimple vertAdd(VertexInput v) { return vertForwardAddSimple(v); }
  57. half4 fragBaseDS(VertexOutputBaseSimple i,in float face : VFACE) : SV_Target{ return fragForwardBaseSimpleInternalDS(i, face); }
  58. half4 fragAddDS(VertexOutputForwardAddSimple i, in float face : VFACE) : SV_Target{ return fragForwardAddSimpleInternalDS(i, face); }
  59. #else
  60. #include "UnityStandardCore.cginc"
  61. half4 fragForwardBaseInternalDS(VertexOutputForwardBase i, in float face : VFACE)
  62. {
  63. UNITY_APPLY_DITHER_CROSSFADE(i.pos.xy);
  64. FRAGMENT_SETUP(s)
  65. float _sign = sign(face);
  66. /// flip direction of normal based on sign of face
  67. float3 normal = s.normalWorld * _sign;
  68. s.normalWorld = normal;
  69. UNITY_SETUP_INSTANCE_ID(i);
  70. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  71. UnityLight mainLight = MainLight();
  72. UNITY_LIGHT_ATTENUATION(atten, i, s.posWorld);
  73. half occlusion = Occlusion(i.tex.xy);
  74. UnityGI gi = FragmentGI(s, occlusion, i.ambientOrLightmapUV, atten, mainLight);
  75. half4 c = UNITY_BRDF_PBS(s.diffColor, s.specColor, s.oneMinusReflectivity, s.smoothness, s.normalWorld, -s.eyeVec, gi.light, gi.indirect);
  76. c.rgb += Emission(i.tex.xy);
  77. UNITY_EXTRACT_FOG_FROM_EYE_VEC(i);
  78. UNITY_APPLY_FOG(_unity_fogCoord, c.rgb);
  79. return OutputForward(c, s.alpha);
  80. }
  81. half4 fragForwardAddInternalDS(VertexOutputForwardAdd i, in float face : VFACE)
  82. {
  83. UNITY_APPLY_DITHER_CROSSFADE(i.pos.xy);
  84. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  85. FRAGMENT_SETUP_FWDADD(s)
  86. float _sign = sign(face);
  87. /// flip direction of normal based on sign of face
  88. float3 normal = s.normalWorld * _sign;
  89. s.normalWorld = normal;
  90. UNITY_LIGHT_ATTENUATION(atten, i, s.posWorld)
  91. UnityLight light = AdditiveLight(IN_LIGHTDIR_FWDADD(i), atten);
  92. UnityIndirect noIndirect = ZeroIndirect();
  93. half4 c = UNITY_BRDF_PBS(s.diffColor, s.specColor, s.oneMinusReflectivity, s.smoothness, s.normalWorld, -s.eyeVec, light, noIndirect);
  94. UNITY_EXTRACT_FOG_FROM_EYE_VEC(i);
  95. UNITY_APPLY_FOG_COLOR(_unity_fogCoord, c.rgb, half4(0, 0, 0, 0)); // fog towards black in additive pass
  96. return OutputForward(c, s.alpha);
  97. }
  98. VertexOutputForwardBase vertBase(VertexInput v) { return vertForwardBase(v); }
  99. VertexOutputForwardAdd vertAdd(VertexInput v) { return vertForwardAdd(v); }
  100. half4 fragBaseDS(VertexOutputForwardBase i, in float face : VFACE) : SV_Target{ return fragForwardBaseInternalDS(i, face); }
  101. half4 fragAddDS(VertexOutputForwardAdd i, in float face : VFACE) : SV_Target{ return fragForwardAddInternalDS(i, face); }
  102. #endif
  103. #endif // UNITY_STANDARD_CORE_FORWARD_INCLUDED