Add model element rotation to modellist

This commit is contained in:
Mike Primm 2022-01-23 22:33:47 -06:00
parent d8cf759671
commit ea79274938
6 changed files with 52 additions and 11 deletions

View File

@ -331,6 +331,7 @@ public class HDBlockModels {
private static class ModelBox { private static class ModelBox {
double[] from = new double[3]; double[] from = new double[3];
double[] to = new double[3]; double[] to = new double[3];
double xrot = 0, yrot = 0, zrot = 0;
ArrayList<ModelBoxSide> sides = new ArrayList<ModelBoxSide>(); ArrayList<ModelBoxSide> sides = new ArrayList<ModelBoxSide>();
}; };
@ -1017,11 +1018,11 @@ public class HDBlockModels {
databits.set(getIntValue(varvals,av[1])); databits.set(getIntValue(varvals,av[1]));
} }
else if(av[0].equals("box")) { else if(av[0].equals("box")) {
// box=fromx/y/z:tox/y/z:<side - upnsew>/<txtidx>/umin/vmin/umax/vmax>:... // box=from-x/y/z:to-x/y/z/rotx/roty/rotz:<side - upnsew>/<txtidx>/umin/vmin/umax/vmax>:...
String[] prms = av[1].split(":"); String[] prms = av[1].split(":");
ModelBox box = new ModelBox(); ModelBox box = new ModelBox();
if (prms.length > 0) { // Handle from if (prms.length > 0) { // Handle from (from-x/y/z)
String[] xyz = prms[0].split("/"); String[] xyz = prms[0].split("/");
if (xyz.length == 3) { if (xyz.length == 3) {
box.from[0] = Double.parseDouble(xyz[0]); box.from[0] = Double.parseDouble(xyz[0]);
@ -1032,12 +1033,17 @@ public class HDBlockModels {
Log.severe("Invalid modellist FROM value (" + prms[0] + " at line " + rdr.getLineNumber()); Log.severe("Invalid modellist FROM value (" + prms[0] + " at line " + rdr.getLineNumber());
} }
} }
if (prms.length > 1) { // Handle to if (prms.length > 1) { // Handle to (to-x/y/z or to-x/y/z/rotx/roty/rotz)
String[] xyz = prms[1].split("/"); String[] xyz = prms[1].split("/");
if (xyz.length == 3) { if (xyz.length >= 3) {
box.to[0] = Double.parseDouble(xyz[0]); box.to[0] = Double.parseDouble(xyz[0]);
box.to[1] = Double.parseDouble(xyz[1]); box.to[1] = Double.parseDouble(xyz[1]);
box.to[2] = Double.parseDouble(xyz[2]); box.to[2] = Double.parseDouble(xyz[2]);
if (xyz.length >= 6) { // If 6, second set are rotations (xrot/yrot/zrot)
box.xrot = Double.parseDouble(xyz[3]);
box.yrot = Double.parseDouble(xyz[4]);
box.zrot = Double.parseDouble(xyz[5]);
}
} }
else { else {
Log.severe("Invalid modellist TO value (" + prms[1] + " at line " + rdr.getLineNumber()); Log.severe("Invalid modellist TO value (" + prms[1] + " at line " + rdr.getLineNumber());
@ -1052,7 +1058,7 @@ public class HDBlockModels {
String face = flds[0]; String face = flds[0];
side.side = toBlockSide.get(face); side.side = toBlockSide.get(face);
if (side.side == null) { if (side.side == null) {
Log.severe("Invalid modellist side value (" + face + " at line " + rdr.getLineNumber()); Log.severe("Invalid modellist side value (" + face + ") at line " + rdr.getLineNumber());
continue; continue;
} }
} }
@ -1081,6 +1087,10 @@ public class HDBlockModels {
for (ModelBoxSide side : bl.sides) { for (ModelBoxSide side : bl.sides) {
PatchDefinition patch = pdf.getModelFace(bl.from, bl.to, side.side, side.uv, side.textureid); PatchDefinition patch = pdf.getModelFace(bl.from, bl.to, side.side, side.uv, side.textureid);
if (patch != null) { if (patch != null) {
// If any rotations, apply them here
if ((bl.xrot != 0) || (bl.yrot != 0) || (bl.zrot != 0)) {
patch = pdf.getPatch(patch, bl.xrot, bl.yrot, bl.zrot, patch.textureindex);
}
pd.add(patch); pd.add(patch);
} }
} }

View File

@ -17,6 +17,7 @@ public class ModelBlockModelImpl extends BlockModelImpl implements ModelBlockMod
private HashMap<BlockSide, ModelSide> sides = new HashMap<BlockSide, ModelSide>(); private HashMap<BlockSide, ModelSide> sides = new HashMap<BlockSide, ModelSide>();
private double[] from = { 0, 0, 0 }; private double[] from = { 0, 0, 0 };
private double[] to = { 16, 16, 16 }; private double[] to = { 16, 16, 16 };
private double xrot = 0, yrot = 0, zrot = 0;
@Override @Override
public void addBlockSide(BlockSide side, double[] uv, int textureid) { public void addBlockSide(BlockSide side, double[] uv, int textureid) {
ModelSide ms = new ModelSide(); ModelSide ms = new ModelSide();
@ -81,6 +82,9 @@ public class ModelBlockModelImpl extends BlockModelImpl implements ModelBlockMod
else { else {
for (ModelBlockImpl mb: boxes) { for (ModelBlockImpl mb: boxes) {
line += String.format(",box=%f/%f/%f:%f/%f/%f", mb.from[0], mb.from[1], mb.from[2], mb.to[0], mb.to[1], mb.to[2]); line += String.format(",box=%f/%f/%f:%f/%f/%f", mb.from[0], mb.from[1], mb.from[2], mb.to[0], mb.to[1], mb.to[2]);
if ((mb.xrot != 0) || (mb.yrot != 0) || (mb.zrot != 0)) { // If needed, add rotation
line += String.format("/%f/%f/%f", mb.xrot, mb.yrot, mb.zrot);
}
for (BlockSide bs : fromBlockSide.keySet()) { for (BlockSide bs : fromBlockSide.keySet()) {
String side = fromBlockSide.get(bs); String side = fromBlockSide.get(bs);
ModelSide mside = mb.sides.get(bs); ModelSide mside = mb.sides.get(bs);
@ -108,12 +112,16 @@ public class ModelBlockModelImpl extends BlockModelImpl implements ModelBlockMod
* *
* @param from - vector of lower left corner of box (0-16 range for coordinates - min x, y, z) * @param from - vector of lower left corner of box (0-16 range for coordinates - min x, y, z)
* @param to - vector of upper right corner of box (0-16 range for coordinates max x, y, z) * @param to - vector of upper right corner of box (0-16 range for coordinates max x, y, z)
* @param xrot - degrees of rotation of block around X
* @param yrot - degrees of rotation of block around Y
* @param zrot - degrees of rotation of block around Z
* @return model block to add faces to * @return model block to add faces to
*/ */
public ModelBlock addModelBlock(double[] from, double[] to) { public ModelBlock addModelBlock(double[] from, double[] to, double xrot, double yrot, double zrot) {
ModelBlockImpl mbi = new ModelBlockImpl(); ModelBlockImpl mbi = new ModelBlockImpl();
if (from != null) { mbi.from[0] = from[0]; mbi.from[1] = from[1]; mbi.from[2] = from[2]; } if (from != null) { mbi.from[0] = from[0]; mbi.from[1] = from[1]; mbi.from[2] = from[2]; }
if (to != null) { mbi.to[0] = to[0]; mbi.to[1] = to[1]; mbi.to[2] = to[2]; } if (to != null) { mbi.to[0] = to[0]; mbi.to[1] = to[1]; mbi.to[2] = to[2]; }
mbi.xrot = xrot; mbi.yrot = yrot; mbi.zrot = zrot;
boxes.add(mbi); boxes.add(mbi);
return mbi; return mbi;
} }

View File

@ -78,7 +78,7 @@ public class PatchDefinition implements RenderPatch {
* @param rotatez - z rotation in degrees * @param rotatez - z rotation in degrees
* @param textureindex - texture index for new patch (-1 = use same as original patch) * @param textureindex - texture index for new patch (-1 = use same as original patch)
*/ */
PatchDefinition(PatchDefinition orig, int rotatex, int rotatey, int rotatez, int textureindex) { PatchDefinition(PatchDefinition orig, double rotatex, double rotatey, double rotatez, int textureindex) {
Vector3D vec = new Vector3D(orig.x0, orig.y0, orig.z0); Vector3D vec = new Vector3D(orig.x0, orig.y0, orig.z0);
rotate(vec, rotatex, rotatey, rotatez); /* Rotate origin */ rotate(vec, rotatex, rotatey, rotatez); /* Rotate origin */
x0 = vec.x; y0 = vec.y; z0 = vec.z; x0 = vec.x; y0 = vec.y; z0 = vec.z;
@ -101,7 +101,7 @@ public class PatchDefinition implements RenderPatch {
update(); update();
} }
private void rotate(Vector3D vec, int xcnt, int ycnt, int zcnt) { private void rotate(Vector3D vec, double xcnt, double ycnt, double zcnt) {
vec.subtract(offsetCenter); /* Shoft to center of block */ vec.subtract(offsetCenter); /* Shoft to center of block */
/* Do X rotation */ /* Do X rotation */
double rot = Math.toRadians(xcnt); double rot = Math.toRadians(xcnt);

View File

@ -81,14 +81,19 @@ public class PatchDefinitionFactory implements RenderPatchFactory {
} }
} }
@Override
public RenderPatch getRotatedPatch(RenderPatch patch, double xrot, double yrot,
double zrot, int textureindex) {
return getPatch((PatchDefinition)patch, xrot, yrot, zrot, textureindex);
}
@Override @Override
public RenderPatch getRotatedPatch(RenderPatch patch, int xrot, int yrot, public RenderPatch getRotatedPatch(RenderPatch patch, int xrot, int yrot,
int zrot, int textureindex) { int zrot, int textureindex) {
return getPatch((PatchDefinition)patch, xrot, yrot, zrot, textureindex); return getPatch((PatchDefinition)patch, xrot, yrot, zrot, textureindex);
} }
public PatchDefinition getPatch(PatchDefinition patch, int xrot, int yrot, public PatchDefinition getPatch(PatchDefinition patch, double xrot, double yrot,
int zrot, int textureindex) { double zrot, int textureindex) {
PatchDefinition pd = new PatchDefinition(patch, xrot, yrot, zrot, textureindex); PatchDefinition pd = new PatchDefinition(patch, xrot, yrot, zrot, textureindex);
if(pd.validate() == false) if(pd.validate() == false)
return null; return null;

View File

@ -1,5 +1,7 @@
package org.dynmap.modsupport; package org.dynmap.modsupport;
import org.dynmap.modsupport.ModelBlockModel.ModelBlock;
// Model for more direct translation of MC models // Model for more direct translation of MC models
// All coordinates are 0-16 range per block, and 0-16 range for UV // All coordinates are 0-16 range per block, and 0-16 range for UV
@ -26,7 +28,10 @@ public interface ModelBlockModel extends BlockModel {
* *
* @param from - vector of lower left corner of box (0-16 range for coordinates - min x, y, z) * @param from - vector of lower left corner of box (0-16 range for coordinates - min x, y, z)
* @param to - vector of upper right corner of box (0-16 range for coordinates max x, y, z) * @param to - vector of upper right corner of box (0-16 range for coordinates max x, y, z)
* @param xrot - degrees of rotation of block around X
* @param yrot - degrees of rotation of block around Y
* @param zrot - degrees of rotation of block around Z
* @return model block to add faces to * @return model block to add faces to
*/ */
public ModelBlock addModelBlock(double[] from, double[] to); public ModelBlock addModelBlock(double[] from, double[] to, double xrot, double yrot, double zrot);
} }

View File

@ -85,6 +85,19 @@ public interface RenderPatchFactory {
*/ */
@Deprecated @Deprecated
public RenderPatch getPatch(double x0, double y0, double z0, double xu, double yu, double zu, double xv, double yv, double zv, double uplusvmax, SideVisible sidevis, int textureidx); public RenderPatch getPatch(double x0, double y0, double z0, double xu, double yu, double zu, double xv, double yv, double zv, double uplusvmax, SideVisible sidevis, int textureidx);
/**
* Get/create patch with given attributes.
*
* Generate from existing patch, after rotating xrot degrees around the X axis then yrot degrees around the Y axis, and then zrot degrees arond Z.
*
* @param patch - original patch
* @param xrot - degrees to rotate around X
* @param yrot - degrees to rotate around Y
* @param zrot - degrees to rotate around Z
* @param textureidx - texture index to be used for rotated patch (-1 means same as original patch)
* @return patch requested
*/
public RenderPatch getRotatedPatch(RenderPatch patch, double xrot, double yrot, double zrot, int textureidx);
/** /**
* Get/create patch with given attributes. * Get/create patch with given attributes.
* *