Help Making COLR particle

  • textmonster404
    22nd Mar 2017 Member 0 Permalink

    I want a particle that has properties that make it a random color on spawn but stays that color until it is desroyed? It is a powder and every particle has a different set color. My code doesn't work.

     

    local COLR = elements.allocate('TMPACK','COLR')
    elements.property(COLR, 'Gravity', 1)
    elements.property(COLR, 'Name', 'COLR')
    elements.property(COLR, 'Description', 'RANDOM COLOR')
    elements.property(COLR, 'Weight', 4)
    elements.property(COLR, 'Falldown', 1)
    elements.property(COLR, 'Color', rgb(math.random(1,255),math.random(1,255),math.random(1,255)))
    elements.property(COLR, 'AirDrag', 0.5)
    elements.property(COLR, 'Advection', 0.30)
    elements.property(COLR, 'AirLoss', 0.70)
    elements.property(COLR, 'Loss', 0.75)
    elements.property(COLR, 'HeatConduct', 1)
    elements.property(COLR, 'MenuSection', SC_SPECIAL)
    elements.property(COLR, 'Properties', TYPE_POWDER)
    elements.property(COLR, 'Flammable', '0')
    elements.property(COLR, 'Explosive', '0')
    elements.property(COLR, 'Hardness', '0')

    Edited once by textmonster404. Last: 22nd Mar 2017
  • LBPHacker
    22nd Mar 2017 Developer 0 Permalink

    This is Lua, not CSS. Unless you define the "rgb" function, your code won't work. Even if you set the Color property correctly (see note), it'd be set only once when this piece of code runs.

     

    You'll want to use a Graphics function instead, a function that's called by every particle in every frame (or only once, depending on what it returns) and it determines how the particle is rendered.

     

    local function funcGraphics(partID, colr, colg, colb)
        return
            1,                   -- cache: true
            ren.PMODE_FLAT,      -- flat rendering, no effects
            255,                 -- constant 255 alpha
            math.random(0, 255), -- random red
            math.random(0, 255), -- random green
            math.random(0, 255), -- random blue
            0,                   -- fire (effect) alpha, 0 for now
            0,                   -- fire (effect) red, 0 for now
            0,                   -- fire (effect) green, 0 for now
            0                    -- fire (effect) blue, 0 for now
    end

    elem.property(COLR, "Graphics", funcGraphics)

     

    Well, that should work, but when I try it, the particles change color every frame, so I guess the cache value is somehow discarded and the function is called again and again and again, generating random colors. This means that you'd have to resort to storing a color value in one of the fields of the particle with sim.partProperty and returning that from the graphics function, but that's ugly. Having no other ideas, I suggest you wait for a dev to show up and help.

     

    Note: The Color property is a packed RGB value, that is, 8 bits of red, 8 bits of green and 8 bits of blue, in this order from high to low bits. In other words, it's 0xRRGGBB. Since you're aiming for completely random colors, even math.random(0x000000, 0xFFFFFF) would work, but math.random(0x00, 0xFF) * 0x10000 + math.random(0x00, 0xFF) * 0x100 + math.random(0x00, 0xFF) is cleaner.

  • textmonster404
    22nd Mar 2017 Member 0 Permalink

    It seems your 

    math.random(0x00, 0xFF) * 0x10000 + math.random(0x00, 0xFF) * 0x100 + math.random(0x00, 0xFF)

    approach is neat, but doen't meet my expectations.  It is neat that every time i load up the script it is a different color though!

  • LBPHacker
    22nd Mar 2017 Developer 0 Permalink

    It seems you haven't read the whole post. What I said is that *that* would be the correct way of generating a random packed RGB value, not that it's the solution to your problem.

    Edited once by LBPHacker. Last: 22nd Mar 2017
  • textmonster404
    6th Apr 2017 Member 0 Permalink

    local COLR = elements.allocate('TMPACK','COLR')
    elements.property(COLR, 'Gravity', 1)
    elements.property(COLR, 'Name', 'COLR')
    elements.property(COLR, 'Description', 'Color powder. Each pixel is a random color.')
    elements.property(COLR, 'Weight', 4)
    elements.property(COLR, 'Color', 0xFFFFFF)
    elements.property(COLR, 'Falldown', 1)
    elements.property(COLR, 'Advection', 0.30)
    elements.property(COLR, 'AirLoss', 0.70)
    elements.property(COLR, 'Loss', 0.75)
    elements.property(COLR, 'HeatConduct', 1)
    elements.property(COLR, 'MenuSection', elem.SC_POWDERS)
    elements.property(COLR, 'Properties', elem.TYPE_LIQUID)
    elements.property(COLR, 'Flammable', '0')
    elements.property(COLR, 'MenuVisible', 1)
    elements.property(COLR, 'Explosive', '0')
    elements.property(COLR, 'Hardness', '0')
    function colorx(i,x,y,s,n)
    itmp = sim.partProperty(i, "tmp")
    itmp2 = sim.partProperty(i, "tmp2")
    ilife = sim.partProperty(i, "life")
    if itmp == 0 then
    sim.partProperty(i, "tmp", math.random(1,255))
    end
    if itmp2 == 0 then
    sim.partProperty(i, "tmp2", math.random(1,255))
    end
    if ilife == 0 then
    sim.partProperty(i, "life", math.random(1,255))
    end
    end
    function colc(i,r,g,b)
    itmp = sim.partProperty(i, "tmp")
    itmp2 = sim.partProperty(i, "tmp2")
    ilife = sim.partProperty(i, "life")
    return 0, 0x00000001, 255, ilife, itmp, itmp2, 0, 0, 0, 0
    end
    elements.property(COLR, 'Graphics', colc)
    elements.property(COLR, 'Update', colorx)

     

     

    IT WORKS! 'life' is red, 'tmp' is green and 'tmp2' is blue