Adding glyphHeightToWidth, glyphEdgeCrop, fade and cycleStyle parameters.

This commit is contained in:
Rezmason
2019-02-18 17:26:02 -08:00
parent d03cd685e1
commit 5528592754
2 changed files with 77 additions and 13 deletions

View File

@@ -26,7 +26,7 @@
const versions = { const versions = {
paradise: { paradise: {
fontTexture: './coptic_msdf.png', fontTexture: "./coptic_msdf.png",
raindropLength: 0.5, raindropLength: 0.5,
glyphSequenceLength: 32, glyphSequenceLength: 32,
bloom: { bloom: {
@@ -46,11 +46,15 @@
hasThunder: false, hasThunder: false,
hasSun: true, hasSun: true,
slant: 0, slant: 0,
glyphEdgeCrop: 0.0,
glyphHeightToWidth:1,
isPolar: true, isPolar: true,
numColumns: 50 numColumns: 50,
fade: true,
cycleStyle: "cycleFasterWhenDimmed"
}, },
nightmare: { nightmare: {
fontTexture: './gothic_msdf.png', fontTexture: "./gothic_msdf.png",
raindropLength: 0.6, raindropLength: 0.6,
glyphSequenceLength: 27, glyphSequenceLength: 27,
bloom: { bloom: {
@@ -70,11 +74,15 @@
hasThunder: true, hasThunder: true,
hasSun: false, hasSun: false,
slant: 360 / 16, slant: 360 / 16,
glyphEdgeCrop: 0.0,
glyphHeightToWidth:1,
isPolar: false, isPolar: false,
numColumns: 60 numColumns: 60,
fade: true,
cycleStyle: "cycleFasterWhenDimmed"
}, },
["1999"]: { ["1999"]: {
fontTexture: './matrixcode_msdf.png', fontTexture: "./matrixcode_msdf.png",
raindropLength: 1, raindropLength: 1,
glyphSequenceLength: 57, glyphSequenceLength: 57,
bloom: { bloom: {
@@ -111,8 +119,12 @@
hasThunder: false, hasThunder: false,
hasSun: false, hasSun: false,
slant: 0, slant: 0,
glyphEdgeCrop: 0.0,
glyphHeightToWidth:1,
isPolar: false, isPolar: false,
numColumns: 80 numColumns: 80,
fade: true,
cycleStyle: "cycleFasterWhenDimmed"
} }
}; };
@@ -139,6 +151,9 @@
const numFontColumns = 8; const numFontColumns = 8;
const glyphSequenceLength = version.glyphSequenceLength; const glyphSequenceLength = version.glyphSequenceLength;
const slant = parseFloat(getParam(["slant", "angle"], version.slant)) * Math.PI / 180; const slant = parseFloat(getParam(["slant", "angle"], version.slant)) * Math.PI / 180;
const glyphEdgeCrop = parseFloat(getParam("encroach", version.glyphEdgeCrop));
const glyphHeightToWidth = parseFloat(getParam("stretch", version.glyphHeightToWidth));
const fade = getParam("fade", version.fade).toString() == "true";
const effect = getParam("effect", "plain"); const effect = getParam("effect", "plain");
@@ -164,8 +179,12 @@
hasSun: version.hasSun, hasSun: version.hasSun,
isPolar: version.isPolar, isPolar: version.isPolar,
slant, slant,
glyphEdgeCrop,
glyphHeightToWidth,
fade,
showComputationTexture: effect === "none", showComputationTexture: effect === "none",
raindropLength raindropLength,
cycleStyle: version.cycleStyle
}); });
matrixRenderer.pass.renderToScreen = false; matrixRenderer.pass.renderToScreen = false;

View File

@@ -8,8 +8,12 @@ const makeMatrixRenderer = (renderer, texture, {
hasSun, hasSun,
isPolar, isPolar,
slant, slant,
glyphHeightToWidth,
glyphEdgeCrop,
fade,
showComputationTexture, showComputationTexture,
raindropLength raindropLength,
cycleStyle
}) => { }) => {
const matrixRenderer = {}; const matrixRenderer = {};
const camera = new THREE.OrthographicCamera( -0.5, 0.5, 0.5, -0.5, 0.0001, 10000 ); const camera = new THREE.OrthographicCamera( -0.5, 0.5, 0.5, -0.5, 0.0001, 10000 );
@@ -22,7 +26,7 @@ const makeMatrixRenderer = (renderer, texture, {
for (let i = 0; i < numColumns * numColumns; i++) { for (let i = 0; i < numColumns * numColumns; i++) {
pixels[i * 4 + 0] = 0; pixels[i * 4 + 0] = 0;
pixels[i * 4 + 1] = showComputationTexture ? 0 : scramble(i); pixels[i * 4 + 1] = scramble(i);
pixels[i * 4 + 2] = 0; pixels[i * 4 + 2] = 0;
pixels[i * 4 + 3] = 0; pixels[i * 4 + 3] = 0;
} }
@@ -95,7 +99,13 @@ const glyphVariable = gpuCompute.addVariable(
brightness = mix(brightness, newBrightness, brightnessChangeBias); brightness = mix(brightness, newBrightness, brightnessChangeBias);
#endif #endif
float glyphCycleSpeed = (brightness <= 0.0) ? 0.0 : pow(1.0 - brightness, 4.0); float glyphCycleSpeed = 0.0;
#ifdef cycleFasterWhenDimmed
if (brightness > 0.0) glyphCycleSpeed = pow(1.0 - brightness, 4.0);
#endif
#ifdef cycleRandomly
glyphCycleSpeed = 1.0; // TODO: should be similar to brightness
#endif
glyphCycle = fract(glyphCycle + deltaTime * cycleSpeed * 0.2 * glyphCycleSpeed); glyphCycle = fract(glyphCycle + deltaTime * cycleSpeed * 0.2 * glyphCycleSpeed);
float symbol = floor(glyphSequenceLength * glyphCycle); float symbol = floor(glyphSequenceLength * glyphCycle);
float symbolX = mod(symbol, numFontColumns); float symbolX = mod(symbol, numFontColumns);
@@ -120,7 +130,8 @@ const glyphVariable = gpuCompute.addVariable(
); );
gpuCompute.setVariableDependencies( glyphVariable, [ glyphVariable ] ); gpuCompute.setVariableDependencies( glyphVariable, [ glyphVariable ] );
const brightnessChangeBias = (animationSpeed * fallSpeed) == 0 ? 1 : Math.min(1, Math.abs(animationSpeed * fallSpeed)); // const brightnessChangeBias = (animationSpeed * fallSpeed) == 0 ? 1 : Math.min(1, Math.abs(animationSpeed * fallSpeed));
const brightnessChangeBias = 1;
Object.assign(glyphVariable.material.uniforms, { Object.assign(glyphVariable.material.uniforms, {
time: { type: "f", value: 0 }, time: { type: "f", value: 0 },
deltaTime: { type: "f", value: 0.01 }, deltaTime: { type: "f", value: 0.01 },
@@ -142,6 +153,17 @@ const glyphVariable = gpuCompute.addVariable(
glyphVariable.material.defines.showComputationTexture = 1.0; glyphVariable.material.defines.showComputationTexture = 1.0;
} }
switch (cycleStyle) {
case "cycleFasterWhenDimmed":
glyphVariable.material.defines.cycleFasterWhenDimmed = 1.0;
break;
case "cycleRandomly":
default:
glyphVariable.material.defines.cycleRandomly = 1.0;
break;
}
const error = gpuCompute.init(); const error = gpuCompute.init();
if ( error !== null ) { if ( error !== null ) {
console.error( error ); console.error( error );
@@ -159,7 +181,9 @@ const glyphVariable = gpuCompute.addVariable(
sharpness: { type: "f", value: sharpness }, sharpness: { type: "f", value: sharpness },
numFontColumns: {type: "f", value: numFontColumns}, numFontColumns: {type: "f", value: numFontColumns},
resolution: {type: "v2", value: new THREE.Vector2() }, resolution: {type: "v2", value: new THREE.Vector2() },
slant: {type: "v2", value: new THREE.Vector2(Math.cos(slant), Math.sin(slant)) } slant: {type: "v2", value: new THREE.Vector2(Math.cos(slant), Math.sin(slant)) },
glyphHeightToWidth: {type: "f", value: glyphHeightToWidth},
glyphEdgeCrop: {type: "f", value: glyphEdgeCrop},
}, },
vertexShader: ` vertexShader: `
attribute vec2 uv; attribute vec2 uv;
@@ -185,6 +209,8 @@ const glyphVariable = gpuCompute.addVariable(
uniform float numColumns; uniform float numColumns;
uniform float numFontColumns; uniform float numFontColumns;
uniform vec2 slant; uniform vec2 slant;
uniform float glyphHeightToWidth;
uniform float glyphEdgeCrop;
varying vec2 vUV; varying vec2 vUV;
float median(float r, float g, float b) { float median(float r, float g, float b) {
@@ -207,6 +233,8 @@ const glyphVariable = gpuCompute.addVariable(
uv = vec2(angle / PI, 1.0 - pow(radius * 0.75, 0.6)); uv = vec2(angle / PI, 1.0 - pow(radius * 0.75, 0.6));
#endif #endif
uv.y /= glyphHeightToWidth;
vec4 glyph = texture2D(glyphs, uv); vec4 glyph = texture2D(glyphs, uv);
#ifdef showComputationTexture #ifdef showComputationTexture
@@ -216,8 +244,20 @@ const glyphVariable = gpuCompute.addVariable(
// Unpack the values from the glyph texture // Unpack the values from the glyph texture
float brightness = glyph.r; float brightness = glyph.r;
#ifndef fade
if (brightness < -1.0) { discard; return; }
if (brightness > 0.55) {
brightness *= 2.0;
} else {
brightness = 0.25;
}
#endif
vec2 symbolUV = glyph.ba; vec2 symbolUV = glyph.ba;
vec4 sample = texture2D(msdf, fract(uv * numColumns) / numFontColumns + symbolUV); vec2 glyphUV = fract(uv * numColumns);
glyphUV -= 0.5;
glyphUV *= clamp(1.0 - glyphEdgeCrop, 0.0, 1.0);
glyphUV += 0.5;
vec4 sample = texture2D(msdf, glyphUV / numFontColumns + symbolUV);
// The rest is straight up MSDF // The rest is straight up MSDF
float sigDist = median(sample.r, sample.g, sample.b) - 0.5; float sigDist = median(sample.r, sample.g, sample.b) - 0.5;
@@ -243,6 +283,11 @@ const glyphVariable = gpuCompute.addVariable(
mesh.material.defines.isPolar = 1.0; mesh.material.defines.isPolar = 1.0;
} }
console.log(fade);
if (fade) {
mesh.material.defines.fade = 1.0;
}
if (showComputationTexture) { if (showComputationTexture) {
mesh.material.defines.showComputationTexture = 1.0; mesh.material.defines.showComputationTexture = 1.0;
} }