System Unit Colour Transition System

uberfoop

~=Admiral Stukov=~
Reaction score
177
Requires vJass

Unit Colour Transition System

ucts.jpg

Basic function list:
JASS:

//Main methods:
static method create takes unit u, real CurrentR, real CurrentG, real CurrentB, real CurrentA returns UCT
method SetInstantRGBA takes real r, real g, real b, real a, boolean targettoo returns nothing
method TargetRGBA takes real r, real g, real b, real a returns nothing
method SetRate takes real r returns nothing
//================================
//Directly use only if color-related members are changed without using the above methods:
method UCTAdd takes nothing returns nothing


JASS:

//==============================================================================
//                   Uberfoop - Unit Colour Transition System 
//======================================1.0=====================================
//
//   *Requires:
//   vJass
//
//   *UCTS is a system allowing a simple means of smoothly transitioning a
//   unit's RGB and Alpha values to another set of values linearily. It works by
//   storing a unit in a struct along with target RGB and Alpha values to
//   approach.
//
//   *Implimentation:
//   Create a trigger call UCTS, go to Edit -> 'Convert to custom text', and
//   replace everything in it with this script.
//   The first global block in the library contains two items along with
//   descriptions of how to use them.
//
//   *Use:
//        *A UCT struct is an object with a unit and data for current RGBA and target
//         RGBA data within in. When methods that change color values are called, 
//         and target values don't match up with current values, the struct is made
//         active and the member unit's vertex coloring will change until the RGBA
//         reaches target RGBA.
//        *UCTS uses 0 through 255 for RGBA values.
//        *The system itself only stores the unit to the UCT struct. An attachment
//         system must be used to reference a UCT struct from a unit.
//        *Useful UCT struct methods:
//             *create takes unit u, real CurrentR, real CurrentG, real CurrentB, real CurrentA
//              Sets up the current values and changes the vertex coloring of the unit instantly
//              to the specified values.
//             *SetRate takes real r
//              Takes r values in change/second, scales them, and inputs the data to
//              the struct.
//             *SetInstantRGBA takes real r, real g, real b, real a, boolean targettoo
//              Replaces SetUnitVertexColor if this system is in use, so that the system
//              knows the changes have taken place. If boolean targettoo is true, the new
//              values will be applied to the target values as well, ceasing any current unit
//              color transitioning.
//             *TargetRGBA takes real r, real g, real b, real a
//              Sets only the target RGBA values.
//             ***SetInstantRGBA and TargetRGBA both activate the struct if current values
//                and target values don't match. If color value members of the struct
//                are changed directly without using these methods, method UCTAdd,
//                which takes nothing, must be called, to ensure that color transition takes
//                place.
//
//==============================================================================

library UCTS initializer Init

//Configurable globals
globals
    private constant real UCTS_TIMEOUT = .03125 //Interval between calls of callback function
                                        //Increasing this will slow transitions for a given 'rate',
                                        //and be choppier, but improve performance
                                        //.03125 is 32 callbacks per second

    private constant real UCTS_RATE = 1 //Default maximum quantity of change per call of callback
endglobals                              //Increasing this will increase the default rate of transition
                                        //'rate' member of UCT structs default to this value
                                        //True rate of RGBA change will equal rate/UCTS_TIMEOUT


//stuff
globals
    private timer UCTS_TIMER
    private UCT array UCTS_ACTIVE
    private integer UCTS_QUANTITY = -1
endglobals


//Applies the color transitions themselves
private function UCTSCallback takes nothing returns nothing
    local integer i = 0
    local real m
    local UCT s
    loop
        exitwhen i > UCTS_QUANTITY
        set s = UCTS_ACTIVE<i>
        set m = s.tr - s.r
        if m*m &lt;= s.rate*s.rate then
            set s.r = s.tr
        elseif m &lt; 1 then
            set s.r = s.r - s.rate
        else
            set s.r = s.r + s.rate
        endif
        set m = s.tg - s.g
        if m*m &lt;= s.rate*s.rate then
            set s.g = s.tg
        elseif m &lt; 1 then
            set s.g = s.g - s.rate
        else
            set s.g = s.g + s.rate
        endif
        set m = s.tb - s.b
        if m*m &lt;= s.rate*s.rate then
            set s.b = s.tb
        elseif m &lt; 1 then
            set s.b = s.b - s.rate
        else
            set s.b = s.b + s.rate
        endif
        set m = s.ta - s.a
        if m*m &lt;= s.rate*s.rate then
            set s.a = s.ta
        elseif m &lt; 1 then
            set s.a = s.a - s.rate
        else
            set s.a = s.a + s.rate
        endif
        if s.r == s.tr and s.g == s.tg and s.b == s.tb and s.a == s.ta then
            set UCTS_ACTIVE<i> = UCTS_ACTIVE[UCTS_QUANTITY]
            set UCTS_QUANTITY = UCTS_QUANTITY - 1
            set s.SAFETY = -1
        endif
        call SetUnitVertexColor(s.u,R2I(s.r),R2I(s.g),R2I(s.b),R2I(s.a))
        set i = i + 1
    endloop
    if UCTS_QUANTITY == -1 then
        call PauseTimer(UCTS_TIMER)
    endif
endfunction

