System FadeUnitTimed

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Out of pure boredom I sat down and started to code this little system, and this is the result.

What does the system do?


FadeUnitTimed allows you to make a unit fade from A transparency to B transparency over C seconds, and then fade in/out from B transparency back to A transparency over D seconds.

Confusing? Let's have us an example then will we?
JASS:

call FadeUnitTimed(GetTriggerUnit(), 100, 100, 100, 0, 100, 2, true, 4)


1) GetTriggerUnit(). This is the unit that will be faded.

2) 100, 100, 100. This is the values for red, green and blue of the unit, entered in percentual values to make things easier for GUI users. Unless you've triggered some tinting on your unit just use 100 in each of these three fields.

3) 0, 100. This is the start and end transparency of the unit, also in percent. In my case the unit starts with 0% transparency and ends with 100% transparency.

4) 2, true, 4. The first number here, 2, is how many seconds it will take for the unit to fade from (in my case) 0 to 100%. The next value, true, is a boolean to tell the system if you want the unit to regain it's starting transparency after the fade. The last number is the time it takes to regain the starting transparency (4 seconds to go from 100% to 0% transparency in my case).


System code:

Note: Coded in vJASS and requires NewGen WE in order to work.
JASS:

//==============================================================================================================\\
//                                                                                                              \\
//                        FadeUnitTimed library v2.0c by Larcenist.                                             \\
//                                                                                                              \\
//==============================================================================================================\\

//==============================================================================================================\\
//                                                                                                              \\
//       Implementation instructions: Copy this trigger, FadeUnitTimed and place it into your map.              \\
//                                                                                                              \\
//==============================================================================================================\\

//==============================================================================================================\\
//                                Available functions:                                                          \\
//                                                                                                              \\
//==============================================================================================================\\
//                                                                                                              \\
//FadeUnitTimed(unit, red, green, blue, start transparency, end transparency, fadetime, turnback, returnrate)   \\
//                                                                                                              \\
//red, green and blue values: Real values that ranges from 0 to 100. This is the percentual tinting of the unit \\
//that is being called, 100 in each field is normal tinting. Only use other values than 100 if your unit has    \\
//triggered tinting since the change will be permanent.                                                         \\
//                                                                                                              \\
//start transparency: real value that ranges from 0 to 100. The percentual initial fading of the unit that is   \\
//being called, 0 means no initial fading.                                                                      \\
//                                                                                                              \\
//end transparency: real value that ranges from 0 to 100. The percentual fading the unit that is being called   \\
//will have when the effect ends, 100 means 100% transperancy.                                                  \\
//                                                                                                              \\
//fadetime: real value that indicates the time for the full effect to finish. Calculated in seconds. if turnback\\
//equal to false this will do nothing.                                                                          \\
//                                                                                                              \\
//turnback: Boolean value, true means that the unit regains normal transperancy when the effect finishes,       \\ 
//false means that the unit retain it's new transparency.                                                       \\
//                                                                                                              \\
//returnrate: Real value that indicates the time for the unit to return to the entered start transparency.      \\
//                                                                                                              \\
//==============================================================================================================\\



library FadeUnitTimed

private struct SData
    unit fadeunit
    real currentfaderate
    real endfaderate
    real fadeamount
    real returnrate
    real startrate
    integer red
    integer green
    integer blue
    boolean turnback
    boolean fadeout
endstruct

globals
    private timer array Timer
    private SData array Data
    
    private integer Counter = 0
    private real TimerPeriod = 0.03125
endglobals

function FadeUnitTimedReturnCallback takes nothing returns nothing
    local SData data
    local integer i = Counter
    local integer Alpha
    local boolean Check
    loop
        exitwhen i <= 0
        if Timer<i> == GetExpiredTimer() then
            set data = Data<i>
            if data.fadeout == true then
                set data.currentfaderate = data.currentfaderate - data.fadeamount
                set Check = data.currentfaderate &lt; data.endfaderate
            else
                set data.currentfaderate = data.currentfaderate + data.fadeamount
                set Check = data.currentfaderate &gt; data.endfaderate
            endif
            if Check then
                call PauseTimer(Timer<i>)
                call data.destroy()
                call DestroyTimer(Timer<i>)
                set Timer<i> = null
                set Timer<i> = Timer[Counter]
                set Data<i> = Data[Counter]
                set Counter = Counter - 1
            else
                set Alpha = R2I((100 - data.currentfaderate) * 255 * 0.01)
                
                call SetUnitVertexColor(data.fadeunit, data.red, data.green, data.blue, Alpha)
            endif
        endif
        set i = i - 1
    endloop
            
endfunction

