|
| 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 | +} |
0 commit comments