Unity Ugui rawimage Rendering Small maps

Source: Internet
Author: User
Tags unity 5

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 TextureThe properties point to this texture picture as shown in:
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. For example, add rawimage to the mask image, add a mask component as shown in the Mapmask object:
The Rawimage object under it is used to draw a small map texture and TextureThe properties point to the small map texture picture, as shown in:
The effect is as follows:

But when the water effect is used in the scene, the water is not drawn to the small map, as shown in:
But the texture generated by the camera is drawn, as shown in:
Then there should be an error when drawing to the UI. View the default Shader used by the UI and discover that it uses a hybridBlend Srcalpha Oneminussrcalpha, it may be because the water portion of the original image has an alpha channel of 0, which makes the alpha blend transparent. The water material is Horizon ColorOf AlphaThe value is changed to 255, as shown in:
The final effect looks like this:

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 the material, set it to this shader, and set its mask picture as shown:
Sets the properties of the small map Raw Image component, which MaterialPoint to this material, as shown in:
The final results are compared as follows:

Unity Ugui rawimage Rendering Small maps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.