Repository navigation
Expand file tree
/
Copy pathGeometryShader.hlsl
More file actions
70 lines (61 loc) · 1.71 KB
/
Copy pathGeometryShader.hlsl
File metadata and controls
70 lines (61 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
cbuffer ConstantBuffer
{
matrix finalMatrix;
matrix world; // the rotation matrix
matrix view;
matrix projection;
};
struct GSInput
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD;
};
struct GSOutput
{
float4 pos : SV_POSITION;
float2 Tex : TEXCOORD;
float4 normal : NORMAL;
float4 worldPos : POSITION;
};
[maxvertexcount(6)]
void GS_main(triangle GSInput input[3], inout TriangleStream< GSOutput > output)
{
// Define our edges
float4 worldPos[3];
float3 edge1 = input[0].Pos.xyz;
float3 edge2 = input[1].Pos.xyz;
float3 edge3 = input[2].Pos.xyz;
// Create vectors for the face
float3 vec1 = edge2 - edge1;
float3 vec2 = edge3 - edge1;
// Calculate the normal for the face and normalize it
float4 normal = float4(cross(vec1, vec2), 1.0f);
normal = normalize(normal);
for (uint k = 0; k < 3; k++)
worldPos[k] = mul(input[k].Pos, world);
float3 worldVec0 = worldPos[1].xyz - worldPos[0].xyz;
float3 worldVec1 = worldPos[2].xyz - worldPos[0].xyz;
float4 normalWorld = float4(cross(worldVec0, worldVec1), 1.0f);
for (uint i = 0; i < 3; i++)
{
GSOutput element;
element.pos = mul(input[i].Pos, finalMatrix);
element.Tex = input[i].Tex;
element.normal = normalWorld;
element.worldPos = mul(input[i].Pos, world);
output.Append(element);
}
output.RestartStrip();
// Create 3 new vertices with a new position, using the normal as offset
for (uint j = 0; j < 3; j++)
{
GSOutput element_1;
element_1.pos = input[j].Pos + normal;
element_1.pos[3] = 1.0f; // fill every position in the element;
element_1.pos = mul(element_1.pos, finalMatrix);
element_1.Tex = input[j].Tex;
element_1.worldPos = mul(input[j].Pos, world);
element_1.normal = normalWorld;
output.Append(element_1);
}
}