Web
Analytics
Log In  

5

I wanted to achieve this effect with fill patterns for a recent project I started so I figured I'd share it here incase anyone else is interested in using it.

The local fill pattern function is shifting each row/column in the fill pattern by given x and y amount. It's using binary arithmetics and a cache of shift masks.

Here's the original code for version 1.0, takes 126 tokens:

--turns a screen-space fill pattern, into an object-space fill pattern by wrapping the patterns row and column by the given x and y amount.
function get_local_fill_pattern(x,y,fill_pattern)
    local add_bits=band(fill_pattern,0x0000.FFFF)
    fill_pattern=band(fill_pattern,0xFFFF)
    y=flr(y)%4
    if(y~=0)then
        local r_masks={0xFFF0,0xFF00,0xF000}
        local l_masks={0x000F,0x00FF,0x0FFF}
        fill_pattern=bxor(lshr(band(fill_pattern,r_masks[y]),y*4),shl(band(fill_pattern,l_masks[y]),(4-y)*4))
    end

    x=flr(x)%4
    if(x~=0)then
        local r_masks={0xEEEE,0xCCCC,0x8888}
        local l_masks={0x1111,0x3333,0x7777}
        fill_pattern=bxor(lshr(band(fill_pattern,r_masks[x]),x),shl(band(fill_pattern,l_masks[x]),4-x))
    end

    return bxor(fill_pattern,add_bits)
end

EDIT:
With great contributions and improvements by Felice here's a both faster, smaller and more API consistent version 2.0, takes 87 tokens:

_fillp_xmask_lo={[0]=0xffff,0x7777,0x3333,0x1111}
_fillp_xmask_hi={[0]=0x0000,0x8888,0xcccc,0xeeee}
_fillp_original=fillp

function fillp(p,x,y)
    if y then
        x=band(x,3)
        local p16=flr(p)
        local p32=rotr(p16+lshr(p16,16),band(y,3)*4+x)
        p+=flr(band(p32,_fillp_xmask_lo[x])+band(rotl(p32,4),_fillp_xmask_hi[x]))-p16
    end
    return _fillp_original(p)
end

Hope you find it useful!

Cart [#48163#] | Copy | Code | 2018-01-14 | Link
5

local fillp object space offset
P#47695 2017-12-27 11:57 ( Edited 2018-01-14 06:23)

::

Excellent work. I've been wondering about how to implement this idea since fillp was released.

P#47697 2017-12-27 12:42

::

Good idea!

I refined your methods and took a fair chunk of the math and tokens off (now 80 tokens). Here's my version:

_fillp_xmask_lo={[0]=0xffff,0x7777,0x3333,0x1111}
_fillp_xmask_hi={[0]=0x0000,0x8888,0xcccc,0xeeee}

function fillp_local(x,y,p)
    x=band(x,3)
    local p16=flr(p)
    local p32=rotr(p16+lshr(p16,16),band(y,3)*4+x)
    return fillp(p-p16+flr(band(p32,_fillp_xmask_lo[x])+band(rotl(p32,4),_fillp_xmask_hi[x])))
end

And a demo thereof:

Cart [#47737#] | Copy | Code | 2017-12-29 | Link
0

P#47706 2017-12-27 17:18 ( Edited 2017-12-29 04:06)

::

Thanks Felice,
this is a very nice addition. Would you mind if I edit the original post with an update of your refined version?

Edit: I just noticed however, that your version doesn't respect the additional (floating point?) bits you can provide to a pattern to specify ignoring the second color from drawing. For instance, this pattern would not draw as intended:

local pattern=0b1110100110011010.1
fillp_local(x,y,pattern)

Once that is fixed I'll update the OP with your improvements, given you're alright with that.

Regards.

P#47728 2017-12-28 09:30 ( Edited 2017-12-28 12:27)

::

Do as you like with the code, I marked it creative commons for that reason. :)

It should be retaining the fractional transparency bit. I don't remember testing that so I may have failed to do it right. That's what the p-p16 is where I send the value on to fillp--the original pattern minus the integer bits, leaving the fractional bits, which are added to the resulting shifted pattern.

P#47734 2017-12-29 02:23 ( Edited 2017-12-29 04:10)

::

Oh, okay, I see what I did.

When I was rearranging code, I accidentally moved that p-p16 inside of the flr(), effectively deleting it.

Easy fix. See my original post for an updated version and a demo that <gasp> actually tests the transparency support. ;)

P#47738 2017-12-29 04:07 ( Edited 2017-12-29 04:07)

::

Actually...

One option would be to write a flat-out replacement for fillp() that takes optional x,y args after the pattern and shifts the pattern if so. That would add to the API pretty seamlessly, while not adding complexity and still keeping backwards compatibility.

Something like this:

_fillp_xmask_lo={[0]=0xffff,0x7777,0x3333,0x1111}
_fillp_xmask_hi={[0]=0x0000,0x8888,0xcccc,0xeeee}
_fillp_original=fillp

function fillp(p,x,y)
    if y then
        x=band(x,3)
        local p16=flr(p)
        local p32=rotr(p16+lshr(p16,16),band(y,3)*4+x)
        p+=flr(band(p32,_fillp_xmask_lo[x])+band(rotl(p32,4),_fillp_xmask_hi[x]))-p16
    end
    return _fillp_original(p)
end

It costs 7 more tokens, but it just feels more natural, and in keeping with the rest of the API, to extend an existing function. Other calls like map() and sspr() do similar things.

Obligatory demo:

Cart [#47739#] | Copy | Code | 2017-12-29 | Link
1

P#47740 2017-12-29 04:56

::

Felice, this is a way better approach so I'll update the original post with your improvements. Thanks and great work!

P#48162 2018-01-14 05:28 ( Edited 2018-01-14 06:04)

::

Ah, nice. I like my demo for having my name in it, but yours definitely demonstrates the functionality better. ;)

P#48167 2018-01-14 06:23

Log in to post a comment

user:
password:

New User | Account Help
:: New User
X
About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2018-10-18 09:00 | 0.298s | 1835k | Q:41