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#import bevy_render::view::View
3@group(0) @binding(0) var<uniform> view: View;
6struct LineGizmoUniform {
7 line_width: f32,
8 depth_bias: f32,
9 resolution: u32,
10#ifdef SIXTEEN_BYTE_ALIGNMENT
11 // WebGL2 structs must be 16 byte aligned.
12 _padding: f32,
13#endif
16@group(1) @binding(0) var<uniform> joints_gizmo: LineGizmoUniform;
18struct VertexInput {
19 @location(0) position_a: vec3<f32>,
20 @location(1) position_b: vec3<f32>,
21 @location(2) position_c: vec3<f32>,
22 @location(3) color: 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};
31const EPSILON: f32 = 4.88e-04;
33@vertex
34fn vertex_bevel(vertex: VertexInput) -> VertexOutput {
35 var positions = array<vec2<f32>, 3>(
36 vec2(0, 0),
37 vec2(0, 0.5),
38 vec2(0.5, 0),
39 );
40 var position = positions[vertex.index];
42 var clip_a = view.clip_from_world * vec4(vertex.position_a, 1.);
43 var clip_b = view.clip_from_world * vec4(vertex.position_b, 1.);
44 var clip_c = view.clip_from_world * vec4(vertex.position_c, 1.);
46 // Manual near plane clipping to avoid errors when doing the perspective divide inside this shader.
47 clip_a = clip_near_plane(clip_a, clip_c);
48 clip_b = clip_near_plane(clip_b, clip_a);
49 clip_c = clip_near_plane(clip_c, clip_b);
50 clip_a = clip_near_plane(clip_a, clip_c);
52 let resolution = view.viewport.zw;
53 let screen_a = resolution * (0.5 * clip_a.xy / clip_a.w + 0.5);
54 let screen_b = resolution * (0.5 * clip_b.xy / clip_b.w + 0.5);
55 let screen_c = resolution * (0.5 * clip_c.xy / clip_c.w + 0.5);
57 var color = vertex.color;
58 var line_width = joints_gizmo.line_width;
60#ifdef PERSPECTIVE
61 line_width /= clip_b.w;
62#endif
64 // Line thinness fade from https://acegikmo.com/shapes/docs/#anti-aliasing
65 if line_width > 0.0 && line_width < 1. {
66 color.a *= line_width;
67 line_width = 1.;
68 }
70 let ab = normalize(screen_b - screen_a);
71 let cb = normalize(screen_b - screen_c);
72 let ab_norm = vec2(-ab.y, ab.x);
73 let cb_norm = vec2(cb.y, -cb.x);
74 let tangent = normalize(ab - cb);
75 let normal = vec2(-tangent.y, tangent.x);
76 let sigma = sign(dot(ab + cb, normal));
78 var p0 = line_width * sigma * ab_norm;
79 var p1 = line_width * sigma * cb_norm;
81 let screen = screen_b + position.x * p0 + position.y * p1;
83 let depth = depth(clip_b);
85 var clip_position = vec4(clip_b.w * ((2. * screen) / resolution - 1.), depth, clip_b.w);
86 return VertexOutput(clip_position, color);
89@vertex
90fn vertex_miter(vertex: VertexInput) -> VertexOutput {
91 var positions = array<vec3<f32>, 6>(
92 vec3(0, 0, 0),
93 vec3(0.5, 0, 0),
94 vec3(0, 0.5, 0),
95 vec3(0, 0, 0),
96 vec3(0, 0.5, 0),
97 vec3(0, 0, 0.5),
98 );
99 var position = positions[vertex.index];
101 var clip_a = view.clip_from_world * vec4(vertex.position_a, 1.);
102 var clip_b = view.clip_from_world * vec4(vertex.position_b, 1.);
103 var clip_c = view.clip_from_world * vec4(vertex.position_c, 1.);
105 // Manual near plane clipping to avoid errors when doing the perspective divide inside this shader.
106 clip_a = clip_near_plane(clip_a, clip_c);
107 clip_b = clip_near_plane(clip_b, clip_a);
108 clip_c = clip_near_plane(clip_c, clip_b);
109 clip_a = clip_near_plane(clip_a, clip_c);
111 let resolution = view.viewport.zw;
112 let screen_a = resolution * (0.5 * clip_a.xy / clip_a.w + 0.5);
113 let screen_b = resolution * (0.5 * clip_b.xy / clip_b.w + 0.5);
114 let screen_c = resolution * (0.5 * clip_c.xy / clip_c.w + 0.5);
116 var color = vertex.color;
117 var line_width = joints_gizmo.line_width;
119#ifdef PERSPECTIVE
120 line_width /= clip_b.w;
121#endif
123 // Line thinness fade from https://acegikmo.com/shapes/docs/#anti-aliasing
124 if line_width > 0.0 && line_width < 1. {
125 color.a *= line_width;
126 line_width = 1.;
127 }
129 let ab = normalize(screen_b - screen_a);
130 let cb = normalize(screen_b - screen_c);
131 let ab_norm = vec2(-ab.y, ab.x);
132 let cb_norm = vec2(cb.y, -cb.x);
133 let tangent = normalize(ab - cb);
134 let normal = vec2(-tangent.y, tangent.x);
135 let sigma = sign(dot(ab + cb, normal));
137 var p0 = line_width * sigma * ab_norm;
138 var p1 = line_width * sigma * normal / dot(normal, ab_norm);
139 var p2 = line_width * sigma * cb_norm;
141 var screen = screen_b + position.x * p0 + position.y * p1 + position.z * p2;
143 var depth = depth(clip_b);
145 var clip_position = vec4(clip_b.w * ((2. * screen) / resolution - 1.), depth, clip_b.w);
146 return VertexOutput(clip_position, color);
149@vertex
150fn vertex_round(vertex: VertexInput) -> VertexOutput {
151 var clip_a = view.clip_from_world * vec4(vertex.position_a, 1.);
152 var clip_b = view.clip_from_world * vec4(vertex.position_b, 1.);
153 var clip_c = view.clip_from_world * vec4(vertex.position_c, 1.);
155 // Manual near plane clipping to avoid errors when doing the perspective divide inside this shader.
156 clip_a = clip_near_plane(clip_a, clip_c);
157 clip_b = clip_near_plane(clip_b, clip_a);
158 clip_c = clip_near_plane(clip_c, clip_b);
159 clip_a = clip_near_plane(clip_a, clip_c);
161 let resolution = view.viewport.zw;
162 let screen_a = resolution * (0.5 * clip_a.xy / clip_a.w + 0.5);
163 let screen_b = resolution * (0.5 * clip_b.xy / clip_b.w + 0.5);
164 let screen_c = resolution * (0.5 * clip_c.xy / clip_c.w + 0.5);
166 var color = vertex.color;
167 var line_width = joints_gizmo.line_width;
169#ifdef PERSPECTIVE
170 line_width /= clip_b.w;
171#endif
173 // Line thinness fade from https://acegikmo.com/shapes/docs/#anti-aliasing
174 if line_width > 0.0 && line_width < 1. {
175 color.a *= line_width;
176 line_width = 1.;
177 }
179 let ab = normalize(screen_b - screen_a);
180 let cb = normalize(screen_b - screen_c);
181 let ab_norm = vec2(-ab.y, ab.x);
182 let cb_norm = vec2(cb.y, -cb.x);
184 // We render `joints_gizmo.resolution`triangles. The vertices in each triangle are ordered as follows:
185 // - 0: The 'center' vertex at `screen_b`.
186 // - 1: The vertex closer to the ab line.
187 // - 2: The vertex closer to the cb line.
188 var in_triangle_index = f32(vertex.index) % 3.0;
189 var tri_index = floor(f32(vertex.index) / 3.0);
190 var radius = sign(in_triangle_index) * 0.5 * line_width;
191 var theta = acos(dot(ab_norm, cb_norm));
192 let sigma = sign(dot(ab_norm, cb));
193 var angle = theta * (tri_index + in_triangle_index - 1) / f32(joints_gizmo.resolution);
194 var position_x = sigma * radius * cos(angle);
195 var position_y = radius * sin(angle);
197 var screen = screen_b + position_x * ab_norm + position_y * ab;
199 var depth = depth(clip_b);
201 var clip_position = vec4(clip_b.w * ((2. * screen) / resolution - 1.), depth, clip_b.w);
202 return VertexOutput(clip_position, color);
205fn clip_near_plane(a: vec4<f32>, b: vec4<f32>) -> vec4<f32> {
206 // Move a if a is behind the near plane and b is in front.
207 if a.z > a.w && b.z <= b.w {
208 // Interpolate a towards b until it's at the near plane.
209 let distance_a = a.z - a.w;
210 let distance_b = b.z - b.w;
211 // Add an epsilon to the interpolator to ensure that the point is
212 // not just behind the clip plane due to floating-point imprecision.
213 let t = distance_a / (distance_a - distance_b) + EPSILON;
214 return mix(a, b, t);
215 }
216 return a;
219fn depth(clip: vec4<f32>) -> f32 {
220 var depth: f32;
221 if joints_gizmo.depth_bias >= 0. {
222 depth = clip.z * (1. - joints_gizmo.depth_bias);
223 } else {
224 // depth * (clip.w / depth)^-depth_bias. So that when -depth_bias is 1.0, this is equal to clip.w
225 // and when equal to 0.0, it is exactly equal to depth.
226 // the epsilon is here to prevent the depth from exceeding clip.w when -depth_bias = 1.0
227 // clip.w represents the near plane in homogeneous clip space in bevy, having a depth
228 // of this value means nothing can be in front of this
229 // The reason this uses an exponential function is that it makes it much easier for the
230 // user to chose a value that is convenient for them
231 depth = clip.z * exp2(-joints_gizmo.depth_bias * log2(clip.w / clip.z - EPSILON));
232 }
233 return depth;
236struct FragmentInput {
237 @location(0) color: vec4<f32>,
238};
240struct FragmentOutput {
241 @location(0) color: vec4<f32>,
242};
244@fragment
245fn fragment(in: FragmentInput) -> FragmentOutput {
246 // return FragmentOutput(vec4(1, 1, 1, 1));
247 return FragmentOutput(in.color);
Contents