屏幕坐标——挖空效果

作者:追风剑情 发布于:2017-5-13 14:55 分类:Shader

一、创建ScreenCutout.shader


  1. Shader "Custom/Screen Cutout"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. _ClipX0 ("Clip x0", float) = 0
  7. _ClipY0 ("Clip y0", float) = 0
  8. _ClipX1 ("Clip x1", float) = 0
  9. _ClipY1 ("Clip y1", float) = 0
  10. }
  11. SubShader
  12. {
  13. Pass
  14. {
  15. CGPROGRAM
  16. #pragma vertex vert
  17. #pragma fragment frag
  18. #pragma target 3.0 //屏幕坐标必须使用3.0
  19. #include "UnityCG.cginc"
  20.  
  21. // note: no SV_POSITION in this struct
  22. struct v2f {
  23. float2 uv : TEXCOORD0;
  24. };
  25.  
  26. v2f vert (
  27. float4 vertex : POSITION, // vertex position input
  28. float2 uv : TEXCOORD0, // texture coordinate input
  29. out float4 outpos : SV_POSITION // clip space position output
  30. )
  31. {
  32. v2f o;
  33. o.uv = uv;
  34. outpos = UnityObjectToClipPos(vertex);
  35. return o;
  36. }
  37.  
  38. sampler2D _MainTex;
  39. float _ClipX0;
  40. float _ClipY0;
  41. float _ClipX1;
  42. float _ClipY1;
  43.  
  44. fixed4 frag (v2f i, UNITY_VPOS_TYPE screenPos : VPOS) : SV_Target
  45. {
  46. if(screenPos.x > _ClipX0 && screenPos.x < _ClipX1 && screenPos.y > _ClipY0 && screenPos.y < _ClipY1)
  47. clip(-1);//结束渲染
  48.  
  49. fixed4 c = tex2D (_MainTex, i.uv);
  50. return c;
  51. }
  52. ENDCG
  53. }
  54. }
  55. }


222.png

效果

1111.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号