Skip to content

Commit 7ddd645

Browse files
committed
Refactor and optimize ASCIIEffect
Closes #683
1 parent cb603f4 commit 7ddd645

6 files changed

Lines changed: 327 additions & 127 deletions

File tree

‎src/effects/ASCIIEffect.js‎

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
import { Color, Uniform, Vector2, Vector4 } from "three";
2+
import { ASCIITexture } from "../textures/ASCIITexture.js";
3+
import { Effect } from "./Effect.js";
4+
5+
import fragmentShader from "./glsl/ascii.frag";
6+
7+
/**
8+
* An ASCII effect.
9+
*
10+
* Warning: This effect cannot be merged with convolution effects.
11+
*/
12+
13+
export class ASCIIEffect extends Effect {
14+
15+
/**
16+
* Constructs a new ASCII effect.
17+
*
18+
* @param {Object} [options] - The options.
19+
* @param {ASCIITexture} [options.asciiTexture] - An ASCII character lookup texture.
20+
* @param {Number} [cellSize=16] - The cell size. It's recommended to use even numbers.
21+
* @param {Number} [color=null] - A color to use instead of the scene colors.
22+
* @param {Boolean} [inverted=false] - Inverts the effect.
23+
*/
24+
25+
constructor({
26+
asciiTexture = new ASCIITexture(),
27+
cellSize = 16,
28+
color = null,
29+
inverted = false
30+
} = {}) {
31+
32+
super("ASCIIEffect", fragmentShader, {
33+
uniforms: new Map([
34+
["asciiTexture", new Uniform(null)],
35+
["cellCount", new Uniform(new Vector4())],
36+
["color", new Uniform(new Color())]
37+
])
38+
});
39+
40+
/**
41+
* @see {@link cellSize}
42+
* @type {Number}
43+
* @private
44+
*/
45+
46+
this._cellSize = -1;
47+
48+
/**
49+
* The current resolution.
50+
*
51+
* @type {Vector2}
52+
* @private
53+
*/
54+
55+
this.resolution = new Vector2();
56+
57+
this.asciiTexture = asciiTexture;
58+
this.cellSize = cellSize;
59+
this.color = color;
60+
this.inverted = inverted;
61+
62+
}
63+
64+
/**
65+
* The current ASCII lookup texture.
66+
*
67+
* @type {ASCIITexture}
68+
*/
69+
70+
get asciiTexture() {
71+
72+
return this.uniforms.get("asciiTexture").value;
73+
74+
}
75+
76+
set asciiTexture(value) {
77+
78+
this.uniforms.get("asciiTexture").value = value;
79+
80+
if(value !== null) {
81+
82+
const cellCount = value.cellCount;
83+
this.defines.set("CHAR_COUNT_MINUS_ONE", (value.characterCount - 1).toFixed(1));
84+
this.defines.set("CELL_COUNT", cellCount.toFixed(1));
85+
this.defines.set("INV_CELL_COUNT", (1.0 / cellCount).toFixed(9));
86+
this.setChanged();
87+
88+
}
89+
90+
}
91+
92+
/**
93+
* A color that overrides the scene colors.
94+
*
95+
* @type {Color | String | Number | null}
96+
*/
97+
98+
get color() {
99+
100+
return this.uniforms.get("color").value;
101+
102+
}
103+
104+
set color(value) {
105+
106+
if(value !== null) {
107+
108+
this.uniforms.get("color").value.set(value);
109+
110+
}
111+
112+
if(this.defines.has("USE_COLOR") && value === null) {
113+
114+
this.defines.delete("USE_COLOR");
115+
this.setChanged();
116+
117+
} else if(!this.defines.has("USE_COLOR") && value !== null) {
118+
119+
this.defines.set("USE_COLOR", "1");
120+
this.setChanged();
121+
122+
}
123+
124+
}
125+
126+
/**
127+
* Controls whether the effect should be inverted.
128+
*
129+
* @type {Boolean}
130+
*/
131+
132+
get inverted() {
133+
134+
return this.defines.has("INVERTED");
135+
136+
}
137+
138+
set inverted(value) {
139+
140+
if(this.inverted !== value) {
141+
142+
if(value) {
143+
144+
this.defines.set("INVERTED", "1");
145+
146+
} else {
147+
148+
this.defines.delete("INVERTED");
149+
150+
}
151+
152+
this.setChanged();
153+
154+
}
155+
156+
}
157+
158+
/**
159+
* The cell size.
160+
*
161+
* @type {Number}
162+
*/
163+
164+
get cellSize() {
165+
166+
return this._cellSize;
167+
168+
}
169+
170+
set cellSize(value) {
171+
172+
if(this._cellSize !== value) {
173+
174+
this._cellSize = value;
175+
this.updateCellCount();
176+
177+
}
178+
179+
}
180+
181+
/**
182+
* Updates the cell count uniform.
183+
*
184+
* @private
185+
*/
186+
187+
updateCellCount() {
188+
189+
const cellCount = this.uniforms.get("cellCount").value;
190+
const resolution = this.resolution;
191+
192+
cellCount.x = resolution.width / this.cellSize;
193+
cellCount.y = resolution.height / this.cellSize;
194+
cellCount.z = 1.0 / cellCount.x;
195+
cellCount.w = 1.0 / cellCount.y;
196+
197+
}
198+
199+
/**
200+
* Updates the size of this pass.
201+
*
202+
* @param {Number} width - The width.
203+
* @param {Number} height - The height.
204+
*/
205+
206+
setSize(width, height) {
207+
208+
this.resolution.set(width, height);
209+
this.updateCellCount();
210+
211+
}
212+
213+
}

