new: lil refactor

This commit is contained in:
ZtereoHYPE 2022-09-11 13:38:02 +02:00
parent 498d17818d
commit d12988e914
11 changed files with 546 additions and 417 deletions

View file

@ -1,97 +1,97 @@
package codes.ztereohype.example; //package codes.ztereohype.example;
//
import codes.ztereohype.example.sky.Star; //import codes.ztereohype.example.sky.Star;
import com.mojang.blaze3d.vertex.*; //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 org.openjdk.jmh.annotations.*; //import org.openjdk.jmh.annotations.*;
//
import java.util.ArrayList; //import java.util.ArrayList;
import java.util.concurrent.TimeUnit; //import java.util.concurrent.TimeUnit;
//
@State(Scope.Benchmark) //@State(Scope.Benchmark)
public class StarUpdateBenchmark { //public class StarUpdateBenchmark {,
public ArrayList<Star> starList = new ArrayList<>(); // public ArrayList<Star> starList = new ArrayList<>();
public BufferBuilder starBufferBuilder = Tesselator.getInstance().getBuilder(); // public BufferBuilder starBufferBuilder = Tesselator.getInstance().getBuilder();
//
@Setup(Level.Trial) // @Setup(Level.Trial)
public void setUp() { // public void setUp() {
starBufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR); // starBufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
RandomSource randomSource = RandomSource.create(123L); // RandomSource randomSource = RandomSource.create(123L);
ImprovedNoise noise = new ImprovedNoise(randomSource); // ImprovedNoise noise = new ImprovedNoise(randomSource);
starList.clear(); // starList.clear();
//
for (int i = 0; i < 1700; ++i) { // for (int i = 0; i < 1700; ++i) {
// -1..1 // // -1..1
float randX = randomSource.nextFloat() * 2.0F - 1.0F; // float randX = randomSource.nextFloat() * 2.0F - 1.0F;
float randY = randomSource.nextFloat() * 2.0F - 1.0F; // float randY = randomSource.nextFloat() * 2.0F - 1.0F;
float randZ = randomSource.nextFloat() * 2.0F - 1.0F; // float randZ = randomSource.nextFloat() * 2.0F - 1.0F;
//
float twinkleSpeed = 0.03f + randomSource.nextFloat() * 0.04f; // float twinkleSpeed = 0.03f + randomSource.nextFloat() * 0.04f;
//
double[] starDerivatives = new double[] {0,0,0}; // double[] starDerivatives = new double[] {0,0,0};
float starValue = (float) noise.noiseWithDerivative(randX*3, randY*3, randZ*3, starDerivatives) * 0.5f + 0.5f; // float starValue = (float) noise.noiseWithDerivative(randX*3, randY*3, randZ*3, starDerivatives) * 0.5f + 0.5f;
//
float maxDerivative = (float) Math.max(Math.abs(starDerivatives[0]), Math.max(Math.abs(starDerivatives[1]), Math.abs(starDerivatives[2]))); //kinda normal distr around 1? // float maxDerivative = (float) Math.max(Math.abs(starDerivatives[0]), Math.max(Math.abs(starDerivatives[1]), Math.abs(starDerivatives[2]))); //kinda normal distr around 1?
float temperature = maxDerivative * 8000; // float temperature = maxDerivative * 8000;
//
// 0.15..0.25 ??? // // 0.15..0.25 ???
float starRadius = 0.15F + randomSource.nextFloat() * 0.15F; // float starRadius = 0.15F + randomSource.nextFloat() * 0.15F;
starRadius *= starValue; // starRadius *= starValue;
//
float squaredDistance = randX * randX + randY * randY + randZ * randZ; // float squaredDistance = randX * randX + randY * randY + randZ * randZ;
//
if (squaredDistance < 1.0 && squaredDistance > 0.01 && starRadius > 0.13) { // if (squaredDistance < 1.0 && squaredDistance > 0.01 && starRadius > 0.13) {
starList.add(new Star(randX, randY, randZ, starRadius, temperature, twinkleSpeed)); // starList.add(new Star(randX, randY, randZ, starRadius, temperature, twinkleSpeed));
} else --i; // } else --i;
} // }
} // }
//
@Warmup(iterations = 1) // @Warmup(iterations = 1)
@Measurement(iterations = 15, time = 2000, timeUnit = TimeUnit.MILLISECONDS) // @Measurement(iterations = 15, time = 2000, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS) // @OutputTimeUnit(TimeUnit.MILLISECONDS)
@Benchmark // @Benchmark
public void updateStars1() { // public void updateStars1() {
starBufferBuilder.clear(); // starBufferBuilder.clear();
for (Star star : starList) { // for (Star star : starList) {
star.tick(134); // star.tick(134);
star.setVertices(starBufferBuilder); // star.setVertices(starBufferBuilder);
} // }
} // }
//
@Warmup(iterations = 1) // @Warmup(iterations = 1)
@Measurement(iterations = 15, time = 2000, timeUnit = TimeUnit.MILLISECONDS) // @Measurement(iterations = 15, time = 2000, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS) // @OutputTimeUnit(TimeUnit.MILLISECONDS)
@Benchmark // @Benchmark
public void updateStars2() { // public void updateStars2() {
starBufferBuilder.clear(); // starBufferBuilder.clear();
starList.stream().parallel().forEach(star -> star.tick(134)); // starList.stream().parallel().forEach(star -> star.tick(134));
for (Star star : starList) { // for (Star star : starList) {
star.setVertices(starBufferBuilder); // star.setVertices(starBufferBuilder);
} // }
} // }
//
@Warmup(iterations = 1) // @Warmup(iterations = 1)
@Measurement(iterations = 15, time = 2000, timeUnit = TimeUnit.MILLISECONDS) // @Measurement(iterations = 15, time = 2000, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS) // @OutputTimeUnit(TimeUnit.MILLISECONDS)
@Benchmark // @Benchmark
public void updateStars3() { // public void updateStars3() {
starBufferBuilder.clear(); // starBufferBuilder.clear();
for (Star star : starList) { // for (Star star : starList) {
star.tick(134); // star.tick(134);
// star.setVerticesTwo(starBufferBuilder); //// star.setVerticesTwo(starBufferBuilder);
} // }
} // }
//
@Warmup(iterations = 1) // @Warmup(iterations = 1)
@Measurement(iterations = 15, time = 2000, timeUnit = TimeUnit.MILLISECONDS) // @Measurement(iterations = 15, time = 2000, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS) // @OutputTimeUnit(TimeUnit.MILLISECONDS)
@Benchmark // @Benchmark
public void updateStars4() { // public void updateStars4() {
starBufferBuilder.clear(); // starBufferBuilder.clear();
starList.stream().parallel().forEach(star -> star.tick(134)); // starList.stream().parallel().forEach(star -> star.tick(134));
for (Star star : starList) { // for (Star star : starList) {
// star.setVerticesTwo(starBufferBuilder); //// star.setVerticesTwo(starBufferBuilder);
} // }
} // }
} //}

