Initial commit: Unity mesh painter project
Unity 6000.3 (URP) project with live mesh painting in play mode. - MeshPainter.cs: paint a SkinnedMesh with the mouse, seamless across UV islands via 3D-space painting, GPU and CPU backends (switchable), edge-padding/dilation against bilinear seam bleed - FrameRateLimiter.cs: simple frame rate limiting component - MeshPaintBrush / MeshPaintDilate shaders for the GPU paint path - Unity-specific .gitignore and .gitattributes Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
commit
fa4474f6d4
61 files changed
+7748
No files matched your search
@@ -0,0 +1,74 @@
|
||||
// GPU brush: paints a 3D sphere 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.
|
||||
//
|
||||
// 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).
|
||||
Shader "Hidden/MeshPaintBrush"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_BrushColor ("Brush Color", Color) = (1,0,0,1)
|
||||
}
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderType" = "Opaque" }
|
||||
Pass
|
||||
{
|
||||
ZTest Always
|
||||
ZWrite Off
|
||||
Cull Off
|
||||
Blend Off
|
||||
|
||||
CGPROGRAM
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
float4 _BrushColor;
|
||||
float3 _BrushCenter; // brush center in local mesh space
|
||||
float _BrushRadius; // brush radius in local mesh space
|
||||
|
||||
struct appdata
|
||||
{
|
||||
float4 vertex : POSITION; // local 3D position
|
||||
float2 uv : TEXCOORD0; // UV (target in the texture layout)
|
||||
};
|
||||
|
||||
struct v2f
|
||||
{
|
||||
float4 pos : SV_POSITION;
|
||||
float3 local : TEXCOORD0; // interpolated local 3D position
|
||||
};
|
||||
|
||||
v2f vert (appdata v)
|
||||
{
|
||||
v2f o;
|
||||
// UV (0..1) -> clip space (-1..1). This makes the GPU rasterize
|
||||
// each triangle exactly at its place in the texture.
|
||||
float2 p = v.uv * 2.0 - 1.0;
|
||||
#if UNITY_UV_STARTS_AT_TOP
|
||||
p.y = -p.y; // compensate the RenderTexture Y-flip (D3D etc.)
|
||||
#endif
|
||||
o.pos = float4(p, 0.0, 1.0);
|
||||
o.local = v.vertex.xyz;
|
||||
return o;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
Fallback Off
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cfff45d9ec693b247909d39c182922ff
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,72 @@
|
||||
// GPU edge padding / texture dilation against visible UV seams.
|
||||
//
|
||||
// Between the UV islands there is an unpainted border (gutter). During bilinear
|
||||
// sampling at an island edge, the GPU mixes the painted edge with the unpainted
|
||||
// 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
|
||||
// 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).
|
||||
Shader "Hidden/MeshPaintDilate"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_MainTex ("Texture", 2D) = "white" {}
|
||||
}
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderType" = "Opaque" }
|
||||
Pass
|
||||
{
|
||||
ZTest Always
|
||||
ZWrite Off
|
||||
Cull Off
|
||||
Blend Off
|
||||
|
||||
CGPROGRAM
|
||||
#pragma vertex vert_img
|
||||
#pragma fragment frag
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
sampler2D _MainTex;
|
||||
float4 _MainTex_TexelSize; // (1/w, 1/h, w, h)
|
||||
int _Pad;
|
||||
|
||||
#define MAXPAD 16
|
||||
|
||||
fixed4 frag (v2f_img i) : SV_Target
|
||||
{
|
||||
fixed4 c = tex2D(_MainTex, i.uv);
|
||||
if (c.a >= 0.5) return c; // already painted -> leave unchanged
|
||||
|
||||
float2 ts = _MainTex_TexelSize.xy;
|
||||
float best = 1e20;
|
||||
fixed3 bestCol = c.rgb;
|
||||
bool found = false;
|
||||
|
||||
// Search the neighborhood (up to _Pad texels) for the nearest painted texel.
|
||||
[loop] for (int y = -MAXPAD; y <= MAXPAD; y++)
|
||||
{
|
||||
if (abs(y) > _Pad) continue;
|
||||
[loop] for (int x = -MAXPAD; x <= MAXPAD; x++)
|
||||
{
|
||||
if (abs(x) > _Pad) continue;
|
||||
fixed4 s = tex2D(_MainTex, i.uv + float2(x, y) * ts);
|
||||
if (s.a >= 0.5)
|
||||
{
|
||||
float dd = (float)(x * x + y * y);
|
||||
if (dd < best) { best = dd; bestCol = s.rgb; found = true; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gutter: color of the nearest painted texel, but alpha = 0 (not a source).
|
||||
if (found) return fixed4(bestCol, 0.0);
|
||||
return c; // out of range -> base color unchanged
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
Fallback Off
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 387abc652c7ac1c449701bdfcf9d0f8b
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in new issue
Block a user