[小明学Shader]10.百叶窗

1.代码

 1 Shader "Custom/百叶窗" {
 2     Properties {
 3         [PerRendererData]_MainTex ("Base (RGB)", 2D) = "white" {}
 4         _Count("Count",float)=1
 5         _Range("Range",Range(0,1))=1
 6     }
 7     SubShader {
 8         Tags{"Queue"="Transparent"}
 9         
10         Pass{
11         CGPROGRAM
12             #pragma vertex vert
13             #pragma fragment frag
14             #include "UnityCG.cginc"
15             
16             sampler2D _MainTex;
17             int _Count;
18             float _Range;
19             
20             struct vertOut{
21                 float4 pos:SV_POSITION;
22                 half2 texcoord:TEXCOORD;
23             };
24             
25             vertOut vert(appdata_full v){
26                 vertOut o;
27                 o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
28                 o.texcoord=v.texcoord;
29                 return o;
30             }
31             
32             half4 frag(vertOut i):Color{
33                 half4 c=tex2D(_MainTex,i.texcoord);
34                 half t=fmod(i.texcoord[1],1.0/_Count);
35                 clip(_Range/_Count-t);
36                 return c;
37             }
38             ENDCG
39         }
40     } 
41     FallBack "Diffuse"
42 }

2.效果

3.注意

  我们从程序中的到数据,在vert中处理后,再交给后面的流水管线,然后喂frag数据,才有了显示在屏幕上的最终图像.

  在计算位置时,用的mul(UNITY_MATRIX_MVP,v.vertex)计算出了渲染后像素点所在屏幕上的位置,texcoord是对应的在自身模型上的位置,用来和纹理对应.

原文地址:https://www.cnblogs.com/WongSiuming/p/5048971.html