jit.mgraphics + alpha (drawing on transparent background)
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"?
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.
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.
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)
> 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.
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.
the easiest way to make this work is to set @blend add on your top jit.gl.videoplane (the one displaying the jit.mgraphics).
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 ;-) ).
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.
> 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… ;-)
ahh, you are correct.
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.
----------begin_max5_patcher---------- 2809.3oc6c80bihiD+4jOEpbMUc6Vq2bHID+4d4l6s6Sv8xLW4BaTrYFLPAJ +Y1s1u6mPRXCNFGYLRFO4RR4XDfn0O0cqVRc27m2e2rk4uRqlA9Gfu.t6t+7 96tSTTcA2oN9tYaidcUZTk3xlkQeIe42lMWdJF8Uln3ukvdXc5CEIu1bpGyy XUI+As9zPzCNphydZaRVJkIpM39Byeh0TJRUZRrnp4Otem32tZyh1Jp1Y+qx jnzlyHqA1OJnx1SMMsXc5hZZ7oR5r4fYy.+W0EWDwVsIIa8hR5Jl75cQNbpD fHj5+QfhC3E08dnkJvQgNbJJIk9LsrJIOqUa5tYQEEsJ9tV2RMj9sbQE4MeW QIYxhb1UTI84jl6OPV3eceS0umvQBB1KT7IVbfKLXOgy6RWuJOUV6eA37P37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FaudKDdZ0agzYXBxzhbbr2n55nXFaO3ApkUO1S5B5oC8XOo KHZLnmCBWJOeQbR4FJWVD2icj7syoKhr6n8Dw305zwFb6o5PPNPCQNxIrbP5 KulRNHskePJK+vzUNu19q6+e.OxpqJC -----------end_max5_patcher-----------
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.
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?
I’m working on Max 6.1.2, OSX. Max is running in 64bit mode.
But this trick works in 32bit mode too (just switched and tested a bit).
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?
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.
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.
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.
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.
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.
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.
David, can you post an example patch and some screenshots?
(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.)
"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.
Would love to share, but appears I can’t post anything anymore. Doing another test post before I give up.
Well that worked, so posting patch as an attachment.
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!
EDIT: f-key toggles fullscreen.
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…
hey guys, there will be a new feature of jit.mgraphcis in the next update that addresses this problem.
Wow. Thank you!
hi guys, the 6.1.3 version of jit.mgraphics has a new message called clear_surface, that should do what you need.
I played with the new [jit.mgraphics] yesterday – works great in both, 32 and 64bit versions (OSX). Thank you!