fix propsBuffer & introduce lastProps

This commit is contained in:
jrkb 2023-04-22 11:59:21 +02:00
parent 1e4588357d
commit 9a7b3edc9e
6 changed files with 71 additions and 55 deletions

View file

@ -19,12 +19,6 @@ void GPUFontLayer::setup(const LayerSettings & settings){
} }
void GPUFontLayer::update(){ void GPUFontLayer::update(){
if(propsBuffer.size() > 0){
color = glm::vec4(propsBuffer[0].color[0],
propsBuffer[0].color[1],
propsBuffer[0].color[2],
propsBuffer[0].color[3]);
}
} }
void GPUFontLayer::draw(glm::vec3 position){ void GPUFontLayer::draw(glm::vec3 position){
@ -68,8 +62,8 @@ void GPUFontLayer::setProps(const Props & props){
OFX_PROFILER_FUNCTION(); OFX_PROFILER_FUNCTION();
bool propsBufferEmpty = propsBuffer.size() == 0; bool propsBufferEmpty = propsBuffer.size() == 0;
bool setAppearanceAlready = false; bool setAppearanceAlready = false;
if(propsBufferEmpty || props.text != propsBuffer[0].text if(propsBufferEmpty || lastProps.text != props.text
|| propsBuffer[0].fontVariations != props.fontVariations){ || lastProps.fontVariations != props.fontVariations){
setAppearanceAlready = true; setAppearanceAlready = true;
OFX_PROFILER_SCOPE("variations"); OFX_PROFILER_SCOPE("variations");
setDirtyDirty(true); setDirtyDirty(true);
@ -85,10 +79,7 @@ void GPUFontLayer::setProps(const Props & props){
ofxGPUFont::GlyphAppearance glyphAppearance; ofxGPUFont::GlyphAppearance glyphAppearance;
glyphIdentity.charcode = ofxGPUFont::decodeCharcode(&c); glyphIdentity.charcode = ofxGPUFont::decodeCharcode(&c);
glyphAppearance.charcode = glyphIdentity.charcode; glyphAppearance.charcode = glyphIdentity.charcode;
glyphAppearance.color.r = vProps.color[0]; glyphAppearance.color = vProps.color;
glyphAppearance.color.g = vProps.color[1];
glyphAppearance.color.b = vProps.color[2];
glyphAppearance.color.a = vProps.color[3];
glyphAppearance.fontSize_px = vProps.fontSize_px; glyphAppearance.fontSize_px = vProps.fontSize_px;
for(const auto & fv : vProps.fontVariations){ for(const auto & fv : vProps.fontVariations){
glyphIdentity.coords.push_back( glyphIdentity.coords.push_back(
@ -103,34 +94,35 @@ void GPUFontLayer::setProps(const Props & props){
} }
} }
if(!setAppearanceAlready if(!setAppearanceAlready
&& !propsBufferEmpty && (lastProps.fontSize_px != props.fontSize_px
&& (propsBuffer[0].fontSize_px != props.fontSize_px || lastProps.color != props.color)){
|| propsBuffer[0].color != props.color)){
for(int i = 0; i < props.text.size(); i++){ for(int i = 0; i < props.text.size(); i++){
const Props & vProps = getProps(i * props.letterDelay); const Props & vProps = propsBufferEmpty ? props : getProps(i * props.letterDelay);
ofxGPUFont::GlyphAppearance & glyphAppearance = variationTextAppearance[i]; ofxGPUFont::GlyphAppearance & glyphAppearance = variationTextAppearance[i];
glyphAppearance.fontSize_px = vProps.fontSize_px; glyphAppearance.fontSize_px = vProps.fontSize_px;
variationTextAppearance[i].color.r = vProps.color[0]; variationTextAppearance[i].color = vProps.color;
variationTextAppearance[i].color.g = vProps.color[1];
variationTextAppearance[i].color.b = vProps.color[2];
variationTextAppearance[i].color.a = vProps.color[3];
} }
} }
if(!propsBufferEmpty && props.fontPath != propsBuffer[0].fontPath){ if(lastProps.fontPath != props.fontPath){
notHappyWithMom = true; notHappyWithMom = true;
} }
while(propsBuffer.size() > max(0, int(props.letterDelay * 30 * props.text.size()))){ int maxPropsBufferSize = max(0, int(props.letterDelay * settings.letterDelayRatio * props.text.size()));
if(maxPropsBufferSize > 0){
propsBuffer.push_front(props);
}
while(propsBuffer.size() > maxPropsBufferSize){
propsBuffer.pop_back(); propsBuffer.pop_back();
} }
propsBuffer.push_front(props); lastProps = props;
} }
const Layer::Props & GPUFontLayer::getProps(float delay) const { const Layer::Props & GPUFontLayer::getProps(float delay_seconds) const {
int index = max(0, min(int(propsBuffer.size() - 1), int(delay * 30))); if(delay_seconds != 0 && propsBuffer.size() > 0){
return propsBuffer[index]; // gets newest int index = max(0, min(int(propsBuffer.size() - 1), int(delay_seconds * settings.letterDelayRatio)));
} return propsBuffer[index]; // gets newest
const glm::vec4 & GPUFontLayer::getColor() const { }else{
return color; return lastProps;
}
} }
ofNode & GPUFontLayer::getOuterNode(){ ofNode & GPUFontLayer::getOuterNode(){
return outerNode; return outerNode;

View file

@ -24,7 +24,6 @@ class GPUFontLayer : public Layer {
ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation()) override; ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation()) override;
void setProps(const Props & props) override; void setProps(const Props & props) override;
const Props & getProps(float delay = 0) const override; const Props & getProps(float delay = 0) const override;
const glm::vec4 & getColor() const;
void clearPropsBuffer() override; void clearPropsBuffer() override;
void setId(const LayerID & id) override; void setId(const LayerID & id) override;
const LayerID & getId() override; const LayerID & getId() override;
@ -61,10 +60,10 @@ class GPUFontLayer : public Layer {
private: private:
std::deque <Props> propsBuffer; std::deque <Props> propsBuffer;
Props lastProps;
ComboIdentifier momsComboIdentifier; ComboIdentifier momsComboIdentifier;
bool notHappyWithMom = false; bool notHappyWithMom = false;
LayerType type = GPUFONT; LayerType type = GPUFONT;
glm::vec4 color;
ofNode outerNode; ofNode outerNode;
ofNode innerNode; ofNode innerNode;
int letterDelayBufferSize = 0; int letterDelayBufferSize = 0;

View file

@ -8,12 +8,13 @@
#include "ofxMsdfgen.h" #include "ofxMsdfgen.h"
#include "Utils.h" #include "Utils.h"
#include <memory> #include <memory>
#include <sstream>
namespace ofxVariableLab { namespace ofxVariableLab {
struct LayerSettings { struct LayerSettings {
uint32_t maxBufferSize = 100;
VFlipBehaviour vFlipBehaviour = V_FLIP_ONCE_AUTO; VFlipBehaviour vFlipBehaviour = V_FLIP_ONCE_AUTO;
float letterDelayRatio = 30.0f; //
}; };
class Layer { class Layer {
@ -30,7 +31,7 @@ class Layer {
float y = 200; float y = 200;
float rotation = 0; float rotation = 0;
float fontSize_px = 42; float fontSize_px = 42;
std::array <float, 4> color; std::array <float, 4> color = {0, 0, 0, 1};
bool mirror_x = false; bool mirror_x = false;
float mirror_x_distance = 0; float mirror_x_distance = 0;
bool mirror_y = false; bool mirror_y = false;
@ -52,11 +53,40 @@ class Layer {
//mirror_y, //mirror_y,
//mirror_y_distance, //mirror_y_distance,
//letterDelay, //letterDelay,
//transformOrigin,
//fontVariations, //fontVariations,
//text, //text,
//fontPath) //fontPath)
}; };
static std::string asString(const ofxVariableLab::Layer::Props & props){
stringstream s;
s << "asString::props:" << endl
<< "\tx:" << ofToString(props.x) << endl
<< "\ty:" << ofToString(props.y) << endl
<< "\trotation:" << ofToString(props.rotation) << endl
<< "\tfontSize_px:" << ofToString(props.fontSize_px) << endl
<< "\tcolor: ["
<< ofToString(props.color[0]) << ","
<< ofToString(props.color[1]) << ","
<< ofToString(props.color[2]) << ","
<< ofToString(props.color[3]) << "]"
<< endl
<< "\tmirror_x:" << ofToString(props.mirror_x) << endl
<< "\tmirror_x_distance:" << ofToString(props.mirror_x_distance) << endl
<< "\tmirror_y:" << ofToString(props.mirror_y) << endl
<< "\tmirror_y_distance:" << ofToString(props.mirror_y_distance) << endl
<< "\tletterDelay:" << ofToString(props.letterDelay) << endl
<< "\ttransformOrigin:" << ofToString(props.transformOrigin) << endl;
for(const auto & fv : props.fontVariations){
s << "\tfontVariations (" << fv.name << "):" << ofToString(fv.value) << endl;
}
s << "\ttext: " << props.text << endl
<< "\tfontPath: " << props.fontPath << endl
;
return s.str();
}
virtual void setup(const LayerSettings & settings = LayerSettings()) = 0; virtual void setup(const LayerSettings & settings = LayerSettings()) = 0;
virtual void update() = 0; virtual void update() = 0;
virtual void draw(glm::vec3 position = glm::vec3(0, 0, 0)) = 0; virtual void draw(glm::vec3 position = glm::vec3(0, 0, 0)) = 0;
@ -68,7 +98,6 @@ class Layer {
ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation()) = 0; ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation()) = 0;
virtual void setProps(const Props & props) = 0; virtual void setProps(const Props & props) = 0;
virtual const Props & getProps(float delay = 0) const = 0; virtual const Props & getProps(float delay = 0) const = 0;
virtual const glm::vec4 & getColor() const = 0;
virtual void clearPropsBuffer() = 0; virtual void clearPropsBuffer() = 0;
virtual void setId(const LayerID & id) = 0; virtual void setId(const LayerID & id) = 0;
virtual const LayerID & getId() = 0; virtual const LayerID & getId() = 0;

View file

@ -9,10 +9,6 @@
namespace ofxVariableLab { namespace ofxVariableLab {
void LayerComposition::setup(){ void LayerComposition::setup(){
//auto combo = make_shared <GPUFontAtlasLayerCombo>();
//ComboIdentifier comboIdentifier = {"lol", Layer::GPUFONT};
//combo->setup(comboIdentifier);
//atlasLayerCombos[comboIdentifier] = combo;
} }
void LayerComposition::update(){ void LayerComposition::update(){
@ -28,7 +24,7 @@ void LayerComposition::update(){
momIdentifier.type momIdentifier.type
}; };
cout << "ideal mom looks like this: " << l.second->getProps().fontPath cout << "ideal mom looks like this: " << l.second->getProps().fontPath
<< (momIdentifier.type == LayerType::GPUFONT ? "gpufont" : "msdfgen") << (momIdentifier.type == LayerType::GPUFONT ? " gpufont" : " msdfgen")
<< endl; << endl;
findOrCreateNewMomForLayer(l.second, idealMom); findOrCreateNewMomForLayer(l.second, idealMom);
} }

View file

@ -17,12 +17,6 @@ void MsdfLayer::setup(const LayerSettings & settings){
} }
void MsdfLayer::update(){ void MsdfLayer::update(){
if(propsBuffer.size() > 0){
color = glm::vec4(propsBuffer[0].color[0],
propsBuffer[0].color[1],
propsBuffer[0].color[2],
propsBuffer[0].color[3]);
}
} }
void MsdfLayer::draw(glm::vec3 position){ void MsdfLayer::draw(glm::vec3 position){
@ -319,16 +313,23 @@ void MsdfLayer::setProps(const Props & props){
if(propsBuffer.size() == 0 || props.text != propsBuffer[0].text){ if(propsBuffer.size() == 0 || props.text != propsBuffer[0].text){
setDirtyDirty(true); setDirtyDirty(true);
} }
while(propsBuffer.size() > max(0, int(settings.maxBufferSize - 1))){ int maxPropsBufferSize = max(0, int(props.letterDelay * settings.letterDelayRatio * props.text.size()));
cout << "maxPropsBufferSize: " << ofToString(maxPropsBufferSize) << endl;
if(maxPropsBufferSize > 0){
propsBuffer.push_front(props);
}
while(propsBuffer.size() > maxPropsBufferSize){
propsBuffer.pop_back(); propsBuffer.pop_back();
} }
propsBuffer.push_front(props); lastProps = props;
} }
const Layer::Props & MsdfLayer::getProps(float delay) const { const Layer::Props & MsdfLayer::getProps(float delay_seconds) const {
return propsBuffer[0]; if(delay_seconds != 0 && propsBuffer.size() > 0){
} int index = max(0, min(int(propsBuffer.size() - 1), int(delay_seconds * settings.letterDelayRatio)));
const glm::vec4 & MsdfLayer::getColor() const { return propsBuffer[index]; // gets newest
return color; }else{
return lastProps;
}
} }
void MsdfLayer::clearPropsBuffer(){ void MsdfLayer::clearPropsBuffer(){
propsBuffer.clear(); propsBuffer.clear();

View file

@ -22,7 +22,6 @@ class MsdfLayer : public Layer {
ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation()) override; ofxVariableLab::FontVariation fontVariation = ofxVariableLab::FontVariation()) override;
void setProps(const Props & props) override; void setProps(const Props & props) override;
const Props & getProps(float delay = 0) const override; const Props & getProps(float delay = 0) const override;
const glm::vec4 & getColor() const;
void clearPropsBuffer() override; void clearPropsBuffer() override;
void setId(const LayerID & id) override; void setId(const LayerID & id) override;
const LayerID & getId() override; const LayerID & getId() override;
@ -45,7 +44,6 @@ class MsdfLayer : public Layer {
shared_ptr <ofxMsdfgen::Atlas> atlas; shared_ptr <ofxMsdfgen::Atlas> atlas;
LayerSettings settings; LayerSettings settings;
std::deque <Props> propsBuffer;
shared_ptr <ofShader> shader; shared_ptr <ofShader> shader;
/// \brief are props updated but not drawn yet /// \brief are props updated but not drawn yet
bool isDirty = true; bool isDirty = true;
@ -54,10 +52,11 @@ class MsdfLayer : public Layer {
VFlipState vFlip = V_FLIP_UNKNOWN; VFlipState vFlip = V_FLIP_UNKNOWN;
private: private:
Props lastProps;
std::deque <Props> propsBuffer;
bool notHappyWithMom = false; bool notHappyWithMom = false;
ComboIdentifier momsComboIdentifier; ComboIdentifier momsComboIdentifier;
LayerType type = MSDFGEN; LayerType type = MSDFGEN;
glm::vec4 color;
ofNode outerNode; ofNode outerNode;
ofNode innerNode; ofNode innerNode;
}; };