虚线的代码完成。

This commit is contained in:
xsl
2025-09-22 18:10:55 +08:00
parent 60afc59582
commit 3207c47dd2
4 changed files with 62 additions and 17 deletions
+50 -10
View File
@@ -1,24 +1,64 @@
#version 450
layout(location = 0) in vec3 fragColor;
layout(location = 1) in float fragLinePosition;
layout(location = 1) in vec3 fragSegmentStart;
layout(location = 2) in vec3 fragSegmentEnd;
layout(location = 3) in vec3 fragWorldPos;
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() {
// 硬编码虚线参数
const float dashSize = 0.05;
const float gapSize = 0.05;
// 转换为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);
// 使用时间创建动画效果(需要GL_EXT_shader_realtime_clock_speed扩展
// 或者使用一个简单的基于帧计数的方法
float time = float(gl_FragCoord.x + gl_FragCoord.y) * 0.01; // 简单的伪时间
// 计算线段在屏幕空间的长度(用于抗锯齿
float segmentLength = length(ndcEnd.xy - ndcStart.xy);
float patternPosition = mod(fragLinePosition, dashSize + gapSize);
// 应用虚线模式
float dashPeriod = dashParams.dashSize + dashParams.gapSize;
float currentPos = mod(projection + dashParams.dashOffset, dashPeriod);
if (patternPosition > dashSize) {
// 抗锯齿处理
float alpha = 1.0;
// 线段开始处的抗锯齿(避免线段起始处出现半截虚线)
alpha *= smoothstep(0.0, dashParams.uAASize / segmentLength, projection);
// 线段结束处的抗锯齿
alpha *= 1.0 - smoothstep(segmentLength - dashParams.uAASize / segmentLength,
segmentLength, projection);
// 虚线模式抗锯齿
alpha *= 1.0 - smoothstep(dashParams.dashSize,
dashParams.dashSize + dashParams.uAASize / dashPeriod,
currentPos);
// 如果alpha接近0,直接丢弃片元
if (alpha < 0.01) {
discard;
}
outColor = vec4(fragColor, 1.0);
outColor = vec4(fragColor, alpha);
}
Binary file not shown.
+12 -7
View File
@@ -2,9 +2,13 @@
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec3 inLineStart;
layout(location = 3) in vec3 inLineEnd;
layout(location = 0) out vec3 fragColor;
layout(location = 1) out float fragLinePosition; // 用于虚线计算
layout(location = 1) out vec3 fragLineStart;
layout(location = 2) out vec3 fragLineEnd;
layout(binding = 0) uniform UniformBufferObject {
mat4 proj;
@@ -13,11 +17,12 @@ layout(binding = 0) uniform UniformBufferObject {
} ubo;
void main() {
mat4 mvp = ubo.proj * ubo.view * ubo.model;
gl_Position = mvp * vec4(inPosition, 1.0);
fragColor = inColor;
gl_PointSize = 2.0;
mat4 worldviewproj = ubo.proj * ubo.view * ubo.model;
gl_Position = worldviewproj * vec4(inPosition, 1.0);
// 硬编码虚线参数:使用模型空间的X坐标,你可以根据需要调整
fragLinePosition = inPosition.x;
fragColor = inColor;
// 传递裁剪空间坐标
fragLineStart = (worldviewproj * vec4(inLineStart, 1.0)).xyz;
fragLineEnd = (worldviewproj * vec4(inLineEnd, 1.0)).xyz;
}
Binary file not shown.