Skip to content

Commit

Permalink
Render planets better!
Browse files Browse the repository at this point in the history
  • Loading branch information
OroArmor committed Jul 31, 2024
1 parent b0c0839 commit 456bb8a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
43 changes: 41 additions & 2 deletions src/main/java/io/github/woodiertexas/planetarium/Planetarium.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
package io.github.woodiertexas.planetarium;

import org.joml.Matrix4f;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Planetarium{
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferRenderer;
import com.mojang.blaze3d.vertex.Tessellator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.blaze3d.vertex.VertexFormats;

import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Axis;

public class Planetarium {
public static final Logger LOGGER = LoggerFactory.getLogger("Planetarium");
public static final String MODID = "planetarium";
//broken ass intelliJ

public static void renderPlanet(MatrixStack matrices, Identifier planet, float procession, float tilt, float rotation, float size, float tickDelta, ClientWorld world) {
matrices.push();

// First, line planet up where the sun is in the sky
matrices.rotate(Axis.Y_POSITIVE.rotationDegrees(90.0F));

// Second, change the orbital tilt of the planet
matrices.rotate(Axis.Y_POSITIVE.rotationDegrees(tilt));

// Third, set the angle of the planet in the sky and offset it.
matrices.rotate(Axis.X_POSITIVE.rotationDegrees(-world.getSkyAngle(tickDelta) * 360.0F + procession));

// Finally, change the rotation of the planet texture
matrices.rotate(Axis.Y_POSITIVE.rotationDegrees(rotation));

Matrix4f matrix4f = matrices.peek().getModel();
RenderSystem.setShaderTexture(0, planet);
BufferBuilder bufferBuilder = Tessellator.getInstance().begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
bufferBuilder.xyz(matrix4f, -size, 99.0F, -size).uv0(0.0F, 0.0F);
bufferBuilder.xyz(matrix4f, size, 99.0F, -size).uv0(1.0F, 0.0F); // u: 1.0
bufferBuilder.xyz(matrix4f, size, 99.0F, size).uv0(1.0F, 1.0F); // u: 1.0, v: 1.0
bufferBuilder.xyz(matrix4f, -size, 99.0F, size).uv0(0.0F, 1.0F); // v: 1.0
BufferRenderer.drawWithShader(bufferBuilder.endOrThrow());

matrices.pop();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package io.github.woodiertexas.planetarium.mixin;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferRenderer;
import com.mojang.blaze3d.vertex.Tessellator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.blaze3d.vertex.VertexFormats;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Axis;
import net.minecraft.world.World;

import io.github.woodiertexas.planetarium.Planetarium;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -131,9 +125,6 @@ private void renderStar(Identifier star, float starSize, MatrixStack matrices, f
@Shadow
private @Nullable ClientWorld world;

@Unique
Tessellator tessellator = Tessellator.getInstance();

@Unique
private static final Identifier MARS = Identifier.of(MODID, "textures/environment/mars.png");

Expand All @@ -145,32 +136,14 @@ private void renderStar(Identifier star, float starSize, MatrixStack matrices, f

@Unique
private static final Identifier NORTH_STAR = Identifier.of("minecraft", "textures/item/nether_star.png");

@Unique
private void renderPlanet(MatrixStack matrices, Identifier planet, float xRot, float yRot, float zRot, float size, float tickDelta) {
matrices.push();
matrices.rotate(Axis.X_POSITIVE.rotationDegrees(xRot));
matrices.rotate(Axis.Z_POSITIVE.rotationDegrees(world.getSkyAngle(tickDelta) * 360.0F * 100 + zRot));
matrices.rotate(Axis.Y_POSITIVE.rotationDegrees(yRot));

Matrix4f matrix4f = matrices.peek().getModel();
RenderSystem.setShaderTexture(0, planet);
BufferBuilder bufferBuilder = tessellator.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
bufferBuilder.xyz(matrix4f, -size, -100.0F, size).uv0(0.0F, 0.0F);
bufferBuilder.xyz(matrix4f, size, -100.0F, size).uv0(1.0F, 0.0F); // u: 1.0
bufferBuilder.xyz(matrix4f, size, -100.0F, -size).uv0(1.0F, 1.0F); // u: 1.0, v: 1.0
bufferBuilder.xyz(matrix4f, -size, -100.0F, -size).uv0(0.0F, 1.0F); // v: 1.0
BufferRenderer.drawWithShader(bufferBuilder.endOrThrow());

matrices.pop();
}

@Inject(method = "renderSky", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/world/ClientWorld;getStarBrightness(F)F"))
private void renderCelestialObjects(Matrix4f modelViewMatrix, Matrix4f projectionMatrix, float tickDelta, Camera preStep, boolean skipRendering, Runnable preRender, CallbackInfo ci) {
MatrixStack matrices = new MatrixStack();
matrices.multiply(modelViewMatrix);

renderPlanet(matrices, MARS, 0, 45, 0, 13.0f, tickDelta);
renderPlanet(matrices, JUPITER, 55, 0, 0, 13.0f, tickDelta);
renderPlanet(matrices, SATURN, 45, 0, 15, 13.0f, tickDelta);
Planetarium.renderPlanet(matrices, MARS, 0, 45, 0, 13.0f, tickDelta, world);
Planetarium.renderPlanet(matrices, JUPITER, 55, 0, 0, 13.0f, tickDelta, world);
Planetarium.renderPlanet(matrices, SATURN, 45, 0, 15, 13.0f, tickDelta, world);
}
}

0 comments on commit 456bb8a

Please sign in to comment.