Skip to content

Commit ddb71df

Browse files
committed
v0.14.16.1 - ColorMap optimizations & Client Config
Took 1 hour 28 minutes
1 parent c09407f commit ddb71df

6 files changed

Lines changed: 135 additions & 26 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ parchment_mappings_version=2024.11.17
1717
mod_id=pmweatherapi
1818
mod_name=PMWeatherAPI
1919
mod_license=GNU GPL 3.0
20-
mod_version=0.14.16.0
20+
mod_version=0.14.16.1
2121
mod_group_id=net.nullved
2222
mod_authors=NullVed
2323
mod_description=An API for interfacing with ProtoManly's Weather Mod

src/main/java/net/nullved/pmweatherapi/PMWeatherAPI.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package net.nullved.pmweatherapi;
22

33
import com.mojang.logging.LogUtils;
4+
import dev.protomanly.pmweather.compat.DistantHorizons;
5+
import dev.protomanly.pmweather.config.ClientConfig;
46
import net.minecraft.resources.ResourceLocation;
57
import net.neoforged.bus.api.IEventBus;
68
import net.neoforged.fml.ModContainer;
79
import net.neoforged.fml.ModList;
810
import net.neoforged.fml.common.Mod;
11+
import net.neoforged.fml.config.ModConfig;
912
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
1013
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
14+
import net.neoforged.fml.loading.FMLEnvironment;
15+
import net.neoforged.neoforge.client.gui.ConfigurationScreen;
16+
import net.neoforged.neoforge.client.gui.IConfigScreenFactory;
1117
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
1218
import net.nullved.pmweatherapi.client.render.RadarOverlays;
19+
import net.nullved.pmweatherapi.config.PMWClientConfig;
1320
import net.nullved.pmweatherapi.example.ExampleOverlay;
1421
import net.nullved.pmweatherapi.network.PMWNetworking;
1522
import org.slf4j.Logger;
@@ -26,6 +33,11 @@ public PMWeatherAPI(IEventBus modEventBus, ModContainer modContainer) {
2633
modEventBus.addListener(this::registerPayloads);
2734

2835
LOGGER.info("Initialized PMWAPI");
36+
37+
if (FMLEnvironment.dist.isClient()) {
38+
modContainer.registerConfig(ModConfig.Type.CLIENT, PMWClientConfig.SPEC);
39+
modContainer.registerExtensionPoint(IConfigScreenFactory.class, ConfigurationScreen::new);
40+
}
2941
}
3042

