-
Notifications
You must be signed in to change notification settings - Fork 127
feat: Ramp #219
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
Merged
Merged
feat: Ramp #219
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7f48f04
feat: ramp
chasedavis 09db3b5
typefix
chasedavis fdd2c51
lint
chasedavis 13929bf
fix(Ramp): types, exports
CodyJasonBennett b8f19bd
Merge branch 'master' into pr/219
CodyJasonBennett 4ac59b6
refactor: add RampType enum
CodyJasonBennett 4637759
docs: add Ramp
CodyJasonBennett 22617be
fix(Ramp): tree-shaking annotations
CodyJasonBennett 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 |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { Uniform } from 'three' | ||
| import { BlendFunction, Effect } from 'postprocessing' | ||
| import { wrapEffect } from '../util' | ||
|
|
||
| const RampShader = { | ||
| fragmentShader: ` | ||
|
|
||
| uniform int rampType; | ||
|
|
||
| uniform vec2 rampStart; | ||
| uniform vec2 rampEnd; | ||
|
|
||
| uniform vec4 startColor; | ||
| uniform vec4 endColor; | ||
|
|
||
| uniform float rampBias; | ||
| uniform float rampGain; | ||
|
|
||
| uniform bool rampMask; | ||
| uniform bool rampInvert; | ||
|
|
||
| float getBias(float time, float bias) { | ||
| return (time / ((((1.0 / bias) - 2.0) * (1.0 - time)) + 1.0)); | ||
| } | ||
|
|
||
| float getGain(float time, float gain) { | ||
| if(time < 0.5) | ||
| return getBias(time * 2.0, gain) / 2.0; | ||
| else | ||
| return getBias(time * 2.0 - 1.0, 1.0 - gain) / 2.0 + 0.5; | ||
| } | ||
|
|
||
| void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) { | ||
|
|
||
| vec2 startPixel = rampStart * resolution.xy; | ||
| vec2 endPixel = rampEnd * resolution.xy; | ||
|
|
||
| float rampAlpha, radius; | ||
|
|
||
| if (rampType == 1) { | ||
| vec2 fuv = uv * resolution.xy / resolution.y; | ||
| vec2 suv = startPixel / resolution.y; | ||
| vec2 euv = endPixel / resolution.y; | ||
|
|
||
| radius = length(suv - euv); | ||
| float falloff = length(fuv - suv); | ||
| rampAlpha = smoothstep(0., radius, falloff); | ||
| } | ||
|
|
||
| else { | ||
| radius = length(startPixel - endPixel); | ||
| vec2 direction = normalize(vec2(endPixel.x - startPixel.x, -(startPixel.y - endPixel.y))); | ||
|
|
||
| float fade = dot(uv * resolution - startPixel, direction); | ||
| if (rampType == 2) { fade = abs(fade); } | ||
|
|
||
| rampAlpha = smoothstep(0.0, 1.0, fade / radius); | ||
| } | ||
|
|
||
| rampAlpha = abs((rampInvert ? 1.0 : 0.0) - getBias(rampAlpha, rampBias) * getGain(rampAlpha, rampGain)); | ||
|
|
||
| if (!rampMask) { | ||
| outputColor = mix(startColor, endColor, rampAlpha); | ||
| } | ||
|
|
||
| else { | ||
| vec4 inputBuff = texture2D(inputBuffer, uv); | ||
| outputColor = mix(inputBuff, inputColor, rampAlpha); | ||
| } | ||
| }`, | ||
| } | ||
|
|
||
| export class RampEffect extends Effect { | ||
| constructor({ | ||
| blendFunction = BlendFunction.NORMAL, // Multiply default, but blend modes are great for ramp | ||
| rampType = 0, // {0 : linear, 1 : radial, 2 : linear (mirrored)} | ||
| rampStart = [0.5, 0.5], // [0, 1) as normalized x,y | ||
| rampEnd = [1, 1], // [0, 1) as normalized x,y | ||
| startColor = [0, 0, 0, 1], // black default | ||
| endColor = [1, 1, 1, 1], // white default | ||
| rampBias = 0.5, // [0, 1] - linear interpolation curve when both bias and gain are 0.5 | ||
| rampGain = 0.5, // [0, 1] - linear interpolation curve when both bias and gain are 0.5 | ||
| rampMask = false, // bool - uses ramp as effect mask, ignores colors when true | ||
| rampInvert = false // when false, rampStart is transparent and rampEnd is opaque | ||
| } = {}) { | ||
| super('RampEffect', RampShader.fragmentShader, { | ||
| blendFunction, | ||
| uniforms: new Map<string, Uniform<number | number[]>>([ | ||
| ['rampType', new Uniform(rampType)], | ||
| ['rampStart', new Uniform(rampStart)], | ||
| ['rampEnd', new Uniform(rampEnd)], | ||
| ['startColor', new Uniform(startColor)], | ||
| ['endColor', new Uniform(endColor)], | ||
| ['rampBias', new Uniform(rampBias)], | ||
| ['rampGain', new Uniform(rampGain)], | ||
| ['rampMask', new Uniform(rampMask)], | ||
| ['rampInvert', new Uniform(rampInvert)], | ||
| ]), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| const Ramp = wrapEffect(RampEffect) | ||
|
|
||
| export default Ramp | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.