Skip to content

Refactor cortical sheet displacement into reusable shader utility - #708

Open
alexhuth wants to merge 1 commit into
mainfrom
claude/issue-704-45ztbm
Open

Refactor cortical sheet displacement into reusable shader utility#708
alexhuth wants to merge 1 commit into
mainfrom
claude/issue-704-45ztbm

Conversation

@alexhuth

Copy link
Copy Markdown
Contributor

Summary

This PR refactors duplicated cortical sheet displacement logic in vertex shaders into a reusable utility function, and ensures the bumpy flatmap feature is consistently applied across all shader variants (pick, depth, and surface shaders).

Key Changes

  • New shader utilities: Added bumpyflat header and cortsheet_displace() function to Shaderlib to eliminate code duplication

    • bumpyflat: Declares uniform and attribute variables needed for bumpy flatmap displacement
    • cortsheet_displace(morphs, thickmix): Encapsulates the identical displacement calculation that was previously copied across multiple shaders
  • Unified shader construction: Replaced inline displacement code in three shader variants with calls to utils.cortsheet_displace():

    • Main surface shader (thickmixer variant)
    • Colored surface shader
    • Depth shader
    • Pick shader
  • Consistent hasflat support: Added hasflat option to shader builders to ensure pick and depth shaders match the surface shader's flatmap configuration

    • Pick shader now receives hasflat:surf.flatlims !== undefined
    • Depth shader now receives hasflat:surf.flatlims !== undefined
    • Both now properly declare HASFLAT preprocessor flag when needed
  • Shared uniform references: Updated pick and depth shaders to reference surf.uniforms.bumpyflat directly (shared object, not a copy) so they automatically follow bumpy flatmap toggles without explicit event listeners

Implementation Details

  • The cortsheet_displace() function is parameterized by morph count and thickmix variable name to support different shader contexts
  • Conditional compilation with #ifdef CORTSHEET and #ifdef HASFLAT is preserved to maintain feature parity
  • The flatheight attribute is now only declared when HASFLAT is defined, reducing unnecessary attribute overhead

https://claude.ai/code/session_01Tr6MczRdE3ad5ss4FcsRcV

The pick and depth shaders hardcoded the thickness-based cortical-sheet
displacement (.62 * thickness) and knew nothing about HASFLAT or the
bumpyflat uniform, so on any subject with a flatmap they tested against
geometry that isn't the geometry being drawn:

  bumpy_flatmap on  -> surface bumps by flatheight, pick/depth used .62*thickness
  bumpy_flatmap off -> surface isn't displaced at all, pick/depth still displaced

The second row is the worse one, and it isn't about bumpy flatmaps: on a
partially unfolded surface with the bump off, the picker pushed vertices
0.62 * thickness along their normals while the surface stayed put, which
shows up as picking that lands a few pixels off (worst near silhouette
edges, where the normal is perpendicular to the view direction) and as
ROI labels occluded against slightly the wrong surface.

Rather than add a fourth copy of the displacement expression -- four
copies is how the shaders drifted apart to begin with -- pull it into
utils.cortsheet_displace and have all four shaders emit it, along with
utils.bumpyflat for the uniform and attribute it needs. pick and depth
now take a hasflat opt, bind the flatheight attribute, and share the
surface's bumpyflat uniform object so they follow the toggle without
listening for it. The GLSL the surface shaders emit is unchanged apart
from where the flatheight declaration sits.

Fixes #704

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tr6MczRdE3ad5ss4FcsRcV
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.

2 participants