要做这个功能有两个简单的方法
一种是在用程序生成一张图片,然后根据毒气圈和安全区位置和大小往里面填颜色
另一种渲染小地图这种图片的同时用shader把毒气圈和安全区位置都画出来
第一种方法太麻烦了,如果生成的图片太大,性能也不好所以这里只介绍第二种方法
- Shader "Unlit/ImageCircle"
- {
- Properties
- {
- _MainTex ("Texture", 2D) = "white" {}
- _CircleColor1("CircleColor1", Color) = (1,1,1,1)
- _Radius1("Radius1", Float) = 0.2
- _CircleWidth1("CircleWidth1", Float) = 0.1
- _CircleColor2("CircleColor2", Color) = (1,1,1,1)
- _Radius2("Radius2", Float) = 0.2
- _CircleWidth2("CircleWidth2", Float) = 0.1
- _CenterPos("CenterPos", Vector) = (0.5, 0.5, 0.5, 0.5) //两个圈的中心位置
- }
- SubShader
- {
- Tags
- {
- "Queue" = "Transparent"
- "RenderType"="Transparent"
- "IgnoreProjector" = "True"
- "PreviewType" = "Plane"
- "CanUseSpriteAtlas" = "True"
- }
- LOD 100
- Blend SrcAlpha OneMinusSrcAlpha
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #include "UnityCG.cginc"
- struct appdata
- {
- float4 vertex : POSITION;
- float2 uv : TEXCOORD0;
- };
- struct v2f
- {
- float2 uv : TEXCOORD0;
- float4 vertex : SV_POSITION;
- };
- sampler2D _MainTex;
- float4 _MainTex_ST;
- fixed4 _CircleColor1;
- fixed _Radius1;
- fixed _CircleWidth1;
- fixed4 _CircleColor2;
- fixed _Radius2;
- fixed _CircleWidth2;
- Vector _CenterPos;
- v2f vert (appdata v)
- {
- v2f o;
- o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
- o.uv = TRANSFORM_TEX(v.uv, _MainTex);
- return o;
- }
- fixed4 frag (v2f i) : SV_Target
- {
- fixed4 col = tex2D(_MainTex, i.uv);
- float2 center = i.uv - float2(_CenterPos.x,_CenterPos.y);
- float dis = sqrt(center.x * center.x + center.y * center.y);
- float minDistance = _Radius1 - _CircleWidth1;
- if(dis < _Radius1 && dis > minDistance)
- {
- col += _CircleColor1;
- }
- center = i.uv - float2(_CenterPos.z,_CenterPos.w);
- dis = sqrt(center.x * center.x + center.y * center.y);
- minDistance = _Radius2 - _CircleWidth2;
- if(dis < _Radius2 && dis > minDistance)
- {
- col += _CircleColor2;
- }
- return col;
- }
- ENDCG
- }
- }
- }