Material: Replace texture color with custom color

Hi,



I’m trying to tweak the Lighting.j3md to allow replaceing some defined color in the DiffuseMap to another defined color.



I added the following in the material definition file:

[java]

// MaterialParameters:

// Replace color

Color Replace



// Replace by color

Color ReplaceBy



//… Defines: (Technique & GBuf)

REPLACE_COLORS : Replace

[/java]



In GBuf.frag:

[java]

#ifdef REPLACE_COLORS

uniform vec4 m_Replace;

uniform vec4 m_ReplaceBy;

#endif



#ifdef DIFFUSEMAP

vec4 diffuseColor = texture2D(m_DiffuseMap, newTexCoord);

#ifdef REPLACE_COLORS

if(diffuseColor.rgb == m_Replace.rgb) {

diffuseColor = m_ReplaceBy;

}

#endif

#else

vec4 diffuseColor = vec4(1.0, 1.0, 1.0, 1.0);

#endif

[/java]

And the same in Lighting.frag.



However, this doesn’t not seem to work. I’m i correctly testing the diffuse color? How can i debug shaders?



Thank you!



cghislai

There are no easy way to debug shaders.

Usually you output a color…but that won’t help since that’s what’s failing for you.



Have you got a shader compilation error, or it just doesn’t display on the screen?

maybe your m_Replace color is never met exactly in the source map…

I didn’t had any compile error. It appeared however i was testing it with the material edior, which doesn’t clear its cache when modifying the definition file.



Testing in game works as expected :slight_smile:

I will post a short snippset for color keying as i couldn’t find any here

cghislai said:
I didn't had any compile error. It appeared however i was testing it with the material edior, which doesn't clear its cache when modifying the definition file.

yeah shaders changes and j3md changes are not updated in the editor yet

j3md changes should be reflected, shader changes not, yeah.

A simple ‘reload’ button in the editor would be great :smiley:



I added my use case example on the wiki: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:jme3_shaders#exampleadding_color_keying_to_the_lightingj3md_material_definition

As a workaround you can load a model, it should clear the assetManager cache completely. A reload button isn’t necessary when theres File listeners checking for changes later.