Custom Sky

This commit is contained in:
paulevsGitch 2020-09-22 22:34:37 +03:00
parent 9eb993f154
commit 754fb0ef92
13 changed files with 339 additions and 7 deletions

View file

@ -1,7 +1,12 @@
package ru.betterend.config;
import ru.betterend.BetterEnd;
import ru.betterend.config.ConfigKeeper.*;
import ru.betterend.config.ConfigKeeper.BooleanEntry;
import ru.betterend.config.ConfigKeeper.Entry;
import ru.betterend.config.ConfigKeeper.FloatEntry;
import ru.betterend.config.ConfigKeeper.IntegerEntry;
import ru.betterend.config.ConfigKeeper.RangeEntry;
import ru.betterend.config.ConfigKeeper.StringEntry;
public abstract class Config {

View file

@ -7,7 +7,6 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.fabricmc.loader.api.FabricLoader;
import ru.betterend.BetterEnd;
import ru.betterend.util.JsonFactory;

View file

@ -0,0 +1,313 @@
package ru.betterend.mixin.client;
import java.util.Random;
import org.lwjgl.opengl.GL11;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.VertexBuffer;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferBuilderStorage;
import net.minecraft.client.render.SkyProperties;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Quaternion;
import ru.betterend.BetterEnd;
import ru.betterend.util.MHelper;
@Mixin(WorldRenderer.class)
public class WorldRendererMixin {
private static final Identifier SKY = new Identifier(BetterEnd.MOD_ID, "textures/sky/nebula_2.png");
private static final Identifier GRADIENT = new Identifier(BetterEnd.MOD_ID, "textures/sky/gradient.png");
private static VertexBuffer customStarsBuffer1;
private static VertexBuffer customStarsBuffer2;
private static VertexBuffer customStarsBuffer3;
private static VertexBuffer farFogBuffer;
private static VertexBuffer gradient;
private static Vector3f axis1;
private static Vector3f axis2;
private static Vector3f axis3;
private static float time;
@Shadow
@Final
private MinecraftClient client;
@Shadow
@Final
private TextureManager textureManager;
@Shadow
private ClientWorld world;
@Inject(method = "<init>*", at = @At("TAIL"))
private void onInit(MinecraftClient client, BufferBuilderStorage bufferBuilders, CallbackInfo info) {
initStars();
Random random = new Random(131);
axis1 = new Vector3f(random.nextFloat(), random.nextFloat(), random.nextFloat());
axis2 = new Vector3f(random.nextFloat(), random.nextFloat(), random.nextFloat());
axis3 = new Vector3f(random.nextFloat(), random.nextFloat(), random.nextFloat());
axis1.normalize();
axis2.normalize();
axis3.normalize();
}
@Inject(method = "renderSky", at = @At("HEAD"), cancellable = true)
private void renderBetterEndSky(MatrixStack matrices, float tickDelta, CallbackInfo info) {
if (client.world.getSkyProperties().getSkyType() == SkyProperties.SkyType.END) {
time += tickDelta * 0.001F;
if (time > 360) time -= 360;
//RenderSystem.clearDepth(0);
RenderSystem.enableAlphaTest();
RenderSystem.enableBlend();
RenderSystem.enableTexture();
//RenderSystem.alphaFunc(GL11.GL_ONE_MINUS_SRC_ALPHA, 0);
RenderSystem.defaultBlendFunc();
RenderSystem.defaultAlphaFunc();
//RenderSystem.blendEquation(mode);
//RenderSystem.enableDepthTest();
//GL14.glBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
//GL14.glBlendEquation(GL14.GL_FUNC_ADD);
//RenderSystem.disableDepthTest();
//GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
//RenderSystem.enableFog();
//BackgroundRenderer.setFogBlack();
//RenderSystem.fogStart(1F);
//RenderSystem.fogEnd(10F);
/*Vec3d vec3d = world.method_23777(this.client.gameRenderer.getCamera().getBlockPos(), tickDelta);
float r = (float) vec3d.x;
float g = (float) vec3d.y;
float b = (float) vec3d.z;*/
textureManager.bindTexture(GRADIENT);
renderBuffer(matrices, gradient, VertexFormats.POSITION_TEXTURE, 0.77F, 0.31F, 0.73F, 1F);
textureManager.bindTexture(SKY);
renderBuffer(matrices, farFogBuffer, VertexFormats.POSITION_TEXTURE, 0.77F, 0.31F, 0.73F, 0.3F);
RenderSystem.disableTexture();
//RenderSystem.disableFog();
//renderBuffer(matrices, gradient, VertexFormats.POSITION_COLOR, 0.77F, 0.31F, 0.73F, 1F);
matrices.push();
matrices.multiply(new Quaternion(axis1, time * 3, true));
renderBuffer(matrices, customStarsBuffer1, VertexFormats.POSITION, 1, 1, 1, 0.6F);
matrices.pop();
matrices.push();
matrices.multiply(new Quaternion(axis2, time * 2, true));
renderBuffer(matrices, customStarsBuffer2, VertexFormats.POSITION, 0.95F, 0.64F, 0.93F, 0.6F);
matrices.pop();
matrices.push();
matrices.multiply(new Quaternion(axis3, time, true));
renderBuffer(matrices, customStarsBuffer3, VertexFormats.POSITION, 0.77F, 0.31F, 0.73F, 0.6F);
matrices.pop();
RenderSystem.enableTexture();
RenderSystem.depthMask(true);
info.cancel();
}
}
private void renderBuffer(MatrixStack matrices, VertexBuffer buffer, VertexFormat format, float r, float g, float b, float a) {
RenderSystem.color4f(r, g, b, a);
buffer.bind();
format.startDrawing(0L);
buffer.draw(matrices.peek().getModel(), 7);
VertexBuffer.unbind();
format.endDrawing();
}
private void initStars() {
BufferBuilder buffer = Tessellator.getInstance().getBuffer();
customStarsBuffer1 = buildBuffer(buffer, customStarsBuffer1, 0.05, 0.3, 3500, 41315);
customStarsBuffer2 = buildBuffer(buffer, customStarsBuffer2, 0.07, 0.4, 2000, 35151);
customStarsBuffer3 = buildBuffer(buffer, customStarsBuffer3, 0.1, 0.5, 1500, 61354);
farFogBuffer = buildBufferFarFog(buffer, farFogBuffer, 40, 60, 100, 11515);
gradient = buildBufferGradient(buffer, gradient);
}
private VertexBuffer buildBuffer(BufferBuilder bufferBuilder, VertexBuffer buffer, double minSize, double maxSize, int count, long seed) {
if (buffer != null) {
buffer.close();
}
buffer = new VertexBuffer(VertexFormats.POSITION);
makeStars(bufferBuilder, minSize, maxSize, count, seed);
bufferBuilder.end();
buffer.upload(bufferBuilder);
return buffer;
}
private VertexBuffer buildBufferFarFog(BufferBuilder bufferBuilder, VertexBuffer buffer, double minSize, double maxSize, int count, long seed) {
if (buffer != null) {
buffer.close();
}
buffer = new VertexBuffer(VertexFormats.POSITION_TEXTURE);
makeFarFog(bufferBuilder, minSize, maxSize, count, seed);
bufferBuilder.end();
buffer.upload(bufferBuilder);
return buffer;
}
private VertexBuffer buildBufferGradient(BufferBuilder bufferBuilder, VertexBuffer buffer) {
if (buffer != null) {
buffer.close();
}
buffer = new VertexBuffer(VertexFormats.POSITION_TEXTURE);
makeGradient(bufferBuilder);
bufferBuilder.end();
buffer.upload(bufferBuilder);
return buffer;
}
private void makeStars(BufferBuilder buffer, double minSize, double maxSize, int count, long seed) {
Random random = new Random(seed);
buffer.begin(7, VertexFormats.POSITION);
for (int i = 0; i < count; ++i) {
double posX = random.nextDouble() * 2.0 - 1.0;
double posY = random.nextDouble() * 2.0 - 1.0;
double posZ = random.nextDouble() * 2.0 - 1.0;
double size = MHelper.randRange(minSize, maxSize, random);
double length = posX * posX + posY * posY + posZ * posZ;
if (length < 1.0 && length > 0.001) {
length = 1.0 / Math.sqrt(length);
posX *= length;
posY *= length;
posZ *= length;
double j = posX * 100.0;
double k = posY * 100.0;
double l = posZ * 100.0;
double m = Math.atan2(posX, posZ);
double n = Math.sin(m);
double o = Math.cos(m);
double p = Math.atan2(Math.sqrt(posX * posX + posZ * posZ), posY);
double q = Math.sin(p);
double r = Math.cos(p);
double s = random.nextDouble() * Math.PI * 2.0;
double t = Math.sin(s);
double u = Math.cos(s);
for (int v = 0; v < 4; ++v) {
double x = (double) ((v & 2) - 1) * size;
double y = (double) ((v + 1 & 2) - 1) * size;
double aa = x * u - y * t;
double ab = y * u + x * t;
double ad = aa * q + 0.0 * r;
double ae = 0.0 * q - aa * r;
double af = ae * n - ab * o;
double ah = ab * n + ae * o;
buffer.vertex(j + af, k + ad, l + ah).next();
}
}
}
}
private void makeFarFog(BufferBuilder buffer, double minSize, double maxSize, int count, long seed) {
Random random = new Random(seed);
buffer.begin(7, VertexFormats.POSITION_TEXTURE);
for (int i = 0; i < count; ++i) {
double posX = random.nextDouble() * 2.0 - 1.0;
double posY = random.nextDouble() * 0.5 - 0.25;
double posZ = random.nextDouble() * 2.0 - 1.0;
double size = MHelper.randRange(minSize, maxSize, random);
double length = posX * posX + posY * posY + posZ * posZ;
double distance = 2.0;
double delta = 1.0 / count;
if (length < 1.0 && length > 0.001) {
length = distance / Math.sqrt(length);
size *= distance;
distance -= delta;
posX *= length;
posY *= length;
posZ *= length;
double j = posX * 100.0;
double k = posY * 100.0;
double l = posZ * 100.0;
double m = Math.atan2(posX, posZ);
double n = Math.sin(m);
double o = Math.cos(m);
double p = Math.atan2(Math.sqrt(posX * posX + posZ * posZ), posY);
double q = Math.sin(p);
double r = Math.cos(p);
double s = random.nextDouble() * Math.PI * 2.0;
double t = Math.sin(s);
double u = Math.cos(s);
int pos = 0;
for (int v = 0; v < 4; ++v) {
double x = (double) ((v & 2) - 1) * size;
double y = (double) ((v + 1 & 2) - 1) * size;
double aa = x * u - y * t;
double ab = y * u + x * t;
double ad = aa * q + 0.0 * r;
double ae = 0.0 * q - aa * r;
double af = ae * n - ab * o;
double ah = ab * n + ae * o;
float texU = (pos >> 1) & 1;
float texV = ((pos + 1) >> 1) & 1;
pos ++;
buffer.vertex(j + af, k + ad, l + ah).texture(texU, texV).next();
}
}
}
}
private void makeGradient(BufferBuilder buffer) {
buffer.begin(7, VertexFormats.POSITION_TEXTURE);
for (int i = 0; i < 8; i ++)
{
double a1 = (double) i * Math.PI / 4.0;
double a2 = (double) (i + 1) * Math.PI / 4.0;
double px1 = Math.sin(a1) * 100;
double pz1 = Math.cos(a1) * 100;
double px2 = Math.sin(a2) * 100;
double pz2 = Math.cos(a2) * 100;
buffer.vertex(px1, -30, pz1).texture(0, 0).next();
buffer.vertex(px1, 30, pz1).texture(0, 1).next();
buffer.vertex(px2, 30, pz2).texture(1, 1).next();
buffer.vertex(px2, -30, pz2).texture(1, 0).next();
/*buffer.vertex(px1, 0, pz1).color(1, 1, 1, 1).next();
buffer.vertex(px1, 0.1, pz1).color(1, 1, 1, 0).next();
buffer.vertex(px2, 0.1, pz2).color(1, 1, 1, 0).next();
buffer.vertex(px2, 0, pz2).color(1, 1, 1, 1).next();
buffer.vertex(px1, 0, pz1).color(1, 1, 1, 1).next();
buffer.vertex(px1, -0.1, pz1).color(1, 1, 1, 0).next();
buffer.vertex(px2, -0.1, pz2).color(1, 1, 1, 0).next();
buffer.vertex(px2, 0, pz2).color(1, 1, 1, 1).next();*/
}
}
}

View file

@ -1,4 +1,4 @@
package ru.betterend.mixin;
package ru.betterend.mixin.common;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

View file

@ -14,7 +14,6 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.resource.Resource;
import ru.betterend.BetterEnd;
public class JsonFactory {

View file

@ -15,6 +15,10 @@ public class MHelper {
return min + random.nextInt(max - min + 1);
}
public static double randRange(double min, double max, Random random) {
return min + random.nextDouble() * (max - min);
}
public static float randRange(float min, float max, Random random) {
return min + random.nextFloat() * (max - min);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 476 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

View file

@ -0,0 +1,12 @@
{
"required": true,
"minVersion": "0.8",
"package": "ru.betterend.mixin.client",
"compatibilityLevel": "JAVA_8",
"client": [
"WorldRendererMixin"
],
"injectors": {
"defaultRequire": 1
}
}

View file

@ -1,12 +1,11 @@
{
"required": true,
"minVersion": "0.8",
"package": "ru.betterend.mixin",
"package": "ru.betterend.mixin.common",
"compatibilityLevel": "JAVA_8",
"mixins": [
"DimensionTypeMixin"
],
"client": [],
"injectors": {
"defaultRequire": 1
}

View file

@ -24,7 +24,8 @@
]
},
"mixins": [
"betterend.mixins.json"
"betterend.mixins.common.json",
"betterend.mixins.client.json"
],
"depends": {