Author Topic: Modified vertex data being reset  (Read 132 times)

gumboots

  • Jr. Member
  • **
  • Posts: 80
Modified vertex data being reset
« on: October 30, 2018, 10:53:48 PM »
Hi Stephan!

I have a method that does the following, in order:

1. Enable the parent GameObject
2. Change the text
3. Call ForceMeshUpdate
4. Modify all of the characters' vertex colours
5. Call UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32)

The code works properly after the initial time, however the first time this happens, it's like the vertex colour update is ignored, and the mesh shows as completely opaque (where it should be completely transparent). I've looked around to see if I'm doing anything else odd, but can't seem to find anything. So my assumption is something is overriding my UpdateVertexData. Am I misunderstanding the order of events here?

In case it helps, this is the code that performs the above:

Code: [Select]
public void UpdateText(string text) {
    _text.text = text;
    _text.ForceMeshUpdate();

    _textInfo = _text.textInfo;

    int visibleCharacters = _textInfo.characterInfo.Count(characterInfo => characterInfo.isVisible);
    float overlap = (visibleCharacters - 1) * Overlap;

    _normalisedRangePerCharacter = 1f / (visibleCharacters - overlap);
}

public void SetValue(float value) {
    SetValue(0, _maxAlpha, value);
}

public void SetValue(byte minAlpha, byte maxAlpha, float value) {
    float baseSubValueStart = _normalisedRangePerCharacter * (1 - Overlap);
    int currentCharacter = 0;

    for (int i = 0; i < _textInfo.characterCount; i++) {
        if (!_textInfo.characterInfo[i].isVisible) { continue; }

        float subValueStart = baseSubValueStart * currentCharacter;
        float subValue = CalculateNormalizedSubvalue(subValueStart, Mathf.Min(subValueStart + _normalisedRangePerCharacter, 1), value);

        SetCharacterAlpha(i, (byte)Mathf.Lerp(minAlpha, maxAlpha, subValue));

        currentCharacter++;
    }

    _text.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
}

static float CalculateNormalizedSubvalue(float start, float end, float value) {
    float adjustedValue = Mathf.Clamp(value - start, 0, end);
    float range = end - start;
    float subvalue = adjustedValue / range;

    return subvalue;
}

void SetCharacterAlpha(int index, byte alpha) {
    Color32[] vertexColors = _textInfo.meshInfo[_textInfo.characterInfo[index].materialReferenceIndex].colors32;

    for (int j = 0; j < 4; j++) { // Update all four vertices
        vertexColors[_textInfo.characterInfo[index].vertexIndex + j].a = alpha;
    }
}

And this is the code that calls it:

Code: [Select]
gameObject.SetActive(true);

FeedbackText.UpdateText(text);
SetValue(0);

void SetValue(float value) {
    Color color = Background.color;
    color.a = Mathf.Lerp(0, _initialBackgroundAlpha, value);
    Background.color = color;

    FeedbackText.SetValue(Helpers.CalculateNormalizedSubvalue(0.2f, 1, value));

    _currentValue = value;
}

If run just once, the mesh is completely visible, where calling SetValue(0) should make it invisible. The next time I call SetValue(0) it works as expected (and each time following, via a tween). Seems like it might have an issue with calling UpdateText (ForceMeshUpdate) and SetValue (UpdateVertexData) in succession? However this only happens the very first time. Every time after that I run it (including UpdateText and SetValue(0)) is works as expected. Could it have anything to do with being the first time the text mesh is enabled?

I appreciate any help!
« Last Edit: October 30, 2018, 11:08:39 PM by gumboots »

Stephan B.

  • Administrator
  • Hero Member
  • *****
  • Posts: 5631
Re: Modified vertex data being reset
« Reply #1 on: October 31, 2018, 12:30:03 AM »
Can you post an image of the text when it "shows as completely opaque"?

Sounds like a potential SDF scale related issue where either the scale of the text object is not uniform or the scale passed to the shader in the Y of UV2 isn't set correctly initially. Compare the Y values of UV2 when it is incorrect vs. when it is correct.

gumboots

  • Jr. Member
  • **
  • Posts: 80
Re: Modified vertex data being reset
« Reply #2 on: October 31, 2018, 03:25:34 AM »
Sorry, just to make sure we're on the same page, 'completely opaque' simply means the letters have no transparency. Nothing is rendering incorrectly with the mesh, it's just I would expect the letters to be transparent but they're not. A screenshot of that would just be the letters as if they had an alpha of 1, right?

gumboots

  • Jr. Member
  • **
  • Posts: 80
Re: Modified vertex data being reset
« Reply #3 on: October 31, 2018, 11:12:55 PM »
I've figured out it happens every time the text is changed, not just on the first run. As far as I can tell, the TextMesh is still being recalculated when the text is changed, thus clearing my UpdateVertexData. Should ForceMeshUpdate stop it from updating when it would normally?

So it seems like this is resulting in other issues, like calculating RectTransform sizes. Directly after I call UpdateText (my method) it seems the ForceMeshUpdate is ignored.

Also in case it's important, I'm using the latest version from these forums.
« Last Edit: November 01, 2018, 12:53:01 AM by gumboots »

Stephan B.

  • Administrator
  • Hero Member
  • *****
  • Posts: 5631
Re: Modified vertex data being reset
« Reply #4 on: November 01, 2018, 03:37:41 AM »
I've figured out it happens every time the text is changed, not just on the first run. As far as I can tell, the TextMesh is still being recalculated when the text is changed, thus clearing my UpdateVertexData. Should ForceMeshUpdate stop it from updating when it would normally?

So it seems like this is resulting in other issues, like calculating RectTransform sizes. Directly after I call UpdateText (my method) it seems the ForceMeshUpdate is ignored.

Also in case it's important, I'm using the latest version from these forums.

Yes but only if none of the text properties are changed that frame. So if any of the text properties are changed after ForceMeshUpdate() then the object will be processed again.