Is it possible to invert the colors of a texture / material?

I have a few thousand black PNG icons on a transparent background. I would like to be able to reuse those, but I need them to be white.

With CSS I can easily invert an image with filter: invert(1); . It’s even possible to use a value like 0.5 to achieve some nice effect.

I’ve read about procedural textures, but find them a bit complex. Is there no easier way to achieve results like the CSS-filters? Maybe processing them on a separate canvas first and use the ArrayBuffer (no idea how to do this though!) ?

Here is an example:
Babylon.js Playground (babylonjs.com)

The NME procedural link:
Babylon.js Node Material Editor (babylonjs.com)

You might be interested in this approach as well:

https://doc.babylonjs.com/features/featuresDeepDive/controls/imageFilter

Wow, that’s even more complex than I thought :slight_smile:
Using a shader and a separate canvas seems so overkill to just invert a simple PNG.

I use XnConvert now to batch convert all the icons to webp, resize them to 128x128 and invert them. Somehow I get strange results though (some artifacts like very tiny flickering horizontal lines), when using the converted images with BabylonJS. That’s why I asked if there’s maybe a builtin method in BabylonJS.

UPDATE
The strange flickering horizontal line is NOT caused by the image conversion. It’s also happening with the original 512x512 PNG file from Flaticon.

Somehow it seems using a StandardMaterial with a transparent texture gives some render problems. This is what I use:

mat = new BABYLON.StandardMaterial('symbolMat', scene);

mat.diffuseTexture = new BABYLON.Texture('/images/symbols/star.png');
mat.diffuseTexture.hasAlpha = true;

Around the edges of the transparent images tiny flickering artifacts appear, especially when the mesh is moving. I didn’t notice this with the black icons, but with the white icons it became very noticable.

Could I use some kind of anti-alias to avoid these issues?

It depends. You write the function once and use multiple times. A quickie while holding a cigarette in my left hand :smiley: with a reusable InvertedColors procedural texture class.

With a PNG from Flaticon:

With PNG and antialias:

We need to see this.

Thank you very much. This helps a lot to understand how it works.

However, with your examples I see the same artifact at the left side of the icon! It appears as a small thin line that flickers when the camera rotates. It’s around where the two horizontal ‘speed lines’ of the car are. To me it seems like it’s caused by the pixels that are at the OTHER side of the image bordering the edge of the image. The thin line is exactly as high as the front of the car.

In my own test it happens with icons that have pixels on the bottom, and then I see the flickering line at the TOP.

With the anti-alias example the effect is less noticable, but still visible.

Maybe it’s a WebGL or driver issue on my system only. My graphics card is old (AMD Radeon HD5750). I tried in both Chrome, Brave, Firefox and Edge and the problem is the same.

Quite strange. Maybe I should just create icons with more transparent space around them!

You(rs) know what? Call me ‘old school’ if you want but, here’s my moto: “a shit source with fx will always remain a shit source flavored with some fx or post”. You want an inverted texture? Here’s my advice: Create one! It’s as simple as it gets. I know you ‘devs’ always think you’re gonna ‘fix things’ with some procedural/post/node/shader automagical trick :magic_wand: :grinning:. I say, to end ‘right’ there’s nothing better than to start ‘right’. Of course this is (as always) just my small designer’s opinion only :face_with_hand_over_mouth: :grin:

I already converted all icons to white with XnConvert. However, this didn’t fix the issue with the artifact.
I thought it was because of XnConvert though, so I figured it maybe would be better to use the original Flaticon image and do it in realtime.

Now I know it’s because of pixels that border the edge of the icon :slight_smile:

Hum? Inverted is inverted. If your white image is not grayscale (but RBG or CMYK) and the edges are semi-transparent, then… what do you expect? Don’t you have PSD or similar? Just a quick check on it (and eventually a fix if necessary) with a batch processing would do the trick in I believe just about no time… A lot less time anyway than trying to set-up a node, a shader or whatever fancy trick I did read about above.

Sounds like you want to disable texture uv wrapping. Set the wrapU and wrapV to clamp

I experimented with the ‘wrap’ properties of Texture and this seems to fix the issue!

mat.diffuseTexture.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
mat.diffuseTexture.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
//mat.diffuseTexture.wrapR = BABYLON.Texture.CLAMP_ADDRESSMODE;

I can understand wrapV and wrapU, but I have no idea what wrapR does though. but wrapV and wrapU do the job :slight_smile:

My best guess is that it’s for 3d textures

I see.The fix is:

    invertedTexture.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE
    invertedTexture.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE

or you can put it in the constructor:

        this.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE
        this.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE

EDIT:
I can see @vinhui has already answered it while I was creating the PG.

Thank you all for helping me, very much appreciated!

Well, at least it’s working (sort of :grin:) I can still see it a fair bit less smooth than the original. May be it’s my eyes playing trick :face_with_monocle: :crazy_face:…I’m not gonna put another layer on top of what I already said. In fine, it’s your choice. Personally, I’d rather stick to my original thought (especially considering all what you(rs) have gone through to achieve something that should have remained very simple (again, my opinion only :face_with_hand_over_mouth:)

Hey, clamp fixes this :wink: not the smoothness!

Hi, If I create original texture with invertY false, then the inverted color texture will be flipped.

So why don’t you set it to true if it’s an issue.

If you already accepted an answer create a new topic in the Question category and link to this topic please.

Yes, of course it does. My bad. 'Shouldn’t have quoted your post. The clamping is just fine (and necessary). I’m still amazed to see for how long this ‘simple’ thread can go on :dizzy_face: :sweat_smile: Don’t you Guys have something better to do on your holidays? :beach_umbrella: :cocktail: :joy:

You are here too :joy::sunglasses::joy::crossed_fingers::see_no_evil: