Math functions in PB

Had my pixel Blaze for about a month now. I was curious about some of the complex math functions. for example:
Export function beforeRender (Delta) {
t1= (time).01


Wouldn’t this be the same as just putting the answer to h in the code.


Or does it create different effects typing out the equations?

let’s reformat this and go over it:

export function beforeRender(delta) {
   t1= time(.01)
   t2= time(.02)

export function render(index){

so while h can be that value, it’ll change value as t1 and t2 change values from 0 to 1, at different rates

Also sin is going to go from -1 to 1, and the times 3 will speed up the color change.
(h wraps anything not 0 to 1, so it handles all values, nicely)

so the equations are about making changing values, not fixed ones.

If you can remove/simplify equations that always are the same value, that speeds things up, otherwise the PB has to spend time doing the math over and over.
So if you are doing t1+t2 and then multiplying by 3 over and over, it might make more sense to do it in beforeRender, and store it in a new variable like t3, and then just do hsv(t3,1,1) and that would work fine, and be slightly faster. This is because t1 and t2 ONLY change values once a render, not while it’s figure out each pixel. So doing that math in render() is a waste of cpu, doing it over and over.

but let’s say you wanted to work in the index… then doing hsv(t3+index/pixelCount,1,1) would make a rainbow of change, rather than all of the pixels being the same hue.

1 Like

awe, thank you! that makes sense.

1 Like

Quite welcome. Also, this just struck me:

t1 and t2 will never exceed 1… They will hit 1 and drop back to 0, and start back toward 1.
So t3 (t1+t2) will never exceed 2, and likely less.
Sin() in PB uses radians, and a full cycle in radians is 2xPi ( or 6.28ish)
So that Sine value will never really cycle fully from -1 to 1… It’ll be some lower value. Multiplying it by 3, assuming it reaches .33 will yield one (and if it exceeds one, it’ll wrap back to 0/red and start over…

But… It’s not a smooth curve… Sometimes t1 or t2 will drop back to zero, the sine of their sum will suddenly change and the color display will jump.

Visually this might be ok, might not

If you wrap the time() in wave(), it’ll be smoother.
Wave() takes the output of time() (which always goes from 0 to 1, drops.hard to 0, and again and again… it’s a sawtooth.) Wave turns that sawtooth into a smooth sine wave from 0 to 1, then back smoothly to 0, and so on.

Exercises to understand this all better:

Try wave(time(.01))
Try moving the x3 Inside the sin()
Try changing it to PI. Try changing it to PIx2 aka PI2
Try using wave() instead of sin()
Try using square()
Try using triangle()