Forums > Misc

jit.mgraphics + alpha (drawing on transparent background)

April 5, 2013 | 11:45 pm

Dear Friends

Did you know, how to fill all pixels of the [jit.mgraphics] object with an arbitrary value (something like "setall" message for the [jit.matrix])? I want to draw on transparent background, and I can’t use the "paint" method of the [jit.mgraphics] – "paint" is working with alpha on typical way, but I need a method to "replace" alpha component into new value.

I have no idea, how to do that. Maybe my post is just a"feature request"?


April 8, 2013 | 7:10 pm

put the following in a message box:
set_source_rgba 1 1 1 1, paint, set_source_rgba 0 0 0 1

this will set the clear color to white, clear, and then set the drawing color back to black.
adjust the color values for your needs.
this is taken from the jit.mgraphics help file, inside the "reset" sub-patcher.


April 8, 2013 | 7:11 pm

sorry, just re-read your post saying you can’t use the paint method.
but i don’t really understand why.
maybe provide a patch to better demonstrate your problem.



ak
April 8, 2013 | 8:09 pm

I have simiar problem in the past – it appears that jit.mgraphics have internal matrix, which alpha channel cannot be cleared. (clear) message clears output matrix, which is immediately replaced by that "internal" one on bang. BTW, the "internal" matrix of newly created jit.mgraphics object has all cells in alpha plane set to 0, but later there is no way to return to this state (except recreating the object).

maybe a solution could be to enable jit.mgraphics to receive jit.matrices to paint on them? (just a shot in the dark)


April 9, 2013 | 10:26 am

> sorry, just re-read your post saying you can’t use the paint method.

I can’t or I just don’t know, how to ;-).

In other words my question is: how to fill the [jit.mgraphics] with rgba value 0. 0. 0. 0.? In my opinion, the "paint" is not good for that, because "set_source_rgba 0. 0. 0. 0., paint" doesn’t clear alpha channel (of course, I can post the patch later, too).

I think, the "setall" message (for [jit.matrix]) is working just like I want, but unfortunately there’s no equivalent of this message for [jit.mgraphics].

Anyway: thank you guys for your time.


April 9, 2013 | 11:52 am

Ok, I prepared a patch explaining the problem I’m trying to solve (my original project is to complicated and containing to many external dependences to post it).

In posted patch I located two [jit.gl.videoplane] objects. One filled with an image taken from [jit.grab], second one containing stuff from [jit.mgraphics]. I’m using @layer parameter to put the [jit.gl.videoplane] containing data from [jit.mgraphics] "over" the [jit.gl.videoplane] with [jit.grab] data. Moreover I’m using alphablending for both [jit.gl.videoplane]‘s.

Now I need a method to change "paint" command used for [jit.mgraphics] to something allowing me to create clear (filled with 0.) alpha channel for the image produced by [jit.mgraphics] – and see both: the drawings from [jit.mgraphics] and background from [jit.grab].

Maybe a good model of the thing, I want to create is "HUD" display.


April 9, 2013 | 6:28 pm

the easiest way to make this work is to set @blend add on your top jit.gl.videoplane (the one displaying the jit.mgraphics).


April 9, 2013 | 8:43 pm

Rob, once again: thank you for your time.

Unfortunately your proposition is not a solution: this works fine if I want too draw white forms over relatively dark background (actually every color different to white will be changed ), but, if the video stream from the camera will be containing white areas and I’ll be trying to draw some black lines… every black line will be just invisible – "add" is not a good method for that. What I need is alpha blending, and for alpha blending I need the access to the alpha channel of the image created by [jit.mgraphics].

I’m thinking how to workaround the problem. Of course I can use [jit.gl.sketch] for drawing shapes, but but (1) I can’t draw texts with [jit.gl.sketch], (2) I need something "pixel oriented"and (3) [jit.mgraphics] is really handy (except this one issue ;-) ).


April 9, 2013 | 9:14 pm

right, i should’ve said @blend screen.

i’m not sure if there’s a way to output jit.mgraphics with an alpha channel.
however, there is no difference between @blend screen with a black background, and @alphablend with an alpha channel.


April 10, 2013 | 12:42 pm

> there is no difference between @blend screen with a black background, and @alphablend with an alpha channel

There is really serious difference between booth blend modes (see attached patch). Trust me – I studied my problem very intensive… ;-)


April 10, 2013 | 7:14 pm

ahh, you are correct.
that’s unfortunate.
to make up for my mistake, i’ve made you a gl.pix patch that’s based on the alphaglue shader.
this will create an alpha channel based on luminance, and gives you some parameters for threshold and fading, which can be very useful.

– Pasted Max Patch, click to expand. –

April 11, 2013 | 1:16 pm

Thank you Rob! I was thinking on a similar, shader-based solution, but your implementation is much better then my own. I think, this is very promising technique.


April 11, 2013 | 5:07 pm

Hey yaniki, I have been trying to solve this same problem and am curious about the solution you posted. When I load the last patch you posted, I can see the camera input, but it does not seem to draw any mgraphics commands. What version of max are you using?


April 11, 2013 | 5:12 pm

Hi Anthony

I’m working on Max 6.1.2, OSX. Max is running in 64bit mode.


