Hi
I have some questions about the texture mapping on trapezoid mesh
to avoid texture warping in trapezoidal mesh, I used homography matrix to generate UV of the meshes one by one.
But after homography transformation, the texture is not seamless between each meshes.
is there any way to fix this problem or other way to make the correct mapping??
if there are any advises I’ll be very happy! Thanks beforehand!
source: https://github.com/kweimingxx/NetMeshQuadWarp
The uv spacing of polygons will be nonuniform along the edge when using homography method.
After some research, I know i should interpolate it manually in fragment shader
But still can’t get the right outcome
My shader:
Shader "Custom/BilinearInterpolate"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_TexelSize;
sampler2D _CheckBoardTex;
sampler2D _SpoutTex;
float _Homography[9];
float _InvHomography[9];
float _InvHomographyUV[9];
float4 _VertexValues[4];
float4 _UVValues[4];
float4x4 _HomographyMat;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float2 q : TEXCOORD1;
float2 b1 : TEXCOORD2;
float2 b2 : TEXCOORD3;
float2 b3 : TEXCOORD4;
};
v2f vert(appdata_img i)
{
v2f o;
o.pos = UnityObjectToClipPos(i.vertex);
o.uv = i.texcoord;
o.q = _VertexValues[0] - i.vertex;
o.b1 = _VertexValues[1] - _VertexValues[0];
o.b2 = _VertexValues[2] - _VertexValues[0];
o.b3 = _VertexValues[0] - _VertexValues[1] - _VertexValues[2] + _VertexValues[3];
return o;
}
float plot(float2 st, float2 pct) {
return smoothstep(pct - 0.02, pct, st.y) -
smoothstep(pct, pct + 0.02, st.y);
}
float Wedge2D(float2 v, float2 w)
{
return v.x * w.y - v.y * w.x;
}
float4 frag0(v2f i) : SV_Target
{
// Set up quadratic formula
float A = Wedge2D(i.b2, i.b3);
float B = Wedge2D(i.b2, i.b1) + Wedge2D(i.q, i.b3);
float C = Wedge2D(i.q, i.b1);
float A1 = Wedge2D(i.b1, i.b3);
float B1 = Wedge2D(i.b1, i.b2) + Wedge2D(i.q, i.b3);
float C1 = Wedge2D(i.q, i.b2);
// Solve for v
float2 uv;
float u1, u2;
float v1, v2;
if (abs(A) < 1.e-14) // == 0
{
// Linear form
u1 = -C / B;
u2 = u1;
}
else
{
float discrim = B * B - 4 * A * C;
if (discrim >= 0 ){
u1 = (-B + sqrt(discrim)) / (2 * A);
u2 = (-B - sqrt(discrim)) / (2 * A);
}
else {
u1 = -1000;
u2 = u1;
}
}
float mu = -10000;
if (u1 >= 0 && u1 <= 1) {
mu = u1;
}
if (u2 >= 0 && u2 <= 1) {
mu = u2;
}
//------------------------------------------------------
if (abs(A1) < 1.e-14) // == 0
{
// Linear form
v1 = -C1 / B1;
v2 = v1;
}
else
{
float discrim1 = B1 * B1 - 4 * A1 * C1;
if (discrim1 >= 0 ){
v1 = (-B1 + sqrt(discrim1)) / (2 * A1);
v2 = (-B1 - sqrt(discrim1)) / (2 * A1);
}
else {
v1 = -1000;
v2 = v1;
}
}
float lambda = -10000;
if (v1 >= 0 && v1 <= 1) {
lambda = v1;
}
if (v2 >= 0 && v2 <= 1) {
lambda = v2;
}
uv = float2(mu, lambda);
float3 p0 = lerp(_UVValues[0], _UVValues[2], uv.x);
float3 p1 = lerp(_UVValues[1], _UVValues[3], uv.x);
float3 ps = lerp(p0, p1, uv.y);
//return float4(i.uv, 0, 1);
//return float4(uv, 0, 1);
//return tex2D(_SpoutTex, ps) * 1;
return tex2D(_SpoutTex, uv) * 1;
}
ENDCG
SubShader
{
// No culling or depth
//Cull Off ZWrite Off ZTest Always
// Pass 0: calculate
Pass
{
//Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag0
ENDCG
}
}
}
reference:
https://numfactory.upc.edu/web/FiniteElements/Pract/P4-QuadInterpolation/html/QuadInterpolation.html
three polygons with weird uv coordinate
but the rest five polygons seems good