2017-02-26 21:07:37 +01:00
|
|
|
|
|
|
|
|
/* Solid Wirefram implementation
|
|
|
|
|
* Mike Erwin, Clément Foucault */
|
|
|
|
|
|
|
|
|
|
/* This shader follows the principles of
|
|
|
|
|
* http://developer.download.nvidia.com/SDK/10/direct3d/Source/SolidWireframe/Doc/SolidWireframe.pdf */
|
|
|
|
|
|
2018-01-11 15:58:20 -02:00
|
|
|
/* This is not perfect. Only a subset of intel gpus are affected.
|
|
|
|
|
* This fix have some performance impact.
|
|
|
|
|
* TODO Refine the range to only affect GPUs. */
|
|
|
|
|
|
2017-03-03 02:48:34 +01:00
|
|
|
uniform float faceAlphaMod;
|
|
|
|
|
|
2017-02-26 21:07:37 +01:00
|
|
|
flat in vec3 edgesCrease;
|
|
|
|
|
flat in vec3 edgesBweight;
|
|
|
|
|
flat in vec4 faceColor;
|
|
|
|
|
flat in int clipCase;
|
2017-03-01 18:54:58 +01:00
|
|
|
#ifdef VERTEX_SELECTION
|
2017-05-19 16:12:13 -04:00
|
|
|
in vec3 vertexColor;
|
2017-03-01 18:54:58 +01:00
|
|
|
#endif
|
2017-09-22 02:42:57 +02:00
|
|
|
#ifdef VERTEX_FACING
|
|
|
|
|
in float facing;
|
|
|
|
|
#endif
|
2017-02-26 21:07:37 +01:00
|
|
|
|
|
|
|
|
/* We use a vec4[2] interface to pass edge data
|
|
|
|
|
* (without fragmenting memory accesses)
|
|
|
|
|
*
|
2017-05-19 14:13:43 -04:00
|
|
|
* There are 2 cases :
|
2017-02-26 21:07:37 +01:00
|
|
|
*
|
|
|
|
|
* - Simple case : geometry shader return edge distances
|
|
|
|
|
* in the first 2 components of the first vec4.
|
|
|
|
|
* This needs noperspective interpolation.
|
|
|
|
|
* The rest is filled with vertex screen positions.
|
2018-01-11 18:07:41 -02:00
|
|
|
* eData2[0] actually contain v2
|
|
|
|
|
* eData2[1] actually contain v1
|
|
|
|
|
* eData2[2] actually contain v0
|
2017-02-26 21:07:37 +01:00
|
|
|
*
|
|
|
|
|
* - Hard case : two 2d edge corner are described by each
|
|
|
|
|
* vec4 as origin and direction. This is constant over
|
|
|
|
|
* the triangle and use to detect the correct case. */
|
|
|
|
|
|
2018-01-11 18:07:41 -02:00
|
|
|
noperspective in vec2 eData1;
|
|
|
|
|
flat in vec2 eData2[3];
|
2018-01-11 15:58:20 -02:00
|
|
|
|
|
|
|
|
/* Some intel Gpu seems to have memory alignement problems. So adding a padding int */
|
|
|
|
|
#ifdef GPU_INTEL
|
|
|
|
|
flat in ivec4 flag;
|
|
|
|
|
#else
|
|
|
|
|
flat in ivec3 flag;
|
|
|
|
|
#endif
|
2017-02-26 21:07:37 +01:00
|
|
|
|
|
|
|
|
out vec4 FragColor;
|
|
|
|
|
|
2017-05-19 14:13:43 -04:00
|
|
|
#define EDGE_EXISTS (1 << 0)
|
|
|
|
|
#define EDGE_ACTIVE (1 << 1)
|
|
|
|
|
#define EDGE_SELECTED (1 << 2)
|
|
|
|
|
#define EDGE_SEAM (1 << 3)
|
|
|
|
|
#define EDGE_SHARP (1 << 4)
|
2017-02-26 21:07:37 +01:00
|
|
|
/* Vertex flag is shifted and combined with the edge flag */
|
2017-05-19 14:13:43 -04:00
|
|
|
#define VERTEX_ACTIVE (1 << (0 + 8))
|
|
|
|
|
#define VERTEX_SELECTED (1 << (1 + 8))
|
|
|
|
|
#define FACE_ACTIVE (1 << (2 + 8))
|
2017-02-26 21:07:37 +01:00
|
|
|
|
|
|
|
|
/* Style Parameters in pixel */
|
|
|
|
|
|
2017-05-19 14:13:43 -04:00
|
|
|
/* Array to retrieve vert/edge indices */
|
2017-02-26 21:07:37 +01:00
|
|
|
const ivec3 clipEdgeIdx[6] = ivec3[6](
|
|
|
|
|
ivec3(1, 0, 2),
|
|
|
|
|
ivec3(2, 0, 1),
|
|
|
|
|
ivec3(2, 1, 0),
|
|
|
|
|
ivec3(2, 1, 0),
|
|
|
|
|
ivec3(2, 0, 1),
|
|
|
|
|
ivec3(1, 0, 2)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const ivec3 clipPointIdx[6] = ivec3[6](
|
|
|
|
|
ivec3(0, 1, 2),
|
|
|
|
|
ivec3(0, 2, 1),
|
|
|
|
|
ivec3(0, 2, 1),
|
|
|
|
|
ivec3(1, 2, 0),
|
|
|
|
|
ivec3(1, 2, 0),
|
|
|
|
|
ivec3(2, 1, 0)
|
|
|
|
|
);
|
|
|
|
|
|
2018-01-11 15:58:20 -02:00
|
|
|
const vec4 stipple_matrix[4] = vec4[4](
|
2017-04-09 16:34:15 +10:00
|
|
|
vec4(1.0, 0.0, 0.0, 0.0),
|
|
|
|
|
vec4(0.0, 0.0, 0.0, 0.0),
|
|
|
|
|
vec4(0.0, 0.0, 1.0, 0.0),
|
|
|
|
|
vec4(0.0, 0.0, 0.0, 0.0)
|
|
|
|
|
);
|
2017-02-26 21:07:37 +01:00
|
|
|
|
2017-03-01 18:54:58 +01:00
|
|
|
void colorDist(vec4 color, float dist)
|
2017-02-26 21:07:37 +01:00
|
|
|
{
|
2017-03-01 18:54:58 +01:00
|
|
|
FragColor = (dist < 0) ? color : FragColor;
|
2017-02-26 21:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float distToEdge(vec2 o, vec2 dir)
|
|
|
|
|
{
|
|
|
|
|
vec2 af = gl_FragCoord.xy - o;
|
|
|
|
|
float daf = dot(dir, af);
|
|
|
|
|
return sqrt(abs(dot(af, af) - daf * daf));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 22:00:48 +02:00
|
|
|
#ifdef ANTI_ALIASING
|
|
|
|
|
void colorDistEdge(vec4 color, float dist)
|
|
|
|
|
{
|
|
|
|
|
FragColor = mix(color, FragColor, clamp(dist, 0.0, 1.0));
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
#define colorDistEdge colorDist
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-02-26 21:07:37 +01:00
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
vec3 e, p;
|
|
|
|
|
|
|
|
|
|
/* Step 1 : Computing Distances */
|
|
|
|
|
|
|
|
|
|
if (clipCase == 0) {
|
2018-01-11 18:07:41 -02:00
|
|
|
e.xy = eData1;
|
2017-02-26 21:07:37 +01:00
|
|
|
|
|
|
|
|
/* computing missing distance */
|
2018-01-11 18:07:41 -02:00
|
|
|
vec2 dir = normalize(eData2[2] - eData2[1]);
|
|
|
|
|
e.z = distToEdge(eData2[2], dir);
|
2017-02-26 21:07:37 +01:00
|
|
|
|
2018-01-11 18:07:41 -02:00
|
|
|
p.x = distance(eData2[2], gl_FragCoord.xy);
|
|
|
|
|
p.y = distance(eData2[1], gl_FragCoord.xy);
|
|
|
|
|
p.z = distance(eData2[0], gl_FragCoord.xy);
|
2017-02-26 21:07:37 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ivec3 eidxs = clipEdgeIdx[clipCase - 1];
|
|
|
|
|
ivec3 pidxs = clipPointIdx[clipCase - 1];
|
|
|
|
|
|
2018-01-11 18:07:41 -02:00
|
|
|
e[eidxs.x] = distToEdge(eData1, eData2[0]);
|
|
|
|
|
e[eidxs.y] = distToEdge(eData2[1], eData2[2]);
|
2017-02-26 21:07:37 +01:00
|
|
|
|
|
|
|
|
/* Three edges visible cases */
|
|
|
|
|
if (clipCase == 1 || clipCase == 2 || clipCase == 4) {
|
2018-01-11 18:07:41 -02:00
|
|
|
e[eidxs.z] = distToEdge(eData1, normalize(eData2[1] - eData1));
|
|
|
|
|
p[pidxs.y] = distance(eData2[1], gl_FragCoord.xy);
|
2017-02-26 21:07:37 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
e[eidxs.z] = 1e10; /* off screen */
|
|
|
|
|
p[pidxs.y] = 1e10; /* off screen */
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 18:07:41 -02:00
|
|
|
p[pidxs.x] = distance(eData1, gl_FragCoord.xy);
|
2017-02-26 21:07:37 +01:00
|
|
|
p[pidxs.z] = 1e10; /* off screen */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Step 2 : coloring (order dependant) */
|
|
|
|
|
|
|
|
|
|
/* First */
|
|
|
|
|
FragColor = faceColor;
|
|
|
|
|
|
2017-03-01 18:54:58 +01:00
|
|
|
if ((flag[0] & FACE_ACTIVE) != 0) {
|
|
|
|
|
int x = int(gl_FragCoord.x) & 0x3; /* mod 4 */
|
|
|
|
|
int y = int(gl_FragCoord.y) & 0x3; /* mod 4 */
|
|
|
|
|
FragColor *= stipple_matrix[x][y];
|
|
|
|
|
}
|
2017-03-03 02:48:34 +01:00
|
|
|
else {
|
|
|
|
|
FragColor.a *= faceAlphaMod;
|
|
|
|
|
}
|
2017-03-01 18:54:58 +01:00
|
|
|
|
2017-02-26 21:07:37 +01:00
|
|
|
/* Edges */
|
|
|
|
|
for (int v = 0; v < 3; ++v) {
|
2017-03-01 18:54:58 +01:00
|
|
|
if ((flag[v] & EDGE_EXISTS) != 0) {
|
2017-09-21 22:00:48 +02:00
|
|
|
/* Outer large edge */
|
|
|
|
|
float largeEdge = e[v] - sizeEdge * 3.0;
|
|
|
|
|
|
|
|
|
|
vec4 large_edge_color = vec4(0.0);
|
|
|
|
|
large_edge_color = ((flag[v] & EDGE_SHARP) != 0) ? colorEdgeSharp : large_edge_color;
|
|
|
|
|
large_edge_color = (edgesCrease[v] > 0.0) ? vec4(colorEdgeCrease.rgb, edgesCrease[v]) : large_edge_color;
|
|
|
|
|
large_edge_color = (edgesBweight[v] > 0.0) ? vec4(colorEdgeBWeight.rgb, edgesBweight[v]) : large_edge_color;
|
|
|
|
|
large_edge_color = ((flag[v] & EDGE_SEAM) != 0) ? colorEdgeSeam : large_edge_color;
|
|
|
|
|
|
2017-09-22 03:44:06 +02:00
|
|
|
if (large_edge_color.a != 0.0) {
|
2017-09-21 22:00:48 +02:00
|
|
|
colorDistEdge(large_edge_color, largeEdge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Inner thin edge */
|
2017-03-01 18:54:58 +01:00
|
|
|
float innerEdge = e[v] - sizeEdge;
|
2017-09-21 22:00:48 +02:00
|
|
|
#ifdef ANTI_ALIASING
|
|
|
|
|
innerEdge += 0.125;
|
|
|
|
|
#endif
|
2017-03-01 18:54:58 +01:00
|
|
|
|
2017-09-21 22:00:48 +02:00
|
|
|
#ifdef VERTEX_SELECTION
|
|
|
|
|
colorDistEdge(vec4(vertexColor, 1.0), innerEdge);
|
2017-03-02 01:07:03 +01:00
|
|
|
#else
|
2017-09-21 22:00:48 +02:00
|
|
|
vec4 inner_edge_color = colorWireEdit;
|
|
|
|
|
inner_edge_color = ((flag[v] & EDGE_SELECTED) != 0) ? colorEdgeSelect : inner_edge_color;
|
|
|
|
|
inner_edge_color = ((flag[v] & EDGE_ACTIVE) != 0) ? vec4(colorEditMeshActive.xyz, 1.0) : inner_edge_color;
|
|
|
|
|
|
|
|
|
|
colorDistEdge(inner_edge_color, innerEdge);
|
2017-03-01 18:54:58 +01:00
|
|
|
#endif
|
|
|
|
|
}
|
2017-02-26 21:07:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Points */
|
2017-03-02 01:07:03 +01:00
|
|
|
#ifdef VERTEX_SELECTION
|
2017-02-26 21:07:37 +01:00
|
|
|
for (int v = 0; v < 3; ++v) {
|
2017-03-01 18:54:58 +01:00
|
|
|
float size = p[v] - sizeVertex;
|
2017-02-26 21:07:37 +01:00
|
|
|
|
2017-09-21 22:00:48 +02:00
|
|
|
vec4 point_color = colorVertex;
|
|
|
|
|
point_color = ((flag[v] & VERTEX_SELECTED) != 0) ? colorVertexSelect : point_color;
|
|
|
|
|
point_color = ((flag[v] & VERTEX_ACTIVE) != 0) ? vec4(colorEditMeshActive.xyz, 1.0) : point_color;
|
|
|
|
|
|
|
|
|
|
colorDist(point_color, size);
|
2017-02-26 21:07:37 +01:00
|
|
|
}
|
2017-03-02 01:07:03 +01:00
|
|
|
#endif
|
2017-03-01 14:08:58 +01:00
|
|
|
|
2017-09-22 02:42:57 +02:00
|
|
|
#ifdef VERTEX_FACING
|
|
|
|
|
FragColor.a *= 1.0 - abs(facing) * 0.4;
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-03-01 14:08:58 +01:00
|
|
|
/* don't write depth if not opaque */
|
2017-09-21 22:00:48 +02:00
|
|
|
if (FragColor.a == 0.0) discard;
|
2017-02-26 21:07:37 +01:00
|
|
|
}
|