‎src/effects/AsciiEffect.js‎

Lines changed: 0 additions & 84 deletions
This file was deleted.

‎src/effects/glsl/ascii.frag‎

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
1-
uniform sampler2D uCharacters;
2-
uniform float uCharactersCount;
3-
uniform float uCellSize;
4-
uniform bool uInvert;
5-
uniform vec3 uColor;
6-
uniform bool uUseSceneColor;
7-
8-
const vec2 SIZE = vec2(16.);
9-
10-
vec3 greyscale(vec3 color, float strength) {
11-
float g = dot(color, vec3(0.299, 0.587, 0.114));
12-
return mix(color, vec3(g), strength);
13-
}
1+
uniform sampler2D asciiTexture;
2+
uniform vec4 cellCount; // XY = cell count, ZW = inv cell count
3+
4+
#ifdef USE_COLOR
5+
6+
uniform vec3 color;
7+
8+
#endif
149

15-
vec3 greyscale(vec3 color) {
16-
return greyscale(color, 1.0);
17-
}
18-
1910
void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
20-
vec2 cell = resolution / uCellSize;
21-
vec2 grid = 1.0 / cell;
22-
vec2 pixelizedUV = grid * (0.5 + floor(uv / grid));
23-
vec4 pixelized = texture2D(inputBuffer, pixelizedUV);
24-
float greyscaled = greyscale(pixelized.rgb).r;
25-
26-
if (uInvert) {
27-
greyscaled = 1.0 - greyscaled;
28-
}
29-
30-
float characterIndex = floor((uCharactersCount - 1.0) * greyscaled);
31-
vec2 characterPosition = vec2(mod(characterIndex, SIZE.x), floor(characterIndex / SIZE.y));
32-
vec2 offset = vec2(characterPosition.x, -characterPosition.y) / SIZE;
33-
vec2 charUV = mod(uv * (cell / SIZE), 1.0 / SIZE) - vec2(0., 1.0 / SIZE) + offset;
34-
vec4 asciiCharacter = texture2D(uCharacters, charUV);
35-
36-
asciiCharacter.rgb = (uUseSceneColor ? pixelized.rgb : uColor) * asciiCharacter.r;
37-
asciiCharacter.a = pixelized.a;
38-
39-
if(asciiCharacter.r == 0.0 && asciiCharacter.g == 0.0 && asciiCharacter.b == 0.0) {
40-
asciiCharacter = vec4(0.0, 0.0, 0.0, 0.0);
41-
}
42-
43-
outputColor = asciiCharacter;
44-
}
11+
12+
vec2 pixelizedUv = cellCount.zw * (0.5 + floor(uv * cellCount.xy));
13+
vec4 texel = texture2D(inputBuffer, pixelizedUv);
14+
float lum = luminance(texel.rgb);
15+
16+
#ifdef INVERTED
17+
18+
// Only LDR colors can be inverted, so make sure lum doesn't exceed 1.
19+
lum = 1.0 - min(lum, 1.0);
20+
21+
#endif
22+
23+
float characterIndex = floor(CHAR_COUNT_MINUS_ONE * lum);
24+
vec2 characterPosition = vec2(mod(characterIndex, CELL_COUNT), floor(characterIndex * INV_CELL_COUNT));
25+
vec2 offset = vec2(characterPosition.x, -characterPosition.y) * INV_CELL_COUNT;
26+
vec2 characterUv = mod(uv * (cellCount.xy * INV_CELL_COUNT), INV_CELL_COUNT) - vec2(0.0, INV_CELL_COUNT) + offset;
27+
vec4 asciiCharacter = texture2D(asciiTexture, characterUv);
28+
29+
#ifdef USE_COLOR
30+
31+
outputColor = vec4(color * asciiCharacter.r, inputColor.a);
32+
33+
#else
34+
35+
outputColor = vec4(texel.rgb * asciiCharacter.r, inputColor.a);
36+
37+
#endif
38+
39+
}

‎src/effects/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from "./blending/index.js";
22

3-
export * from "./AsciiEffect.js";
3+
export * from "./ASCIIEffect.js";
44
export * from "./BloomEffect.js";
55
export * from "./BokehEffect.js";
66
export * from "./BrightnessContrastEffect.js";

0 commit comments

Comments
 (0)