Skip to content

Commit

Permalink
make sky renderers dependent on dimension id instead of dimension typ…
Browse files Browse the repository at this point in the history
…e, add support for custom shaders(?)
  • Loading branch information
screret committed Aug 30, 2024
1 parent da9fbe0 commit b11af04
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/main/java/argent_matter/gcyr/GCYRClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
import argent_matter.gcyr.api.space.planet.PlanetRing;
import argent_matter.gcyr.api.space.planet.PlanetSkyRenderer;
import argent_matter.gcyr.api.space.planet.SolarSystem;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class GCYRClient {

public static boolean isDysonSphereActive = false;

public static boolean hasUpdatedPlanets = false;
public static List<PlanetSkyRenderer> skyRenderers = new ArrayList<>();
public static Map<ResourceLocation, ShaderInstance> skyShaders = new HashMap<>();
public static List<SolarSystem> solarSystems = new ArrayList<>();
public static List<PlanetRing> planetRings = new ArrayList<>();
public static List<Galaxy> galaxies = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3f;

import java.util.List;
import java.util.Locale;
import java.util.Optional;

public record PlanetSkyRenderer(ResourceKey<Level> dimension, PlanetSkyRenderer.StarsRenderer starsRenderer,
public record PlanetSkyRenderer(ResourceKey<Level> dimension, Optional<ResourceLocation> skyShaderLocation,
PlanetSkyRenderer.StarsRenderer starsRenderer,
PlanetSkyRenderer.DimensionEffects effects,
PlanetSkyRenderer.CloudEffects cloudEffects,
PlanetSkyRenderer.WeatherEffects weatherEffects, int horizonAngle,
List<SkyObject> skyObjects) {

public static final Codec<PlanetSkyRenderer> CODEC = RecordCodecBuilder.create(instance -> instance.group(
ResourceKey.codec(Registries.DIMENSION).fieldOf("world").forGetter(PlanetSkyRenderer::dimension),
ResourceLocation.CODEC.optionalFieldOf("custom_shader").forGetter(PlanetSkyRenderer::skyShaderLocation),
StarsRenderer.CODEC.fieldOf("stars").forGetter(PlanetSkyRenderer::starsRenderer),
DimensionEffects.CODEC.fieldOf("dimension_effects").forGetter(PlanetSkyRenderer::effects),
CloudEffects.CODEC.fieldOf("cloud_effects").forGetter(PlanetSkyRenderer::cloudEffects),
Expand Down Expand Up @@ -115,4 +116,4 @@ public record DimensionEffects(DimensionEffectType type, int colour) {
Codec.INT.fieldOf("color").orElse(0xFFFFFFFF).forGetter(DimensionEffects::colour)
).apply(instance, DimensionEffects::new));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public DimensionEffects(PlanetSkyRenderer renderer) {

@Override
public Vec3 getBrightnessDependentFogColor(Vec3 color, float sunHeight) {
if (renderer.effects().type().equals(PlanetSkyRenderer.DimensionEffectType.COLORED_HORIZON)) {
if (renderer.effects().type() == PlanetSkyRenderer.DimensionEffectType.COLORED_HORIZON) {
int colour = renderer.effects().colour();
return new Vec3((colour >> 16 & 0xFF) / 255.0f, (colour >> 8 & 0xFF) / 255.0f, (colour & 0xFF) / 255.0f);
}
Expand Down Expand Up @@ -81,4 +81,4 @@ public boolean renderSnowAndRain(ClientLevel level, int ticks, float tickDelta,
public boolean shouldRenderSnowAndRain() {
return renderer.weatherEffects() != PlanetSkyRenderer.WeatherEffects.VANILLA;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package argent_matter.gcyr.client.dimension.renderer;

import argent_matter.gcyr.GCYRClient;
import argent_matter.gcyr.api.space.planet.PlanetSkyRenderer;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
Expand All @@ -12,13 +13,17 @@
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.FogRenderer;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.resources.ResourceLocation;
import org.joml.Matrix4f;
import org.joml.Vector3f;

import java.util.List;
import java.util.Optional;

public class ModSkyRenderer {

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private final Optional<ResourceLocation> skyShaderLocation;
private final PlanetSkyRenderer.StarsRenderer starsRenderer;
private final List<PlanetSkyRenderer.SkyObject> skyObjects;
private final int horizonAngle;
Expand All @@ -28,6 +33,7 @@ public class ModSkyRenderer {
private int starsCount;

public ModSkyRenderer(PlanetSkyRenderer skyRenderer) {
this.skyShaderLocation = skyRenderer.skyShaderLocation();
this.starsRenderer = skyRenderer.starsRenderer();
this.skyObjects = skyRenderer.skyObjects();
this.horizonAngle = skyRenderer.horizonAngle();
Expand All @@ -49,6 +55,12 @@ public void render(ClientLevel level, int ticks, float tickDelta, PoseStack pose

SkyUtil.preRender(level, minecraft.levelRenderer, camera, projectionMatrix, bufferBuilder, horizonAngle, poseStack, tickDelta);

skyShaderLocation.ifPresent(shaderId -> {
if (GCYRClient.skyShaders.containsKey(shaderId)) {
RenderSystem.setShader(() -> GCYRClient.skyShaders.get(shaderId));
}
});

// Stars
if (this.starsRenderer.fastStars() > 0) {
int stars = (!minecraft.options.graphicsMode().get().equals(GraphicsStatus.FAST) ? this.starsRenderer.fancyStars() : this.starsRenderer.fastStars());
Expand Down Expand Up @@ -112,4 +124,4 @@ private void createStarBuffer(BufferBuilder bufferBuilder, int stars) {
starsBuffer.upload(renderedBuffer);
VertexBuffer.unbind();
}
}
}
44 changes: 32 additions & 12 deletions src/main/java/argent_matter/gcyr/data/loader/PlanetResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.serialization.JsonOps;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
import net.minecraft.util.GsonHelper;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RegisterShadersEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.*;

@Mod.EventBusSubscriber(modid = GCYR.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public class PlanetResources implements ResourceManagerReloadListener {
public static final PlanetResources INSTANCE = new PlanetResources();

Expand All @@ -30,6 +36,7 @@ public class PlanetResources implements ResourceManagerReloadListener {
@Override
public void onResourceManagerReload(ResourceManager manager) {
List<PlanetSkyRenderer> skyRenderers = new ArrayList<>();
Map<ResourceLocation, ShaderInstance> skyShaders = new HashMap<>();
List<SolarSystem> solarSystems = new ArrayList<>();
List<PlanetRing> planetRings = new ArrayList<>();
List<Galaxy> galaxies = new ArrayList<>();
Expand All @@ -42,12 +49,15 @@ public void onResourceManagerReload(ResourceManager manager) {
JsonObject jsonObject = GsonHelper.fromJson(GSON, reader, JsonObject.class);

if (jsonObject != null) {
skyRenderers.add(PlanetSkyRenderer.CODEC.parse(JsonOps.INSTANCE, jsonObject).getOrThrow(false, GCYR.LOGGER::error));
PlanetSkyRenderer renderer = PlanetSkyRenderer.CODEC.parse(JsonOps.INSTANCE, jsonObject).getOrThrow(false, GCYR.LOGGER::error);
if (renderer.skyShaderLocation().isPresent()) {
skyShaders.put(renderer.skyShaderLocation().get(), null);
}
skyRenderers.add(renderer);
}
}
} catch (Exception e) {
GCYR.LOGGER.error("Failed to load Gregicality Rocketry sky rendering assets from: \"" + id.toString() + "\"", e);
e.printStackTrace();
GCYR.LOGGER.error("Failed to load Gregicality Rocketry sky rendering assets from: \"{}\"", id.toString(), e);
}
}

Expand All @@ -63,8 +73,7 @@ public void onResourceManagerReload(ResourceManager manager) {
}
}
} catch (Exception e) {
GCYR.LOGGER.error("Failed to load Gregicality Rocketry solar system assets from: \"" + id.toString() + "\"", e);
e.printStackTrace();
GCYR.LOGGER.error("Failed to load Gregicality Rocketry solar system assets from: \"{}\"", id.toString(), e);
}
}

Expand All @@ -79,8 +88,7 @@ public void onResourceManagerReload(ResourceManager manager) {
}
}
} catch (Exception e) {
GCYR.LOGGER.error("Failed to load Gregicality Rocketry planet ring assets from: \"" + id.toString() + "\"", e);
e.printStackTrace();
GCYR.LOGGER.error("Failed to load Gregicality Rocketry planet ring assets from: \"{}\"", id.toString(), e);
}
}

Expand All @@ -96,17 +104,29 @@ public void onResourceManagerReload(ResourceManager manager) {
}
}
} catch (Exception e) {
GCYR.LOGGER.error("Failed to load Gregicality Rocketry galaxy assets from: \"" + id.toString() + "\"", e);
e.printStackTrace();
GCYR.LOGGER.error("Failed to load Gregicality Rocketry galaxy assets from: \"{}\"", id.toString(), e);
}
}

solarSystems.sort(Comparator.comparing(SolarSystem::solarSystem));
galaxies.sort(Comparator.comparing(Galaxy::galaxy));
GCYRClient.skyRenderers = skyRenderers;
GCYRClient.skyShaders = skyShaders;
GCYRClient.solarSystems = solarSystems;
GCYRClient.planetRings = planetRings;
GCYRClient.galaxies = galaxies;
ClientModSkies.register();
}

@SubscribeEvent
public void shaderRegistry(RegisterShadersEvent event) {
for (var entry : GCYRClient.skyShaders.entrySet()) {
try {
ShaderInstance shader = new ShaderInstance(event.getResourceProvider(), entry.getKey(), DefaultVertexFormat.POSITION_TEX_COLOR);
event.registerShader(shader, entry::setValue);
} catch (IOException e) {
throw new RuntimeException("Failed to register shader with id " + entry.getKey(), e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package argent_matter.gcyr.mixin;

import argent_matter.gcyr.client.dimension.ClientModSkies;
import argent_matter.gcyr.client.dimension.renderer.DimensionEffects;
import argent_matter.gcyr.client.dimension.renderer.DimensionRenderer;
import com.mojang.blaze3d.vertex.PoseStack;
Expand Down Expand Up @@ -44,7 +45,7 @@ public abstract class LevelRendererMixin {
@Inject(at = @At("HEAD"), method = "renderSnowAndRain", cancellable = true)
private void gcyr$renderSnowAndRain(LightTexture manager, float tickDelta, double x, double y, double z, CallbackInfo info) {
if (this.minecraft.level != null) {
DimensionSpecialEffects effects = DimensionEffects.forType(this.level.dimensionType());
DimensionSpecialEffects effects = ClientModSkies.DIMENSION_SPECIAL_EFFECTS.get(this.level.dimension().location());
if (effects instanceof DimensionRenderer dimensionRenderer && dimensionRenderer.shouldRenderSnowAndRain()) {
dimensionRenderer.renderSnowAndRain(this.level, this.ticks, tickDelta, manager, x, y, z);
info.cancel();
Expand All @@ -55,7 +56,7 @@ public abstract class LevelRendererMixin {
@Inject(at = @At("HEAD"), method = "renderClouds", cancellable = true)
private void gcyr$renderClouds(PoseStack matrices, Matrix4f matrix4f, float tickDelta, double cameraX, double cameraY, double cameraZ, CallbackInfo info) {
if (this.minecraft.level != null) {
DimensionSpecialEffects effects = DimensionEffects.forType(this.level.dimensionType());
DimensionSpecialEffects effects = ClientModSkies.DIMENSION_SPECIAL_EFFECTS.get(this.level.dimension().location());
if (effects instanceof DimensionRenderer dimensionRenderer && dimensionRenderer.shouldRenderClouds()) {
dimensionRenderer.renderClouds(level, ticks, tickDelta, matrices, cameraX, cameraY, cameraZ, matrix4f);
info.cancel();
Expand All @@ -66,7 +67,7 @@ public abstract class LevelRendererMixin {
@Inject(at = @At(value = "INVOKE", target = "Ljava/lang/Runnable;run()V", shift = At.Shift.AFTER, ordinal = 0, remap = false), method = "renderSky", cancellable = true)
private void gcyr$renderSky(PoseStack matrices, Matrix4f matrix4f, float tickDelta, Camera camera, boolean isFoggy, Runnable setupFog, CallbackInfo info) {
if (this.minecraft.level != null) {
DimensionSpecialEffects effects = DimensionEffects.forType(this.level.dimensionType());
DimensionSpecialEffects effects = ClientModSkies.DIMENSION_SPECIAL_EFFECTS.get(this.level.dimension().location());
if (effects instanceof DimensionRenderer dimensionRenderer && dimensionRenderer.shouldRenderSky()) {
dimensionRenderer.renderSky(this.level, this.ticks, tickDelta, matrices, camera, matrix4f, isFoggy, setupFog);
info.cancel();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"world": "gcyr:luna_orbit",
"stars": {
"fancy_count": 13000,
"fast_count": 6000,
"colored_stars": true,
"daylight_visible": true
},
"sunset_color": "vanilla",
"dimension_effects": {
"type": "none"
},
"cloud_effects": "none",
"weather_effects": "none",
"horizon_angle": 0,
"sky_objects": [
{
"texture": "gcyr:textures/sky/sun.png",
"blending": true,
"render_type": "dynamic",
"scale": 30.0,
"rotation": [
0.0,
-90.0,
0.0
]
},
{
"texture": "gcyr:textures/sky/luna.png",
"blending": false,
"render_type": "static",
"scale": 15.0,
"rotation": [
0.0,
0.0,
0.0
]
},
{
"texture": "gcyr:textures/sky/light.png",
"blending": true,
"render_type": "static",
"scale": 20.0,
"color": 8438015,
"rotation": [
0.0,
0.0,
0.0
]
},
{
"texture": "gcyr:textures/sky/earth.png",
"blending": false,
"render_type": "static",
"scale": 9.0,
"rotation": [
30.0,
0.0,
0.0
]
},
{
"texture": "gcyr:textures/sky/light.png",
"blending": true,
"render_type": "static",
"scale": 25.0,
"color": 8438015,
"rotation": [
30.0,
0.0,
0.0
]
}
]
}
Loading

0 comments on commit b11af04

Please sign in to comment.