原创内容,转载请标明出处
Shader "Custom/AddWatermarkShader" { Properties { _MainTex("Main Texture", 2D) = "white" {} _WatermarkTex("Watermark Texture", 2D) = "white" {} _WatermarkPosition("Watermark Position", Vector) = (0, 0, 122, 34) } SubShader { Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" } 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_TexelSize; sampler2D _WatermarkTex; float4 _WatermarkTex_TexelSize; float4 _WatermarkPosition; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } fixed4 frag(v2f i) : SV_Target { //计算水印贴图相对于底图的尺寸 float2 ratio = float2(_WatermarkTex_TexelSize.z / _MainTex_TexelSize.z, _WatermarkTex_TexelSize.w / _MainTex_TexelSize.w); if (i.uv.x >= _WatermarkPosition.x && i.uv.x <= _WatermarkPosition.x + ratio.x && i.uv.y >= _WatermarkPosition.y && i.uv.y <= _WatermarkPosition.y + ratio.y) { float2 watermarkUV = (i.uv - _WatermarkPosition.xy) / float2(ratio.x, ratio.y); fixed4 watermarkColor = tex2D(_WatermarkTex, watermarkUV); return watermarkColor; } else { fixed4 mainColor = tex2D(_MainTex, i.uv); return mainColor; } } ENDCG } } }
标签:贴图,MainTex,WatermarkTex,uv,vertex,水印,Unity,float4,TexelSize From: https://www.cnblogs.com/Jason-c/p/18046606