-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/Add ScanlineEffect & demo to v7 (#602)
* Add working scanline effect and demo * Export ScanlineEffect * Add unit test * Update formatting * Remove stale todo * Formatting * Fix uniforms being updated before they're defined * Update setSize * Improve count uniform update * Add end of file newline * Apply some suggestions from code review Co-authored-by: Raoul v. R. <[email protected]> * Alphabetise exports * Address further code review items of feedback * Restore update count method --------- Co-authored-by: Raoul v. R. <[email protected]>
- Loading branch information
1 parent
f096f15
commit 8c59fee
Showing
6 changed files
with
182 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,134 @@ | ||
import { Uniform } from "three"; | ||
import { Effect } from "./Effect.js"; | ||
import { Resolution } from "../utils/Resolution.js"; | ||
|
||
import fragmentShader from "./shaders/scanline.frag"; | ||
|
||
/** | ||
* ScanlineEffect options. | ||
* | ||
* @category Effects | ||
*/ | ||
|
||
export interface ScanlineEffectOptions { | ||
|
||
/** | ||
* The scanline density. | ||
* | ||
* @defaultValue 1.25 | ||
*/ | ||
|
||
density?: number; | ||
|
||
/** | ||
* The scanline scroll speed. | ||
* | ||
* @defaultValue 0.0 | ||
*/ | ||
|
||
scrollSpeed?: number; | ||
|
||
} | ||
|
||
/** | ||
* A scanline effect. | ||
* | ||
* Based on an implementation by Georg 'Leviathan' Steinrohder (CC BY 3.0): | ||
* http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html | ||
* | ||
* @category Effects | ||
*/ | ||
|
||
export class ScanlineEffect extends Effect { | ||
|
||
/** | ||
* @see {@link density} | ||
*/ | ||
|
||
private _density: number; | ||
|
||
/** | ||
* Constructs a new scanline effect. | ||
* | ||
* @param {Object} [options] - The options. | ||
*/ | ||
|
||
constructor({ | ||
density = 1.25, | ||
scrollSpeed = 0.0 | ||
}: ScanlineEffectOptions = {}) { | ||
|
||
super("ScanlineEffectOptions"); | ||
|
||
this.fragmentShader = fragmentShader; | ||
|
||
const uniforms = this.input.uniforms; | ||
uniforms.set("count", new Uniform(0.0)); | ||
uniforms.set("scrollSpeed", new Uniform(0.0)); | ||
|
||
this._density = density; | ||
this.scrollSpeed = scrollSpeed; | ||
|
||
} | ||
|
||
/** | ||
* The scanline density. | ||
*/ | ||
|
||
get density() { | ||
|
||
return this._density; | ||
|
||
} | ||
|
||
set density(value: number) { | ||
|
||
this._density = value; | ||
this.updateCount(this.resolution.height); | ||
|
||
} | ||
|
||
/** | ||
* The scanline scroll speed. | ||
*/ | ||
|
||
get scrollSpeed() { | ||
|
||
return this.input.uniforms.get("scrollSpeed")!.value as number; | ||
|
||
} | ||
|
||
set scrollSpeed(value: number) { | ||
|
||
this.input.uniforms.get("scrollSpeed")!.value = value; | ||
|
||
if(value === 0) { | ||
|
||
if(this.input.defines.delete("SCROLL")) { | ||
|
||
this.setChanged(); | ||
|
||
} | ||
|
||
} else if(!this.input.defines.has("SCROLL")) { | ||
|
||
this.input.defines.set("SCROLL", "1"); | ||
this.setChanged(); | ||
|
||
} | ||
|
||
} | ||
|
||
protected override onResolutionChange(resolution: Resolution) { | ||
|
||
this.updateCount(resolution.height); | ||
|
||
} | ||
|
||
private updateCount(height: number) { | ||
|
||
this.input.uniforms.get("count")!.value = Math.round(height * this.density); | ||
|
||
} | ||
|
||
} |
This file contains 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
This file contains 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,23 @@ | ||
uniform float count; | ||
|
||
#ifdef SCROLL | ||
|
||
uniform float scrollSpeed; | ||
|
||
#endif | ||
|
||
vec4 mainImage(const in vec4 inputColor, const in vec2 uv, const in GData gData) { | ||
|
||
float y = uv.y; | ||
|
||
#ifdef SCROLL | ||
|
||
y += time * scrollSpeed; | ||
|
||
#endif | ||
|
||
vec2 sl = vec2(sin(y * count), cos(y * count)); | ||
|
||
return vec4(sl.xyx, inputColor.a); | ||
|
||
} |
This file contains 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,11 @@ | ||
import test from "ava"; | ||
import { ScanlineEffect } from "postprocessing"; | ||
|
||
test("can be created and destroyed", t => { | ||
|
||
const object = new ScanlineEffect(); | ||
object.dispose(); | ||
|
||
t.pass(); | ||
|
||
}); |