View file

@ -1,8 +1,10 @@
package codes.ztereohype.example; package codes.ztereohype.example;
import codes.ztereohype.example.nebula.Skybox;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
public static Skybox nebulaSkybox;
public static boolean toggle = true; public static boolean toggle = true;
@Override @Override

View file

@ -0,0 +1,60 @@
package codes.ztereohype.example;
import java.awt.*;
import java.util.Map;
import java.util.TreeMap;
public class Gradient {
private final TreeMap<Double, Color> gradient;
public Gradient() {
this.gradient = new TreeMap<>();
}
public void add(double index, int red, int green, int blue) {
if (index < 0 || index > 1) {
throw new IllegalArgumentException("Index must be between 0 and 1");
}
Color color = new Color(red,green,blue);
gradient.put(index, color);
}
public void remove(double index) {
gradient.remove(index);
}
public void clear() {
gradient.clear();
}
public Color getAt(double value) {
if (value < 0D || value > 1D) {
throw new IllegalArgumentException("Value must be between 0 and 1");
}
Map.Entry<Double, Color> floorEntry, ceilingEntry;
floorEntry = gradient.floorEntry(value);
if (floorEntry == null) { // we're under the lowest, return the lowest
return gradient.firstEntry().getValue();
}
ceilingEntry = gradient.ceilingEntry(value);
if (ceilingEntry == null) { // we're over the highest, return the highest
return gradient.lastEntry().getValue();
}
double ratio = (value - floorEntry.getKey()) / (ceilingEntry.getKey() - floorEntry.getKey());
double invRatio = 1 - ratio;
Color firstColor = floorEntry.getValue();
Color 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);
return new Color((int)red, (int)green, (int)blue);
}
}

View file

@ -1,6 +1,5 @@
package codes.ztereohype.example.mixin; package codes.ztereohype.example.mixin;
import codes.ztereohype.example.ExampleMod;
import com.mojang.math.Vector3f; import com.mojang.math.Vector3f;
import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.LightTexture;
@ -18,25 +17,20 @@ public abstract class MixinLightTexutre {
locals = LocalCapture.CAPTURE_FAILHARD locals = LocalCapture.CAPTURE_FAILHARD
) )
private void injectWarmLight(float partialTicks, CallbackInfo ci, ClientLevel clientLevel, float f, float g, float h, float i, float j, float l, float k, Vector3f vector3f, float m, Vector3f vector3f2, int n, int o, float p, float q, float r, float s, float t, boolean bl, float v, Vector3f vector3f5) { private void injectWarmLight(float partialTicks, CallbackInfo ci, ClientLevel clientLevel, float f, float g, float h, float i, float j, float l, float k, Vector3f vector3f, float m, Vector3f vector3f2, int n, int o, float p, float q, float r, float s, float t, boolean bl, float v, Vector3f vector3f5) {
//todo: find a way to make a touch more saturated and brighter(?) Vector3f warmTint = new Vector3f(0.36F, 0.13F, -0.15F);
if (ExampleMod.toggle) {
Vector3f warmTint = new Vector3f(0.36F, 0.13F, -0.15F);
float warmness = o / 15f * // increase w/ blocklight float warmness = o / 15f * // increase w/ blocklight
(1f - vector3f.x() * (1 - n / 15f)) * // decrease in skylight w/ dayness (1f - vector3f.x() * (1 - n / 15f)) * // decrease in skylight w/ dayness
Math.min((15 - o) / 9f, 1f); // decrease for the 3 highest block light levels Math.min((15 - o) / 9f, 1f); // decrease for the 3 highest block light levels
warmTint.mul(warmness); warmTint.mul(warmness);
warmTint.add(1f, 1f, 1f); warmTint.add(1f, 1f, 1f);
Vector3f dramaticFactor = vector3f2.copy(); Vector3f dramaticFactor = vector3f2.copy();
dramaticFactor.mul(0.20f); dramaticFactor.mul(0.20f);
dramaticFactor.add(0.80f, 0.80f, 0.81f); dramaticFactor.add(0.80f, 0.80f, 0.81f);
vector3f2.mul(dramaticFactor.x(), dramaticFactor.y(), dramaticFactor.z()); vector3f2.mul(dramaticFactor.x(), dramaticFactor.y(), dramaticFactor.z());
vector3f2.mul(warmTint.x(), warmTint.y(), warmTint.z()); vector3f2.mul(warmTint.x(), warmTint.y(), warmTint.z());
// if (o == 0 && vector3f.x() < 0.1f) vector3f2.mul(0.96f, 0.96f, 1.05f); //todo: smoothly make night bluer no ifs
}
} }
} }

