Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1// TODO use common view binding
2#import bevy_render::view::View
4@group(0) @binding(0) var<uniform> view: View;
7struct LineGizmoUniform {
8 line_width: f32,
9 depth_bias: f32,
10#ifdef SIXTEEN_BYTE_ALIGNMENT
11 // WebGL2 structs must be 16 byte aligned.
12 _padding: vec2<f32>,
13#endif
16@group(1) @binding(0) var<uniform> line_gizmo: LineGizmoUniform;
18struct VertexInput {
19 @location(0) position_a: vec3<f32>,
20 @location(1) position_b: vec3<f32>,
21 @location(2) color_a: vec4<f32>,
22 @location(3) color_b: vec4<f32>,
23 @builtin(vertex_index) index: u32,
24};
26struct VertexOutput {
27 @builtin(position) clip_position: vec4<f32>,
28 @location(0) color: vec4<f32>,
29 @location(1) uv: f32,
30};
32const EPSILON: f32 = 4.88e-04;
34@vertex
35fn vertex(vertex: VertexInput) -> VertexOutput {
36 var positions = array<vec2<f32>, 6>(
37 vec2(-0.5, 0.),
38 vec2(-0.5, 1.),
39 vec2(0.5, 1.),
40 vec2(-0.5, 0.),
41 vec2(0.5, 1.),
42 vec2(0.5, 0.)
43 );
44 let position = positions[vertex.index];
46 // algorithm based on https://wwwtyro.net/2019/11/18/instanced-lines.html
47 var clip_a = view.clip_from_world * vec4(vertex.position_a, 1.);
48 var clip_b = view.clip_from_world * vec4(vertex.position_b, 1.);
50 // Manual near plane clipping to avoid errors when doing the perspective divide inside this shader.
51 clip_a = clip_near_plane(clip_a, clip_b);
52 clip_b = clip_near_plane(clip_b, clip_a);
53 let clip = mix(clip_a, clip_b, position.y);
55 let resolution = view.viewport.zw;
56 let screen_a = resolution * (0.5 * clip_a.xy / clip_a.w + 0.5);
57 let screen_b = resolution * (0.5 * clip_b.xy / clip_b.w + 0.5);
59 let y_basis = normalize(screen_b - screen_a);
60 let x_basis = vec2(-y_basis.y, y_basis.x);
62 var color = mix(vertex.color_a, vertex.color_b, position.y);
64 var line_width = line_gizmo.line_width;
65 var alpha = 1.;
67 var uv: f32;
68#ifdef PERSPECTIVE
69 line_width /= clip.w;
71 // get height of near clipping plane in world space
72 let pos0 = view.view_from_clip * vec4(0, -1, 0, 1); // Bottom of the screen
73 let pos1 = view.view_from_clip * vec4(0, 1, 0, 1); // Top of the screen
74 let near_clipping_plane_height = length(pos0.xyz - pos1.xyz);
76 // We can't use vertex.position_X because we may have changed the clip positions with clip_near_plane
77 let position_a = view.world_from_clip * clip_a;
78 let position_b = view.world_from_clip * clip_b;
79 let world_distance = length(position_a.xyz - position_b.xyz);
81 // Offset to compensate for moved clip positions. If removed dots on lines will slide when position a is ofscreen.
82 let clipped_offset = length(position_a.xyz - vertex.position_a);
84 uv = (clipped_offset + position.y * world_distance) * resolution.y / near_clipping_plane_height / line_gizmo.line_width;
85#else
86 // Get the distance of b to the camera along camera axes
87 let camera_b = view.view_from_clip * clip_b;
89 // This differentiates between orthographic and perspective cameras.
90 // For orthographic cameras no depth adaptment (depth_adaptment = 1) is needed.
91 var depth_adaptment: f32;
92 if (clip_b.w == 1.0) {
93 depth_adaptment = 1.0;
94 }
95 else {
96 depth_adaptment = -camera_b.z;
97 }
98 uv = position.y * depth_adaptment * length(screen_b - screen_a) / line_gizmo.line_width;
99#endif
101 // Line thinness fade from https://acegikmo.com/shapes/docs/#anti-aliasing
102 if line_width > 0.0 && line_width < 1. {
103 color.a *= line_width;
104 line_width = 1.;
105 }
107 let x_offset = line_width * position.x * x_basis;
108 let screen = mix(screen_a, screen_b, position.y) + x_offset;
110 var depth: f32;
111 if line_gizmo.depth_bias >= 0. {
112 depth = clip.z * (1. - line_gizmo.depth_bias);
113 } else {
114 // depth * (clip.w / depth)^-depth_bias. So that when -depth_bias is 1.0, this is equal to clip.w
115 // and when equal to 0.0, it is exactly equal to depth.
116 // the epsilon is here to prevent the depth from exceeding clip.w when -depth_bias = 1.0
117 // clip.w represents the near plane in homogeneous clip space in bevy, having a depth
118 // of this value means nothing can be in front of this
119 // The reason this uses an exponential function is that it makes it much easier for the
120 // user to chose a value that is convenient for them
121 depth = clip.z * exp2(-line_gizmo.depth_bias * log2(clip.w / clip.z - EPSILON));
122 }
124 var clip_position = vec4(clip.w * ((2. * screen) / resolution - 1.), depth, clip.w);
126 return VertexOutput(clip_position, color, uv);
129fn clip_near_plane(a: vec4<f32>, b: vec4<f32>) -> vec4<f32> {
130 // Move a if a is behind the near plane and b is in front.
131 if a.z > a.w && b.z <= b.w {
132 // Interpolate a towards b until it's at the near plane.
133 let distance_a = a.z - a.w;
134 let distance_b = b.z - b.w;
135 // Add an epsilon to the interpolator to ensure that the point is
136 // not just behind the clip plane due to floating-point imprecision.
137 let t = distance_a / (distance_a - distance_b) + EPSILON;
138 return mix(a, b, t);
139 }
140 return a;
143struct FragmentInput {
144 @builtin(position) position: vec4<f32>,
145 @location(0) color: vec4<f32>,
146 @location(1) uv: f32,
147};
149struct FragmentOutput {
150 @location(0) color: vec4<f32>,
151};
153@fragment
154fn fragment_solid(in: FragmentInput) -> FragmentOutput {
155 return FragmentOutput(in.color);
157@fragment
158fn fragment_dotted(in: FragmentInput) -> FragmentOutput {
159 var alpha: f32;
160#ifdef PERSPECTIVE
161 alpha = 1 - floor(in.uv % 2.0);
162#else
163 alpha = 1 - floor((in.uv * in.position.w) % 2.0);
164#endif
166 return FragmentOutput(vec4(in.color.xyz, in.color.w * alpha));
Contents