Transformation Shader Contribution

i modified unshaded shader to allow it to do the following :

  1. Translate uv coordinates.
  2. Rotate uv coordinates
  3. Scale uv coordinates

    This can be useful to create animated textures ( clouds, slime, goo, river, water, lava).

    Also it has same speed as Common/MatDefs/Misc/Unshaded.j3md because if user doesnt use said features it removes them with #ifdef.

    Usage:

    [java]

    public static Material createMaterial(Texture texture, ColorRGBA color, AssetManager assetManager)

    {

    Material mat = new Material(assetManager, "shaders/Unshaded.j3md");

    if (texture != null)

    {

    texture.setWrap(Texture.WrapMode.Repeat);

    mat.setTexture("ColorMap", texture);

    }

    if (color != null) mat.setColor("Color", color);



    mat.setBoolean("TranslateUV", true);

    mat.setVector2("TranslateAmount", new Vector2f(1f,1f));



    //mat.setBoolean("RotateUV", true);

    //mat.setFloat("RotateAmount", 10 * FastMath.DEG_TO_RAD);



    //mat.setBoolean("ScaleUV", true);

    //mat.setVector2("ScaleAmount", new Vector2f(20, 20));



    return mat;

    }

    [/java]

    File : shaders/Unshaded.j3md

//Requirements for nice looking transformations :
//1) Texture has texture.setWrap(Texture.WrapMode.Repeat);
//2) Only 1 transformation can be applied translation or rotation or scale.
MaterialDef Unshaded {
MaterialParameters {
Texture2D ColorMap
Texture2D LightMap
Color Color ( Color )
Boolean VertexColor
Boolean SeparateTexCoord
// Texture of the glowing parts of the material
Texture2D GlowMap
// The glow color of the object
Color GlowColor
// if true it will translate (u,v) coordinates by (TranslateAmount.x, TranslateAmount.y) per second.
Boolean TranslateUV
Vector2 TranslateAmount
// if true it will rotate (u,v) coordinates by RotateAmount per second.
Boolean RotateUV
Float RotateAmount
//if true it will scale (u,v) coordinates by (ScaleAmount.x, ScaleAmount.y) per second.
Boolean ScaleUV
Vector2 ScaleAmount
}
Technique {
VertexShader GLSL100: Common/MatDefs/Misc/Unshaded.vert
FragmentShader GLSL100: shaders/Unshaded.frag
WorldParameters {
WorldViewProjectionMatrix
Time
}
Defines {
SEPARATE_TEXCOORD : SeparateTexCoord
HAS_COLORMAP : ColorMap
HAS_LIGHTMAP : LightMap
HAS_VERTEXCOLOR : VertexColor
HAS_COLOR : Color
TRANSLATE_UV : TranslateUV
ROTATE_UV : RotateUV
SCALE_UV : ScaleUV
}
}
Technique PreNormalPass {
VertexShader GLSL100 : Common/MatDefs/SSAO/normal.vert
FragmentShader GLSL100 : Common/MatDefs/SSAO/normal.frag
WorldParameters {
WorldViewProjectionMatrix
WorldViewMatrix
NormalMatrix
}
RenderState {
}
}
Technique Glow {
VertexShader GLSL100: Common/MatDefs/Misc/Unshaded.vert
FragmentShader GLSL100: Common/MatDefs/Light/Glow.frag
WorldParameters {
WorldViewProjectionMatrix
}
Defines {
HAS_GLOWMAP : GlowMap
HAS_GLOWCOLOR : GlowColor
HAS_COLORMAP // Must be passed so that Unshaded.vert exports texCoord.
}
}
Technique FixedFunc {
}
}

File : shaders/Unshaded.frag

uniform vec4 m_Color;
#if defined(TRANSLATE_UV) || defined(ROTATE_UV) || defined(SCALE_UV)
uniform float g_Time;
#endif
#ifdef TRANSLATE_UV
uniform vec2 m_TranslateAmount;
#endif
#ifdef ROTATE_UV
uniform float m_RotateAmount;
#endif
#ifdef SCALE_UV
uniform vec2 m_ScaleAmount;
#endif
#if defined(HAS_GLOWMAP) || defined(HAS_COLORMAP) || (defined(HAS_LIGHTMAP) && !defined(SEPARATE_TEXCOORD))
#define NEED_TEXCOORD1
#endif
#ifdef HAS_COLORMAP
uniform sampler2D m_ColorMap;
#endif
#ifdef NEED_TEXCOORD1
varying vec2 texCoord1;
#endif
#ifdef HAS_LIGHTMAP
uniform sampler2D m_LightMap;
#ifdef SEPARATE_TEXCOORD
varying vec2 texCoord2;
#endif
#endif
#ifdef HAS_VERTEXCOLOR
varying vec4 vertColor;
#endif
void main(){
vec4 color = vec4(1.0);
#ifdef HAS_COLORMAP
#ifdef TRANSLATE_UV
color *= texture2D(m_ColorMap, texCoord1 + m_TranslateAmount * g_Time );
#elif defined(ROTATE_UV)
float rotateAngle = g_Time * m_RotateAmount;
color *= texture2D(m_ColorMap, vec2(texCoord1.x * cos(rotateAngle) - texCoord1.y * sin(rotateAngle), texCoord1.x * sin(rotateAngle) + texCoord1.y * cos(rotateAngle)));
#elif defined(SCALE_UV)
color *= texture2D(m_ColorMap, texCoord1 * g_Time / m_ScaleAmount );
#else
color *= texture2D(m_ColorMap, texCoord1 );
#endif
#endif
#ifdef HAS_VERTEXCOLOR
color *= vertColor;
#endif
#ifdef HAS_COLOR
color *= m_Color;
#endif
#ifdef HAS_LIGHTMAP
#ifdef SEPARATE_TEXCOORD
color.rgb *= texture2D(m_LightMap, texCoord2).rgb;
#else
color.rgb *= texture2D(m_LightMap, texCoord1).rgb;
#endif
#endif
gl_FragColor = color;
}
2 Likes

Thanks!



I was wondering how to use my sprite sheet texture on my model made in blender3D.

Now I used your code as an example for my shader. I built your uv displacement code into the JME3 lighting shader.



The UV offset in this shader isn’t changed by the time, but can only be set manually.



I use a seperate file that holds, the fps, amount of frames and the uv_offsets per frame for the sprite sheet texture



for anyone interested here are the files for the shader :

AnimatedShaded.j3md

UVOffsetShaded.frag



to set the uv offset just use this command :



material.setVector2(“TranslateAmount”, new Vector2f(offset_x, offset_y));

1 Like

To clarify : there’s an animation in the sprite sheet , that’s why I couldn’t just UV-Map the model.

I have to update the UV-offset for each frame.

cool, thank you, nice idea !!!