View file

@ -1,7 +1,7 @@
package codes.ztereohype.example.mixin; package codes.ztereohype.example.mixin;
import codes.ztereohype.example.sky.StarManager; import codes.ztereohype.example.ExampleMod;
import com.mojang.blaze3d.systems.RenderSystem; import codes.ztereohype.example.star.SkyManager;
import com.mojang.blaze3d.vertex.*; import com.mojang.blaze3d.vertex.*;
import com.mojang.math.Matrix4f; import com.mojang.math.Matrix4f;
import net.minecraft.client.Camera; import net.minecraft.client.Camera;
@ -27,16 +27,16 @@ public abstract class MixinStarRendering {
@Inject(at = @At("HEAD"), method = "createStars", cancellable = true) @Inject(at = @At("HEAD"), method = "createStars", cancellable = true)
private void generateStars(CallbackInfo ci) { private void generateStars(CallbackInfo ci) {
StarManager.generateSky(); SkyManager.generateSky();
starBuffer = new VertexBuffer(); starBuffer = new VertexBuffer();
StarManager.updateStars(ticks, starBuffer); SkyManager.updateStars(ticks, starBuffer);
ci.cancel(); ci.cancel();
} }
@Inject(at = @At("HEAD"), method = "tick") @Inject(at = @At("HEAD"), method = "tick")
private void tickStars(CallbackInfo ci) { private void tickStars(CallbackInfo ci) {
if (this.level.getStarBrightness(0) < 0.0F) return; if (this.level.getStarBrightness(0) < 0.0F) return;
StarManager.updateStars(ticks, starBuffer); SkyManager.updateStars(ticks, starBuffer);
} }
@ModifyArg( @ModifyArg(
@ -53,58 +53,7 @@ public abstract class MixinStarRendering {
at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/RenderSystem;disableTexture()V", ordinal = 2, shift = At.Shift.AFTER), at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/RenderSystem;disableTexture()V", ordinal = 2, shift = At.Shift.AFTER),
locals = LocalCapture.CAPTURE_FAILHARD locals = LocalCapture.CAPTURE_FAILHARD
) )
private void drawNebulas(PoseStack poseStack, Matrix4f projectionMatrix, float partialTick, Camera camera, boolean bl, Runnable skyFogSetup, CallbackInfo ci, FogType fogType, Vec3 vec3, float f, float g, float h, BufferBuilder bufferBuilder, ShaderInstance shaderInstance, float[] fs, float i, Matrix4f matrix4f2, float k, int r, int s, int m, float t, float o, float p, float q) { private void drawSkybox(PoseStack poseStack, Matrix4f projectionMatrix, float partialTick, Camera camera, boolean bl, Runnable skyFogSetup, CallbackInfo ci, FogType fogType, Vec3 vec3, float f, float g, float h, BufferBuilder bufferBuilder, ShaderInstance shaderInstance, float[] fs, float i, Matrix4f matrix4f2, float k, int r, int s, int m, float t, float o, float p, float q) {
Matrix4f matrix = matrix4f2; ExampleMod.nebulaSkybox.render(poseStack, projectionMatrix);
// matrix.multiply(projectionMatrix);
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, StarManager.skyTexture.getId());
BufferBuilder leBufferBuilder = Tesselator.getInstance().getBuilder();
leBufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
// +z face
leBufferBuilder.vertex(matrix, -1F, -1F, 1F).uv(0.25f, 0.25f).endVertex();
leBufferBuilder.vertex(matrix, -1F, 1F, 1F).uv(0.25f, 0.5f).endVertex();
leBufferBuilder.vertex(matrix, 1F, 1F, 1F).uv(0.5f, 0.5f).endVertex();
leBufferBuilder.vertex(matrix, 1F, -1F, 1F).uv(0.5f, 0.25f).endVertex();
// -z face
leBufferBuilder.vertex(matrix, -1F, -1F, -1F).uv(0.75f, 0.25f).endVertex();
leBufferBuilder.vertex(matrix, 1F, -1F, -1F).uv(1f, 0.25f).endVertex();
leBufferBuilder.vertex(matrix, 1F, 1F, -1F).uv(1f, 0.5f).endVertex();
leBufferBuilder.vertex(matrix, -1F, 1F, -1F).uv(0.75f, 0.5f).endVertex();
// bottom face
leBufferBuilder.vertex(matrix, -1F, -1F, -1F).uv(0.5f, 0.5f).endVertex();
leBufferBuilder.vertex(matrix, -1F, -1F, 1F).uv(0.5f, 0.75f).endVertex();
leBufferBuilder.vertex(matrix, 1F, -1F, 1F).uv(0.75f, 0.75f).endVertex();
leBufferBuilder.vertex(matrix, 1F, -1F, -1F).uv(0.75f, 0.5f).endVertex();
// leBufferBuilder.vertex(matrix, -1F, -1F, -1F).uv(0f, 0f).endVertex();
// leBufferBuilder.vertex(matrix, -1F, -1F, 1F).uv(0f, 1f).endVertex();
// leBufferBuilder.vertex(matrix, 1F, -1F, 1F).uv(1f, 1f).endVertex();
// leBufferBuilder.vertex(matrix, 1F, -1F, -1F).uv(1f, 0f).endVertex();
// top face
leBufferBuilder.vertex(matrix, -1F, 1F, -1F).uv(0.5f, 0f).endVertex();
leBufferBuilder.vertex(matrix, 1F, 1F, -1F).uv(0.75f, 0f).endVertex();
leBufferBuilder.vertex(matrix, 1F, 1F, 1F).uv(0.75f, 0.25f).endVertex();
leBufferBuilder.vertex(matrix, -1F, 1F, 1F).uv(0.5f, 0.25f).endVertex();
// +x face
leBufferBuilder.vertex(matrix, 1F, -1F, -1F).uv(0.5f, 0.25f).endVertex();
leBufferBuilder.vertex(matrix, 1F, -1F, 1F).uv(0.75f, 0.25f).endVertex();
leBufferBuilder.vertex(matrix, 1F, 1F, 1F).uv(0.75f, 0.5f).endVertex();
leBufferBuilder.vertex(matrix, 1F, 1F, -1F).uv(0.5f, 0.5f).endVertex();
// -x face
leBufferBuilder.vertex(matrix, -1F, -1F, -1F).uv(0f, 0.25f).endVertex();
leBufferBuilder.vertex(matrix, -1F, 1F, -1F).uv(0f, 0.5f).endVertex();
leBufferBuilder.vertex(matrix, -1F, 1F, 1F).uv(0.25f, 0.5f).endVertex();
leBufferBuilder.vertex(matrix, -1F, -1F, 1F).uv(0.25f, 0.25f).endVertex();
BufferUploader.drawWithShader(leBufferBuilder.end());
} }
} }

View file

@ -1,7 +1,7 @@
package codes.ztereohype.example.mixin.debug; package codes.ztereohype.example.mixin.debug;
import codes.ztereohype.example.ExampleMod; import codes.ztereohype.example.ExampleMod;
import codes.ztereohype.example.sky.StarManager; import codes.ztereohype.example.star.SkyManager;
import net.minecraft.client.KeyboardHandler; import net.minecraft.client.KeyboardHandler;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@ -10,17 +10,11 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(KeyboardHandler.class) @Mixin(KeyboardHandler.class)
public class MixinKeyboardHandler { public class MixinKeyboardHandler {
private long cooldown = 0;
@Inject(at = @At("HEAD"), method = "keyPress") @Inject(at = @At("HEAD"), method = "keyPress")
private void printKey(long windowPointer, int key, int scanCode, int action, int modifiers, CallbackInfo ci) { private void printKey(long windowPointer, int key, int scanCode, int action, int modifiers, CallbackInfo ci) {
if (key == 92) { if (key == 92 && action == 1) {
long time = System.currentTimeMillis(); SkyManager.generateSky();
if (time - cooldown > 200) { ExampleMod.toggle = !ExampleMod.toggle;
StarManager.generateSky();
ExampleMod.toggle = !ExampleMod.toggle;
cooldown = time;
}
} }
} }
} }

View file

@ -0,0 +1,80 @@
package codes.ztereohype.example.nebula;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.math.Matrix4f;
import lombok.Getter;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.texture.DynamicTexture;
public class Skybox {
public static final int RESOLUTION = 256;
public final DynamicTexture skyTexture = new DynamicTexture(RESOLUTION * 4, RESOLUTION * 4, false);
private final @Getter VertexBuffer skyboxBuffer = new VertexBuffer();
public Skybox() {
generateVertices();
}
public void render(PoseStack poseStack, Matrix4f projectionMatrix) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, skyTexture.getId());
this.skyboxBuffer.bind();
this.skyboxBuffer.drawWithShader(poseStack.last()
.pose(), projectionMatrix, GameRenderer.getPositionTexShader());
VertexBuffer.unbind();
}
private void generateVertices() {
BufferBuilder skyboxBuilder = Tesselator.getInstance().getBuilder();
skyboxBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
// +z face
skyboxBuilder.vertex(-1F, -1F, 1F).uv(0.25f, 0.25f).endVertex();
skyboxBuilder.vertex(-1F, 1F, 1F).uv(0.25f, 0.5f).endVertex();
skyboxBuilder.vertex(1F, 1F, 1F).uv(0.5f, 0.5f).endVertex();
skyboxBuilder.vertex(1F, -1F, 1F).uv(0.5f, 0.25f).endVertex();
// -z face
skyboxBuilder.vertex(-1F, -1F, -1F).uv(0.75f, 0.25f).endVertex();
skyboxBuilder.vertex(1F, -1F, -1F).uv(1f, 0.25f).endVertex();
skyboxBuilder.vertex(1F, 1F, -1F).uv(1f, 0.5f).endVertex();
skyboxBuilder.vertex(-1F, 1F, -1F).uv(0.75f, 0.5f).endVertex();
// bottom face
skyboxBuilder.vertex(-1F, -1F, -1F).uv(0.5f, 0.5f).endVertex();
skyboxBuilder.vertex(-1F, -1F, 1F).uv(0.5f, 0.75f).endVertex();
skyboxBuilder.vertex(1F, -1F, 1F).uv(0.75f, 0.75f).endVertex();
skyboxBuilder.vertex(1F, -1F, -1F).uv(0.75f, 0.5f).endVertex();
// skyboxBuilder.vertex(-1F, -1F, -1F).uv(0f, 0f).endVertex();
// skyboxBuilder.vertex(-1F, -1F, 1F).uv(0f, 1f).endVertex();
// skyboxBuilder.vertex(1F, -1F, 1F).uv(1f, 1f).endVertex();
// skyboxBuilder.vertex(1F, -1F, -1F).uv(1f, 0f).endVertex();
// top face
skyboxBuilder.vertex(-1F, 1F, -1F).uv(0.5f, 0f).endVertex();
skyboxBuilder.vertex(1F, 1F, -1F).uv(0.75f, 0f).endVertex();
skyboxBuilder.vertex(1F, 1F, 1F).uv(0.75f, 0.25f).endVertex();
skyboxBuilder.vertex(-1F, 1F, 1F).uv(0.5f, 0.25f).endVertex();
// +x face
skyboxBuilder.vertex(1F, -1F, -1F).uv(0.5f, 0.25f).endVertex();
skyboxBuilder.vertex(1F, -1F, 1F).uv(0.75f, 0.25f).endVertex();
skyboxBuilder.vertex(1F, 1F, 1F).uv(0.75f, 0.5f).endVertex();
skyboxBuilder.vertex(1F, 1F, -1F).uv(0.5f, 0.5f).endVertex();
// -x face
skyboxBuilder.vertex(-1F, -1F, -1F).uv(0f, 0.25f).endVertex();
skyboxBuilder.vertex(-1F, 1F, -1F).uv(0f, 0.5f).endVertex();
skyboxBuilder.vertex(-1F, 1F, 1F).uv(0.25f, 0.5f).endVertex();
skyboxBuilder.vertex(-1F, -1F, 1F).uv(0.25f, 0.25f).endVertex();
skyboxBuffer.bind();
skyboxBuffer.upload(skyboxBuilder.end());
// VertexBuffer.unbind();
}
}

