change: switch gradient object return from Colour to integer array for performance

This commit is contained in:
ZtereoHYPE 2022-12-10 04:11:04 +01:00
parent cf2a1cefc7
commit c10f87bd1a
4 changed files with 22 additions and 27 deletions

View file

@ -1,11 +1,10 @@
package codes.ztereohype.example.core; package codes.ztereohype.example.core;
import java.awt.*;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
public class Gradient { public class Gradient {
private final TreeMap<Double, Color> gradient; private final TreeMap<Double, int[]> gradient;
public Gradient() { public Gradient() {
this.gradient = new TreeMap<>(); this.gradient = new TreeMap<>();
@ -16,7 +15,7 @@ public class Gradient {
throw new IllegalArgumentException("Index must be between 0 and 1"); throw new IllegalArgumentException("Index must be between 0 and 1");
} }
Color color = new Color(red,green,blue); int[] color = {red, green, blue};
gradient.put(index, color); gradient.put(index, color);
} }
@ -28,12 +27,12 @@ public class Gradient {
gradient.clear(); gradient.clear();
} }
public Color getAt(double value) { public int[] getAt(double value) {
if (value < 0D || value > 1D) { if (value < 0D || value > 1D) {
throw new IllegalArgumentException("Value must be between 0 and 1"); throw new IllegalArgumentException("Value must be between 0 and 1");
} }
Map.Entry<Double, Color> floorEntry, ceilingEntry; Map.Entry<Double, int[]> floorEntry, ceilingEntry;
floorEntry = gradient.floorEntry(value); floorEntry = gradient.floorEntry(value);
if (floorEntry == null) { // we're under the lowest, return the lowest if (floorEntry == null) { // we're under the lowest, return the lowest
@ -48,13 +47,13 @@ public class Gradient {
double ratio = (value - floorEntry.getKey()) / (ceilingEntry.getKey() - floorEntry.getKey()); double ratio = (value - floorEntry.getKey()) / (ceilingEntry.getKey() - floorEntry.getKey());
double invRatio = 1 - ratio; double invRatio = 1 - ratio;
Color firstColor = floorEntry.getValue(); int[] firstColor = floorEntry.getValue();
Color secondColor = ceilingEntry.getValue(); int[] secondColor = ceilingEntry.getValue();
long red = Math.round(secondColor.getRed() * ratio + firstColor.getRed() * invRatio); long red = Math.round(secondColor[0] * ratio + firstColor[0] * invRatio);
long green = Math.round(secondColor.getGreen() * ratio + firstColor.getGreen() * invRatio); long green = Math.round(secondColor[1] * ratio + firstColor[1] * invRatio);
long blue = Math.round(secondColor.getBlue() * ratio + firstColor.getBlue() * invRatio); long blue = Math.round(secondColor[2] * ratio + firstColor[2] * invRatio);
return new Color((int)red, (int)green, (int)blue); return new int[]{(int) red, (int) green, (int) blue};
} }
} }

View file

@ -5,8 +5,6 @@ import net.minecraft.util.FastColor;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.level.levelgen.synth.PerlinNoise; import net.minecraft.world.level.levelgen.synth.PerlinNoise;
import java.awt.*;
public class StarSkyboxPainter extends SkyboxPainter { public class StarSkyboxPainter extends SkyboxPainter {
private static final float SCALING_FACTOR = 1.5f; private static final float SCALING_FACTOR = 1.5f;
private static final float BASE_NOISE_AMOUNT = 0.45f; // the amount of base noise to keep private static final float BASE_NOISE_AMOUNT = 0.45f; // the amount of base noise to keep
@ -47,8 +45,8 @@ public class StarSkyboxPainter extends SkyboxPainter {
double colourValue = Mth.clamp((alpha / 255D), 0D, 1D); double colourValue = Mth.clamp((alpha / 255D), 0D, 1D);
Color color = starryGradient.getAt(colourValue); int[] color = starryGradient.getAt(colourValue);
return FastColor.ARGB32.color(alpha, color.getBlue(), color.getGreen(), color.getRed()); return FastColor.ARGB32.color(alpha, color[2], color[1], color[0]);
} }
} }

View file

@ -1,10 +1,9 @@
package codes.ztereohype.example.sky.star; package codes.ztereohype.example.sky.star;
import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.BufferBuilder;
import lombok.Getter;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import java.awt.*;
public class Star { public class Star {
private final float xCoord; private final float xCoord;
private final float yCoord; private final float yCoord;
@ -27,12 +26,12 @@ public class Star {
private final int b; private final int b;
private float currentAngle; private float currentAngle;
private float currentRadius; private @Getter float currentRadius;
public Star(float randX, float randY, float randZ, float size, Color color, float resizeSpeed, float spinSpeed) { public Star(float randX, float randY, float randZ, float size, int[] color, float resizeSpeed, float spinSpeed) {
this.r = color.getRed(); this.r = color[0];
this.g = color.getGreen(); this.g = color[1];
this.b = color.getBlue(); this.b = color[2];
float invsqrtDistance = Mth.fastInvSqrt(randX * randX + randY * randY + randZ * randZ); float invsqrtDistance = Mth.fastInvSqrt(randX * randX + randY * randY + randZ * randZ);
this.xCoord = randX * invsqrtDistance * 100.0F; this.xCoord = randX * invsqrtDistance * 100.0F;

View file

@ -5,7 +5,6 @@ import com.mojang.blaze3d.vertex.*;
import net.minecraft.util.RandomSource; import net.minecraft.util.RandomSource;
import net.minecraft.world.level.levelgen.synth.ImprovedNoise; import net.minecraft.world.level.levelgen.synth.ImprovedNoise;
import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
public class Starbox { public class Starbox {
@ -37,7 +36,7 @@ public class Starbox {
float resizeSpeed = 0.03f + randomSource.nextFloat() * 0.04f; float resizeSpeed = 0.03f + randomSource.nextFloat() * 0.04f;
float spinSpeed = randomSource.nextFloat() * 0.02f - 0.01f; float spinSpeed = randomSource.nextFloat() * 0.02f - 0.01f;
Color starColor = starGradient.getAt(randomSource.nextFloat()); int[] starColor = starGradient.getAt(randomSource.nextFloat());
float starRadius = 0.15F + randomSource.nextFloat() * 0.15F; float starRadius = 0.15F + randomSource.nextFloat() * 0.15F;
double starValue = noise.noise(randX * 2.5f, randY * 2.5f, randZ * 2.5f) + 0.5; double starValue = noise.noise(randX * 2.5f, randY * 2.5f, randZ * 2.5f) + 0.5;