|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | + |
| 3 | +#include <QVector2D> |
| 4 | + |
| 5 | +#include "effecttransform.h" |
| 6 | + |
| 7 | +using namespace scratchcpprender; |
| 8 | + |
| 9 | +QRgb EffectTransform::transformColor(const std::unordered_map<ShaderManager::Effect, double> &effectValues, QRgb color) |
| 10 | +{ |
| 11 | + // https://github.com/scratchfoundation/scratch-render/blob/e075e5f5ebc95dec4a2718551624ad587c56f0a6/src/EffectTransform.js#L40-L119 |
| 12 | + // If the color is fully transparent, don't bother attempting any transformations. |
| 13 | + if (qAlpha(color) == 0) |
| 14 | + return color; |
| 15 | + |
| 16 | + QColor inOutColor = QColor::fromRgba(color); |
| 17 | + |
| 18 | + std::unordered_map<ShaderManager::Effect, float> uniforms; |
| 19 | + ShaderManager::getUniformValuesForEffects(effectValues, uniforms); |
| 20 | + |
| 21 | + const bool enableColor = uniforms[ShaderManager::Effect::Color] != 0; |
| 22 | + const bool enableBrightness = uniforms[ShaderManager::Effect::Brightness] != 0; |
| 23 | + |
| 24 | + if (enableColor || enableBrightness) { |
| 25 | + // gl_FragColor.rgb /= gl_FragColor.a + epsilon; |
| 26 | + // Here, we're dividing by the (previously pre-multiplied) alpha to ensure HSV is properly calculated |
| 27 | + // for partially transparent pixels. |
| 28 | + const float alpha = inOutColor.alphaF(); |
| 29 | + |
| 30 | + if (alpha == 0) { |
| 31 | + inOutColor.setRed(255); |
| 32 | + inOutColor.setGreen(255); |
| 33 | + inOutColor.setBlue(255); |
| 34 | + } else { |
| 35 | + inOutColor.setRedF(inOutColor.redF() / alpha); |
| 36 | + inOutColor.setGreenF(inOutColor.greenF() / alpha); |
| 37 | + inOutColor.setBlueF(inOutColor.blueF() / alpha); |
| 38 | + } |
| 39 | + |
| 40 | + if (enableColor) { |
| 41 | + // vec3 hsv = convertRGB2HSV(gl_FragColor.xyz); |
| 42 | + QColor hsv = inOutColor.toHsv(); |
| 43 | + |
| 44 | + // this code forces grayscale values to be slightly saturated |
| 45 | + // so that some slight change of hue will be visible |
| 46 | + // const float minLightness = 0.11 / 2.0; |
| 47 | + const float minV = 0.11f / 2.0f; |
| 48 | + // const float minSaturation = 0.09; |
| 49 | + const float minS = 0.09f; |
| 50 | + // if (hsv.z < minLightness) hsv = vec3(0.0, 1.0, minLightness); |
| 51 | + if (hsv.valueF() < minV) { |
| 52 | + hsv.setHsvF(0.0f, 1.0f, minV); |
| 53 | + // else if (hsv.y < minSaturation) hsv = vec3(0.0, minSaturation, hsv.z); |
| 54 | + } else if (hsv.saturationF() < minS) { |
| 55 | + hsv.setHsvF(0.0f, minS, hsv.valueF()); |
| 56 | + } |
| 57 | + |
| 58 | + // hsv.x = mod(hsv.x + u_color, 1.0); |
| 59 | + // if (hsv.x < 0.0) hsv.x += 1.0; |
| 60 | + float hue = std::fmod(uniforms[ShaderManager::Effect::Color] + hsv.hueF(), 1.0f); |
| 61 | + |
| 62 | + if (hue < 0.0f) |
| 63 | + hue += 1.0f; |
| 64 | + |
| 65 | + hsv.setHsvF(hue, hsv.saturationF(), hsv.valueF()); |
| 66 | + |
| 67 | + // gl_FragColor.rgb = convertHSV2RGB(hsl); |
| 68 | + inOutColor = hsv.toRgb(); |
| 69 | + } |
| 70 | + |
| 71 | + if (enableBrightness) { |
| 72 | + const float brightness = uniforms[ShaderManager::Effect::Brightness] * 255.0f; |
| 73 | + // gl_FragColor.rgb = clamp(gl_FragColor.rgb + vec3(u_brightness), vec3(0), vec3(1)); |
| 74 | + inOutColor.setRed(std::clamp(inOutColor.red() + brightness, 0.0f, 255.0f)); |
| 75 | + inOutColor.setGreen(std::clamp(inOutColor.green() + brightness, 0.0f, 255.0f)); |
| 76 | + inOutColor.setBlue(std::clamp(inOutColor.blue() + brightness, 0.0f, 255.0f)); |
| 77 | + } |
| 78 | + |
| 79 | + // gl_FragColor.rgb *= gl_FragColor.a + epsilon; |
| 80 | + // Now we're doing the reverse, premultiplying by the alpha once again. |
| 81 | + inOutColor.setRedF(inOutColor.redF() * alpha); |
| 82 | + inOutColor.setGreenF(inOutColor.greenF() * alpha); |
| 83 | + inOutColor.setBlueF(inOutColor.blueF() * alpha); |
| 84 | + |
| 85 | + // Restore alpha |
| 86 | + inOutColor.setAlphaF(alpha); |
| 87 | + } |
| 88 | + |
| 89 | + const float ghost = uniforms[ShaderManager::Effect::Ghost]; |
| 90 | + |
| 91 | + if (ghost != 1) { |
| 92 | + // gl_FragColor *= u_ghost |
| 93 | + inOutColor.setRedF(inOutColor.redF() * ghost); |
| 94 | + inOutColor.setGreenF(inOutColor.greenF() * ghost); |
| 95 | + inOutColor.setBlueF(inOutColor.blueF() * ghost); |
| 96 | + inOutColor.setAlphaF(inOutColor.alphaF() * ghost); |
| 97 | + } |
| 98 | + |
| 99 | + return inOutColor.rgba(); |
| 100 | +} |
| 101 | + |
| 102 | +void EffectTransform::transformPoint(const std::unordered_map<ShaderManager::Effect, double> &effectValues, const QVector2D &vec, QVector2D &dst) |
| 103 | +{ |
| 104 | + // TODO: Implement remaining effects |
| 105 | + dst = vec; |
| 106 | +} |
0 commit comments