View file

@ -1,182 +0,0 @@
package codes.ztereohype.example.sky;
import codes.ztereohype.example.ExampleMod;
import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.blaze3d.vertex.*;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.util.FastColor;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.levelgen.synth.ImprovedNoise;
import net.minecraft.world.level.levelgen.synth.PerlinNoise;
import java.util.ArrayList;
import java.util.stream.IntStream;
public class StarManager {
private static final BufferBuilder starBufferBuilder = Tesselator.getInstance().getBuilder();
private static final int STARS = 1500;
private static final int NEBULAS_RESOLUTION = 256;
private static final float SCALING_FACTOR = 1f;
public static ArrayList<Star> starList = new ArrayList<>();
// ez optimisation: use less vram by using only height 3 (requires pain uv mapping)
public static final DynamicTexture skyTexture = new DynamicTexture(NEBULAS_RESOLUTION * 4, NEBULAS_RESOLUTION * 4, false);
public static void generateNebulaTextures(RandomSource randomSource) {
PerlinNoise perlinNoise = PerlinNoise.create(randomSource, IntStream.of(1, 2, 3, 4, 5));
ImprovedNoise distortionNoise = new ImprovedNoise(randomSource);
// top face
NativeImage skyNativeTex = skyTexture.getPixels();
for (int texY = 0; texY < NEBULAS_RESOLUTION; texY++) {
for (int texX = 0; texX < NEBULAS_RESOLUTION; texX++) {
float x = (texX / (float) NEBULAS_RESOLUTION) * 2 - 1;
float y = 1;
float z = (texY / (float) NEBULAS_RESOLUTION) * 2 - 1;
skyNativeTex.setPixelRGBA(texX + 2 * NEBULAS_RESOLUTION, texY, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
// bottom face
for (int texY = 0; texY < NEBULAS_RESOLUTION; texY++) {
for (int texX = 0; texX < NEBULAS_RESOLUTION; texX++) {
float x = (texX / (float) NEBULAS_RESOLUTION) * 2 - 1;
float y = -1;
float z = (texY / (float) NEBULAS_RESOLUTION) * 2 - 1;
skyNativeTex.setPixelRGBA(texX + 2 * NEBULAS_RESOLUTION, texY + 2 * NEBULAS_RESOLUTION, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
// -x face
for (int texY = 0; texY < NEBULAS_RESOLUTION; texY++) {
for (int texX = 0; texX < NEBULAS_RESOLUTION; texX++) {
float x = -1;
float y = (texY / (float) NEBULAS_RESOLUTION) * 2 - 1;
float z = (texX / (float) NEBULAS_RESOLUTION) * 2 - 1;
skyNativeTex.setPixelRGBA(texX, texY + NEBULAS_RESOLUTION, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
// +x face
for (int texY = 0; texY < NEBULAS_RESOLUTION; texY++) {
for (int texX = 0; texX < NEBULAS_RESOLUTION; texX++) {
float x = 1;
float y = (texY / (float) NEBULAS_RESOLUTION) * 2 - 1;
float z = (texX / (float) NEBULAS_RESOLUTION) * 2 - 1;
skyNativeTex.setPixelRGBA(texX + 2 * NEBULAS_RESOLUTION, texY + NEBULAS_RESOLUTION, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
// +z face
for (int texY = 0; texY < NEBULAS_RESOLUTION; texY++) {
for (int texX = 0; texX < NEBULAS_RESOLUTION; texX++) {
float x = (texX / (float) NEBULAS_RESOLUTION) * 2 - 1;
float y = (texY / (float) NEBULAS_RESOLUTION) * 2 - 1;
float z = 1;
skyNativeTex.setPixelRGBA(texX + NEBULAS_RESOLUTION, texY + NEBULAS_RESOLUTION, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
// -z face
for (int texY = 0; texY < NEBULAS_RESOLUTION; texY++) {
for (int texX = 0; texX < NEBULAS_RESOLUTION; texX++) {
float x = (texX / (float) NEBULAS_RESOLUTION) * 2 - 1;
float y = (texY / (float) NEBULAS_RESOLUTION) * 2 - 1;
float z = -1;
skyNativeTex.setPixelRGBA(texX + 3 * NEBULAS_RESOLUTION, texY + NEBULAS_RESOLUTION, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
skyTexture.upload();
}
public static int getFunnyColour(float x, float y, float z, PerlinNoise noise, ImprovedNoise distortionNoise) {
float invDistance = Mth.fastInvSqrt(x * x + y * y + z * z);
//divide by distance to get projection on sphere (shorten the vector)
x *= invDistance;
y *= invDistance;
z *= invDistance;
float offset = (float) noise.getValue(x * SCALING_FACTOR * 3, y * SCALING_FACTOR * 3, z * SCALING_FACTOR * 3);
x += offset/6f;
y += offset/6f;
z += offset/6f;
// 0..1
double noiseValue = noise.getValue(x * SCALING_FACTOR, y * SCALING_FACTOR, z * SCALING_FACTOR) * 0.5 + 0.5;
// float temperature = (float) (Math.max(Math.max(derivates[0], derivates[1]), derivates[2]) * 2500 + 3500);
int alpha = Mth.clamp((int) (noiseValue * 255) - 128, 0, 128);
// 0..1
double colourLerp = (alpha/128.0)*2;
int red = (int) (Mth.lerp(colourLerp, 1.0, 0.2) * 255);
int green = (int) (Mth.lerp(colourLerp, 0.7, 0.7) * 255);
int blue = (int) (Mth.lerp(colourLerp, 0.2, 0.9) * 255);
return FastColor.ARGB32.color(alpha, red, green, blue);
// Star.getBlueFromKelvin(noiseLerp*7000),
// Star.getGreenFromKelvin(noiseLerp*7000),
// Star.getRedFromKelvin(noiseLerp*7000));
// blue, green, red);
}
public static void generateSky() {
RandomSource randomSource = RandomSource.create(123L);
generateNebulaTextures(randomSource);
ImprovedNoise noise = new ImprovedNoise(randomSource);
starList.clear();
for (int i = 0; i < STARS; ++i) {
float randX = randomSource.nextFloat() * 2.0F - 1.0F;
float randY = randomSource.nextFloat() * 2.0F - 1.0F;
float randZ = randomSource.nextFloat() * 2.0F - 1.0F;
float twinkleSpeed = 0.03f + randomSource.nextFloat() * 0.04f;
float temperature = (randomSource.nextFloat() * 10f + 10f) * 600;
float starRadius = 0.20F + randomSource.nextFloat() * 0.15F;
float starValue = (float) noise.noise(randX*2.5f, randY*2.5f, randZ*2.5f) * 0.55f + 0.45f;
starRadius *= starValue;
float squaredDistance = randX * randX + randY * randY + randZ * randZ;
if (squaredDistance < 1.0 && squaredDistance > 0.01 && starRadius > 0.13) {
starList.add(new Star(randX, randY, randZ, starRadius, temperature, twinkleSpeed));
} else --i;
}
}
public static void updateStars(int ticks, VertexBuffer starBuffer) {
if (!ExampleMod.toggle) return;
starBufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
for (Star star : starList) {
star.tick(ticks);
star.setVertices(starBufferBuilder);
}
starBuffer.bind();
starBuffer.upload(starBufferBuilder.end());
VertexBuffer.unbind();
}
}

View file

@ -0,0 +1,226 @@
package codes.ztereohype.example.star;
import codes.ztereohype.example.ExampleMod;
import codes.ztereohype.example.Gradient;
import codes.ztereohype.example.nebula.Skybox;
import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.blaze3d.vertex.*;
import net.minecraft.util.FastColor;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.levelgen.synth.ImprovedNoise;
import net.minecraft.world.level.levelgen.synth.PerlinNoise;
import java.awt.*;
import java.util.ArrayList;
import java.util.stream.IntStream;
import static codes.ztereohype.example.nebula.Skybox.RESOLUTION;
public class SkyManager {
private static final Gradient STAR_GRADIENT = new Gradient();
private static final Gradient NEBULA_GRADIENT = new Gradient();
private static final BufferBuilder starBufferBuilder = Tesselator.getInstance().getBuilder();
private static final int STARS = 1500;
private static final float SCALING_FACTOR = 1f;
public static ArrayList<Star> starList = new ArrayList<>();
public static void generateSky() {
NEBULA_GRADIENT.clear();
STAR_GRADIENT.clear();
if (ExampleMod.nebulaSkybox == null) {
ExampleMod.nebulaSkybox = new Skybox();
}
RandomSource randomSource = RandomSource.create(1234L); //todo: world seed/hash server ip
buildGradients();
generateNebulaTextures(randomSource);
starList.clear();
generateStars(randomSource);
}
public static void buildGradients() {
STAR_GRADIENT.add(0.0f, 255, 179, 97);
STAR_GRADIENT.add(0.2f, 255, 249, 253);
STAR_GRADIENT.add(1.0f, 175, 199, 255);
NEBULA_GRADIENT.add(0.2f, 41, 98, 146);
NEBULA_GRADIENT.add(0.5f, 120, 59, 93);
NEBULA_GRADIENT.add(0.7f, 209, 72, 103);
NEBULA_GRADIENT.add(0.8f, 255, 200, 123);
NEBULA_GRADIENT.add(1.0f, 253, 243, 220);
// NEBULA_GRADIENT.add(0.0f, 128, 0, 0);
// NEBULA_GRADIENT.add(0.4f, 128, 0, 0);
// NEBULA_GRADIENT.add(0.5f, 128, 0, 0);
// NEBULA_GRADIENT.add(0.7f, 128, 0, 0);
// NEBULA_GRADIENT.add(1.0f, 128, 128, 128);
}
public static void generateStars(RandomSource randomSource) {
ImprovedNoise noise = new ImprovedNoise(randomSource);
for (int i = 0; i < STARS; ++i) {
float randX = randomSource.nextFloat() * 2.0F - 1.0F;
float randY = randomSource.nextFloat() * 2.0F - 1.0F;
float randZ = randomSource.nextFloat() * 2.0F - 1.0F;
float resizeSpeed = 0.03f + randomSource.nextFloat() * 0.04f;
float spinSpeed = randomSource.nextFloat() * 0.02f - 0.01f; // wtf is this?
Color starColor = STAR_GRADIENT.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;
float squaredDistance = randX * randX + randY * randY + randZ * randZ;
if (squaredDistance < 1.0 && squaredDistance > 0.01 && starValue > 0.2) {
starList.add(new Star(randX, randY, randZ, starRadius, starColor, resizeSpeed, spinSpeed));
} else --i;
}
}
public static void updateStars(int ticks, VertexBuffer starBuffer) {
starBufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
for (Star star : starList) {
star.tick(ticks);
star.setVertices(starBufferBuilder);
}
starBuffer.bind();
starBuffer.upload(starBufferBuilder.end());
// VertexBuffer.unbind();
}
public static void generateNebulaTextures(RandomSource randomSource) {
PerlinNoise perlinNoise = PerlinNoise.create(randomSource, IntStream.of(1, 2, 3, 4, 5));
ImprovedNoise distortionNoise = new ImprovedNoise(randomSource);
NativeImage skyNativeTex = ExampleMod.nebulaSkybox.skyTexture.getPixels();
// for (int face = 0; face < 6; ++face) {
// for (int texY = 0; texY < NEBULAS_RESOLUTION; texY++) {
// for (int texX = 0; texX < NEBULAS_RESOLUTION; texX++) {
// float x = (texX / (float) NEBULAS_RESOLUTION) * 2 - 1;
// float y = 1;
// float z = (texY / (float) NEBULAS_RESOLUTION) * 2 - 1;
//
//
//
// skyNativeTex.setPixelRGBA(texX + 2 * NEBULAS_RESOLUTION, texY, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
// }
// }
// }
// top face
for (int texY = 0; texY < RESOLUTION; texY++) {
for (int texX = 0; texX < RESOLUTION; texX++) {
float x = (texX / (float) RESOLUTION) * 2 - 1;
float y = 1;
float z = (texY / (float) RESOLUTION) * 2 - 1;
skyNativeTex.setPixelRGBA(texX + 2 * RESOLUTION, texY, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
// bottom face
for (int texY = 0; texY < RESOLUTION; texY++) {
for (int texX = 0; texX < RESOLUTION; texX++) {
float x = (texX / (float) RESOLUTION) * 2 - 1;
float y = -1;
float z = (texY / (float) RESOLUTION) * 2 - 1;
skyNativeTex.setPixelRGBA(texX + 2 * RESOLUTION, texY + 2 * RESOLUTION, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
// -x face
for (int texY = 0; texY < RESOLUTION; texY++) {
for (int texX = 0; texX < RESOLUTION; texX++) {
float x = -1;
float y = (texY / (float) RESOLUTION) * 2 - 1;
float z = (texX / (float) RESOLUTION) * 2 - 1;
skyNativeTex.setPixelRGBA(texX, texY + RESOLUTION, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
// +x face
for (int texY = 0; texY < RESOLUTION; texY++) {
for (int texX = 0; texX < RESOLUTION; texX++) {
float x = 1;
float y = (texY / (float) RESOLUTION) * 2 - 1;
float z = (texX / (float) RESOLUTION) * 2 - 1;
skyNativeTex.setPixelRGBA(texX + 2 * RESOLUTION, texY + RESOLUTION, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
// +z face
for (int texY = 0; texY < RESOLUTION; texY++) {
for (int texX = 0; texX < RESOLUTION; texX++) {
float x = (texX / (float) RESOLUTION) * 2 - 1;
float y = (texY / (float) RESOLUTION) * 2 - 1;
float z = 1;
skyNativeTex.setPixelRGBA(texX + RESOLUTION, texY + RESOLUTION, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
// -z face
for (int texY = 0; texY < RESOLUTION; texY++) {
for (int texX = 0; texX < RESOLUTION; texX++) {
float x = (texX / (float) RESOLUTION) * 2 - 1;
float y = (texY / (float) RESOLUTION) * 2 - 1;
float z = -1;
skyNativeTex.setPixelRGBA(texX + 3 * RESOLUTION, texY + RESOLUTION, getFunnyColour(x, y, z, perlinNoise, distortionNoise));
}
}
ExampleMod.nebulaSkybox.skyTexture.upload();
}
public static int getFunnyColour(float x, float y, float z, PerlinNoise noise, ImprovedNoise subtractionNoise) {
double baseNoiseAmount = 0.7f; // the amount of base noise to keep
float invDistance = Mth.fastInvSqrt(x * x + y * y + z * z);
//divide by distance to get projection on sphere (shorten the vector)
x *= invDistance;
y *= invDistance;
z *= invDistance;
float offset = (float) noise.getValue(x * SCALING_FACTOR * 3, y * SCALING_FACTOR * 3, z * SCALING_FACTOR * 3);
x += offset/5f;
y += offset/5f;
z += offset/5f;
// 0..1
double noiseValue = Mth.clamp(noise.getValue(x * SCALING_FACTOR, y * SCALING_FACTOR, z * SCALING_FACTOR) + 0.5, 0D, 1D);
double subtractionValue = Mth.clamp(subtractionNoise.noise(x * SCALING_FACTOR, y * SCALING_FACTOR, z * SCALING_FACTOR) + 0.5, 0D, 1D);
// double[] derivates = new double[3];
// 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 / baseNoiseAmount) - (1D / baseNoiseAmount - 1)) * 255D, 1D, 254.99D)); // otherwise death occurs
alpha = (int) Mth.clamp(alpha - subtractionValue * 255, 0, 255); //todo subtract colour channels separately
double colourValue = Mth.clamp((alpha / 255D), 0D, 1D);
Color color = NEBULA_GRADIENT.getAt(colourValue);
return FastColor.ARGB32.color(alpha, color.getBlue(), color.getGreen(), color.getRed());
// return FastColor.ARGB32.color(255, 255, 255, 255);
}
}

View file

@ -1,8 +1,10 @@
package codes.ztereohype.example.sky; package codes.ztereohype.example.star;
import com.mojang.blaze3d.vertex.BufferBuilder; import com.mojang.blaze3d.vertex.BufferBuilder;
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;
@ -14,7 +16,9 @@ public class Star {
private final float latitudeSin; private final float latitudeSin;
private final float latitudeCos; private final float latitudeCos;
private final float twinkleSpeed; private final float resizeSpeed;
private final float spinSpeed;
private final float minRadius; private final float minRadius;
private final float maxRadius; private final float maxRadius;
@ -25,10 +29,10 @@ public class Star {
private float currentAngle; private float currentAngle;
private float currentRadius; private float currentRadius;
public Star(float randX, float randY, float randZ, float size, float temperature, float twinkleSpeed) { public Star(float randX, float randY, float randZ, float size, Color color, float resizeSpeed, float spinSpeed) {
this.r = getRedFromKelvin(temperature); this.r = color.getRed();
this.g = getGreenFromKelvin(temperature); this.g = color.getGreen();
this.b = getBlueFromKelvin(temperature); this.b = color.getBlue();
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;
@ -43,16 +47,18 @@ public class Star {
this.latitudeSin = (float) Math.sin(proj); this.latitudeSin = (float) Math.sin(proj);
this.latitudeCos = (float) Math.cos(proj); this.latitudeCos = (float) Math.cos(proj);
this.twinkleSpeed = twinkleSpeed; this.spinSpeed = spinSpeed;
this.minRadius = size - 0.13f; this.resizeSpeed = resizeSpeed;
this.maxRadius = size + 0.13f;
this.minRadius = size - 0.15f;
this.maxRadius = size + 0.15f;
this.currentRadius = size; this.currentRadius = size;
this.currentAngle = (float) ((twinkleSpeed - 0.03) * 157); // random angle from 0 to this.currentAngle = (spinSpeed + 0.01f) * 628.3f; // random angle from 0 to
} }
public void tick(int ticks) { public void tick(int ticks) {
currentAngle += 0.07f * twinkleSpeed; currentAngle += spinSpeed;
currentRadius = Mth.lerp(Mth.sin(ticks * twinkleSpeed), minRadius, maxRadius); currentRadius = Mth.lerp(Mth.sin(ticks * resizeSpeed), minRadius, maxRadius);
} }
//return 4*3 coords for 4 vertices //return 4*3 coords for 4 vertices
@ -110,45 +116,45 @@ public class Star {
} }
// source: https://tannerhelland.com/2012/09/18/convert-temperature-rgb-algorithm-code.html // source: https://tannerhelland.com/2012/09/18/convert-temperature-rgb-algorithm-code.html
public static int getRedFromKelvin(double kelvins) { // public static int getRedFromKelvin(double kelvins) {
kelvins /= 100; // kelvins /= 100;
//
if (kelvins <= 66) { // if (kelvins <= 66) {
return 255; // return 255;
//
} else { // } else {
double red = 329.698727446D * Math.pow(kelvins - 60, -0.1332047592D); // double red = 329.698727446D * Math.pow(kelvins - 60, -0.1332047592D);
return Mth.clamp((int)red, 0, 255); // return Mth.clamp((int)red, 0, 255);
} // }
} // }
//
public static int getGreenFromKelvin(double kelvins) { // public static int getGreenFromKelvin(double kelvins) {
kelvins /= 100; // kelvins /= 100;
//
double green; // double green;
if (kelvins <= 66) { // if (kelvins <= 66) {
green = 99.4708025861D * Math.log(kelvins) - 161.1195681661D; // green = 99.4708025861D * Math.log(kelvins) - 161.1195681661D;
//
} else { // } else {
green = 288.1221695283D * Math.pow(kelvins - 60, -0.0755148492D); // green = 288.1221695283D * Math.pow(kelvins - 60, -0.0755148492D);
} // }
//
return Mth.clamp((int)green, 0, 255); // return Mth.clamp((int)green, 0, 255);
} // }
//
public static int getBlueFromKelvin(double kelvins) { // public static int getBlueFromKelvin(double kelvins) {
kelvins /= 100; // kelvins /= 100;
//
if (kelvins >= 66) { // if (kelvins >= 66) {
return 255; // return 255;
//
} else if (kelvins <= 19){ // } else if (kelvins <= 19){
return 0; // return 0;
//
} else { // } else {
double blue = 138.5177312231D * Math.log(kelvins - 60) - 305.0447927307D; // double blue = 138.5177312231D * Math.log(kelvins - 60) - 305.0447927307D;
// double blue = 138.5177312231D * Math.log(kelvins - 60) - 205.0447927307D; //// double blue = 138.5177312231D * Math.log(kelvins - 60) - 205.0447927307D;
return Mth.clamp((int)blue, 0, 255); // return Mth.clamp((int)blue, 0, 255);
} // }
} // }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB