- Home /
Geometry Shader misses vertices problem.
So I've been working on a Geometry Shader, based on this: http://forum.unity3d.com/threads/geometry-shaders.156553/
Basically, I want it to generate an upright triangle off of every vertex of the base mesh. At this point I've simplified it as much as I could. The first Pass renders the base geometry (the Quad in the image) and the second Pass creates and renders the triangles (the red triangles in the image).
Here's the problem. The way I understand it my Geometry Shader program should be called for every vertex, no? it takes points as input, and emits a triangle stream. And yet, as you can see it seems to execute only once per base mesh triangle (if that is the case, how do I access all vertices of the triangle,if my input is a single point?)
What am I doing/understanding wrong?
Anyways,here's the code:
Shader "Custom/Geometry_Shader_Test"
{
Properties
{
_MainTex ("Base (RGBA)",2D) ="white"{}
}
SubShader
{
Pass ///////////////////////PASS 1
{
Tags { "RenderType"="Opaque" }
LOD 200
Cull Off
CGPROGRAM
#pragma target 4.0
#pragma vertex VS_Main
#pragma fragment FS_Main
#include "UnityCG.cginc"
// **************************************************************
// Data structures *
// **************************************************************
struct vertexInput
{
float4 vertex : POSITION;
float2 texcoord :TEXCOORD0;
};
struct fs_input // also geometry shader input
{
float4 pos : POSITION;
float2 tex : TEXCOORD0;
};
// **************************************************************
// Vars *
// **************************************************************
sampler2D _MainTex;
// **************************************************************
// Shader Programs *
// **************************************************************
// Vertex Shader ------------------------------------------------
fs_input VS_Main(vertexInput v)
{
fs_input output;
output.pos = mul(UNITY_MATRIX_MVP,v.vertex);
output.tex = v.texcoord;
return output;
}
// Fragment Shader -----------------------------------------------
float4 FS_Main(fs_input input) : COLOR
{
return tex2D(_MainTex, float2(input.tex.xy));
}
ENDCG
}
Pass ///////////////////////// PASS 2
{
Tags { "RenderType"="Opaque" }
LOD 200
Cull Off
CGPROGRAM
#pragma target 4.0
#pragma vertex VS_Main
#pragma fragment FS_Main
#pragma geometry GS_Main
#include "UnityCG.cginc"
// **************************************************************
// Data structures *
// **************************************************************
struct vertexOutput // also geometry shader input
{
float4 pos : POSITION;
};
struct gs_output
{
float4 pos : POSITION;
};
// **************************************************************
// Shader Programs *
// **************************************************************
// Vertex Shader ------------------------------------------------
vertexOutput VS_Main(float4 vertexPos: POSITION)
{
vertexOutput output;
output.pos= mul(_Object2World, vertexPos);
return output;
}
// Geometry Shader -----------------------------------------------------
[maxvertexcount(3)]
void GS_Main(point vertexOutput p[1], inout TriangleStream<gs_output> triStream)
{
gs_output pIn;
float4 v[3]; // the triangle to be generated
v[0] = float4(p[0].pos.xyz, 1.0f);
v[1] = float4(p[0].pos.xyz+float3(0.2,1,0), 1.0f);
v[2] = float4(p[0].pos.xyz+float3(-0.2,1,0), 1.0f);
// append each vertex into the triangle stram:
pIn.pos.w = 1.0f;
pIn.pos.xyz = mul(_World2Object,v[0]).xyz * unity_Scale.w;
pIn.pos = mul(UNITY_MATRIX_MVP,pIn.pos);
triStream.Append(pIn);
pIn.pos.w = 1.0f;
pIn.pos.xyz = mul(_World2Object,v[1]).xyz * unity_Scale.w;
pIn.pos = mul(UNITY_MATRIX_MVP,pIn.pos);
triStream.Append(pIn);
pIn.pos.w = 1.0f;
pIn.pos.xyz = mul(_World2Object,v[2]).xyz * unity_Scale.w;
pIn.pos = mul(UNITY_MATRIX_MVP,pIn.pos);
triStream.Append(pIn);
}
// Fragment Shader -----------------------------------------------
float4 FS_Main(gs_output input) : COLOR
{
return float4(0.5,0,0,1);
}
ENDCG
}
}
}
Answer by unisip · Mar 03, 2015 at 01:02 PM
I'm having similar problems understanding how point geometry shaders are working. From what I gather, it has to do with the triangles you submit in your mesh description. In your case, you have 4 vertices and 2 triangles. The geometry shader seems to be applied once per triangle, on the first vertex of the triangle. That doesn't quite tell you what to do to have the geometry shader run on all 4 vertices (aside from changing the triangles in the mesh), but at least I think that explains it. Haven't found a thorough explaination in unity doc about how to handle that yet
Answer by Jeizar · Aug 02, 2017 at 08:12 AM
I resurrect an old topic, but i had some difficulties to find an answer to that problem. If anybody encounter the same issue here is an explanation and a solution :