[Solved] geometry shader , passing information?

          mesh = new Mesh();
            mesh.SetVertices(lPositions);
          
            mesh.SetIndices(indeices, MeshTopology.Points, 0);
            mesh.SetColors(colours);
            mesh.SetNormals(normals);
            meshFilter.mesh = mesh;

and in the shader

    struct v2g
    {
        float4 pos : SV_POSITION;
        float3 norm : NORMAL;
        float2 uv : TEXCOORD0;
        float3 color : TEXCOORD1;
    };

the:
mesh.SetVertices(lPositions); = float4 pos : SV_POSITION;
mesh.SetNormals(normals); = float2 uv : TEXCOORD0;

but a cant get :
mesh.SetColors(colours); = float3 color : TEXCOORD1;

to work ? any idea why ?
also what does mesh.SetTriangles set in the shader ?

None of those mesh functions have any relation to the shader itself, you’re just constructing the mesh data on the CPU side and then it gets sent to the GPU when you finalize it and assign it to a Renderer component. If the vertex colors were properly set then they should be accessible in the shader. Vertex color isn’t stored in TEXCOORD1, it’s stored in COLOR.

Also color can be a float4 you know, make full use of the whole vector width if you can. Store other data in the alpha channel for other stuff if you want even.

1 Like

You can store the color in whatever semantic you want for the vertex to geometry struct. The COLOR semantic only matters for the vertex shader input. Really apart from a handful of special semantics, like SV_Position and other “SV_” semantics, all of the vertex shader outputs are really just TEXCOORDs. You get up to 32 float4 values in total per vertex (including the SV_Position) in Direct3D 11, and each vertex shader output is passing a full float4 regardless of if you define it as a float3 or a fixed2, so pack accordingly. OpenGL is a little more friendly here and you get some multiple of float4 data, but a float varying won’t use up full a float4.

However the important bit of code here is going to be in the vertex shader, its input struct, and what it does with the input COLOR variable. Really it’s the only part that matters, and that’s not something you show any code for.

The vertex shader gets the mesh data as its input, and its output gets passed to the geometry shader (if one exists). This is what @Invertex is getting at.

// the mesh vertex data
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
fixed4 color : COLOR; // Important!
}

// the vertex shader output and geometry input
struct v2g {
float4 vertex : SV_Position;
float2 texcoord : TEXCOORD0;
float4 color : TEXCOORD1;
}

v2g vert(appdata v)
{
v2g o;
o.vertex = v.vertex; // input POSITION output SV_Position
o.texcoord = v.texcoord; // input TEXCOORD0 output TEXCOORD0
o.color = v.color; // input COLOR output TEXCOORD1
return o;
}

2 Likes