April 11, 2013 | 5:14 pm

But this trick works in 32bit mode too (just switched and tested a bit).


April 12, 2013 | 2:13 pm

Hey yaniki, I upgraded to 6.1 and it looks like your example works. So in the end, do you feel the patch you posted last is the best solution to this problem?


April 12, 2013 | 2:35 pm

Hello Anthony!

I’m glad my trick works. Thank you for testing and information.

> do you feel the patch you posted last is the best solution to this problem?

I don’t know yet… Workaround proposed by Rob is very efficient, and – what is also important in my opinion – is based on standard programming workflow in Max. My solution is a little bit tricky. More tests are required, that’s for sure. I’m, especially, interested in memory management when [jit.mgraphics] @dim parameter is constantly changing…

I can imagine, the best solution will be to add to the [jit.mgraphics] API methods for direct access to the alpha channel or direct access to internal matrix of the object – but it’s rather question we can ask Cycling’74 team as a feature request. In my opinion [jit.mgraphics] object is potentially very strong tool for various applications (custom GUI, for example) – with direct access to the alpha channel will be even stronger.

Moreover in Max we can do things in many ways: it’s simple to use [jit.gl.sketch] and [jit.gl.text2d] (I think, a solution for missing 64bit version of [jit.gl.text2d] will be prepared) to produce very similar results. In my case the problem is missing 64bit version of [jit.gl.text2d] and (but it’s not a "problem", rather… hmmm… rather something opposite to "problem") style in which [jit.mgraphics] is working with graphics – it’s very clear and handy, so I want to use [jit.mgraphics] instead of other solutions.


April 12, 2013 | 3:53 pm

I would like to keep pushing this issue. I am working on a porting library that allows me to easily load Processing sketches in Max. Up to this point I had been getting along pretty well with mgraphics until I realized that it can not draw transparently. It would be nice to know what the best design is before I start changing code.


April 12, 2013 | 6:03 pm

hi yaniki.
thanks for persisting with this.
i agree that neither of these solutions is ideal, however your solution seems like a good work-around for the time being.

i will add a feature-request for jit.mgraphics to allow this behavior without the work-around.


April 13, 2013 | 12:06 pm

Thank you, Rob.

@Anthony: loading P5 sketches into Max? Sounds very interesting. Many of my projects are – from technical point of view – combined Max and Processing (typically joined via OSC). I’ll be keeping crossed fingers.


May 26, 2013 | 1:43 am

Hi all,
I’m struggling with the same issue here. I’ve been using the Yaniki’s dim-reset method and does seem to work well. But I’m also seeing an odd byproduct and not sure if it is a result of trying to set all the pixels in jit.mgraphics with an alpha of 0. or a byproduct of how I’m rendering it via jit.gl.videoplane: all the lines/shapes have a very thin outline in a light grey, which is really noticeable drawing a fat white line where the when other layers in the background are also white (trying to use white as an eraser, if that makes any sense).

I’m working on whittling down a patch to share, but wanted to ask to see if anyone had noticed this.

Thanks,
David

EDIT: to clarify a bit more…I only see the outline when the line or shape borders a pixel that still has its alpha set to 0. Not sure if that’s helpful info or not.


May 27, 2013 | 1:19 am

David, can you post an example patch and some screenshots?


May 27, 2013 | 4:57 am

(OT : @yaniki : i’m sorry, i clicked by error on spam button on one of your posts, it disappeared for me… then i tried again to verify if i could do something and i couldn’t… hope it doesn’t disappear for everyone, they are the  #243089 and #243090… rather stupid, and i find no way to get them back.)


May 27, 2013 | 5:30 am

"i clicked by error on spam button on one of your posts" ;-)

Not sure, which one… Everything important seems to be ok… Don’t worry.


May 27, 2013 | 9:25 am

@Yaniki:
Would love to share, but appears I can’t post anything anymore. Doing another test post before I give up.

David


May 27, 2013 | 9:27 am

Well that worked, so posting patch as an attachment.

@Yaniki:

Here’s a whittled down patch. You *should* see what I’m seeing wrt the outline once you set the pen color to white (unless it’s GPU dependent). Notes in patch. Max 6.1.2 (32-bit), MacOS 10.6.8 & 10.8.3.

Thanks in advance for any pointers!

Cheers,
David

EDIT: f-key toggles fullscreen.


May 27, 2013 | 10:33 am

Yes, I can reproduce the problem (unfortunately…). Hmmm… I don’t have any idea now, how to fix it, but your problem is, in fact, interfering with my projects too, so I have to dig into this more deeply during this week…


May 28, 2013 | 7:52 am

hey guys, there will be a new feature of jit.mgraphcis in the next update that addresses this problem.


May 28, 2013 | 9:34 am

Wow. Thank you!


May 28, 2013 | 1:00 pm

Nice!


May 28, 2013 | 11:46 pm

Luvly!


June 19, 2013 | 12:45 pm

hi guys, the 6.1.3 version of jit.mgraphics has a new message called clear_surface, that should do what you need.


June 20, 2013 | 3:33 am

I played with the new [jit.mgraphics] yesterday – works great in both, 32 and 64bit versions (OSX). Thank you!


Viewing 33 posts - 1 through 33 (of 33 total)