Skip to content

Commit

Permalink
[#133] Finished intensity calculation. Removed intensity and added of…
Browse files Browse the repository at this point in the history
…fset as parameter.
  • Loading branch information
Danielxs01 committed Dec 26, 2023
1 parent 2f7bc01 commit d2c0260
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ public class Flare {
* Required. Needs to be named after the element in the model.
*/
private String id;

/**
* Optional. Will set the color of the flare. Default: OxFFFFFF (white)
*/
private String color = "#FFFFFF";
/**
* Optional. Will set the intensity of the light flare. Range 0-2. Default 2.
*/
private float intensity = 2f;

/**
* Optional. Will always activate light flare. Default: false
Expand All @@ -33,18 +30,16 @@ public class Flare {
*/
private Map<String, String> groupStates;

public Flare(String id, String color, float intensity, boolean alwaysOn, String[] states) {
public Flare(String id, String color, boolean alwaysOn, String[] states) {
this.id = id;
this.color = color;
this.intensity = intensity;
this.alwaysOn = alwaysOn;
this.states = states;
}

public Flare(String id, String color, float intensity, boolean alwaysOn, Map<String, String> groupStates) {
public Flare(String id, String color, boolean alwaysOn, Map<String, String> groupStates) {
this.id = id;
this.color = color;
this.intensity = intensity;
this.alwaysOn = alwaysOn;
this.groupStates = groupStates;
}
Expand All @@ -65,14 +60,6 @@ public void setColor(String color) {
this.color = color;
}

public float getIntensity() {
return intensity;
}

public void setIntensity(float intensity) {
this.intensity = intensity;
}

public boolean isAlwaysOn() {
return alwaysOn;
}
Expand Down Expand Up @@ -100,13 +87,13 @@ public void setGroupStates(Map<String, String> groupStates) {
public float[] getRenderColor(){
float[] rgb = new float[3];

// 80;160;240 or 010:020:030 or 0-050-230
Predicate<String> isRGB = rawColor -> rawColor.matches("((\\d{1,3}[;:-]){2}\\d{1,3})");
// 33,66,99 or 80;160;240 or 010:020:030 or 0-050-230
Predicate<String> isRGB = rawColor -> rawColor.matches("((\\d{1,3}[,;:-]){2}\\d{1,3})");
// #123DEF or 0xFFAA00
Predicate<String> isHEX = rawColor -> rawColor.matches("(#|0x)[A-Z\\d]{6}");

if(isRGB.test(color)){
String[] rawColors = color.split("[;:-]");
String[] rawColors = color.split("[,;:-]");
rgb[0] = Integer.parseInt(rawColors[0]) / 255f;
rgb[1] = Integer.parseInt(rawColors[1]) / 255f;
rgb[2] = Integer.parseInt(rawColors[2]) / 255f;
Expand Down
4 changes: 2 additions & 2 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("lightflare_1", "165;32;25", 2f, false, new String[]{"red"}),
new Flare("lightflare_2", "229;189;1", 2f, false, new String[]{"yellow"})
new Flare("lightflare_1", "165;32;25", false, new String[]{"red"}),
new Flare("lightflare_2", "229;189;1", false, new String[]{"yellow"})
});
registerSignalContentPack(light_flare);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.landofrails.landofsignals.render.block;

import cam72cam.mod.MinecraftClient;
import cam72cam.mod.ModCore;
import cam72cam.mod.math.Vec3d;
import cam72cam.mod.model.obj.OBJGroup;
Expand All @@ -19,6 +20,7 @@
import net.landofrails.landofsignals.tile.TileSignalPart;
import net.landofrails.landofsignals.utils.HighlightingUtil;
import net.landofrails.landofsignals.utils.Static;
import net.landofrails.landofsignals.utils.VecUtil;

import java.util.*;
import java.util.function.Predicate;
Expand Down Expand Up @@ -202,6 +204,13 @@ private static void renderFlares(String id, ContentPackSignal signal, TileSignal
return matcher.group().replace("pitch", "");
};

Pattern offsetPattern = Pattern.compile("offset\\d{1,5}");
UnaryOperator<String> retrieveOffset = flareGroup -> {
Matcher matcher = offsetPattern.matcher(flareGroup);
matcher.find();
return matcher.group().replace("offset", "");
};

for(Flare flare : flares){
RenderState flareState = renderState.clone();
String flareId = flare.getId();
Expand All @@ -215,13 +224,34 @@ private static void renderFlares(String id, ContentPackSignal signal, TileSignal
float red = flare.getRenderColor()[0];
float green = flare.getRenderColor()[1];
float blue = flare.getRenderColor()[2];
float intensity = flare.getIntensity();

int flareRotation = Integer.parseInt(retrieveRotation.apply(flareGroup.getKey()));
int flarePitch = Integer.parseInt(retrievePitch.apply(flareGroup.getKey()));
float flareOffset = Float.parseFloat(retrieveOffset.apply(flareGroup.getKey())) / 1000f;

Identifier lightTex = new Identifier(LandOfSignals.MODID, "textures/light/antivignette.png");

double scale = Math.max(flareGroup.getValue().max.z - flareGroup.getValue().min.z, flareGroup.getValue().max.x - flareGroup.getValue().min.x);

// Translation and Intensity calculations

Vec3d centerOfModel = model.centerOfGroups(model.groups());
Vec3d centerOfLightFlare = model.centerOfGroups(Collections.singleton(flareGroup.getKey()));
Vec3d modelOffset = centerOfLightFlare.subtract(centerOfModel);
modelOffset = new Vec3d(modelOffset.x, modelOffset.y, -modelOffset.z - flareOffset);
Vec3d flareCenterOffset = new Vec3d(0.5f, 0.5f,0.5f); // Set position to center of block
Vec3d combinedOffset = flareCenterOffset.add(modelOffset);

Vec3d playerOffset = VecUtil.rotateWrongYaw(
new Vec3d(tile.getPos()).subtract(MinecraftClient.getPlayer().getPosition()),
tile.getBlockRotate() + 180).
subtract(combinedOffset);

int viewAngle = 45;
float intensity = 1 - Math.abs(Math.max(-viewAngle, Math.min(viewAngle, VecUtil.toWrongYaw(playerOffset) - 90))) / viewAngle;
intensity *= Math.abs(playerOffset.x/50);
intensity = Math.min(intensity, 1.5f);

//

flareState.texture(Texture.wrap(lightTex))
Expand All @@ -230,22 +260,35 @@ private static void renderFlares(String id, ContentPackSignal signal, TileSignal
.depth_mask(false)
.alpha_test(false).blend(new BlendMode(BlendMode.GL_SRC_ALPHA, BlendMode.GL_ONE_MINUS_SRC_ALPHA));

flareState.color((float)Math.sqrt(red), (float)Math.sqrt(green), (float)Math.sqrt(blue), 1 - (intensity/3f));

Vec3d centerOfModel = model.centerOfGroups(model.groups());
Vec3d centerOfLightFlare = model.centerOfGroups(Collections.singleton(flareGroup.getKey()));

Vec3d flareOffset = new Vec3d(0.5f, 0.5f,0.5f); // Set position to center of block
flareState.translate(flareOffset);
flareState.translate(flareCenterOffset);

flareState.rotate(flarePitch, 0, 0, 1);
flareState.rotate(tile.getBlockRotate() + flareRotation,0,1,0);
flareState.rotate((double) tile.getBlockRotate() + flareRotation,0,1,0);

Vec3d modelOffset = centerOfLightFlare.subtract(centerOfModel);
modelOffset = new Vec3d(modelOffset.x, modelOffset.y, -modelOffset.z - 0.45); // 0.45 implement custom offset?
flareState.translate(modelOffset); // move it towards the position of the light flare

double scale = Math.max(flareGroup.getValue().max.z - flareGroup.getValue().min.z, flareGroup.getValue().max.x - flareGroup.getValue().min.x);
// Moving flare

if (intensity > 0.01) {
RenderState matrix = flareState.clone();
matrix.translate(0, 0, -((intensity / 2) * 3));
double scale2 = Math.max(scale * 0.5, intensity * 2);
matrix.scale(scale2, scale2, scale2);

matrix.color(red, green, blue, 1 - (intensity/3f));

DirectDraw buffer = new DirectDraw();
buffer.vertex(-1, -1, 0).uv(0, 0);
buffer.vertex(-1, 1, 0).uv(0, 1);
buffer.vertex(1, 1, 0).uv(1, 1);
buffer.vertex(1, -1, 0).uv(1, 0);
buffer.draw(matrix);
}

//

flareState.color((float)Math.sqrt(red), (float)Math.sqrt(green), (float)Math.sqrt(blue), 1 - (intensity/3f));

flareState.scale(scale, scale, scale);

DirectDraw buffer = new DirectDraw();
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/net/landofrails/landofsignals/utils/VecUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.landofrails.landofsignals.utils;

import cam72cam.mod.math.Vec3d;
import cam72cam.mod.util.FastMath;
import util.Matrix4;

public class VecUtil {
private VecUtil() {
// Disable construction since java does not have static classes
}

public static Vec3d fromYaw(double distance, float yaw) {
return new Vec3d(Math.sin(Math.toRadians(yaw)) * distance, 0, Math.cos(Math.toRadians(yaw)) * distance);
}
public static float toYaw(Vec3d delta) {
float yaw = (float) Math.toDegrees(FastMath.atan2(delta.x, delta.z));
return (yaw + 360f) % 360f;
}
public static Vec3d rotateYaw(Vec3d pos, float rotationYaw) {
if (rotationYaw - 90 == 0) {
return pos;
}
//return new Matrix4().rotate(Math.toRadians(rotationYaw-90), 0, 1, 0).apply(pos);
double cos = Math.cos(Math.toRadians(rotationYaw - 90));
double sin = Math.sin(Math.toRadians(rotationYaw - 90));
return new Vec3d(
cos * pos.x + sin * pos.z,
pos.y,
-sin * pos.x + cos * pos.z
);
}
public static Vec3d rotatePitch(Vec3d pos, float rotationPitch) {
if (Math.abs(rotationPitch) < 0.01) {
return pos;
}
// TODO optimize me!
return new Matrix4().rotate(Math.toRadians(rotationPitch), 0, 0, 1).apply(pos);
}

public static Vec3d fromWrongYaw(double distance, float yaw) {
return new Vec3d(-Math.sin(Math.toRadians(yaw)) * distance, 0, Math.cos(Math.toRadians(yaw)) * distance);
}

public static float toWrongYaw(Vec3d delta) {
float yaw = (float) Math.toDegrees(FastMath.atan2(-delta.x, delta.z));
return (yaw + 360f) % 360f;
}
public static float toPitch(Vec3d delta) {
float yaw = (float) Math.toDegrees(FastMath.atan2(Math.sqrt(delta.z * delta.z + delta.x * delta.x), delta.y));
return (yaw + 360f) % 360f;
}

public static Vec3d rotateWrongYaw(Vec3d pos, float rotationYaw) {
return fromWrongYaw(pos.x, rotationYaw).add(fromWrongYaw(pos.z, rotationYaw + 90).add(0, pos.y, 0));
}

public static Vec3d fromWrongYawPitch(float distance, float rotationYaw, float rotationPitch) {
return fromWrongYaw(distance, rotationYaw).add(0, Math.tan(Math.toRadians(rotationPitch)) * distance, 0);
}

public static Vec3d between(Vec3d front, Vec3d rear) {
return new Vec3d((front.x + rear.x) / 2, (front.y + rear.y) / 2, (front.z + rear.z) / 2);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,7 @@ f 160/712/712 155/713/713 153/714/714
usemtl m_2
f 154/715/715 156/716/716 157/717/717
f 156/718/718 159/719/719 157/720/720
o lightflare_1_rot180_pitch0
o lightflare_1_rot180_pitch0_offset440
v -0.12499999999999993 0.875 0.6625
v -0.12499999999999993 0.875 0.725
v -0.12499999999999993 0.625 0.6625
Expand Down Expand Up @@ -4059,7 +4059,7 @@ f 328/1468/1468 323/1469/1469 321/1470/1470
usemtl m_1
f 322/1471/1471 324/1472/1472 325/1473/1473
f 324/1474/1474 327/1475/1475 325/1476/1476
o lightflare_2_rot180_pitch0
o lightflare_2_rot180_pitch0_offset440
v -0.12499999999999993 0.375 0.6625
v -0.12499999999999993 0.375 0.725
v -0.12499999999999993 0.125 0.6625
Expand Down

0 comments on commit d2c0260

Please sign in to comment.