画出虚线来了
This commit is contained in:
@@ -1,67 +1,48 @@
|
||||
#version 450
|
||||
|
||||
layout(location = 0) in vec3 fragColor;
|
||||
layout(location = 1) in vec3 fragSegmentStart;
|
||||
layout(location = 2) in vec3 fragSegmentEnd;
|
||||
//layout(location = 3) in vec3 fragWorldPos;
|
||||
layout(location = 1) in vec3 fragLineStart;
|
||||
layout(location = 2) in vec3 fragLineEnd;
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
layout(binding = 0) uniform UniformBufferObject {
|
||||
mat4 proj;
|
||||
mat4 model;
|
||||
mat4 view;
|
||||
} ubo;
|
||||
|
||||
layout(binding = 1) uniform DashParameters {
|
||||
float dashSize;
|
||||
float gapSize;
|
||||
float dashOffset;
|
||||
float uAASize;
|
||||
int useWorldSpace;
|
||||
} dashParams;
|
||||
|
||||
void main() {
|
||||
// 转换为NDC坐标
|
||||
vec3 ndcStart = fragSegmentStart / fragSegmentStart.z;
|
||||
vec3 ndcEnd = fragSegmentEnd / fragSegmentEnd.z;
|
||||
vec3 fragNDC = gl_FragCoord.xyz / gl_FragCoord.z;
|
||||
fragNDC = (fragNDC * 2.0) - 1.0;
|
||||
|
||||
// 计算当前片元在线段上的投影
|
||||
vec2 lineDir = normalize(ndcEnd.xy - ndcStart.xy);
|
||||
vec2 startToFrag = fragNDC.xy - ndcStart.xy;
|
||||
float projection = dot(startToFrag, lineDir);
|
||||
vec2 fragPos = gl_FragCoord.xy;
|
||||
|
||||
// 计算线段在屏幕空间的长度(用于抗锯齿)
|
||||
float segmentLength = length(ndcEnd.xy - ndcStart.xy);
|
||||
vec2 start = (fragLineStart.xy * 0.5 + 0.5) * 480.0;
|
||||
vec2 end = (fragLineEnd.xy * 0.5 + 0.5) * 480.0;
|
||||
|
||||
// 应用虚线模式
|
||||
float dashPeriod = dashParams.dashSize + dashParams.gapSize;
|
||||
float currentPos = mod(projection + dashParams.dashOffset, dashPeriod);
|
||||
vec2 lineDir = end - start;
|
||||
float lineLength = length(lineDir);
|
||||
|
||||
// 抗锯齿处理
|
||||
float alpha = 1.0;
|
||||
if (lineLength < 1.0) {
|
||||
outColor = vec4(fragColor, 1.0);
|
||||
return;
|
||||
}
|
||||
|
||||
// 线段开始处的抗锯齿(避免线段起始处出现半截虚线)
|
||||
alpha *= smoothstep(0.0, dashParams.uAASize / segmentLength, projection);
|
||||
lineDir = normalize(lineDir);
|
||||
vec2 toFrag = fragPos - start;
|
||||
float projection = dot(toFrag, lineDir);
|
||||
projection = clamp(projection, 0.0, lineLength);
|
||||
|
||||
// 线段结束处的抗锯齿
|
||||
alpha *= 1.0 - smoothstep(segmentLength - dashParams.uAASize / segmentLength,
|
||||
segmentLength, projection);
|
||||
vec2 closestPoint = start + lineDir * projection;
|
||||
float distanceToLine = length(fragPos - closestPoint);
|
||||
|
||||
// 虚线模式抗锯齿
|
||||
alpha *= 1.0 - smoothstep(dashParams.dashSize,
|
||||
dashParams.dashSize + dashParams.uAASize / dashPeriod,
|
||||
currentPos);
|
||||
float lineWidth = 6.0;
|
||||
|
||||
// 如果alpha接近0,直接丢弃片元
|
||||
if (distanceToLine > lineWidth) {
|
||||
discard;
|
||||
}
|
||||
|
||||
// if (alpha < 0.01) {
|
||||
// discard;
|
||||
// }
|
||||
float dashLength = 50.0;
|
||||
float gapLength = 20.0;
|
||||
|
||||
// outColor = vec4(fragColor, alpha);
|
||||
|
||||
outColor = vec4(1, 0, 0, 1.0);
|
||||
float patternLength = dashLength + gapLength;
|
||||
float patternPos = mod(projection, patternLength);
|
||||
|
||||
if (patternPos > dashLength) {
|
||||
discard;
|
||||
}
|
||||
|
||||
outColor = vec4(fragColor, 1.0);
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user