当前位置:   article > 正文

Unity URP世界空间后处理扫描圈效果Shader_unity 场景扫描 urp

unity 场景扫描 urp

实现原理

见这篇文章Unity Shader-深度相关知识总结与效果实现(LinearDepth,Reverse Z,世界坐标重建,软粒子,高度雾,运动模糊,扫描线效果)_puppet_master的专栏-CSDN博客_shader深度

 核心Shader代码

  1. Shader "Universal Render Pipeline/Dejavu/WorldDepthScanCircle"
  2. {
  3. Properties
  4. {
  5. _MainTex("Base (RGB)", 2D) = "white" {}
  6. [HDR]_ScanLineColor("_ScanLineColor (default = 1,1,1,1)", color) = (1,1,1,1)
  7. _ScanValue("ScanValue", float) = 0
  8. _ScanLineWidth("ScanLineWidth", float) = 1
  9. _ScanLightStrength("ScanLightStrength", float) = 1
  10. }
  11. HLSLINCLUDE
  12. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  13. CBUFFER_START(UnityPerMaterial)
  14. float4 _MainTex_ST;
  15. half4 _ScanLineColor;
  16. float _ScanValue;
  17. float _ScanLineWidth;
  18. float _ScanLightStrength;
  19. float _DistortFactor;
  20. float3 _Center;
  21. float _Radius;
  22. CBUFFER_END
  23. sampler2D _MainTex;
  24. // sampler2D _ScanTex;
  25. TEXTURE2D(_CameraDepthTexture);
  26. SAMPLER(sampler_CameraDepthTexture);
  27. struct appdata {
  28. float4 positionOS : POSITION;
  29. float2 uv : TEXCOORD0;
  30. UNITY_VERTEX_INPUT_INSTANCE_ID
  31. };
  32. struct v2f {
  33. float4 positionCS : SV_POSITION;
  34. float2 uv : TEXCOORD0;
  35. float3 viewRayWorld : TEXCOORD1;
  36. UNITY_VERTEX_OUTPUT_STEREO
  37. };
  38. //vertex shader
  39. v2f vert(appdata v)
  40. {
  41. v2f o;
  42. UNITY_SETUP_INSTANCE_ID(v);
  43. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  44. o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
  45. float sceneRawDepth = 1;
  46. #if defined(UNITY_REVERSED_Z)
  47. sceneRawDepth = 1 - sceneRawDepth;
  48. #endif
  49. float3 worldPos = ComputeWorldSpacePosition(v.uv, sceneRawDepth, UNITY_MATRIX_I_VP);
  50. o.viewRayWorld = worldPos - _WorldSpaceCameraPos.xyz;
  51. o.uv = v.uv;
  52. return o;
  53. }
  54. //fragment shader
  55. float4 frag(v2f i) : SV_Target
  56. {
  57. float4 screenCol = tex2D(_MainTex, i.uv);
  58. float sceneRawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.uv);
  59. float linear01Depth = Linear01Depth(sceneRawDepth, _ZBufferParams);
  60. float3 worldPos = _WorldSpaceCameraPos.xyz + (linear01Depth)*i.viewRayWorld;
  61. float3 distVector = worldPos - _Center;
  62. float distance = sqrt(distVector.x* distVector.x + distVector.z*distVector.z);
  63. if (distance > _Radius * _ScanValue && distance < _Radius * _ScanValue + _ScanLineWidth)
  64. {
  65. return screenCol * _ScanLightStrength * _ScanLineColor;
  66. }
  67. return screenCol;
  68. }
  69. ENDHLSL
  70. //开始SubShader
  71. SubShader
  72. {
  73. Tags { "RenderPipeline" = "UniversalPipeline" "RenderType" = "Overlay" "Queue" = "Transparent-499" "DisableBatching" = "True" }
  74. LOD 100
  75. ZTest Always Cull Off ZWrite Off
  76. Blend one OneMinusSrcAlpha
  77. Pass
  78. {
  79. Name "ScanLine"
  80. //后处理效果一般都是这几个状态
  81. //使用上面定义的vertex和fragment shader
  82. HLSLPROGRAM
  83. #pragma vertex vert
  84. #pragma fragment frag
  85. ENDHLSL
  86. }
  87. }
  88. //后处理效果一般不给fallback,如果不支持,不显示后处理即可
  89. }

实现效果

工程链接

GitHub - Dejavu0709/StudyForShader 中的DepthScanLine文件夹

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/101307?site
推荐阅读
相关标签
  

闽ICP备14008679号