nicklockwood / Euclid

A Swift library for creating and manipulating 3D geometry
MIT License
644 stars 53 forks source link

Feature/vertex color component #74

Closed zilmarinen closed 2 years ago

zilmarinen commented 3 years ago

Adds a .color property to the Vertex to allow for per vertex colors when creating SCNGeometry.

Continuation from this PR which I mistakenly closed when trying to rebase on develop.

zilmarinen commented 3 years ago

@nicklockwood this has now all been rebased on develop. I like the vector semantic changes by the way 👍

You mentioned here that this PR is on hold due to a serialisation issue. Is this the same issue I raised on the previous PR, or is this another separate issue?

nicklockwood commented 3 years ago

@zilmarinen

Is this the same issue I raised on the previous PR

Yes, it's the issue you raised.

zilmarinen commented 3 years ago

Great, thanks for clarifying.

I shall spend some time investigating a resolution.

zilmarinen commented 3 years ago

@nicklockwood it looks like this may be an issue with the way the polygons are encoded around this check to see if vertex information can be omitted.

From inspecting the exported meshes, it appears that only the positions for each polygon are being serialised so the color component is lost which then defaults to clear.

Would simply changing this to include a check for colors suffice?

vertices.allSatisfy({ $0.texcoord == .origin && $0.normal == plane.normal && $0.color == .clear })

zilmarinen commented 3 years ago

I have confirmed that this is a symptom when not specifying UVs for vertices. I have added UVs for each face polygon and everything appears to be rendering correctly when exporting.

codecov-commenter commented 3 years ago

Codecov Report

Merging #74 (bac39ac) into develop (0a78b83) will decrease coverage by 0.50%. The diff coverage is 50.74%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop      #74      +/-   ##
===========================================
- Coverage    73.39%   72.88%   -0.51%     
===========================================
  Files           27       27              
  Lines         3525     3574      +49     
===========================================
+ Hits          2587     2605      +18     
- Misses         938      969      +31     
Impacted Files Coverage Δ
Sources/Euclid+SceneKit.swift 37.96% <0.00%> (-4.02%) :arrow_down:
Sources/Transforms.swift 52.13% <66.66%> (+0.15%) :arrow_up:
Sources/Color.swift 83.78% <100.00%> (+6.86%) :arrow_up:
Sources/Polygon.swift 91.40% <100.00%> (ø)
Sources/Vertex.swift 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 0a78b83...bac39ac. Read the comment docs.

nicklockwood commented 2 years ago

@zilmarinen sorry for the long delay, but I finally had a chance to look into this properly. I've merged a solution into develop - please take a look and check you're happy with it. Things to note:

1) A problem with the previous solutions was that they caused the Example project to render a blank object. After some investigation I realized that this was because the vertex color is multiplied by the mesh material, so using a clear color to mean "ignore the color" make the object transparent. Changing the default vertex color to white instead solves the problem (I think we may have discussed this before, but I'm not sure when/where). 2) The custom Metal renderer you wrote before doesn't seem to be needed - SceneKit is quite happy to render vertex colors out of the box.

There isn't currently a way to get Euclid to generate vertex colors for standard shapes, but you can either build a mesh with vertex colors polygon-by-polygon, or change the default color in Vertex.init(unchecked:) to something other than white, which will show up in the Example project, e.g.

self.color = color ?? .white.withAlpha(0.5) // makes mesh 50% transparent

or

self.color = color ?? .gray // makes mesh darker
nicklockwood commented 2 years ago

I've added a color property to PathPoint, so it's now possible to create vertex-colored geometry fairly easily, for example:

let mesh = Mesh.lathe(Path.curve([
    .point(0, -1, color: .red),
    .curve(-1, 0, color: .green),
    .point(0, 1, color: .blue),
]))
Screenshot 2021-12-31 at 14 19 59
nicklockwood commented 2 years ago

Landed in 0.5.16

zilmarinen commented 2 years ago

@nicklockwood Apologies for the delayed response. This is perfect, thank you very much!