Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for materials in obj files #98

Merged
merged 9 commits into from
Oct 17, 2024

Conversation

ffreyer
Copy link
Collaborator

@ffreyer ffreyer commented Sep 15, 2024

Replacement for #95 after #97 is merged.
Tests pass with warnings locally due to missing cube.mtl

Example from #95 becomes:

using FileIO, MeshIO, GeometryBasics
path = "..."
filepath = joinpath(path, "14-girl-obj/girl OBJ.obj")
mesh = load(filepath)

meshes = GeometryBasics.split_mesh(mesh.mesh)
# filename incorrect in mtl file...?
mesh[:materials]["FACE"]["diffuse map"]["filename"] = "D:/data/Julia/14-girl-obj/tEXTURE/FACE Base Color apha.png"

using GLMakie

begin
    fig = Figure()
    ax = LScene(fig[1, 1])

    for (i, m) in enumerate(meshes)
        # get material associated with a submesh
        material_name = mesh[:material_names][i]
        material = mesh[:materials][material_name]

        # load texture from filename specified in .mtl file
        texture = try
            load(material["diffuse map"]["filename"])
        catch
            RGBf(0,1,0)
        end

        # texture uses white as transparent for some reason...
        if material_name == "FACE"
            texture = map(texture) do c
                RGBAf(c.r, c.g, c.b, 1 - c.r)
            end
        end

        # render mesh with specified material properties if specified
        mesh!(ax, m, color = texture,
            diffuse = get(material, "diffuse", Vec3f(1)),
            specular = get(material, "specular", Vec3f(0.2)),
            shininess = get(material, "shininess", 32f0),
        )
    end

    fig
end

@ffreyer ffreyer changed the title Add support for meterials in obj files Add support for materials in obj files Sep 15, 2024
@ffreyer ffreyer marked this pull request as ready for review October 17, 2024 12:48
@ffreyer ffreyer merged commit c72b507 into ff/GeometryBasics_refactor Oct 17, 2024
0 of 4 checks passed
@ffreyer ffreyer deleted the ff/materials2 branch October 17, 2024 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant