Forum

Geometry Shader Pyramid

I’m attempting a geometry shader that will output a pyramid for every triangle in. It kind of works, but it seems to be creating additional unexpected geometry which is causing z-fighting and worse. Here is my GS

[maxvertexcount(12)](maxvertexcount(12))
void GS(triangle VS_OUTPUT input[3](3), inout TriangleStream<VS_OUTPUT> gsout)
{
	VS_OUTPUT elem = (VS_OUTPUT)0;
	elem.pos.w=1.0;
	
	float3 p1 = input[0](0).pos.xyz;
	float3 p2 = input[1](1).pos.xyz;
	float3 p3 = input[2](2).pos.xyz;
	
	float3 ce = (p1+p2+p3)/3.0f;	
	
	
	float3 ceNew = float3(20,0,-0);
	
	float amt = distance(ceNew, ce);
	
	float3 faceEdgeA = p2 - p1;
    float3 faceEdgeB = p1 - p3;
    float3 norm = cross(faceEdgeB, faceEdgeA);
	norm = normalize(norm);	
	float3 apex = ce +(height*norm);

	//floor
	elem.pos.xyz = input[0](0).pos.xyz+(norm*amt);
	elem.norm = norm;
	elem.uv = input[0](0).uv;
	gsout.Append(elem);
	
	elem.pos.xyz = input[1](1).pos.xyz+(norm*amt);
	elem.norm = norm;
	elem.uv = input[1](1).uv;
	gsout.Append(elem);
	
	elem.pos.xyz = input[2](2).pos.xyz+(norm*amt);
	elem.norm = norm;
	elem.uv = input[2](2).uv;
	gsout.Append(elem);
	
	gsout.RestartStrip();

	//side 1
	
	faceEdgeA = p2-p1;
	faceEdgeB = p1-apex;
	float3 face1Norm = cross(faceEdgeB, faceEdgeA);
	face1Norm =normalize(face1Norm);
	
	elem.pos.xyz = input[0](0).pos.xyz+(norm*amt);
	elem.norm = face1Norm;
	elem.uv = input[0](0).uv;
	gsout.Append(elem);

	elem.pos.xyz = input[1](1).pos.xyz+(norm*amt);
	elem.norm = face1Norm;
	elem.uv = input[1](1).uv;
	gsout.Append(elem);
	
	elem.pos.xyz = apex+(norm*amt);
	elem.norm = face1Norm;
	elem.uv = input[0](0).uv;
	gsout.Append(elem);
	
	gsout.RestartStrip();
	
	//side2
	
	faceEdgeA = p3-p2;
	faceEdgeB = p2-apex;
	float3 face2Norm = cross(faceEdgeB, faceEdgeA);
	face2Norm =normalize(face2Norm);
	
	elem.pos.xyz = input[1](1).pos.xyz+(norm*amt);
	elem.norm = face2Norm;
	elem.uv = input[1](1).uv;
	gsout.Append(elem);

	elem.pos.xyz = input[2](2).pos.xyz+(norm*amt);
	elem.norm = face2Norm;
	elem.uv = input[2](2).uv;
	gsout.Append(elem);
	
	elem.pos.xyz = apex+(norm*amt);
	elem.norm = face2Norm;
	elem.uv = input[0](0).uv;
	gsout.Append(elem);
	
	gsout.RestartStrip();
	
	//side3
	
	faceEdgeA = p1-p3;
	faceEdgeB = p3-apex;
	float3 face3Norm = cross(faceEdgeB, faceEdgeA);
	face3Norm =normalize(face3Norm);
	
	elem.pos.xyz = input[2](2).pos.xyz+(norm*amt);
	elem.norm = face3Norm;
	elem.uv = input[2](2).uv;
	gsout.Append(elem);

	elem.pos.xyz = input[0](0).pos.xyz+(norm*amt);
	elem.norm = face3Norm;
	elem.uv = input[0](0).uv;
	gsout.Append(elem);
	
	elem.pos.xyz = apex+(norm*amt);
	elem.norm = face3Norm;
	elem.uv = input[0](0).uv;
	gsout.Append(elem);
	
	gsout.RestartStrip();
}

Added test patch

Pyramids.zip (4.8 kB)