Skip to content

Commit

Permalink
feat: add test case to show tile alias
Browse files Browse the repository at this point in the history
  • Loading branch information
闫茂源 committed Apr 21, 2024
1 parent 3050322 commit 9c924da
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,16 @@ private void readAnimation(Tile tile, Node parent) {
*/
public void createVisual(Tileset tileset, TiledMap map) {

Point offset = tileset.getTileOffset();
Point tileOffset = tileset.getTileOffset();
Vector2f offset = new Vector2f(tileOffset.getX(), tileOffset.getY());
Vector2f origin = new Vector2f(0, map.getTileHeight());

List<Tile> tiles = tileset.getTiles();
for (Tile tile : tiles) {
String name = "tile#" + tileset.getFirstGid() + "#" + tile.getId();

Point coord = new Point(tile.getX(), tile.getY());
Point size = new Point(tile.getWidth(), tile.getHeight());
Vector2f coord = new Vector2f(tile.getX(), tile.getY());
Vector2f size = new Vector2f(tile.getWidth(), tile.getHeight());
TileMesh mesh = new TileMesh(coord, size, offset, origin);

Geometry geometry = new Geometry(name, mesh);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,19 @@ private TileMesh getTileMesh(MapObject obj) {
Tile tile = obj.getTile();
float tw = tile.getWidth();

Point coord = new Point(tile.getX(), tile.getY());
Point size = new Point(tile.getWidth(), tile.getHeight());
Point offset;
Vector2f coord = new Vector2f(tile.getX(), tile.getY());
Vector2f size = new Vector2f(tile.getWidth(), tile.getHeight());
Vector2f offset;
if (tile.getTileset() != null) {
Tileset tileset = tile.getTileset();
offset = tileset.getTileOffset();
Point tileOffset = tileset.getTileOffset();
offset = new Vector2f(tileOffset.getX(), tileOffset.getY());
// scale the tile
if (tileset.getFillMode() == FillMode.STRETCH) {
size.set((int) obj.getWidth(), (int) obj.getHeight());
}
} else {
offset = new Point(0, 0);
offset = new Vector2f(0, 0);
}

Vector2f origin = new Vector2f(0, 0);// In orthogonal, it's aligned to the bottom-left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*/
public class TileMesh extends Mesh {

private final Point coord;
private final Point size;
private final Point offset;
private final Vector2f coord;
private final Vector2f size;
private final Vector2f offset;
private final Vector2f origin;

/**
Expand All @@ -33,14 +33,14 @@ public class TileMesh extends Mesh {
* @param offset the offset of the tile
* @param origin the origin of the tile
*/
public TileMesh(Point coord, Point size, Point offset, Vector2f origin) {
public TileMesh(Vector2f coord, Vector2f size, Vector2f offset, Vector2f origin) {
this.coord = coord;
this.size = size;
this.offset = offset;
this.origin = origin;

int x = coord.getX();
int y = coord.getY();
float x = coord.getX();
float y = coord.getY();

float[] vertices = getPositions(size, offset, origin);

Expand Down Expand Up @@ -78,7 +78,7 @@ public TileMesh(Point coord, Point size, Point offset, Vector2f origin) {
* @param offset the offset of the tile
* @param origin the origin of the tile
*/
private float[] getPositions(Point size, Point offset, Vector2f origin) {
private float[] getPositions(Vector2f size, Vector2f offset, Vector2f origin) {
float[] vertices = new float[]{
0, 0, -1,
1, 0, -1,
Expand All @@ -93,7 +93,7 @@ private float[] getPositions(Point size, Point offset, Vector2f origin) {
return vertices;
}

public TileMesh(Point coord, Point size, Point offset, Vector2f origin, int gid, Orientation orientation) {
public TileMesh(Vector2f coord, Vector2f size, Vector2f offset, Vector2f origin, int gid, Orientation orientation) {
this(coord, size, offset, origin);

boolean isFlipHorizontally = (gid & Tile.FLIPPED_HORIZONTALLY_FLAG) != 0;
Expand Down Expand Up @@ -172,15 +172,15 @@ private void rotate(float rotate) {

this.setBuffer(VertexBuffer.Type.Position, 3, vertices);
}
public Point getCoord() {
public Vector2f getCoord() {
return coord;
}

public Point getSize() {
public Vector2f getSize() {
return size;
}

public Point getOffset() {
public Vector2f getOffset() {
return offset;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.github.jmecn.tiled.render;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.TextureKey;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.material.Material;
import com.jme3.math.*;
import com.jme3.scene.Geometry;
import com.jme3.system.AppSettings;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
import io.github.jmecn.tiled.render.shape.TileMesh;

import static io.github.jmecn.tiled.TiledConst.TILED_J3MD;

/**
* @author yanmaoyuan
*/
public class TestTiledShader extends SimpleApplication {

public static void main(String[] args) {
AppSettings settings = new AppSettings(true);
settings.setResolution(1280, 720);
settings.setFrameRate(60);
settings.setSamples(4);
settings.setGammaCorrection(false);

TestTiledShader app = new TestTiledShader();
app.setSettings(settings);
app.start();
}

private Geometry tile;

@Override
public void simpleInitApp() {
assetManager.registerLocator("examples", FileLocator.class);

viewPort.setBackgroundColor(ColorRGBA.Pink);

setupTile();

setupCamera();
}

private void setupTile() {

TextureKey key = new TextureKey("Orthogonal/perspective_walls.png", true);
key.setGenerateMips(false);

Texture2D texture = (Texture2D) assetManager.loadTexture(key);
texture.setMagFilter(Texture.MagFilter.Nearest);

Vector2f imageSize = new Vector2f(texture.getImage().getWidth(), texture.getImage().getHeight());// 256x256
TileMesh mesh = new TileMesh(new Vector2f(128, 0), new Vector2f(1, 1), new Vector2f(0, 0), new Vector2f(-0.5f, 0.5f));

// create material
Material mat = new Material(assetManager, TILED_J3MD);
mat.setTexture("ColorMap", texture);
mat.setVector2("ImageSize", imageSize);
mat.setBoolean("UseTilesetImage", true);
mat.setVector4("TileSize", new Vector4f(64, 64, 0, 0));

Geometry geom = new Geometry("Tile", mesh);
geom.setMaterial(mat);

tile = geom;
rootNode.attachChild(geom);
}

private void setupCamera() {
flyCam.setEnabled(false);

float near = -1f;
float far = 1f;
float ratio = (float) cam.getWidth() / cam.getHeight();
cam.setFrustum(near, far, -ratio, ratio, 1f, -1f);

cam.setParallelProjection(true);
cam.lookAtDirection(new Vector3f(0f, -1f, 0f), new Vector3f(0f, 0f, -1f));
cam.setLocation(new Vector3f(0f, 0, 0f));
}
}

0 comments on commit 9c924da

Please sign in to comment.