When making a similar RPG game, you may need to display a small map. One way to make a small map is to use another camera to render to a texture and display it in real time to the UI interface. Take the Unity 5.0 UI system as an example: Place a camera directly above the map, set its culling Mask, such as: Ground (ground), Minimapsign (player Monster logo), create a Render Texture,
Target TextureProperty points to this texture picture, as shown in the following illustration:
Create a UI panel because the small map here is intended to be made into a circle, using
MaskComponent and a mask image can achieve this effect. As shown in the Mapmask object above, add Rawimage to the mask image, add a mask component, as in the following image:
The Rawimage object under it is used to draw a small map texture and
TextureProperties point to the small map texture image, as shown in the following illustration:
The results of the operation are as follows:
But when the water effect is used in the scene, the water is not drawn to the small map, as shown in the following illustration:
But the textures generated by the camera are drawn, as shown in the following illustration:
Then there should be an error when drawing to the UI. Viewing the default Shader used by the UI and discovering that it incorporates blend blend Srcalpha Oneminussrcalpha, it is possible that the water portion of the original image has an alpha channel of 0, making the alpha blend transparent. The water material is
Horizon ColorOf
AlphaThe value is changed to 255, as shown in the following illustration:
The final effect is shown in the following figure:
In addition, use
MaskAlthough the components are relatively convenient, but not smooth enough, can also write Shader to implement the Mask function, the specific content of
Ui-default.shaderMake a copy slightly modified, named
Maskshader.shader, the contents are as follows: Shader Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
|
Shader "Uiex/default-mask" { Properties { [Perrendererdata] _maintex ("Sprite Texture", 2D) = "White" {} _mask ("Mask Texture", 2D) = "White" {} _color ("Tint", Color) = (1,1,1,1) _stencilcomp ("Stencil Comparison", Float) = 8 _stencil ("Stencil ID", Float) = 0 _stencilop ("stencil operation", Float) = 0 _stencilwritemask ("Stencil Write Mask", Float) = 255 _stencilreadmask ("Stencil Read Mask", Float) = 255 _colormask ("Color Mask", Float) = 15 } Subshader { Tags { "Queue" = "Transparent" "Ignoreprojector" = "True" "Rendertype" = "Transparent" "Previewtype" = "Plane" "Canusespriteatlas" = "True" } Stencil { Ref [_stencil] Comp [_stencilcomp] Pass [_stencilop] Readmask [_stencilreadmask] Writemask [_stencilwritemask] } Cull OFF Lighting OFF Zwrite OFF ZTest [Unity_guiztestmode] Blend Srcalpha Oneminussrcalpha Colormask [_colormask] Pass { Cgprogram #pragma vertex vert #pragma fragment Frag #include "Unitycg.cginc" struct appdata_t { FLOAT4 vertex:position; FLOAT4 Color:color; FLOAT2 texcoord:texcoord0; }; struct V2F { FLOAT4 vertex:sv_position; Fixed4 Color:color; Half2 texcoord:texcoord0; }; Fixed4 _color; v2f Vert (appdata_t in) { V2F out; Out.vertex = Mul (UNITY_MATRIX_MVP, In.vertex); Out.texcoord = In.texcoord; #ifdef Unity_half_texel_offset OUT.vertex.xy + = (_screenparams.zw-1.0) *float2 ( -1,1); #endif Out.color = In.color * _COLOR; return out; } Sampler2d _maintex; Sampler2d _mask; Fixed4 Frag (v2f in): Sv_target { Half4 color = tex2d (_maintex, In.texcoord) * IN.COLOR; Clip (color.a-0.01); Half4 Color2 = tex2d (_mask, In.texcoord); Return Fixed4 (COLOR.R, COLOR.G, Color.b, COLOR2.R); } Endcg } } } |
Create a material, set it to this shader, set its mask picture, as shown in the following image:
Sets the properties of the small map Raw Image component, which
MaterialPoint to this material, as shown in the following illustration:
The final results are compared as follows: