21.5 C
New York
Monday, April 28, 2025

Alpha information provide issues by vertex to Monogame Shader


I’m attempting to make a path of paths in Monogame. What I presently have is a path of trails created in process with correctly utilized UV coordinates, and what I’ve left to do is apply a ‘visibility’ worth per vertex (alpha) in order that it may possibly fad the trail of 1 finish to the opposite. Nonetheless, the shador is appearing bizarre once I add the visibility information.

First, let me present you the useful half. Right here is the customized vertex construction that I’ve written: it accommodates place and UV information:

struct TrailVertex : IVertexType
{
    readonly VertexDeclaration IVertexType.VertexDeclaration => VertexDeclaration;

    public Vector3 place;
    public Vector2 uv;

    public TrailVertex ( Vector3 place, Vector2 uv )
    {
        this.place = place;
        this.uv = uv;
    }

    public static VertexDeclaration VertexDeclaration => new
    (
        new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Place, 0),
        new VertexElement(12, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
    );
}

Right here is the shador I wrote: Use the UV information within the exit colour for now, so you may see if it really works accurately. Remember that I’ve encoded the alpha to be 0.5! It is going to be essential later.

matrix WorldViewProjection;

struct VertexShaderInput
{
    float4 Place : POSITION0;
    float2 UV : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Place : SV_POSITION;
    float4 Coloration : COLOR0;
};

VertexShaderOutput MainVS(in VertexShaderInput enter)
{
    VertexShaderOutput output = (VertexShaderOutput)0;

    output.Place = mul(enter.Place, WorldViewProjection);
    output.Coloration = float4(enter.UV, 0, 0.5);

    return output;
}

float4 MainPS(VertexShaderOutput enter) : COLOR
{
    return enter.Coloration;
}

approach BasicColorDrawing
{
    go P0
    {
        VertexShader = compile VS_SHADERMODEL MainVS();
        PixelShader = compile PS_SHADERMODEL MainPS();
    }
};

And right here is the end result within the sport:

It’s working completely, UV coordinates and transparency are precisely as I hope.

However now I attempt to provide alpha by means of my customized vertex. Right here is the brand new construction of vertices:

struct TrailVertex : IVertexType
{
    readonly VertexDeclaration IVertexType.VertexDeclaration => VertexDeclaration;

    public Vector3 place;
    public Vector2 uv;
    public float visibility = 0.5f;

    public TrailVertex ( Vector3 place, Vector2 uv )
    {
        this.place = place;
        this.uv = uv;
    }

    public static VertexDeclaration VertexDeclaration => new
    (
        new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Place, 0),
        new VertexElement(12, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0),
        new VertexElement(20, VertexElementFormat.Single, VertexElementUsage.TextureCoordinate, 0)
    );
}

UV information is a vector2, so the displacement for visibility information within the vertex declaration should be 12 + sizeof(float) * 2 = 12 + 8 = 20good?

Right here is the shador who makes use of the visibility information equipped:

matrix WorldViewProjection;

struct VertexShaderInput
{
    float4 Place : POSITION0;
    float2 UV : TEXCOORD0;
    float Visibility : TEXCOORD1;
};

struct VertexShaderOutput
{
    float4 Place : SV_POSITION;
    float4 Coloration : COLOR0;
};

VertexShaderOutput MainVS(in VertexShaderInput enter)
{
    VertexShaderOutput output = (VertexShaderOutput)0;

    output.Place = mul(enter.Place, WorldViewProjection);
    output.Coloration = float4(enter.UV, 0, enter.Visibility);

    return output;
}

float4 MainPS(VertexShaderOutput enter) : COLOR
{
    return enter.Coloration;
}

approach BasicColorDrawing
{
    go P0
    {
        VertexShader = compile VS_SHADERMODEL MainVS();
        PixelShader = compile PS_SHADERMODEL MainPS();
    }
};

However right here is the trail within the sport:

Enter the image description here

The alpha appears to be right, however the UV information for all vertices appear to be annulled to a price of (1, 0)ensuing on a very pink path.

I don’t know why this occurs. I’ve tried to make use of totally different information information within the vertex assertion, however the different values ​​that I’ve tried give chaotic outcomes (in fact). And in addition to, if I had made an error within the compensation, the info that come after the UV information to spoil the UV information?

I be sure to use the three sorts of information within the shador in order that nothing is compiled. The one issues that change from having UV work to have a pink path is what I’ve proven above. What is going on on the earth?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles