Repository navigation
fix: Prevent hero effect from exceeding white and triggering HDR #3294
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
Open
iwoplaza
wants to merge
2
commits into
release
Choose a base branch
from
fix/hero-effect-premultiplied-alpha
base: release
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+22
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,14 @@ const Uniforms = d.struct({ | |||||||||||
| modelMatrix: d.mat4x4f, | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| // Page backgrounds (in sRGB) the canvas is composited onto, matching `bg-white` and `bg-navy-bg` | ||||||||||||
| const lightBackground = d.vec3f(1); | ||||||||||||
| const darkBackground = d.vec3f(35 / 255, 39 / 255, 54 / 255); | ||||||||||||
|
|
||||||||||||
| function isDarkMode() { | ||||||||||||
| return document.documentElement.dataset.theme === 'dark' ? 1 : 0; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| export async function initHeroEffect(options: HeroEffectOptions) { | ||||||||||||
| const { root, context } = options; | ||||||||||||
| const canvas = context.canvas as HTMLCanvasElement; | ||||||||||||
|
|
@@ -32,6 +40,7 @@ export async function initHeroEffect(options: HeroEffectOptions) { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| const uniforms = root.createUniform(Uniforms); | ||||||||||||
| const darkMode = root.createUniform(d.u32, isDarkMode()); | ||||||||||||
| const fsampler = root.createSampler({ | ||||||||||||
| minFilter: 'linear', | ||||||||||||
| magFilter: 'linear', | ||||||||||||
|
|
@@ -140,8 +149,17 @@ export async function initHeroEffect(options: HeroEffectOptions) { | |||||||||||
| ); | ||||||||||||
|
|
||||||||||||
| const grayscale = d.vec4f(d.vec3f(c2.x + c2.y + c2.z + 0.8) * tint, 1) * 0.5 * c2.w; | ||||||||||||
|
|
||||||||||||
| return std.mix(grayscale, c2, std.smoothstep(0, 0.2, downSample)); | ||||||||||||
| const result = std.saturate(std.mix(grayscale, c2, std.smoothstep(0, 0.2, downSample))); | ||||||||||||
|
|
||||||||||||
| // The canvas is premultiplied, and the effect relies on color exceeding alpha (additive light). | ||||||||||||
| // The compositor outputs `color + background * (1 - alpha)`, which can go above 1.0 and | ||||||||||||
| // make the display switch to HDR. Clamping against the page background keeps it within range. | ||||||||||||
| let background = d.vec3f(lightBackground); | ||||||||||||
| if (darkMode.$ === 1) { | ||||||||||||
| background = d.vec3f(darkBackground); | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+157
to
+160
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| const maxColor = 1 - background * (1 - result.w); | ||||||||||||
| return d.vec4f(std.min(result.xyz, maxColor), result.w); | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| const postProcessPipeline = root.createRenderPipeline({ | ||||||||||||
|
|
@@ -165,6 +183,7 @@ export async function initHeroEffect(options: HeroEffectOptions) { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fluidSim.update(timestamp); | ||||||||||||
| darkMode.write(isDarkMode()); | ||||||||||||
|
|
||||||||||||
| const viewProjection = mat4.perspective( | ||||||||||||
| 0.5, | ||||||||||||
|
|
@@ -236,6 +255,7 @@ export async function initHeroEffect(options: HeroEffectOptions) { | |||||||||||
| fluidSim.destroy(); | ||||||||||||
| screenTextures.destroy(); | ||||||||||||
| uniforms.buffer.destroy(); | ||||||||||||
| darkMode.buffer.destroy(); | ||||||||||||
| model.destroy(); | ||||||||||||
| }, | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if this is a good idea that the effect relies on
undefinedbehavior (source).Clamping against the page background does not guarantee that
color <= alpha.See:
Legend:
x-> alphay-> colorb-> arbitrary channel from background color