一.实现简单的Text+贴图效果
我们先效果如下:
原理:
UGUI的Text和Image本身就支持Material,我们添加PositionAsUV组件,让Text识别到UV变化就行,然后接下来就是我们给材质添加一张贴图就行了。
-
1.创建一个Text文本框,调整合适的大小,填写测试文字 -
2.添加Position As UV1 组件,给Text加材质比加组件 -
3.创建Material材质,附加shader:UI/Unlit/Text Detail -
4.给Text的Material属性赋值材质。 -
5.给材质添加Detail(RGB)贴图,自定义 -
6.设置Canvas的Additional Shader Channles 选中TexCoord1
随着我们修改Text的大小,我们可以看到纹理的UV会变化,这些就是Position As UV1组件的作用,他会根据Text中字体的位置修改UV坐标
二.实现Text+贴图炫光流动
原理
和上面原理相同,只不过我们稍微修改下系统的TextDetail.shader 功能添加:
- 1.添加一个NoiseTex 噪波扰动图,
- 2.使用Time,修改NoiseTex的UV,然后传递给DetialTex,使图片的UV流动。
效果如下:
shader代码如下:
Shader "UI/Unlit/Custom Text Detail"
{
Properties
{
[PerRendererData]
_MainTex ("Alpha (A)", 2D) = "white" {}
[PerRendererData]
_Color ("Main Color", Color) = (1,1,1,1)
_DetailTex ("Detail (RGB)", 2D) = "white" {}
_detail_panner_u ("tex01_panner_u", Float ) = 0
_detail_panner_v ("tex01_panner_v", Float ) = 0
_detail_strong ("tex01_strong", Range(0.0, 1.0)) = 1
_NoiseTex ("Noise (R)", 2D) = "white" {}
_noise_strong ("tex02_strong", Float ) = 1
_noise_panner_u ("tex02_panner_u", Float ) = 0
_noise_panner_v ("tex02_panner_v", Float ) = 0
_flow_color("flow_color", Color) = (1,1,1,1)
_flow_strong("flow_strong", Float) = 1
[hideinInspector]
_StencilComp ("Stencil Comparison", Float) = 8
[hideinInspector]
_Stencil ("Stencil ID", Float) = 0
[hideinInspector]
_StencilOp ("Stencil Operation", Float) = 0
[hideinInspector]
_StencilWriteMask ("Stencil Write Mask", Float) = 255
[hideinInspector]
_StencilReadMask ("Stencil Read Mask", Float) = 255
[hideinInspector]
_ColorMask ("Color Mask", Float) = 15
}
SubShader
{
LOD 200
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType"="Plane"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Offset -1, -1
Blend one OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord2 : TEXCOORD1;
fixed4 color : COLOR;
};
struct v2f
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord2 : TEXCOORD1;
fixed4 color : COLOR;
};
sampler2D _MainTex;
sampler2D _DetailTex;
sampler2D _NoiseTex;
float4 _MainTex_ST;
float4 _DetailTex_ST;
float4 _NoiseTex_ST;
float4 _DetailTex_TexelSize;
fixed4 _Color;
fixed _detail_strong;
fixed _noise_strong;
fixed _detail_panner_u;
fixed _detail_panner_v;
fixed _noise_panner_u;
fixed _noise_panner_v;
fixed4 _flow_color;
fixed _flow_strong;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
o.texcoord2 = TRANSFORM_TEX(v.texcoord2 * _DetailTex_TexelSize.xy, _DetailTex);
o.color = v.color;
return o;
}
half4 frag (v2f i) : COLOR
{
half4 col = i.color*_Color;
float2 dist_uv = i.texcoord2+(_Time.g*float2(_noise_panner_u,_noise_panner_u));
float4 noise = tex2D(_NoiseTex,TRANSFORM_TEX(dist_uv, _NoiseTex));
float2 detail_uv = i.texcoord2+_Time.g*float2(_detail_panner_u,_detail_panner_v)+noise.r*_noise_strong;
fixed4 detail = tex2D(_DetailTex, detail_uv) + noise.r*_flow_color*_flow_strong;
col.rgb = lerp(col.rgb, col.rgb * detail.rgb, detail.a * _detail_strong);
col.a *= tex2D(_MainTex, i.texcoord).a;
clip (col.a - 0.01);
return col;
}
ENDCG
}
}
}
好了,这篇文章就到这里,希望对你有所帮助
|