Skip to content

Commit

Permalink
[#133] Use center of model for offset correction
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielxs01 committed Mar 3, 2024
1 parent 56186ca commit 127ffac
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
23 changes: 12 additions & 11 deletions src/main/java/net/landofrails/api/contentpacks/v2/flares/Flare.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public void setObjPath(String objPath) {
}

public String[] getObjGroups() {
if(objGroups == null)
objGroups = new String[0];
return objGroups;
}

Expand Down Expand Up @@ -196,10 +198,10 @@ public float[] getRenderColor(){
return rgb;
}

public void savePrecalculatedData(Map<String, OBJGroup> flareGroups, double scale, Vec3d modelOffset, Vec3d flareCenterOffset, Vec3d combinedOffset) {
public void savePrecalculatedData(Map<String, OBJGroup> flareGroups, Vec3d scale, double lampScale, Vec3d offset, Vec3d rotation) {
if(this.precalculatedData != null)
return;
this.precalculatedData = new PrecalculatedData(flareGroups, scale, modelOffset, flareCenterOffset, combinedOffset);
this.precalculatedData = new PrecalculatedData(flareGroups, scale, lampScale, offset, rotation);
}

public PrecalculatedData getPrecalculatedData() {
Expand All @@ -210,23 +212,22 @@ public class PrecalculatedData {

public final Map<String, OBJGroup> flareGroups;

public final double scale;
public final Vec3d scale;

public final Vec3d modelOffset;
public final double lampScale;

public final Vec3d flareCenterOffset;
public final Vec3d rotation;

public final Vec3d combinedOffset;
public final Vec3d offset;

public PrecalculatedData(Map<String, OBJGroup> flareGroups, double scale, Vec3d modelOffset, Vec3d flareCenterOffset, Vec3d combinedOffset){
public PrecalculatedData(Map<String, OBJGroup> flareGroups, Vec3d scale, double lampScale, Vec3d offset, Vec3d rotation){
this.flareGroups = flareGroups;
this.scale = scale;
this.modelOffset = modelOffset;
this.flareCenterOffset = flareCenterOffset;
this.combinedOffset = combinedOffset;
this.lampScale = lampScale;
this.offset = offset;
this.rotation = rotation;
}


}

}
8 changes: 4 additions & 4 deletions src/main/java/net/landofrails/landofsignals/LOSBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public static void register() {
"red"
});
light_flare.setFlares(new Flare[]{
new Flare("ul", "165;32;25", 180, 0, 0.426f, false, new String[]{"red"}),
new Flare("ll", "229;189;1", 180, 0, 0.426f, false, new String[]{"yellow"})
new Flare("ul", "165;32;25", 180, 0, 0.019f, false, new String[]{"red"}),
new Flare("ll", "229;189;1", 180, 0, 0.019f, false, new String[]{"yellow"})
});
registerSignalContentPack(light_flare);

Expand All @@ -137,10 +137,10 @@ public static void register() {
registerSignContentPack("block_sign_part_gsar_sh2_full_wo_light", "GSAR Schutzsignal SH2 (Full, w/o light)", false, Collections.singletonMap("models/block/landofsignals/signs/gsar/sh2/signalsh2.obj", new ContentPackModel[]{new ContentPackModel(new float[]{0.5f, 0f, 0.5f}, new float[]{1f, 1f, 1f}, new float[]{1f, 1f, 1f}, new float[]{0.5f, 0.5f, 0.5f}, new String[]{"Sign01_SI01", "MetalRodLong_MR02"})}));
registerSignContentPack("block_sign_part_gsar_sh2_top_wo_light", "GSAR Schutzsignal SH2 (Top, w/o light)", false, Collections.singletonMap("models/block/landofsignals/signs/gsar/sh2/signalsh2.obj", new ContentPackModel[]{new ContentPackModel(new float[]{0.5f, 0f, 0.5f}, new float[]{0.5f, 0f, 0.5f}, new float[]{1f, 1f, 1f}, new String[]{"Sign01_SI01", "MetalRod_MR01"})}));
registerSignContentPack("block_sign_part_gsar_sh2_full_w_light", "GSAR Schutzsignal SH2 (Full, with light)", false, Collections.singletonMap("models/block/landofsignals/signs/gsar/sh2/signalsh2.obj", new ContentPackModel[]{new ContentPackModel(new float[]{0.5f, 0f, 0.5f}, new float[]{1f, 1f, 1f}, new float[]{1f, 1f, 1f}, new float[]{0.5f, 0.5f, 0.5f}, new String[]{"Sign01_SI01", "MetalRodLong_MR02", "RedLightOn_RLOn", "RedLightOff_RLOff"})}), new Flare[]{
new Flare("RedLightOn_RLOn", "#FF0000", 180,0, 0.0025f, true)
new Flare("RedLightOn_RLOn", "#FF0000", 180,0, 0.001f, true)
});
registerSignContentPack("block_sign_part_gsar_sh2_top_w_light", "GSAR Schutzsignal SH2 (Top, with light)", false, Collections.singletonMap("models/block/landofsignals/signs/gsar/sh2/signalsh2.obj", new ContentPackModel[]{new ContentPackModel(new float[]{0.5f, 0f, 0.5f}, new float[]{0.5f, 0f, 0.5f}, new float[]{1f, 1f, 1f}, new String[]{"Sign01_SI01", "MetalRod_MR01", "RedLightOn_RLOn", "RedLightOff_RLOff"})}), new Flare[]{
new Flare("RedLightOn_RLOn", "#FF0000", 180,0, 0.0925f, true)
new Flare("RedLightOn_RLOn", "#FF0000", 180,0, 0.001f, true)
});

// Signs: Hecto Signs
Expand Down
47 changes: 33 additions & 14 deletions src/main/java/net/landofrails/landofsignals/utils/FlareUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,16 @@ public static void renderFlares(Flare[] flares, Vec3i pos, int blockRotate, Vec3
float green = flare.getRenderColor()[1];
float blue = flare.getRenderColor()[2];

int flareRotation = flare.getRotation();
int flarePitch = flare.getPitch();
Vec3d flareRotation = flare.getPrecalculatedData().rotation;

double scale = flare.getPrecalculatedData().scale;
double scale = flare.getPrecalculatedData().lampScale;

// Translation and Intensity calculations

Vec3d playerOffset = VecUtil.rotateWrongYaw(
new Vec3d(pos).subtract(MinecraftClient.getPlayer().getPosition()),
blockRotate + 180).
subtract(flare.getPrecalculatedData().combinedOffset);
subtract(flare.getPrecalculatedData().offset);

int viewAngle = 45;
float intensity = 1 - Math.abs(Math.max(-viewAngle, Math.min(viewAngle, VecUtil.toWrongYaw(playerOffset) - 90))) / viewAngle;
Expand All @@ -177,14 +176,13 @@ public static void renderFlares(Flare[] flares, Vec3i pos, int blockRotate, Vec3
.alpha_test(false).blend(new BlendMode(BlendMode.GL_SRC_ALPHA, BlendMode.GL_ONE_MINUS_SRC_ALPHA));

flareState.scale(scaling);
flareState.translate(offset);

flareState.translate(flare.getPrecalculatedData().flareCenterOffset);

flareState.rotate(flarePitch, 0, 0, 1);
flareState.rotate((double) blockRotate + flareRotation,0,1,0);
flareState.translate(offset);
flareState.translate(flare.getPrecalculatedData().offset);

flareState.translate(flare.getPrecalculatedData().modelOffset); // move it towards the position of the light flare
flareState.rotate(flareRotation.x, 1,0,0);
flareState.rotate(flareRotation.y + blockRotate,0,1,0);
flareState.rotate(flareRotation.z, 0,0, 1);

// Moving flare

Expand Down Expand Up @@ -234,9 +232,9 @@ private static void cacheFlare(Flare flare, String flareId, OBJModel model, Stri
double maxX = flareGroupsOBJGroups.stream().mapToDouble(g -> g.max.x).max().getAsDouble();
double minX = flareGroupsOBJGroups.stream().mapToDouble(g -> g.min.x).min().getAsDouble();

double scale = Math.max((maxZ - minZ) * modelScaling[2], (maxX - minX) * modelScaling[0]);
double lampScale = Math.max((maxZ - minZ) * modelScaling[2], (maxX - minX) * modelScaling[0]);

//
/*
float flareOffset = flare.getOffset();
Set<String> groups = flare.getObjGroups() != null && flare.getObjGroups().length > 0
Expand All @@ -250,9 +248,30 @@ private static void cacheFlare(Flare flare, String flareId, OBJModel model, Stri
Vec3d flareCenterOffset = new Vec3d(modelTranslation[0], modelTranslation[1] + yCorrection, modelTranslation[2]);
Vec3d combinedOffset = flareCenterOffset.add(modelOffset);
//
*/

// Scaling for the flare from the block in the contentpack
Vec3d scaling = new Vec3d(modelScaling[0], modelScaling[1], modelScaling[2]);

Set<String> groups = flare.getObjGroups().length > 0
? Arrays.stream(flare.getObjGroups()).collect(Collectors.toSet())
: model.groups();
Vec3d centerOfModel = model.centerOfGroups(groups);

double xCorrection = centerOfModel.x;
double yCorrection = centerOfModel.y;
double zCorrection= -(centerOfModel.z + flare.getOffset());
Vec3d offset = new Vec3d(modelTranslation[0] + xCorrection, modelTranslation[1] + yCorrection, modelTranslation[2] + zCorrection);

Vec3d centerOfLightFlare = model.centerOfGroups(flareGroups.keySet());
Vec3d modelOffset = centerOfLightFlare.subtract(centerOfModel);
modelOffset = new Vec3d(modelOffset.x, modelOffset.y, -modelOffset.z);
offset = offset.add(modelOffset);

// Rotation for the flare from the contentpack
Vec3d rotation = new Vec3d(0, flare.getRotation(), flare.getPitch());

flare.savePrecalculatedData(flareGroups, scale, modelOffset, flareCenterOffset, combinedOffset);
flare.savePrecalculatedData(flareGroups, scaling, lampScale, offset, rotation);
}

}

0 comments on commit 127ffac

Please sign in to comment.