View Full Version : How to get alpha component of texture?
masanori
February 8th, 2010, 11:13
Hi,
Could someone tell me how to get the alpha component of a texture in mental mill 1.0 Standard Edition? I have a texture with RGBA components, and try to get the A component. But I can't find any node that extracts the A component of a texture.
What I want to do is a decal mapping, like:
Diffuse Color = ( 1 - alpha ) * base color + texture color(alpha-premultiplied)
Best regards,
--
Masanori
ruediger
February 8th, 2010, 20:53
You can extract the alpha channel from a texture using the "Conversion_color_to_floats" node which can be found in the conversion folder of mental mill.
If you fot mental mill standard edition you can also put the following snippet of code into an .msl file and add it to your shader toolbox in mental mill. (Note that I discarded the 'premultiplied' term as I can't see where it should come from and what it is used for)
shader decal_map
{
input:
Color base_color;
Color tex_color;
output:
Color result;
void main()
{
result = (1 - tex_color.a) * base_color + tex_color * tex_color.a;
}
};
The image attached shows a graph containing the conversion nodes in the top area of the workspace and using the shader from above in the lower part.
Best Regards,
Ruediger
dima.david
February 14th, 2010, 11:17
Here is another way to do it
result = lerp(baseColor, addColor, addColor.a );
Or if you wnat to have control over transparency
you can add calar and multiply the addColorWeight
result = lerp(baseColor, addColor, (addColor.a * addColorWeight));
I am new to coding, i used to do basic mel scripts before, so I'm sorry if i do dirty coding
@ ruediger:
does the compute goes faster if using Lerp function instead of expression?
--
Thanks,
David
masanori
February 18th, 2010, 06:35
Hi Ruediger, David,
Thank you for the info for Conversion_color_to_floats. That's what I wanted to know. Also, thank you for the suggestions for MetaSL samples.
Regards,
-- Masanori
kogi04
February 18th, 2010, 11:01
There's a 2D Texture lookup with separated Alpha in the Shader Pack which is a sticky thread in this very forum. I prefer it to avoid multiple nodes, got enough to keep track of as it is.
ruediger
February 18th, 2010, 14:08
@David: It should be the same. The lerp function is a convenience function that does exactly do that thing for you, so the instructions that are generated should be the same. You can easily test this by exporting the shader to cg(fx) and checking the instruction count of the shader using either FX Composer or cgc.exe (which is the standalone shader compiler)
@Kogi:
Having swizzling like it can be done in the unreal material editor would be a desired feature, but for now using the conversion nodes is the only way to do it. Mental mill rather insists on doing everything explicitely. Doing implicit conversions might become a feature at some point, but for now the lookup shader from the shader pack with the separated alpha is the way to go.
Cheers,
Ruediger
kogi04
February 18th, 2010, 21:50
Not following what you meant Ruediger.
Just wanted to point out that there is the "Ext_texture_lookup_2d_alpha.msl" which was provided in the Shader pack provided here on the forum. I've used it several times to avoid a few extra nodes.
You also seem to be referring that this is the "way to go"...?
ruediger
February 19th, 2010, 11:03
Kogi,
maybe I was thinking one step to far ahead... Just to clearify: It would be nice if users could choose which component to pick from a node like they can in the sample nodes of the unreal material editor.
see the image attached to this thread. Note that the data flow in the Unreal material editor is from right to left. It must have been designed in an arabic country ;)
The node "texture_sample" has five connectable outputs which are black, red, green, blue and white. Thus the user can choose which component of the texture lookup he wants to use.
When you write this as (HLSL) code, this is known as swizzling:
Tex2D(sampler, uv).r; //using the 'r' component of a texture lookup
This node is integrated into the material editor. While this allows the Unreal material editor to generate efficient HLSL code the set of node availabe in the editor can't be extended with custom shaders as the generated shader code is tighly bound to the game engine.
However this behavior is really nice and mental mill could provide similar functionality at some point. Currently you need to write a a custom shader in mental mill to get similar behavior. That's why we provide the convenience shader with the the shader pack.
To implement such built-in behavior like in the unreal material editor, this would require a major amount of implentation work, though it would be worth it.
All the Best,
Ruediger
kogi04
February 20th, 2010, 15:37
Nice. That does seem very handy. I understand what you mean by swizzling. Thanks for the info.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.