3143
private void commonSetup(FMLCommonSetupEvent event) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package net.nullved.pmweatherapi.config;
2+
3+
import net.neoforged.api.distmarker.Dist;
4+
import net.neoforged.bus.api.SubscribeEvent;
5+
import net.neoforged.fml.common.EventBusSubscriber;
6+
import net.neoforged.fml.event.config.ModConfigEvent;
7+
import net.neoforged.neoforge.common.ModConfigSpec;
8+
import net.nullved.pmweatherapi.PMWeatherAPI;
9+
10+
@EventBusSubscriber(modid = PMWeatherAPI.MODID, bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
11+
public class PMWClientConfig {
12+
private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();
13+
14+
private static final ModConfigSpec.BooleanValue DISABLE_CUSTOM_RADAR_MODE_RENDERING;
15+
public static boolean disableCustomRadarModeRendering;
16+
public static final ModConfigSpec SPEC;
17+
18+
@SubscribeEvent
19+
private static void onLoad(ModConfigEvent event) {
20+
if (event.getConfig().getSpec() == SPEC && !(event instanceof ModConfigEvent.Unloading)) {
21+
PMWeatherAPI.LOGGER.info("Loading Client PMWeatherAPI Configs");
22+
disableCustomRadarModeRendering = DISABLE_CUSTOM_RADAR_MODE_RENDERING.getAsBoolean();
23+
}
24+
}
25+
26+
static {
27+
DISABLE_CUSTOM_RADAR_MODE_RENDERING = BUILDER.comment("Disables custom radar mode rendering").define("disable_custom_radar_mode_rendering", false);
28+
SPEC = BUILDER.build();
29+
}
30+
}

src/main/java/net/nullved/pmweatherapi/mixin/RadarRendererMixin.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import net.nullved.pmweatherapi.client.render.PixelRenderData;
3535
import net.nullved.pmweatherapi.client.render.RadarOverlays;
3636
import net.nullved.pmweatherapi.client.render.RenderData;
37+
import net.nullved.pmweatherapi.config.PMWClientConfig;
3738
import net.nullved.pmweatherapi.data.PMWExtras;
3839
import net.nullved.pmweatherapi.radar.RadarMode;
3940
import org.joml.Matrix4fStack;
@@ -328,20 +329,23 @@ private void render(BlockEntity blockEntity, float partialTicks, PoseStack poseS
328329
radarBlockEntity.velocityMap.put(longID, vel);
329330

330331
// PMWeatherAPI: Support custom radar modes
331-
PixelRenderData pixelRenderData = new PixelRenderData(canRender, dbz * 60.0F, vel, temp, x, z, resolution, renderData);
332-
color = radarMode.getColorForPixel(pixelRenderData);
333-
PMWClientStorages.RADAR_MODE_COLORS.get(radarMode).put(id, color);
332+
if (!PMWClientConfig.disableCustomRadarModeRendering) {
333+
PixelRenderData pixelRenderData = new PixelRenderData(canRender, dbz * 60.0F, vel, temp, x, z, resolution, renderData);
334+
color = radarMode.getColorForPixel(pixelRenderData);
335+
PMWClientStorages.RADAR_MODE_COLORS.get(radarMode).put(id, color);
336+
}
334337
}
335338

336339
float rdbz = dbz * 60.0F;
337340

338-
// Color color = ColorTables.getReflectivity(rdbz);
339-
// RadarBlock.Mode mode = blockEntity.getBlockState().getValue(RadarBlock.RADAR_MODE);
340-
// if (mode == dev.protomanly.pmweather.block.RadarBlock.Mode.VELOCITY) {
341-
// color = new Color(0, 0, 0);
342-
// vel /= 1.75F;
343-
// color = ColorTables.lerp(Mth.clamp(Math.max(rdbz, (Mth.abs(vel) - 18.0F) / 0.65F) / 12.0F, 0.0F, 1.0F), color, ColorTables.getVelocity(vel));
344-
// }
341+
if (PMWClientConfig.disableCustomRadarModeRendering) {
342+
color = ColorTables.getReflectivity(rdbz);
343+
if (radarMode == RadarMode.VELOCITY) {
344+
color = new Color(0, 0, 0);
345+
vel /= 1.75F;
346+
color = ColorTables.lerp(Mth.clamp(Math.max(rdbz, (Mth.abs(vel) - 18.0F) / 0.65F) / 12.0F, 0.0F, 1.0F), color, ColorTables.getVelocity(vel));
347+
}
348+
}
345349

346350
if (ClientConfig.radarDebugging && update) {
347351
if (clientRadarMode == ClientConfig.RadarMode.TEMPERATURE) {

src/main/java/net/nullved/pmweatherapi/util/ColorMap.java

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,28 @@
1616
*/
1717
public class ColorMap {
1818
private final boolean overrideModeGreater;
19-
private final List<LerpSegment> segments;
19+
private final NavigableMap<Float, LerpSegment> segments;
2020
private final NavigableMap<Float, Color> overridePoints;
2121
private final Color base;
2222

23-
private ColorMap(Color base, boolean overrideModeGreater, List<LerpSegment> segments, NavigableMap<Float, Color> overridePoints) {
23+
private final float min;
24+
private final float max;
25+
private Color[] lookup;
26+
private float resolution;
27+
28+
private ColorMap(Color base, boolean overrideModeGreater, List<LerpSegment> segments, NavigableMap<Float, Color> overridePoints, float resolution) {
29+
this.min = Math.round(segments.getFirst().start / resolution) * resolution;
30+
this.max = Math.round(segments.getLast().end / resolution) * resolution;
31+
2432
this.base = base;
2533
this.overrideModeGreater = overrideModeGreater;
26-
this.segments = segments;
34+
this.segments = new TreeMap<>();
35+
for (LerpSegment seg : segments) {
36+
this.segments.put(seg.start, seg);
37+
}
2738
this.overridePoints = overridePoints;
39+
40+
recomputeLookups(resolution);
2841
}
2942

3043
/**
@@ -33,7 +46,7 @@ private ColorMap(Color base, boolean overrideModeGreater, List<LerpSegment> segm
3346
* @since 0.14.15.6
3447
*/
3548
public float minValue() {
36-
return segments.getFirst().start;
49+
return min;
3750
}
3851

3952
/**
@@ -42,26 +55,65 @@ public float minValue() {
4255
* @since 0.14.15.6
4356
*/
4457
public float maxValue() {
45-
return segments.getLast().end;
58+
return max;
59+
}
60+
61+
/**
62+
* Recomputes the lookup table with the given resolution.
63+
* @param resolution The new resolution
64+
* @since 0.14.16.1
65+
*/
66+
public void recomputeLookups(float resolution) {
67+
this.resolution = resolution;
68+
int size = (int) (((max - min) / resolution) + 1);
69+
this.lookup = new Color[size];
70+
71+
for (int i = 0; i < size; i++) {
72+
float val = min + i * resolution;
73+
lookup[i] = getAccurate(val);
74+
}
4675
}
4776

4877
/**
49-
* Gets the {@link Color} for the specific value
78+
* Retrieves the color value using the closest value from the lookup table.
79+
* If you need the accurate value, use {@link #getAccurate(float)} instead
80+
* @param val The value to get a color for
81+
* @return The approximate color for this value
82+
* @since 0.14.16.1
83+
* @see #getAccurate(float)
84+
*/
85+
public Color get(float val) {
86+
float newVal = Math.round(val / resolution) * resolution;
87+
if (newVal <= min) return lookup[0];
88+
if (newVal >= max) return lookup[lookup.length - 1];
89+
int idx = (int) ((newVal - min) / resolution);
90+
return lookup[idx];
91+
}
92+
93+
/**
94+
* Gets the {@link Color} for the specific value.
95+
* <br>
96+
* This method is <strong>SLOWER</strong> and oftentimes the same as {@link #get(float)}.
97+
* Use that instead if you only need the approximate value
5098
* @param val The value to get the {@link Color} of
5199
* @return The {@link Color} for the given value
52100
* @since 0.14.15.6
101+
* @see #get(float)
53102
*/
54-
public Color get(float val) {
103+
public Color getAccurate(float val) {
55104
Color currentColor = base;
56105

57106
Map.Entry<Float, Color> override = overridePoints.floorEntry(val);
58107
if (override != null) {
59108
currentColor = override.getValue();
60109
}
61110

62-
for (LerpSegment seg : segments) {
63-
if (val >= seg.start && (overrideModeGreater ? val <= seg.end : val < seg.end)) {
111+
Map.Entry<Float, LerpSegment> entry = segments.floorEntry(val);
112+
if (entry != null) {
113+
LerpSegment seg = entry.getValue();
114+
if (overrideModeGreater ? val <= seg.end : val < seg.end) {
64115
float delta = (val - seg.start) / (seg.end - seg.start);
116+
delta = delta > 1 ? 1 : delta < 0 ? 0 : delta;
65117
return lerp(delta, seg.from, seg.to);
66118
}
67119
}
@@ -78,7 +130,6 @@ public Color get(float val) {
78130
* @since 0.14.15.6
79131
*/
80132
public static Color lerp(float delta, Color c1, Color c2) {
81-
delta = Math.max(0.0F, Math.min(1.0F, delta));
82133
float r = c1.getRed() + delta * (c2.getRed() - c1.getRed());
83134
float g = c1.getGreen() + delta * (c2.getGreen() - c1.getGreen());
84135
float b = c1.getBlue() + delta * (c2.getBlue() - c1.getBlue());
@@ -104,7 +155,7 @@ public static class Builder {
104155
private final NavigableMap<Float, Color> overridePoints = new TreeMap<>();
105156
private final Color base;
106157
private boolean overrideModeGreater = false;
107-
private float lastThreshold = Float.NEGATIVE_INFINITY;
158+
private float lastThreshold = Float.NEGATIVE_INFINITY, resolution = 0.1F;
108159
private Color lastColor;
109160

110161
private Builder(Color base) {
@@ -122,6 +173,18 @@ public static Builder of(Color base) {
122173
return new Builder(base);
123174
}
124175

176+
/**
177+
* Sets the step size between each value in the lookup table.
178+
* A value too small may be storing the same color multiple times!
179+
* @param resolution The resolution of the lookup table. Default 0.1F
180+
* @return This {@link Builder}
181+
* @since 0.14.16.1
182+
*/
183+
public Builder lookupResolution(float resolution) {
184+
this.resolution = resolution;
185+
return this;
186+
}
187+
125188
/**
126189
* Use in cases where you want overrides to only apply when the value is greater, but not equal to the threshold.
127190
* Used in {@link ColorMaps#POSITIVE_VELOCITY} and {@link ColorMaps#NEGATIVE_VELOCITY}
@@ -182,7 +245,7 @@ public ColorMap build(Color finalColor, float finalThreshold) {
182245
else overridePoints.put(finalThreshold, finalColor);
183246

184247
segments.add(new LerpSegment(lastThreshold, lastColor, finalThreshold, finalColor));
185-
return new ColorMap(base, overrideModeGreater, segments, overridePoints);
248+
return new ColorMap(base, overrideModeGreater, segments, overridePoints, resolution);
186249
}
187250
}
188251
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"itemGroup.pmweatherapi": "Example Mod Tab",
3-
"block.pmweatherapi.example_block": "Example Block",
4-
"item.pmweatherapi.example_item": "Example Item",
2+
"pmweatherapi.configuration.disable_custom_radar_mode_rendering": "Disable Custom Radar Mode Rendering",
3+
"pmweatherapi.configuration.disable_custom_radar_mode_rendering.tooltip": "Disables all custom radar modes from rendering. Falls back to PMWeather for Reflectivity and Velocity. Any custom radar modes will not appear, but still be cycled through",
4+
55
"commands.pmweatherapi.non_player": "You must be a player to execute this command!"
66
}

0 commit comments

Comments
 (0)