Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jcfandino
Created January 28, 2024 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcfandino/c49ed6ff70807da7a18216d5b00a0aaa to your computer and use it in GitHub Desktop.
Save jcfandino/c49ed6ff70807da7a18216d5b00a0aaa to your computer and use it in GitHub Desktop.
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.scene.control.AbstractControl;
import com.jme3.util.BufferUtils;
public class Anim2dDemoApp extends SimpleApplication {
public static void main(String[] args) {
new Anim2dDemoApp().start();
}
@Override
public void simpleInitApp() {
var quad = new Geometry("quad", createMesh());
var mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
var tex = assetManager.loadTexture("Materials/computers/BigScreen001_Anim2.png");
mat.setTexture("ColorMap", tex);
quad.setMaterial(mat);
quad.setLocalTranslation(00, 00, 0);
quad.scale(500);
// cycle animation every 1 second
quad.addControl(new AnimationControl(1, 2, 2));
guiNode.attachChild(quad);
}
private Mesh createMesh() {
var m = new Mesh();
m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(new Vector3f(0, 0, 0f), new Vector3f(1, 0, 0f),
new Vector3f(0, 1, 0f), new Vector3f(1, 1, 0f)));
m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(new Vector2f(0f, 0f), new Vector2f(1f, 0f),
new Vector2f(0f, 1f), new Vector2f(1f, 1f)));
m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(2, 0, 1, 1, 3, 2));
var normal = Vector3f.UNIT_Z;
m.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normal, normal, normal, normal));
m.updateBound();
return m;
}
private class AnimationControl extends AbstractControl {
double maxTtl = 1;
int sheetSizeX = 2;
int sheetSizeY = 2;
public AnimationControl(double maxTtl, int sheetSizeX, int sheetSizeY) {
this.maxTtl = maxTtl;
this.sheetSizeX = sheetSizeX;
this.sheetSizeY = sheetSizeY;
}
double ttl = maxTtl;
protected void controlUpdate(float tpf) {
ttl -= tpf;
if (ttl < 0) {
ttl += maxTtl;
}
int n = (int) ((sheetSizeX * sheetSizeY / maxTtl) * (maxTtl - ttl));
int i = n % sheetSizeX;
int j = -1 + sheetSizeY - n / sheetSizeX;
float stepX = 1f / sheetSizeX;
float stepY = 1f / sheetSizeY;
var mesh = ((Geometry) getSpatial()).getMesh();
mesh.setBuffer(Type.TexCoord, 2,
BufferUtils.createFloatBuffer(
new Vector2f(i * stepX, j * stepY),
new Vector2f(i * stepX + stepX, j * stepY),
new Vector2f(i * stepX, j * stepY + stepY),
new Vector2f(i * stepX + stepX, j * stepY + stepY)));
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment