How to define an integer constant in shader preprocessor?

As explained here, it is possible to switch some features of a shader on and off by preprocessor defines. However, these defines all have a boolean character (defined or not defined). I would like to do something like this:


uniform vec3 m_ProjectorLocation[NUM_PROJECTORS];

uniform mat4 m_ProjectorViewProjectionMatrix[NUM_PROJECTORS];

for (int i = 0; i < NUM_PROJECTORS; i++)


vec3 pl = m_ProjectorLocation;

mat4 pm = m_ProjectorViewProjectionMatrix;

// process an arbitrary number of projectors in a single pass



Is it possible to define an integer constant at runtime by something like “mat.setParam(…);”. If so, please tell me how.

uh, exactly as you said yeah ^^ …read the wiki entry on shaders in jme3

Unfortunately, the wiki says nothing about defining integer constants. It’s all (implicit) boolean. :confused:

Edit: This should do what I want:

[java]material.getMaterialDef().getDefaultTechniques().get(0).addShaderPresetDefine(“NUM_PROJECTORS”, VarType.Int, 10);[/java]

You have to do like you do with booleans. Set this in the j3md material parameters:

Int NumProjectors

Then you do a define:

NUM_PROJECTORS : NumProjectors

You always work with the NumProjectors variable from the app, but with NUM_PROJECTORS in the shader.

Btw. defines aren’t implicit boolean, works like in C.

EDIT: Not sure you have to do that tbh, but it works.

1 Like

Thank you! I had a brain bug. I thought the preprocessor defines were just defined like

[java]#define NUM_PROJECTORS[/java]

but they are in fact defined with an adequate value depending on the variable type.

Sorry, sometimes I’m a bit slow on the uptake. Thanks again to both of you! :smiley: