I am developing a face sticker feature using this shader, but when adding other filters, the sticker rendering is being enlarged and its

chfeizy

New Member
Code:
uniform float4x4 ViewProj;
uniform texture2d image;

uniform float3 u1_p0;
uniform float3 u1_p1;
uniform float3 u1_p2;
uniform float3 u1_p3;

uniform texture2d target_ErDuo_left;

sampler_state def_sampler {
    Filter   = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

struct VertInOut {
    float4 pos : POSITION;
    float2 uv  : TEXCOORD0;
};
float cross2D(float2 v, float2 w)
{
    return v.x * w.y - v.y * w.x;
}

VertInOut VSDefault(VertInOut vert_in)
{
    VertInOut vert_out;

    vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj);
    vert_out.uv = vert_in.uv;

    return vert_out;
}

float4 PSDrawBaretzerduoleft(VertInOut vert_in) : TARGET
{
    VertInOut vert_out;
    
    vert_out.pos = float4(vert_in.pos.xyz, 1.0);

    float2 v_q;
    float2 v_b1;
    float2 v_b2;
    float2 v_b3;

    
    float4 u4_p0= float4(u1_p0, 1.0);
    float4 u4_p1= float4(u1_p1, 1.0);
    float4 u4_p2= float4(u1_p2, 1.0);
    float4 u4_p3= float4(u1_p3, 1.0);
    
    v_q = (vert_out.pos - u4_p0).xy;
    v_b1 = (u4_p1 - u4_p0).xy;
    v_b2 = (u4_p2 - u4_p0).xy;
    v_b3 = (u4_p0 - u4_p1 - u4_p2 + u4_p3).xy;

    float A = cross2D(v_b2, v_b3);
    float B = cross2D(v_b3, v_q) - cross2D(v_b1, v_b2);
    float C = cross2D(v_b1, v_q);

    float2 uv;
    if (abs(A) < 0.2)
    {
    
        // Linear form
        uv.y = -C / B;
    }
    else
    {
    
        // Quadratic form. Take positive root for CCW winding with V-up
        float discrim = B * B - 4 * A * C;
        uv.y = 0.5 * (-B + sqrt(discrim)) / A; //CW
    }
    // Solve for u, using largest-magnitude component
    float2 denom = v_b1 + uv.y * v_b3;
    if (abs(denom.x) > abs(denom.y))
        uv.x = (v_q.x - v_b2.x * uv.y) / denom.x;
    else
        uv.x = (v_q.y - v_b2.y * uv.y) / denom.y;

    float4 rgba = target_ErDuo_left.Sample(def_sampler, uv);
    float4 MaskColor = image.Sample(def_sampler, vert_in.uv);
    float4 outputRGBA;
    outputRGBA.rgb = rgba.rgb * rgba.a + MaskColor.rgb * (1.0 - rgba.a);
    outputRGBA.a = 1;
    return outputRGBA;
}

technique Draw
{
    pass
    {
        vertex_shader = VSDefault(vert_in);
        pixel_shader  = PSDrawBaretzerduoleft(vert_in);
    }
}

The shader like this!
During the filter preview, the rendering is also applied to the entire scene area. It seems that the issue lies in the usage of "vert_out.pos = float4(vert_in.pos.xyz, 1.0);" which targets the entire scene area instead of the current video window area. How can this be resolved?
 
Top