一日一Shader·探照灯【SS

    技术2022-07-10  101

    接下来我要制作一个较复杂的shader,而每天能抽出来的时间更少了,所以我将这个shader拆分成了多个部分,然后每天实现一个功能。今天要实现的是扇形,需要用cs脚本控制位置、方向和弧度。

    原理是根据两条点斜式方程表示的直线,将可渲染区域限定在一个角度内。

    效果图:

    代码:

    Shader "MyShader/SS_11_0" { Properties { _MainTex ("Texture", 2D) = "white" {} _Color("Color",Color)=(1,1,1,1) _Size("Size",float)=30 _Dir("Dir",vector)=(1,1,1,1) _Pos("Pos",vector)=(1,1,1,1) } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { fixed4 vertex : POSITION; fixed2 uv : TEXCOORD0; }; struct v2f { fixed2 uv : TEXCOORD0; fixed4 vertex : SV_POSITION; }; sampler2D _MainTex; fixed4 _MainTex_ST; fixed4 _Color; fixed _Size; fixed4 _Dir; fixed4 _Pos; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); fixed tanA=_Dir.y/_Dir.x; fixed tanB=tanh(_Size); fixed tanC1=(tanA+tanB)/(1-tanA*tanB); fixed tanC2=(tanA-tanB)/(1+tanA*tanB); fixed a=(i.uv.y-_Pos.y)/(i.uv.x-_Pos.x); fixed2 d=i.uv- _Pos; if(_Dir.x>0){ if(a<=tanC1&&a>=tanC2&&d.x>=0) col=_Color; }else{ if(a<=tanC1&&a>=tanC2&&d.x<=0) col=_Color; } return col; } ENDCG } } }

    这个脚本写的还是有缺陷,垂直方向上不会渲染,应该是界限判断有问题。这个回头再研究下,今天就先到这吧。

    Processed: 0.012, SQL: 9