ShaderGraphFunctions.hlsl 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef UNITY_GRAPHFUNCTIONS_LW_INCLUDED
  2. #define UNITY_GRAPHFUNCTIONS_LW_INCLUDED
  3. #define SHADERGRAPH_SAMPLE_SCENE_DEPTH(uv) shadergraph_LWSampleSceneDepth(uv)
  4. #define SHADERGRAPH_SAMPLE_SCENE_COLOR(uv) shadergraph_LWSampleSceneColor(uv)
  5. #define SHADERGRAPH_BAKED_GI(positionWS, normalWS, uvStaticLightmap, uvDynamicLightmap, applyScaling) shadergraph_LWBakedGI(positionWS, normalWS, uvStaticLightmap, uvDynamicLightmap, applyScaling)
  6. #define SHADERGRAPH_REFLECTION_PROBE(viewDir, normalOS, lod) shadergraph_LWReflectionProbe(viewDir, normalOS, lod)
  7. #define SHADERGRAPH_FOG(position, color, density) shadergraph_LWFog(position, color, density)
  8. #define SHADERGRAPH_AMBIENT_SKY unity_AmbientSky
  9. #define SHADERGRAPH_AMBIENT_EQUATOR unity_AmbientEquator
  10. #define SHADERGRAPH_AMBIENT_GROUND unity_AmbientGround
  11. #if defined(REQUIRE_DEPTH_TEXTURE)
  12. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  13. #endif
  14. #if defined(REQUIRE_OPAQUE_TEXTURE)
  15. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"
  16. #endif
  17. float shadergraph_LWSampleSceneDepth(float2 uv)
  18. {
  19. #if defined(REQUIRE_DEPTH_TEXTURE)
  20. return SampleSceneDepth(uv);
  21. #else
  22. return 0;
  23. #endif
  24. }
  25. float3 shadergraph_LWSampleSceneColor(float2 uv)
  26. {
  27. #if defined(REQUIRE_OPAQUE_TEXTURE)
  28. return SampleSceneColor(uv);
  29. #else
  30. return 0;
  31. #endif
  32. }
  33. float3 shadergraph_LWBakedGI(float3 positionWS, float3 normalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap, bool applyScaling)
  34. {
  35. #ifdef LIGHTMAP_ON
  36. if (applyScaling)
  37. uvStaticLightmap = uvStaticLightmap * unity_LightmapST.xy + unity_LightmapST.zw;
  38. return SampleLightmap(uvStaticLightmap, normalWS);
  39. #else
  40. return SampleSH(normalWS);
  41. #endif
  42. }
  43. float3 shadergraph_LWReflectionProbe(float3 viewDir, float3 normalOS, float lod)
  44. {
  45. float3 reflectVec = reflect(-viewDir, normalOS);
  46. return DecodeHDREnvironment(SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0, samplerunity_SpecCube0, reflectVec, lod), unity_SpecCube0_HDR);
  47. }
  48. void shadergraph_LWFog(float3 position, out float4 color, out float density)
  49. {
  50. color = unity_FogColor;
  51. density = ComputeFogFactor(TransformObjectToHClip(position).z);
  52. }
  53. // This function assumes the bitangent flip is encoded in tangentWS.w
  54. float3x3 BuildTangentToWorld(float4 tangentWS, float3 normalWS)
  55. {
  56. // tangentWS must not be normalized (mikkts requirement)
  57. // Normalize normalWS vector but keep the renormFactor to apply it to bitangent and tangent
  58. float3 unnormalizedNormalWS = normalWS;
  59. float renormFactor = 1.0 / length(unnormalizedNormalWS);
  60. // bitangent on the fly option in xnormal to reduce vertex shader outputs.
  61. // this is the mikktspace transformation (must use unnormalized attributes)
  62. float3x3 tangentToWorld = CreateTangentToWorld(unnormalizedNormalWS, tangentWS.xyz, tangentWS.w > 0.0 ? 1.0 : -1.0);
  63. // surface gradient based formulation requires a unit length initial normal. We can maintain compliance with mikkts
  64. // by uniformly scaling all 3 vectors since normalization of the perturbed normal will cancel it.
  65. tangentToWorld[0] = tangentToWorld[0] * renormFactor;
  66. tangentToWorld[1] = tangentToWorld[1] * renormFactor;
  67. tangentToWorld[2] = tangentToWorld[2] * renormFactor; // normalizes the interpolated vertex normal
  68. return tangentToWorld;
  69. }
  70. // Always include Shader Graph version
  71. // Always include last to avoid double macros
  72. #include "Packages/com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl"
  73. #endif // UNITY_GRAPHFUNCTIONS_LW_INCLUDED