Skip to content

Commit

Permalink
fix Sampler, allow mipmap via Sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreyer committed Sep 15, 2024
1 parent 31aa47d commit a8a6120
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion GLMakie/src/GLAbstraction/GLTexture.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function Texture(s::ShaderAbstractions.Sampler{T, N}; kwargs...) where {T, N}
pointer(s.data), size(s.data),
minfilter = s.minfilter, magfilter = s.magfilter,
x_repeat = s.repeat[1], y_repeat = s.repeat[min(2, N)], z_repeat = s.repeat[min(3, N)],
anisotropic = s.anisotropic; kwargs...
mipmap = s.mipmap, anisotropic = s.anisotropic; kwargs...
)
obsfunc = ShaderAbstractions.connect!(s, tex)
push!(tex.observers, obsfunc)
Expand Down
4 changes: 4 additions & 0 deletions GLMakie/src/drawing_primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,10 @@ function mesh_inner(screen::Screen, mesh, transfunc, gl_attributes, plot, space=
return convert_attribute(uv_transform, key"uv_transform"())
end
end
elseif to_value(color) isa ShaderAbstractions.Sampler
gl_attributes[:image] = Texture(lift(el32convert, plot, color))
delete!(gl_attributes, :color_map)
delete!(gl_attributes, :color_norm)
elseif to_value(color) isa AbstractMatrix{<:Colorant}
gl_attributes[:image] = Texture(lift(el32convert, plot, color), minfilter = interp)
delete!(gl_attributes, :color_map)
Expand Down
4 changes: 3 additions & 1 deletion WGLMakie/src/Serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function delete_plots(plot_uuids) {
function convert_texture(scene, data) {
const tex = create_texture(scene, data);
tex.needsUpdate = true;
tex.generateMipmaps = data.mipmap;
tex.minFilter = THREE[data.minFilter];
tex.magFilter = THREE[data.magFilter];
tex.anisotropy = data.anisotropy;
Expand Down Expand Up @@ -266,12 +267,13 @@ function connect_uniforms(mesh, updater) {
function convert_RGB_to_RGBA(rgbArray) {
const length = rgbArray.length;
const rgbaArray = new rgbArray.constructor((length / 3) * 4);
const a = (rgbArray instanceof Uint8Array) ? 255 : 1.0;

for (let i = 0, j = 0; i < length; i += 3, j += 4) {
rgbaArray[j] = rgbArray[i]; // R
rgbaArray[j + 1] = rgbArray[i + 1]; // G
rgbaArray[j + 2] = rgbArray[i + 2]; // B
rgbaArray[j + 3] = 1.0; // A
rgbaArray[j + 3] = a; // A
}

return rgbaArray;
Expand Down
2 changes: 2 additions & 0 deletions WGLMakie/src/meshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function handle_color!(plot, uniforms, buffers, uniform_color_name = :uniform_co

if color[] isa Colorant
uniforms[uniform_color_name] = color
elseif color[] isa ShaderAbstractions.Sampler
uniforms[uniform_color_name] = to_value(color)
elseif color[] isa AbstractVector
buffers[:color] = Buffer(color)
elseif color[] isa Makie.AbstractPattern
Expand Down
16 changes: 12 additions & 4 deletions WGLMakie/src/serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function serialize_three(array::Buffer)
return serialize_three(flatten_buffer(array))
end

function serialize_three(array::AbstractArray{T}) where {T<:Union{UInt8,Int32,UInt32,Float32,Float16,Float64}}
function serialize_three(array::AbstractArray{T}) where {T<:Union{N0f8,UInt8,Int32,UInt32,Float32,Float16,Float64}}
vec(convert(Array, array))
end

Expand All @@ -78,6 +78,10 @@ three_type(::Type{UInt8}) = "UnsignedByteType"
function three_filter(sym::Symbol)
sym === :linear && return "LinearFilter"
sym === :nearest && return "NearestFilter"
sym == :nearest_mipmap_nearest && return "NearestMipmapNearestFilter"
sym == :nearest_mipmap_linear && return "NearestMipmapLinearFilter"
sym == :linear_mipmap_nearest && return "LinearMipmapNearestFilter"
sym == :linear_mipmap_linear && return "LinearMipmapLinearFilter"
error("Unknown filter mode '$sym'")
end

Expand All @@ -89,12 +93,16 @@ function three_repeat(s::Symbol)
end

function serialize_three(color::Sampler{T,N}) where {T,N}
tex = Dict(:type => "Sampler", :data => serialize_three(color.data),
:size => Int32[size(color.data)...], :three_format => three_format(T),
tex = Dict(:type => "Sampler",
:data => serialize_three(color.data),
:size => Int32[size(color.data)...],
:three_format => three_format(T),
:three_type => three_type(eltype(T)),
:minFilter => three_filter(color.minfilter),
:magFilter => three_filter(color.magfilter),
:wrapS => three_repeat(color.repeat[1]), :anisotropy => color.anisotropic)
:wrapS => three_repeat(color.repeat[1]),
:mipmap => color.mipmap,
:anisotropy => color.anisotropic)
if N > 1
tex[:wrapT] = three_repeat(color.repeat[2])
end
Expand Down
4 changes: 3 additions & 1 deletion WGLMakie/src/wglmakie.bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -21263,6 +21263,7 @@ function delete_plots(plot_uuids) {
function convert_texture(scene, data) {
const tex = create_texture(scene, data);
tex.needsUpdate = true;
tex.generateMipmaps = data.mipmap;
tex.minFilter = mod[data.minFilter];
tex.magFilter = mod[data.magFilter];
tex.anisotropy = data.anisotropy;
Expand Down Expand Up @@ -22501,11 +22502,12 @@ function connect_uniforms(mesh, updater) {
function convert_RGB_to_RGBA(rgbArray) {
const length = rgbArray.length;
const rgbaArray = new rgbArray.constructor(length / 3 * 4);
const a = rgbArray instanceof Uint8Array ? 255 : 1.0;
for(let i = 0, j = 0; i < length; i += 3, j += 4){
rgbaArray[j] = rgbArray[i];
rgbaArray[j + 1] = rgbArray[i + 1];
rgbaArray[j + 2] = rgbArray[i + 2];
rgbaArray[j + 3] = 1.0;
rgbaArray[j + 3] = a;
}
return rgbaArray;
}
Expand Down

0 comments on commit a8a6120

Please sign in to comment.