function FadeUnitTimedCallback takes nothing returns nothing
    local SData data
    local integer i = Counter
    local integer Alpha
    local boolean Check
    loop
        exitwhen i &lt;= 0
        if Timer<i> == GetExpiredTimer() then
            set data = Data<i>
            if data.fadeout == true then
                set data.currentfaderate = data.currentfaderate + data.fadeamount
                set Check = data.currentfaderate &gt; data.endfaderate
            else
                set data.currentfaderate = data.currentfaderate - data.fadeamount
                set Check = data.currentfaderate &lt; data.endfaderate
            endif
            if Check then
                call PauseTimer(Timer<i>)
                if data.turnback == true then
                    set data.endfaderate = data.startrate
                    if data.fadeout == true then
                        set data.fadeamount = ((data.currentfaderate - data.endfaderate) / data.returnrate) * TimerPeriod
                    else
                        set data.fadeamount = ((data.endfaderate - data.currentfaderate) / data.returnrate) * TimerPeriod
                    endif
                    call TimerStart(Timer<i>, TimerPeriod, true, function FadeUnitTimedReturnCallback)
                else
                    call DestroyTimer(Timer<i>)
                    call data.destroy()
                    set Timer<i> = null
                    set Timer<i> = Timer[Counter]
                    set Data<i> = Data[Counter]
                    set Counter = Counter - 1
                endif
            else
                set Alpha = R2I((100 - data.currentfaderate) * 255 * 0.01)
                
                call SetUnitVertexColor(data.fadeunit, data.red, data.green, data.blue, Alpha)
            endif
        endif
        set i = i - 1
    endloop
            
endfunction

function FadeUnitTimed takes unit u, real red, real green, real blue, real startfade, real endfade, real fadetime, boolean turnback, real returnrate returns nothing
    local SData data
    local integer Alpha
    local real FadeTime
    
    set Counter = Counter + 1
    
    if Timer[Counter] == null then
        set data = SData.create()
        set Timer[Counter] = CreateTimer()
        set Data[Counter] = data
        set data.fadeunit = u
        set data.endfaderate = endfade
        set data.startrate = startfade
        if fadetime &lt; TimerPeriod then
            set FadeTime = TimerPeriod
        else
            set FadeTime = fadetime
        endif
        if startfade &lt; endfade then
            set data.fadeamount = ((endfade - startfade) / FadeTime) * TimerPeriod
            set data.currentfaderate = startfade
            set data.startrate = startfade
            set data.fadeout = true
        else
            set data.fadeamount = ((startfade - endfade) / FadeTime) * TimerPeriod
            set data.currentfaderate = startfade
            set data.fadeout = false
        endif
        set data.red = R2I(red * 255 * 0.01)
        set data.green = R2I(green * 255 * 0.01)
        set data.blue = R2I(blue * 255 * 0.01)
        set data.turnback = turnback
        if returnrate &lt; TimerPeriod then
            set data.returnrate = TimerPeriod
        else
            set data.returnrate = returnrate
        endif
        
        set Alpha = R2I((100 - startfade) * 255 * 0.01)
        if Alpha &lt; 0 then
            set Alpha = 0
        elseif Alpha &gt; 255 then
            set Alpha = 255
        endif
    
        call SetUnitVertexColor(u, data.red, data.green, data.blue, Alpha)
        call TimerStart(Timer[Counter], TimerPeriod, true, function FadeUnitTimedCallback)
    else
        call BJDebugMsg(&quot;Timer is not nulled or still in use&quot;)
    endif
    
endfunction

endlibrary</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>


Version notes:
Version 2.0c: Fixed a hardly noticable mini-bugg with units fading out.
Version 2.0b: Fixed some stupid "bug" with invisible units flashing before starting to fade in.
Version 2.0: System now supports fading both in and out.
Version 1.0: Initial release.
 

Attachments

  • FadeUnitTimed v2.0c.w3x
    21.4 KB · Views: 184

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Does that one allow a simple fade-back to the original transparency of the unit that's being faded without any extra functions and stuff? In that case I guess I missed that.

This system was made to be able to work without any other special system-requirements too.
 
Reaction score
456
>Does that one allow a simple fade-back to the original transparency of the unit that's being faded without any extra functions and stuff?
Yes if I understood what you mean. It allows you to do anything to the faded unit.

>This system was made to be able to work without any other special system-requirements too.
Usually people have some kind of an attachement system in their map. So it's not a problem :p..

But let's not talk about that system anymore.

..........

You've inlined the function PercentToInt many times. You should not.
 

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Yeah I kinda noticed that going through the code too. I'm currently working on a new version that'll allow fading in both directions using nothing but the three main-functions, FadeUnitTimed and the two callback functions. I'll clean up those PercentToInt in the next version that hopefully includes this new update.
 

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Updated:

The system now supports both fading units in and out with a turnback. I didn't really change a lot just removed some useless lines, so I'm pretty confident there is a lot of optimization possibilities here.

Please report any bugs you find here, the only one I know of just now is that if you spam this system 5-6 times / second it wont pass the data properly, still working on this issue.
 

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Updated:

Fixed some minor details with the system. Now testable for both fading in/out with and without returning transparency.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +1
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top