Skip to content

Commit

Permalink
apply f32c.scale to meshscatter vertices in GLMakie, WGLMakie
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreyer committed Nov 22, 2024
1 parent 9d07bf5 commit 31ffdcd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
6 changes: 5 additions & 1 deletion GLMakie/assets/shader/particles.vert
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ vec3 _scale(samplerBuffer scale, int index);
vec3 _scale(vec3 scale, int index);
vec3 _scale(vec2 scale, int index);

uniform vec3 f32c_scale;

{{color_type}} color;
{{color_map_type}} color_map;
{{intensity_type}} intensity;
Expand Down Expand Up @@ -115,7 +117,7 @@ void main(){
int index = gl_InstanceID;
o_id = uvec2(objectid, index+1);
vec3 s = _scale(scale, index);
vec3 V = vertices * s;
vec3 V = s * vertices;
vec3 N = normals / s; // see issue #3702
vec3 pos;
{{position_calc}}
Expand All @@ -124,6 +126,8 @@ void main(){
o_uv = get_uv(index, texturecoordinates);
o_InstanceID = index;
rotate(rotation, index, V, N);
V = f32c_scale * V;
N = N / f32c_scale;
if (scale_primitive)
render(model * vec4(pos + V, 1), N, view, projection);
else
Expand Down
11 changes: 11 additions & 0 deletions GLMakie/src/drawing_primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,17 @@ function draw_atomic(screen::Screen, scene::Scene, @nospecialize(plot::Union{Sca
return draw_pixel_scatter(screen, positions, gl_attributes)
else
if plot isa MeshScatter
# If the vertices of the scattered mesh, markersize and (if it applies) model
# are float32 safe we should be able to just correct for any scaling from
# float32convert in the shader, after those conversions.
# We should also be fine as long as rotation = identity (also in model).
# If neither is the case we would have to combine vertices with positions and
# transform them to world space (post float32convert) on the CPU. We then can't
# do instancing anymore, so meshscatter becomes pointless.
if !isnothing(scene.float32convert)
gl_attributes[:f32c_scale] = map(x -> Vec3f(x.scale), plot, scene.float32convert.scaling)
end

if haskey(gl_attributes, :color) && to_value(gl_attributes[:color]) isa AbstractMatrix{<: Colorant}
gl_attributes[:image] = gl_attributes[:color]
end
Expand Down
1 change: 1 addition & 0 deletions GLMakie/src/glshaders/particles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function draw_mesh_particle(screen, p, data)
position = p[2] => TextureBuffer
scale = Vec3f(1) => TextureBuffer
rotation = rot => TextureBuffer
f32c_scale = Vec3f(1) # drawing_primitives.jl
texturecoordinates = nothing
end

Expand Down
6 changes: 4 additions & 2 deletions WGLMakie/assets/particles.vert
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ void main(){
vec3 vertex_position = get_markersize() * to_vec3(get_position());
vec3 N = get_normals() / get_markersize(); // see issue #3702
rotate(get_rotation(), vertex_position, N);
vertex_position = get_f32c_scale() * vertex_position;
N = N / get_f32c_scale();
vec4 position_world;
if (get_transform_marker())
position_world = model * vec4(to_vec3(get_offset()) + vertex_position, 1);
else
position_world = model * to_vec4(get_offset()) + vec4(vertex_position, 0);
position_world = model * to_vec4(to_vec3(get_offset())) + vec4(vertex_position, 0);

process_clip_planes(position_world.xyz);
o_normal = N;
o_normal = normalize(N);
frag_color = vertex_color(get_color(), get_colorrange(), colormap);
frag_uv = apply_uv_transform(get_uv_transform(), get_uv());
// direction to camera
Expand Down
10 changes: 10 additions & 0 deletions WGLMakie/src/particles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ function create_shader(scene::Scene, plot::MeshScatter)
uniform_dict[:interpolate_in_fragment_shader] = get(plot, :interpolate_in_fragment_shader, false)
uniform_dict[:transform_marker] = get(plot, :transform_marker, false)

# See GLMakie/drawing_primtives.jl
if isnothing(f32c)
uniform_dict[:f32c_scale] = Vec3f(1)
else
uniform_dict[:f32c_scale] = map(x -> Vec3f(x.scale), plot, scene.float32convert.scaling)
end

if haskey(uniform_dict, :color) && haskey(per_instance, :color)
to_value(uniform_dict[:color]) isa Bool && delete!(uniform_dict, :color)
to_value(per_instance[:color]) isa Bool && delete!(per_instance, :color)
Expand All @@ -84,6 +91,9 @@ function create_shader(scene::Scene, plot::MeshScatter)
if !hasproperty(instance, :uv)
uniform_dict[:uv] = Vec2f(0)
end
if !hasproperty(instance, :normals)
uniform_dict[:normals] = Vec3f(0)
end

uniform_dict[:depth_shift] = get(plot, :depth_shift, Observable(0f0))
uniform_dict[:backlight] = plot.backlight
Expand Down

0 comments on commit 31ffdcd

Please sign in to comment.