Sunday, March 2, 2014

UDK and Photoshop filters

I thought it'd be interesting to try replicating two photoshop filters in UDK (before I found out they were already in the material functions) but the exercise was still fun and taught me the math behind these two filters. I think understanding how they work is useful so I wanted to lay it out here.

Screen:
This one was simple enough and my solution looked exactly like the material function in UDK.
The math for Screen is:
1-(1-a)(1-b)


Overlay:
I never got the result I wanted for this one and now that I've seen the UDK solution I know it's because I was trying to do it without splitting the channels. The reason is that If only takes float values (UDK even tells you this but I didn't connect the dots) so you have to break the images into their separate channels and append them back together after running each one through an If.
The math for Overlay is:
2ab,                     if a < .5
1-2(1-a)(1-b),    otherwise
(Sorry the notation doesn't look pretty. I couldn't get MathJax working the way I wanted)

I'm not sure why they're running the base through a subtract and a clamp (set to 50). I couldn't test it with these textures though because the result is the same as 2ab. I'll try to figure that out some other time.