how to change select entity texture's color

andyhebear1

02-08-2010 11:46:09

how to change select entity texture's color !
have ideas?
thanks

smiley80

02-08-2010 12:34:56

var tus = entityMaterial.GetTechnique(0).GetPass(0).CreateTextureUnitState();
tus.SetColourOperationEx(LayerBlendOperationEx.LBX_MODULATE, LayerBlendSource.LBS_MANUAL, LayerBlendSource.LBS_CURRENT, colour);

This will tint the texture using the 'colour' ColourValue.

Note:
It'll effect all enities that use this material.
To avoid that, create a clone of the material, do the above with the clone and change the materialname of the entity to the name of the clone.

andyhebear1

03-08-2010 01:50:58

good thanks! this blend color and texture picture

smiley80

03-08-2010 09:11:31

You can also use a simple fragment shader, which will work better than the above when the texture isn't greyscale.

colorize.cg:
float4 main(float2 vIn : TEXCOORD0,
uniform sampler2D tex0 : TEXUNIT0,
uniform float4 newColor
) : COLOR
{
float4 weightsBW = float4(0.3, 0.59, 0.11, 0);
float4 color = tex2D(tex0, vIn);
float brightness = dot(color, weightsBW);
float4 cOut = brightness * newColor;
cOut.a = color.a;
return cOut;
}


colorize.material:
fragment_program colorize cg
{
source colorize.cg
entry_point main
profiles ps_2_0 arbfp1
}


Usage (assumes the texture is in technique 0 pass 0):
Pass p = entityMaterial.GetTechnique(0).GetPass(0);
p.SetFragmentProgram("colorize");
p.GetFragmentProgramParameters().SetNamedConstant("newColor", colour);

Beauty

11-08-2010 17:07:33

I think a shader is code which will be processed by the GPU.
What's the advantage of a fragment shader for the case of changing a material colour?


In a ported code for creating a material with a custom colour. There I also can define a transparency (by the last SetDiffuse() parameter).
It's different to the other code. I published it on the wiki page MOGRE Line 3D. Here is a copy:

// create material (colour)
MaterialPtr moMaterial = MaterialManager.Singleton.Create("line_material", "debugger");
moMaterial.ReceiveShadows = false;
moMaterial.GetTechnique(0).SetLightingEnabled(true);
moMaterial.GetTechnique(0).GetPass(0).SetDiffuse(0, 0, 1, 0);
moMaterial.GetTechnique(0).GetPass(0).SetAmbient(0, 0, 1);
moMaterial.GetTechnique(0).GetPass(0).SetSelfIllumination(0, 0, 1);
moMaterial.Dispose(); // dispose pointer, not the material