//===============//
//Start of struct//
//===============//
struct UCT

    unit u

    //Current RGBA values for a unit
    real r
    real g
    real b
    real a

    //RGBA values to approach
    real tr
    real tg
    real tb
    real ta

    //Safety
    integer SAFETY

    //Rate, change per callback
    real rate

    //Unit u will apply the stated basic RGBA values immediately
    static method create takes unit u, real CurrentR, real CurrentG, real CurrentB, real CurrentA returns UCT
        local UCT s = UCT.allocate()
        set s.u = u
        set s.r = CurrentR
        set s.g = CurrentG
        set s.b = CurrentB
        set s.a = CurrentA
        set s.tr = s.r
        set s.tg = s.g
        set s.tb = s.b
        set s.ta = s.a
        set s.rate = UCTS_RATE
        set s.SAFETY = -1
        call SetUnitVertexColor(u,R2I(CurrentR),R2I(CurrentG),R2I(CurrentB),R2I(CurrentA))
        return s
    endmethod

    //Makes a UCT struct active if applicable. Called automatically by SetInstantRGBA and TargetRGBA.
    //Should only be called directly if someone changes color-related members without using one of
    //the SetInstantRGBA or TargetRGBA methods.
    method UCTAdd takes nothing returns nothing
        if (.r != .tr or .g != .tg or .b != .tb or .a != .ta) and .SAFETY == -1 and .u != null then
            set UCTS_QUANTITY = UCTS_QUANTITY + 1
            set UCTS_ACTIVE[UCTS_QUANTITY] = this
            set .SAFETY = UCTS_QUANTITY
            if UCTS_QUANTITY == 0 then
                call TimerStart(UCTS_TIMER,UCTS_TIMEOUT,true,function UCTSCallback)
            endif
        endif
    endmethod

    //Intended as a replacement for an instant SetUnitVertexColor call if this system is in use, so that the
    //struct will take into account the changes and not immediately change to something else.
    //Boolean targettoo determines whether or not the target values should be changed as well.
    method SetInstantRGBA takes real r, real g, real b, real a, boolean targettoo returns nothing
        set .r = r
        set .g = g
        set .b = b
        set .a = a
        call SetUnitVertexColor(.u,R2I(r),R2I(g),R2I(b),R2I(a))
        if targettoo == true then
            set .tr = r
            set .tg = g
            set .tb = b
            set .ta = a
        else
            call .UCTAdd()
        endif
    endmethod

    //Sets the target values for the unit&#039;s RGBA to approach.
    //If you don&#039;t need to change all the values, just input their struct members into the
    //appropriate slots, ie: call thing.TargetRGBA(225,thing.tg,thing.tb,thing.ta)
    method TargetRGBA takes real r, real g, real b, real a returns nothing
        set .tr = r
        set .tg = g
        set .tb = b
        set .ta = a
        call this.UCTAdd()
    endmethod
    
    //Input a rate in change/second, and this method will scale
    //it and fill in the spot.
    method SetRate takes real r returns nothing
        set .rate = r*UCTS_TIMEOUT
    endmethod
    
    //Detaches the unit from the struct and removes it from active use.
    method onDestroy takes nothing returns nothing
        set .u = null
        if .SAFETY &gt;= 0 then
            set UCTS_ACTIVE[.SAFETY] = UCTS_ACTIVE[UCTS_QUANTITY]
            set UCTS_QUANTITY = UCTS_QUANTITY - 1
        endif
    endmethod
endstruct

//Creates the timer
function Init takes nothing returns nothing
    set UCTS_TIMER = CreateTimer()
endfunction
endlibrary
</i></i>


UCTS is a system that allows relatively easy smooth unit color transitions. UCT structs hold a unit, information for current RGBA (red, green, blue, alpha) values, and information for approaching another set of RGBA values. If the current values and the values to approach are not equal, each value independantly approaches the target value at a specified rate independant to each struct.
UCTS only stores a unit to a struct; it does not store a struct to a unit, so a separate attachment system such as PUI is needed to attach a UCT struct to a unit.

This can be used for anything from unit coloring in spells to terrain camouflage effects.


A few demonstration spells are in the attached UCTS map.
 

Attachments

  • UCTS.w3x
    27.1 KB · Views: 220

BRUTAL

I'm working
Reaction score
118
ou cool ;o
can this constantly change a units colours; like a chameleon?
i made a small thing that does that if your system doesnt do that you should add something like that in

though in the test map i dont get why it turns invisible when they change colours >.>
 

BlackRose

Forum User
Reaction score
239
I like.

But I suggest, in documentation at top, you add in a easy function list like:

call create( u, r, g, b, a)
Easy to CnP.

Useful, but there are others of these types also.
 

uberfoop

~=Admiral Stukov=~
Reaction score
177
ou cool ;o
can this constantly change a units colours; like a chameleon?
i made a small thing that does that if your system doesnt do that you should add something like that in

Well, the system isn't made to choose for itself what colors you want the unit to be, but it's easy to program that sort of thing using the system. The dark grass/dirt blending with the five footmen is a chameleon effect.

edit: When you said constantly, did you just mean smooth transitions? Because that's exactly what the system does.

Though, there's also similar but less practical and more flashy things like this:

JASS:

scope cham initializer Init
globals
    private integer i = 0
    private UCT s
endglobals

private function Callback takes nothing returns nothing
    if i == 0 then
        call s.TargetRGBA(255,0,0,255)
        set i = 1
    elseif i == 1 then
        call s.TargetRGBA(0,255,0,255)
        set i = 2
    else
        call s.TargetRGBA(0,0,255,255)
        set i = 0
    endif
endfunction

private function Init takes nothing returns nothing
    local unit u = CreateUnit(Player(0),&#039;hfoo&#039;,100,100,0)
    set s = UCT.create(u,255,255,255,255)
    call s.SetRate(127)
    call TimerStart(CreateTimer(),2,true,function Callback)
endfunction
endscope


I'll stick this in the test map too to demonstrate how simple it is.

though in the test map i dont get why it turns invisible when they change colours >.>
Because I manipulated the alpha values; I felt like demonstrating all four fields of RGBA.

But I suggest, in documentation at top, you add in a easy function list like
Agreed. I'll get right on that.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top