Making Alpha channel work in jit.gl.multiple while light_enable is on
Hello everyone, i'm having a problem using jit.gl.multiple. I'll explain it.
Take a jit.gl.gridshape bound to a jit.gl.multiple (through left jit.gl.multiple outlet):
If jit.gl.gridshape has @lighting_enable 0 and @blend_enable 0, then I can change RGB color array only with jit.gl.multiple, not with jit.gl.gridshape (Alpha channel won't change);
If jit.gl.gridshape has @lighting_enable 0 and @blend_enable_1, then I can change RGBA color array only with jit.gl.multiple, not with jit.gl.gridshape;
If jit.gl.gridshape has @lighting_enable 1 and @blend_enable_0, then I can change RGB color array only with jit.gl.multiple, not with jit.gl.gridshape (Alpha channel won't change);
If jit.gl.gridshape has @lighting_enable 1 and @blend_enable_1, then I can change RGB color array only with jit.gl.multiple, and A channel only with jit.gl.gridshape.
jit.gl.multiple @blend_enable appears to be useless, i can't see any variations changing it.
Case 4. shows that is possible to work with both light and alpha channel activated. The problem is that I want to change A channel with jit.gl.multiple and not with jit.gl.gridshape while having light_enable 1. If I could modify alpha channel with jit.gl.multiple while light_enable is 1, it would give me more possibility to change it individually on each copy made with jit.gl.multiple, unless to do it globally. I can't figure out how to do it. I hope someone can help me.
please provide a patch and i'll take a look
ok I understand what you're attempting. It looks like the default shader generator is ignoring the instance color alpha (as you've discovered). I would consider this a bug, and I'll file a ticket to get it fixed. I think if instance colors are provided, it should take alpha from those instance colors.
For now you can get what you want by using a custom shader rather than the default shader. I've updated your patch with this custom shader. Keep in mind that this means lighting is baked into the shader (so the lighting_enable toggle no longer has any effect).
Does this shader require Max 9 to work properly? Because I'm using Max 8 and I can't get it to work
I tried to copy the text inside the jit.gl.shader you wrote in the first file, save it as .jxs with notepad++, and then read it in the patch with jit.gl.shader but apparently it didn't work. I'm not familiar with written shaders at moment, but i understand that it's currently the way to fix the problem. You said that it could be a bug to fix. Well, the same issue, it seems to me, happens with jit.gl.mesh too: when I give @lighting_enable 1 and blend_enable 1 on jit.gl.mesh, it reads correctly alpha channels only trough @gl_color (that is global), but again alpha channel gives problems when given through jit.gl.mesh color array inlet when @lighting_enable 1 and blend_enable 1.
I see the same bug with jit.gl.mesh. thanks for the report, will note that on the ticket.
To make the custom shader workaround work in Max 8, download the "multshade.jxs" file I attached to my second reply to you.
Yes, it works. Thank you Rob! lighting + blend + jit.gl.multiple single shape control. I knew it was possible because case 4. of my first patch showed that possibility. I also appreciate your will to report jit.gl.multiple and jit.gl.mesh issue and hope they fix the problem.
If they fix this problem both on jit.gl.multiple and jit.gl.mesh, a step further would be to make everything work while jit.gl.material (and jit.gl.pbr too?) is attached to them. At moment it doesn't work.
Devs have to take care of that alpha problem which is spread on some jit.gl objects and/or their connections.
In the meantime I'll learn how to make my own shaders, starting from this input you gave me, Rob.
Ok, I'm starting to understand something. The shader you gave me is the internal code of jit.gl.gridshape object with @lighting_enable 1 and @blend_enable 1, plus your fix. I understood that because when I send message "get shader" to jit.gl.gridshape with those attributes on, it gives me the same code, except for 2 things (that I assume are your fixes):
Nn *= -1.; has been removed
jit_out.color.a = frontMaterial.diffuse.a; has been changed with jit_out.color.a = color.a;
I don't know why Nn has been removed but I assume that a means alpha channel. jit_out.color.a is the final alpha channel vector. frontMaterial.diffuse.a is the global alpha channel (frorm jit.gl.gridshape), and color.a is the individual alpha channel at the end of the chain (from jit.gl.multiple).
So it should work also in this way jit_out.color.a = frontMaterial.diffuse.a * color.a; to make both working togheter at the same time(I tested it and it works).
Anyway jit_out.color.a = color.a; is still the most consistent logic when @lighting_enable 1 and @blend_enable 1, because it works the same way as RGB channels do.
Your assumptions are mostly all correct with 1 caveat:
Nn *= -1.; has been removed
This is a bit tricky to explain, but this line simply reverses the normals for drawing back faces. The get_shader messages simply grabs the last shader that was used to draw, and if cull_face is 0, will most often be the back faces. If we don't care about back faces then we can ignore this pass (and should therefore set @cull_face 1 for efficiency).
Anyway I've changed my mind about learning how to make my own shaders. I still prefer standard jit.gl objects rather than written code. XD
I don't want to go off topic, but I'm working on a project and things keep showing up while working on it. I'll explain here since the core of this topic is always the alpha channel.
I need to have the control on the alpha channel while using jit.gl.material linked to jit.gl.gridshape or jit.gl.mesh, but despite I put @blend_enable 1 on them, i can't find a way to have it.
I tried to move the alpha both on jit.gl.material different light attributes and on the object generator (jit.gl.gridshape or jit.gl.mesh), but nothing happens.
This time I was equipped with the get_shader message (xd) and I used it; anyway I don't find any jit.color.a anywhere. I can just read the final outColor = jit_Material.color in the fragment program.
Now I see 2 way to solve this problem to keep going on the project:
I don't use jit.gl.material. Instead I link the generator object both to jit.gl.light and the texture in standard way ; in this way i can keep alpha channel control -> in this scenario the question is: If I balance the lighting properly, will the result be the same as using jit.gl.material?
I have to change the final shader inside the generator object. In this scenario, I would like to avoid if 1st one works, the question is: can you tell me what line/lines of code i have to add and/or change ?
P.S. i found jit_Material.color = vec4(0., 0., 0., 0.); in the void main() of fragment program. I'm trying to changing it using luck without knwoledge in something like jit_Material.color = vec4(0., 0., 0., color.a); but it doesn't work xd
for jit.gl.material you can control alpha with color or mat_diffuse alpha value, but you must 0 out the alpha component of the other material colors. put the following in a message box to your material, adjust to taste:
mat_ambient 0 0 0 0, mat_specular 0 0 0 0, mat_emission 0 0 0 0
Yes, thank you Rob. I didn't know that i have to put to zero all 4 mat_ambient, mat_specular, mat_emission and mat_diffuse to bring alpha to 0. Now it works.
It kept failing the patch also because there were too many wires, and I connected some of them to jit.gl.gridshape instead of jit.gl.material. My bad. I can keep going with the project.
When I specify parameters other than the color (position, scale, rotatexyz) to a jit.gl.multiple linked to a jit.gl.gridshape, it happens that jit.gl.gridshape defines the global value and jit.gl.multiple defines the individual value -> so both work. And I think there is a * between them. So "jit_out.color.a = frontMaterial.diffuse.a * color.a;" could be a good way to fix the alpha issue, but that shoud be done for RGB values too (in fact at moment jit.gl.gridshape rgb channels become useless after adding jit.gl.multiple). So is it possible that the issue is about all the argb values (with subtle difference between alpha channel and rgb channels)? In the end what i'm saying is that even if alpha channel behaviour is being fixed with "jit_out.color.a = color.a;", there is still a discrepancy between scale/position behaviour and argb behaviour.
It should be something like "jit_out.color.a = frontMaterial.diffuse.a * color.a;" for alpha channel, AND the same logic applied to rgb channels; and it should work with the same logic (argb channels = global value from jit.gl.gridshape (0.-1.) * individual value from jit.gl.multiple (0.-1.)) both when @lighting_enable is 0 and 1. I would fix it in this way as a permanent fix.
you are correct there are differing intentional behaviors between how colors and spatial transform attributes are applied to jit.gl.multiple instances. If instance colors are being used, those will overwrite the object color (or mat_diffuse color if using lighting). This is working as expected other than the alpha bug you discovered (which will be fixed in a future update). On the other hand spatial transform instance attributes are combined with the object attributes. I understand this is not consistent, but is what it is.
I noticed this discrepancy and pointed it out, but if it’s a deliberate choice, then that’s fine. I have no doubt that what you’re doing with MaxMSP on purpose is good, as it’s excellent software.