-
-
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.
- Loading branch information
Showing
7 changed files
with
208 additions
and
18 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,141 @@ | ||
import { Texture, Uniform, UnsignedByteType } from "three"; | ||
import { ColorChannel } from "../enums/ColorChannel.js"; | ||
import { Effect } from "./Effect.js"; | ||
|
||
import fragmentShader from "./shaders/texture.frag"; | ||
import vertexShader from "./shaders/texture.vert"; | ||
|
||
/** | ||
* TextureEffect options. | ||
* | ||
* @category Effects | ||
*/ | ||
|
||
export interface TextureEffectOptions { | ||
|
||
/** | ||
* A texture. | ||
*/ | ||
|
||
texture?: Texture | null; | ||
|
||
} | ||
|
||
/** | ||
* A texture effect. | ||
* | ||
* @category Effects | ||
*/ | ||
|
||
export class TextureEffect extends Effect { | ||
|
||
/** | ||
* Constructs a new texture effect. | ||
* | ||
* @param options - The options. | ||
*/ | ||
|
||
constructor({ texture = null }: TextureEffectOptions = {}) { | ||
|
||
super("TextureEffect"); | ||
|
||
this.fragmentShader = fragmentShader; | ||
|
||
const defines = this.input.defines; | ||
defines.set("TEXEL", "texel"); | ||
|
||
const uniforms = this.input.uniforms; | ||
uniforms.set("map", new Uniform(null)); | ||
uniforms.set("uvTransform", new Uniform(null)); | ||
|
||
this.texture = texture; | ||
|
||
} | ||
|
||
/** | ||
* The current texture. | ||
*/ | ||
|
||
get texture(): Texture | null { | ||
|
||
return this.input.uniforms.get("map")!.value as Texture; | ||
|
||
} | ||
|
||
set texture(value: Texture | null) { | ||
|
||
const prevTexture = this.texture; | ||
const uniforms = this.input.uniforms; | ||
const defines = this.input.defines; | ||
|
||
if(prevTexture !== value) { | ||
|
||
uniforms.get("map")!.value = value; | ||
defines.delete("TEXTURE_PRECISION_HIGH"); | ||
|
||
if(value !== null) { | ||
|
||
if(value.matrixAutoUpdate) { | ||
|
||
defines.set("UV_TRANSFORM", true); | ||
uniforms.get("uvTransform")!.value = value.matrix; | ||
this.vertexShader = vertexShader; | ||
|
||
} else { | ||
|
||
defines.delete("UV_TRANSFORM"); | ||
uniforms.get("uvTransform")!.value = null; | ||
this.vertexShader = null; | ||
|
||
} | ||
|
||
if(value.type !== UnsignedByteType) { | ||
|
||
defines.set("TEXTURE_PRECISION_HIGH", true); | ||
|
||
} | ||
|
||
} | ||
|
||
this.setChanged(); | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* Sets the swizzles that will be applied to the components of a texel before it is written to the output color. | ||
* | ||
* @param r - The swizzle for the `r` component. | ||
* @param g - The swizzle for the `g` component. Defaults to the same value used for `r`. | ||
* @param b - The swizzle for the `b` component. Defaults to the same value used for `r`. | ||
* @param a - The swizzle for the `a` component. Defaults to the same value used for `r`. | ||
*/ | ||
|
||
setTextureSwizzleRGBA(r: ColorChannel, g = r, b = r, a = r) { | ||
|
||
const rgba = "rgba"; | ||
let swizzle = ""; | ||
|
||
if(r !== ColorChannel.RED || g !== ColorChannel.GREEN || b !== ColorChannel.BLUE || a !== ColorChannel.ALPHA) { | ||
|
||
swizzle = [".", rgba[r], rgba[g], rgba[b], rgba[a]].join(""); | ||
|
||
} | ||
|
||
this.input.defines.set("TEXEL", "texel" + swizzle); | ||
this.setChanged(); | ||
|
||
} | ||
|
||
override render(): void { | ||
|
||
if(this.texture !== null && this.texture.matrixAutoUpdate) { | ||
|
||
this.texture.updateMatrix(); | ||
|
||
} | ||
|
||
} | ||
|
||
} |
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,31 @@ | ||
#ifdef TEXTURE_PRECISION_HIGH | ||
|
||
uniform mediump sampler2D map; | ||
|
||
#else | ||
|
||
uniform lowp sampler2D map; | ||
|
||
#endif | ||
|
||
#ifdef UV_TRANSFORM | ||
|
||
in vec2 vUv2; | ||
|
||
#endif | ||
|
||
vec4 mainImage(const in vec4 inputColor, const in vec2 uv, const in GData gData) { | ||
|
||
#ifdef UV_TRANSFORM | ||
|
||
vec4 texel = texture(map, vUv2); | ||
|
||
#else | ||
|
||
vec4 texel = texture(map, uv); | ||
|
||
#endif | ||
|
||
return TEXEL; | ||
|
||
} |
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,9 @@ | ||
uniform mat3 uvTransform; | ||
|
||
out vec2 vUv2; | ||
|
||
void mainSupport(const in vec2 uv) { | ||
|
||
vUv2 = (uvTransform * vec3(uv, 1.0)).xy; | ||
|
||
} |
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 { TextureEffect } from "postprocessing"; | ||
|
||
test("can be created and destroyed", t => { | ||
|
||
const object = new TextureEffect(); | ||
object.dispose(); | ||
|
||
t.pass(); | ||
|
||
}); |