Lux URP Fast Outline.shader 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. Shader "Lux URP/Fast Outline"
  2. {
  3. Properties
  4. {
  5. [HeaderHelpLuxURP_URL(gpukpasbzt01)]
  6. [Header(Surface Options)]
  7. [Space(8)]
  8. [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest ("ZTest", Int) = 4
  9. [Enum(UnityEngine.Rendering.CullMode)] _Cull ("Culling", Float) = 2
  10. [Space(5)]
  11. [IntRange] _StencilRef ("Stencil Reference", Range (0, 255)) = 0
  12. [IntRange] _ReadMask (" Read Mask", Range (0, 255)) = 255
  13. [Enum(UnityEngine.Rendering.CompareFunction)] _StencilCompare ("Stencil Comparison", Int) = 6
  14. [Header(Outline)]
  15. [Space(8)]
  16. _Color ("Color", Color) = (1,1,1,1)
  17. _Border ("Width", Float) = 3
  18. [Space(5)]
  19. [Toggle(_APPLYFOG)] _ApplyFog("Enable Fog", Float) = 0.0
  20. }
  21. SubShader
  22. {
  23. Tags
  24. {
  25. "RenderPipeline" = "UniversalPipeline"
  26. "RenderType"="Opaque"
  27. "Queue"= "Transparent+59" // +59 smalltest to get drawn on top of transparents
  28. }
  29. Pass
  30. {
  31. Name "StandardUnlit"
  32. Tags{"LightMode" = "UniversalForward"}
  33. Stencil {
  34. Ref [_StencilRef]
  35. ReadMask [_ReadMask]
  36. Comp [_StencilCompare]
  37. Pass Keep
  38. }
  39. Blend SrcAlpha OneMinusSrcAlpha
  40. // We only use backfaces here – so we do not need a stencil test. Well - now we use stencil. so only front faces.
  41. // Cull Back
  42. Cull [_Cull]
  43. ZTest [_ZTest]
  44. // Make sure we do not get overwritten
  45. ZWrite On
  46. HLSLPROGRAM
  47. // Required to compile gles 2.0 with standard srp library
  48. #pragma prefer_hlslcc gles
  49. #pragma exclude_renderers d3d11_9x
  50. #pragma target 2.0
  51. #pragma shader_feature_local_fragment _APPLYFOG
  52. // -------------------------------------
  53. // Lightweight Pipeline keywords
  54. // -------------------------------------
  55. // Unity defined keywords
  56. #pragma multi_compile_fog
  57. //--------------------------------------
  58. // GPU Instancing
  59. #pragma multi_compile_instancing
  60. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  61. #pragma vertex vert
  62. #pragma fragment frag
  63. // Lighting include is needed because of GI
  64. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  65. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  66. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  67. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
  68. #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
  69. CBUFFER_START(UnityPerMaterial)
  70. half4 _Color;
  71. half _Border;
  72. CBUFFER_END
  73. struct VertexInput
  74. {
  75. float4 vertex : POSITION;
  76. float3 normal : NORMAL;
  77. UNITY_VERTEX_INPUT_INSTANCE_ID
  78. };
  79. struct VertexOutput
  80. {
  81. float4 position : POSITION;
  82. #if defined(_APPLYFOG)
  83. half fogCoord : TEXCOORD0;
  84. #endif
  85. UNITY_VERTEX_INPUT_INSTANCE_ID
  86. UNITY_VERTEX_OUTPUT_STEREO
  87. };
  88. VertexOutput vert (VertexInput v)
  89. {
  90. VertexOutput o = (VertexOutput)0;
  91. UNITY_SETUP_INSTANCE_ID(v);
  92. UNITY_TRANSFER_INSTANCE_ID(v, o);
  93. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  94. o.position = TransformObjectToHClip(v.vertex.xyz);
  95. #if defined(_APPLYFOG)
  96. o.fogCoord = ComputeFogFactor(o.position.z);
  97. #endif
  98. // Extrude
  99. if (_Border > 0.0h) {
  100. //float3 normal = mul(UNITY_MATRIX_MVP, float4(v.normal, 0)).xyz; // to clip space
  101. float3 normal = mul(GetWorldToHClipMatrix(), mul(GetObjectToWorldMatrix(), float4(v.normal, 0.0))).xyz;
  102. float2 offset = normalize(normal.xy);
  103. float2 ndc = _ScreenParams.xy * 0.5;
  104. o.position.xy += ((offset * _Border) / ndc * o.position.w);
  105. }
  106. return o;
  107. }
  108. half4 frag (VertexOutput input ) : SV_Target
  109. {
  110. UNITY_SETUP_INSTANCE_ID(input);
  111. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  112. #if defined(_APPLYFOG)
  113. _Color.rgb = MixFog(_Color.rgb, input.fogCoord);
  114. #endif
  115. return half4(_Color);
  116. }
  117. ENDHLSL
  118. }
  119. }
  120. FallBack "Hidden/InternalErrorShader"
  121. }