Add square mask brush with opacity blending to mesh painter

Replace the 3D sphere brush with a square stamp in the surface tangent
plane whose shape and opacity come from a grayscale/alpha mask. Masks
are assignable via an inspector list with a live-switchable active index;
an empty list falls back to a procedurally generated circle mask that
reproduces the previous round brush.

- Mask drives opacity (luma * alpha * brushOpacity) via source-over blend
  instead of a hard threshold cut; gray 0.5 = 50%.
- GPU and CPU paths stay identical; non-readable textures are supported on
  the CPU via GPU blit-readback.
- Dilate pass treats any alpha > 0 as painted so soft edges survive seam
  padding.
- Brush alignment enum (Camera/World) plus manual rotation; tangent frame
  handedness fixed so stamps read upright and unmirrored in both modes.
This commit is contained in:
admin committed 2026-06-23 11:28:19 +02:00
1 parent 680d9eafea
commit c67d519f68
3 files changed
+327 -35

No files matched your search

+42 -13
View File
@@ -1,18 +1,25 @@
// GPU brush: paints a 3D sphere seamlessly into the paint texture.
// GPU brush: paints a square mask stamp seamlessly into the paint texture.
//
// In the vertex shader the mesh is "unwrapped" into the texture layout using its
// UVs (uv -> clip space), while the actual local 3D position of the vertex is
// passed through to the fragment shader. There, per texel, the distance to the
// brush center (_BrushCenter, in local mesh space) is tested against _BrushRadius.
// passed through to the fragment shader. There, per texel, the offset to the brush
// center (_BrushCenter, in local mesh space) is projected onto the stamp's tangent
// frame: _BrushU (right), _BrushV (up) and _BrushNormal. The texel is painted only
// if it lies inside the box (|du|,|dv|,|dn| <= _BrushRadius); the brush mask
// (_MaskTex, sampled along du/dv) then provides the per-texel OPACITY:
// opacity = luma(mask.rgb) * mask.a * _BrushOpacity
// and the color is alpha-blended over the existing paint (source-over). So a mask
// gray value of 0.5 paints at 50%, white = full, black/transparent = nothing.
//
// This bridges UV seams automatically: triangles on both sides of a seam share
// the same 3D edge and are captured by the same brush stroke, no matter where
// they lie in the UV layout (identical behavior to the CPU path).
// This bridges UV seams automatically: triangles on both sides of a seam share the
// same 3D edge and are captured by the same brush stamp, no matter where they lie in
// the UV layout (identical behavior to the CPU path).
Shader "Hidden/MeshPaintBrush"
{
Properties
{
_BrushColor ("Brush Color", Color) = (1,0,0,1)
_MaskTex ("Brush Mask", 2D) = "white" {}
}
SubShader
{
@@ -22,7 +29,9 @@ Shader "Hidden/MeshPaintBrush"
ZTest Always
ZWrite Off
Cull Off
Blend Off
// Source-over for color; alpha accumulates coverage toward 1 (painted marker
// for the dilation pass). Soft edges keep partial alpha.
Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
@@ -31,7 +40,12 @@ Shader "Hidden/MeshPaintBrush"
float4 _BrushColor;
float3 _BrushCenter; // brush center in local mesh space
float _BrushRadius; // brush radius in local mesh space
float3 _BrushU; // stamp right axis (unit, local space)
float3 _BrushV; // stamp up axis (unit, local space)
float3 _BrushNormal; // surface normal at the hit (unit, local space)
float _BrushRadius; // stamp half-extent in local mesh space
float _BrushOpacity; // global opacity multiplier (0..1)
sampler2D _MaskTex;
struct appdata
{
@@ -61,11 +75,26 @@ Shader "Hidden/MeshPaintBrush"
fixed4 frag (v2f i) : SV_Target
{
// Only color texels inside the 3D brush sphere.
float d = distance(i.local, _BrushCenter);
clip(_BrushRadius - d);
// Alpha = 1 marks "painted by the brush" (source for the edge padding).
return fixed4(_BrushColor.rgb, 1.0);
// Offset from the brush center, projected onto the stamp frame.
float3 d = i.local - _BrushCenter;
float du = dot(d, _BrushU);
float dv = dot(d, _BrushV);
float dn = dot(d, _BrushNormal);
// Square stamp in the tangent plane + depth bound (keeps the seam-
// bridging 3D behavior while preventing bleed to far surfaces).
clip(_BrushRadius - abs(du));
clip(_BrushRadius - abs(dv));
clip(_BrushRadius - abs(dn));
// Mask -> opacity. du/dv in [-r,r] -> uv in [0,1].
float2 muv = float2(du, dv) / _BrushRadius * 0.5 + 0.5;
fixed4 m = tex2D(_MaskTex, muv);
float luma = dot(m.rgb, fixed3(0.299, 0.587, 0.114));
float opacity = luma * m.a * _BrushOpacity;
clip(opacity - 1e-4); // skip fully transparent texels
return fixed4(_BrushColor.rgb, opacity);
}
ENDCG
}
+5 -4
View File
@@ -5,9 +5,10 @@
// base color -> bright seam lines. This pass "bleeds" the painted color up to
// _Pad texels beyond the island edges.
//
// The dilation source is exclusively painted texels (alpha >= 0.5). The padding
// The dilation source is exclusively painted texels (alpha > 0). The padding
// texels produced get alpha = 0 -> next frame they are NO LONGER a source, so the
// padding does not grow unbounded (it stays at _Pad texels).
// padding does not grow unbounded (it stays at _Pad texels). The threshold is > 0
// (not >= 0.5) so soft, partially transparent brush edges count as painted too.
Shader "Hidden/MeshPaintDilate"
{
Properties
@@ -38,7 +39,7 @@ Shader "Hidden/MeshPaintDilate"
fixed4 frag (v2f_img i) : SV_Target
{
fixed4 c = tex2D(_MainTex, i.uv);
if (c.a >= 0.5) return c; // already painted -> leave unchanged
if (c.a > 0.0) return c; // already painted (even partially) -> leave unchanged
float2 ts = _MainTex_TexelSize.xy;
float best = 1e20;
@@ -53,7 +54,7 @@ Shader "Hidden/MeshPaintDilate"
{
if (abs(x) > _Pad) continue;
fixed4 s = tex2D(_MainTex, i.uv + float2(x, y) * ts);
if (s.a >= 0.5)
if (s.a > 0.0)
{
float dd = (float)(x * x + y * y);
if (dd < best) { best = dd; bestCol = s.rgb; found = true; }