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
2import bevy_render::view::View;
3import bevy_render::maths::affine3_to_square;
5@group(0) @binding(0) var<uniform> view: View;
7struct LineGizmoUniform {
8 world_from_local: mat3x4<f32>,
9 line_width: f32,
10 depth_bias: f32,
11 _joints_resolution: u32,
12 gap_scale: f32,
13 line_scale: f32,
14 animation_offset: f32,
15 @if(SIXTEEN_BYTE_ALIGNMENT)
16 // WebGL2 structs must be 16 byte aligned.
17 _padding: vec2<f32>,
20@group(1) @binding(0) var<uniform> line_gizmo: LineGizmoUniform;
22struct VertexInput {
23 @location(0) position_a: vec3<f32>,
24 @location(1) position_b: vec3<f32>,
25 @location(2) color_a: vec4<f32>,
26 @location(3) color_b: vec4<f32>,
27 @builtin(vertex_index) index: u32,
28};
30struct VertexOutput {
31 @builtin(position) clip_position: vec4<f32>,
32 @location(0) color: vec4<f32>,
33 @location(1) uv: f32,
34 @location(2) line_fraction: f32,
35};
37const EPSILON: f32 = 4.88e-04;
39@vertex
40fn vertex(vertex: VertexInput) -> VertexOutput {
41 var positions = array<vec2<f32>, 6>(
42 vec2(-0.5, 0.),
43 vec2(-0.5, 1.),
44 vec2(0.5, 1.),
45 vec2(-0.5, 0.),
46 vec2(0.5, 1.),
47 vec2(0.5, 0.)
48 );
49 let position = positions[vertex.index];
51 let world_from_local = affine3_to_square(line_gizmo.world_from_local);
53 // algorithm based on https://wwwtyro.net/2019/11/18/instanced-lines.html
54 var clip_a = view.clip_from_world * world_from_local * vec4(vertex.position_a, 1.);
55 var clip_b = view.clip_from_world * world_from_local * vec4(vertex.position_b, 1.);
57 // Manual near plane clipping to avoid errors when doing the perspective divide inside this shader.
58 clip_a = clip_near_plane(clip_a, clip_b);
59 clip_b = clip_near_plane(clip_b, clip_a);
60 let clip = mix(clip_a, clip_b, position.y);
62 let resolution = view.viewport.zw;
63 let screen_a = resolution * (0.5 * clip_a.xy / clip_a.w + 0.5);
64 let screen_b = resolution * (0.5 * clip_b.xy / clip_b.w + 0.5);
66 let y_basis = normalize(screen_b - screen_a);
67 let x_basis = vec2(-y_basis.y, y_basis.x);
69 var color = mix(vertex.color_a, vertex.color_b, position.y);
71 var line_width = line_gizmo.line_width;
72 var alpha = 1.;
74 var uv: f32;
75@if(PERSPECTIVE) {
76 line_width /= clip.w;
78 // get height of near clipping plane in world space
79 let pos0 = view.view_from_clip * vec4(0, -1, 0, 1); // Bottom of the screen
80 let pos1 = view.view_from_clip * vec4(0, 1, 0, 1); // Top of the screen
81 let near_clipping_plane_height = length(pos0.xyz - pos1.xyz);
83 // We can't use vertex.position_X because we may have changed the clip positions with clip_near_plane
84 let position_a = view.world_from_clip * clip_a;
85 let position_b = view.world_from_clip * clip_b;
86 let world_distance = length(position_a.xyz - position_b.xyz);
88 // Offset to compensate for moved clip positions. If removed dots on lines will slide when position a is ofscreen.
89 let clipped_offset = length(position_a.xyz - vertex.position_a);
91 uv = (clipped_offset + position.y * world_distance) * resolution.y / near_clipping_plane_height / line_gizmo.line_width;
92} @else {
93 // Get the distance of b to the camera along camera axes
94 let camera_b = view.view_from_clip * clip_b;
96 // This differentiates between orthographic and perspective cameras.
97 // For orthographic cameras no depth adaptment (depth_adaptment = 1) is needed.
98 var depth_adaptment: f32;
99 if (clip_b.w == 1.0) {
100 depth_adaptment = 1.0;
101 }
102 else {
103 depth_adaptment = -camera_b.z;
104 }
105 uv = position.y * depth_adaptment * length(screen_b - screen_a) / line_gizmo.line_width;
108 // Line thinness fade from https://acegikmo.com/shapes/docs/#anti-aliasing
109 if line_width > 0.0 && line_width < 1. {
110 color.a *= line_width;
111 line_width = 1.;
112 }
114 let x_offset = line_width * position.x * x_basis;
115 let screen = mix(screen_a, screen_b, position.y) + x_offset;
117 var depth: f32;
118 if line_gizmo.depth_bias >= 0. {
119 depth = clip.z * (1. - line_gizmo.depth_bias);
120 } else {
121 // depth * (clip.w / depth)^-depth_bias. So that when -depth_bias is 1.0, this is equal to clip.w
122 // and when equal to 0.0, it is exactly equal to depth.
123 // the epsilon is here to prevent the depth from exceeding clip.w when -depth_bias = 1.0
124 // clip.w represents the near plane in homogeneous clip space in bevy, having a depth
125 // of this value means nothing can be in front of this
126 // The reason this uses an exponential function is that it makes it much easier for the
127 // user to chose a value that is convenient for them
128 depth = clip.z * exp2(-line_gizmo.depth_bias * log2(clip.w / clip.z - EPSILON));
129 }
131 var clip_position = vec4(clip.w * ((2. * screen) / resolution - 1.), depth, clip.w);
133 let line_fraction = 2.0 * line_gizmo.line_scale / (line_gizmo.gap_scale + line_gizmo.line_scale);
134 uv /= (line_gizmo.gap_scale + line_gizmo.line_scale) / 2.0;
135 return VertexOutput(clip_position, color, uv, line_fraction);
138fn clip_near_plane(a: vec4<f32>, b: vec4<f32>) -> vec4<f32> {
139 // Move a if a is behind the near plane and b is in front.
140 if a.z > a.w && b.z <= b.w {
141 // Interpolate a towards b until it's at the near plane.
142 let distance_a = a.z - a.w;
143 let distance_b = b.z - b.w;
144 // Add an epsilon to the interpolator to ensure that the point is
145 // not just behind the clip plane due to floating-point imprecision,
146 // which can lead to lines showing up where they should not.
147 let t = distance_a / (distance_a - distance_b) + EPSILON;
148 return mix(a, b, t);
149 }
150 return a;
153struct FragmentInput {
154 @builtin(position) position: vec4<f32>,
155 @location(0) color: vec4<f32>,
156 @location(1) uv: f32,
157 @location(2) line_fraction: f32,
158};
160struct FragmentOutput {
161 @location(0) color: vec4<f32>,
162};
164fn non_solid_uv(in: FragmentInput) -> f32 {
165 @if(PERSPECTIVE)
166 let uv = in.uv;
167 @else
168 let uv = in.uv * in.position.w;
170 // subtract the animation offset here since otherwise the animation is
171 // running "backwards", e.g.
172 // - from the line end to the line start
173 // - from the arrow tip to the arrow start
174 let animated = uv - line_gizmo.animation_offset;
176 const PERIOD: f32 = 2.0;
177 return animated - floor(animated / PERIOD) * PERIOD;
180@fragment
181fn fragment_solid(in: FragmentInput) -> FragmentOutput {
182 return FragmentOutput(in.color);
185@fragment
186fn fragment_dotted(in: FragmentInput) -> FragmentOutput {
187 let uv = non_solid_uv(in);
188 let alpha = 1.0 - floor(uv);
190 return FragmentOutput(vec4(in.color.xyz, in.color.w * alpha));
193@fragment
194fn fragment_dashed(in: FragmentInput) -> FragmentOutput {
195 let uv = non_solid_uv(in);
196 let alpha = 1.0 - floor(min(uv / in.line_fraction, 1.0));
198 return FragmentOutput(vec4(in.color.xyz, in.color.w * alpha));
Contents