Hi! I’m trying to mix 3 textures in shader: _MainTex, _MaskTex(RenderTexture) and _BrushTex.
When _BrushTex has opacity on the edges everything is fine:
But when _BrushTex has hard edges it looks not as I expected:
All textures has WrapMode.Clamp, so I don’t know where is the problem. I need to mix textures correctly, without stretching edges by X and Y axis. Can anyone help me?
Shader
Shader "MyShader/Paint with Preview" {
Properties {
_MainTex ("Main", 2D) = "white" {}
_MaskTex ("Mask", 2D) = "black" {}
_BrushTex ("Brush", 2D) = "black" {}
_BrushOffset ("Brush offset", Vector) = (0, 0, 0, 0)
_Color ("Main Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
SetTexture [_MainTex]
{
combine texture
}
}
Pass
{
Blend One OneMinusSrcAlpha
SetTexture [_MaskTex]
{
combine texture * previous
}
}
Pass
{
Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _BrushTex;
float4 _BrushOffset;
float4 _Color;
float4 frag (v2f_img i) : SV_Target
{
float4 result = tex2D(_BrushTex, float2(i.uv.x / _BrushOffset.z - _BrushOffset.x + 0.5f, i.uv.y / _BrushOffset.w - _BrushOffset.y + 0.5f)) * _Color;
return result;
}
ENDCG
}
}
}
_BrushOffset code
_BrushOffset - Vector4 for _MainTex / _BrushTex sizes ratio, and paint position:
var brushRatio = new Vector2(MainTextureWidth / (float)BrushTextureWidth, MainTextureHeight / (float)BrushTextureHeight);
var brushOffset = new Vector4(paintPositionUV.x / SourceTextureWidth * brushRatio.x,paintPositionUV.y / SourceTextureHeight * brushRatio.y, 1f / brushRatio.x, 1f / brushRatio.y);