Lux URP Toon Outline.shader 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. Shader "Lux URP/Toon Outline"
  2. {
  3. Properties
  4. {
  5. [HeaderHelpLuxURP_URL(68hb5r7b3dnz)]
  6. [Space(8)]
  7. [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest ("ZTest", Int) = 4
  8. [Enum(UnityEngine.Rendering.CullMode)] _Cull ("Culling", Float) = 1
  9. [Header(Outline)]
  10. _Color ("Color", Color) = (0,0,0,1)
  11. _Border ("Width", Float) = 3
  12. [Toggle(_COMPENSATESCALE)]
  13. _CompensateScale (" Compensate Scale", Float) = 0
  14. [Toggle(_OUTLINEINSCREENSPACE)]
  15. _OutlineInScreenSpace (" Calculate width in Screen Space", Float) = 0
  16. }
  17. SubShader
  18. {
  19. Tags
  20. {
  21. "RenderPipeline" = "UniversalPipeline"
  22. "RenderType"="Opaque"
  23. "Queue"= "Geometry+1"
  24. }
  25. Pass
  26. {
  27. Name "StandardUnlit"
  28. Tags{"LightMode" = "UniversalForward"}
  29. Blend SrcAlpha OneMinusSrcAlpha
  30. Cull[_Cull]
  31. ZTest [_ZTest]
  32. // Make sure we do not get overwritten
  33. ZWrite On
  34. HLSLPROGRAM
  35. // Required to compile gles 2.0 with standard srp library
  36. #pragma prefer_hlslcc gles
  37. #pragma exclude_renderers d3d11_9x
  38. #pragma target 2.0
  39. #pragma shader_feature_local _COMPENSATESCALE
  40. #pragma shader_feature_local _OUTLINEINSCREENSPACE
  41. // -------------------------------------
  42. // Lightweight Pipeline keywords
  43. // -------------------------------------
  44. // Unity defined keywords
  45. #pragma multi_compile_fog
  46. //--------------------------------------
  47. // GPU Instancing
  48. #pragma multi_compile_instancing
  49. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  50. #pragma vertex vert
  51. #pragma fragment frag
  52. // Lighting include is needed because of GI
  53. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  54. CBUFFER_START(UnityPerMaterial)
  55. half4 _Color;
  56. half _Border;
  57. CBUFFER_END
  58. struct VertexInput
  59. {
  60. float4 vertex : POSITION;
  61. float3 normal : NORMAL;
  62. UNITY_VERTEX_INPUT_INSTANCE_ID
  63. };
  64. struct VertexOutput
  65. {
  66. float4 position : POSITION;
  67. half fogCoord : TEXCOORD0;
  68. UNITY_VERTEX_INPUT_INSTANCE_ID
  69. UNITY_VERTEX_OUTPUT_STEREO
  70. };
  71. VertexOutput vert (VertexInput v)
  72. {
  73. VertexOutput o = (VertexOutput)0;
  74. UNITY_SETUP_INSTANCE_ID(v);
  75. UNITY_TRANSFER_INSTANCE_ID(v, o);
  76. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  77. // Extrude
  78. #if !defined(_OUTLINEINSCREENSPACE)
  79. #if defined(_COMPENSATESCALE)
  80. float3 scale;
  81. scale.x = length(float3(UNITY_MATRIX_M[0].x, UNITY_MATRIX_M[1].x, UNITY_MATRIX_M[2].x));
  82. scale.y = length(float3(UNITY_MATRIX_M[0].y, UNITY_MATRIX_M[1].y, UNITY_MATRIX_M[2].y));
  83. scale.z = length(float3(UNITY_MATRIX_M[0].z, UNITY_MATRIX_M[1].z, UNITY_MATRIX_M[2].z));
  84. #endif
  85. v.vertex.xyz += v.normal * 0.001 * _Border
  86. #if defined(_COMPENSATESCALE)
  87. / scale
  88. #endif
  89. ;
  90. #endif
  91. o.position = TransformObjectToHClip(v.vertex.xyz);
  92. o.fogCoord = ComputeFogFactor(o.position.z);
  93. // Extrude
  94. #if defined(_OUTLINEINSCREENSPACE)
  95. if (_Border > 0.0h) {
  96. //float3 normal = mul(UNITY_MATRIX_MVP, float4(v.normal, 0)).xyz; // to clip space
  97. float3 normal = mul(GetWorldToHClipMatrix(), mul(GetObjectToWorldMatrix(), float4(v.normal, 0.0))).xyz;
  98. float2 offset = normalize(normal.xy);
  99. float2 ndc = _ScreenParams.xy * 0.5;
  100. o.position.xy += ((offset * _Border) / ndc * o.position.w);
  101. }
  102. #endif
  103. return o;
  104. }
  105. half4 frag (VertexOutput input ) : SV_Target
  106. {
  107. UNITY_SETUP_INSTANCE_ID(input);
  108. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  109. _Color.rgb = MixFog(_Color.rgb, input.fogCoord);
  110. return half4(_Color);
  111. }
  112. ENDHLSL
  113. }
  114. }
  115. FallBack "Hidden/InternalErrorShader"
  116. }