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;
import java.awt.*;
import java.util.Map;
import java.util.TreeMap;
public class Gradient {
private final TreeMap<Double, Color> gradient;
private final TreeMap<Double, int[]> gradient;
public Gradient() {
this.gradient = new TreeMap<>();
@ -15,8 +14,8 @@ public class Gradient {
if (index < 0 || index > 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);
}
@ -28,12 +27,12 @@ public class Gradient {
gradient.clear();
}
public Color getAt(double value) {
public int[] getAt(double value) {
if (value < 0D || value > 1D) {
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);
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 invRatio = 1 - ratio;
Color firstColor = floorEntry.getValue();
Color secondColor = ceilingEntry.getValue();
int[] firstColor = floorEntry.getValue();
int[] secondColor = ceilingEntry.getValue();
long red = Math.round(secondColor.getRed() * ratio + firstColor.getRed() * invRatio);
long green = Math.round(secondColor.getGreen() * ratio + firstColor.getGreen() * invRatio);
long blue = Math.round(secondColor.getBlue() * ratio + firstColor.getBlue() * invRatio);
long red = Math.round(secondColor[0] * ratio + firstColor[0] * invRatio);
long green = Math.round(secondColor[1] * ratio + firstColor[1] * 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.world.level.levelgen.synth.PerlinNoise;
import java.awt.*;
public class StarSkyboxPainter extends SkyboxPainter {
private static final float SCALING_FACTOR = 1.5f;
private static final float BASE_NOISE_AMOUNT = 0.45f; // the amount of base noise to keep
@ -41,14 +39,14 @@ public class StarSkyboxPainter extends SkyboxPainter {
// noise.getOctaveNoise(0).noiseWithDerivative(x * SCALING_FACTOR, y * SCALING_FACTOR, z * SCALING_FACTOR, derivates);
// double maxDerivative = Mth.clamp(Math.max(Math.max(derivates[0], derivates[1]), derivates[2]) * 0.5 + 0.5, 0, 0);
int alpha = (int)(Mth.clamp((noiseValue * (1D / BASE_NOISE_AMOUNT) - (1D / BASE_NOISE_AMOUNT - 1)) * 35D, 1D, 255.99D)); // otherwise death occurs
int alpha = (int) (Mth.clamp((noiseValue * (1D / BASE_NOISE_AMOUNT) - (1D / BASE_NOISE_AMOUNT - 1)) * 35D, 1D, 255.99D)); // otherwise death occurs
// alpha = (int) Mth.clamp(alpha - subtractionValue * 128, 0, 255); //todo subtract colour channels separately
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;
import com.mojang.blaze3d.vertex.BufferBuilder;
import lombok.Getter;
import net.minecraft.util.Mth;
import java.awt.*;
public class Star {
private final float xCoord;
private final float yCoord;
@ -27,12 +26,12 @@ public class Star {
private final int b;
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) {
this.r = color.getRed();
this.g = color.getGreen();
this.b = color.getBlue();
public Star(float randX, float randY, float randZ, float size, int[] color, float resizeSpeed, float spinSpeed) {
this.r = color[0];
this.g = color[1];
this.b = color[2];
float invsqrtDistance = Mth.fastInvSqrt(randX * randX + randY * randY + randZ * randZ);
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.world.level.levelgen.synth.ImprovedNoise;
import java.awt.*;
import java.util.ArrayList;
public class Starbox {
@ -37,10 +36,10 @@ public class Starbox {
float resizeSpeed = 0.03f + randomSource.nextFloat() * 0.04f;
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;
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;
float squaredDistance = randX * randX + randY * randY + randZ * randZ;
if (squaredDistance < 1.0 && squaredDistance > 0.01 && starValue > 0.2) {