Skip to content

Commit

Permalink
feat: Bubble rim lighting based off of https://github.com/chrischrist…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Jun 30, 2024
1 parent abfe283 commit 39d466d
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 24 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod.group=dev.galacticraft
mod.name=Galacticraft

# Dependencies
fabric.version=0.96.11+1.20.4
fabric.version=0.97.1+1.20.4
cloth.config.version=13.0.121
modmenu.version=9.0.0
rei.version=14.0.688
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import dev.galacticraft.api.entity.rocket.render.RocketPartRenderer;
import dev.galacticraft.api.rocket.entity.Rocket;
import dev.galacticraft.mod.client.model.GCBakedModel;
import dev.galacticraft.mod.client.model.GCSheets;
import dev.galacticraft.mod.client.model.GCRenderTypes;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -97,7 +97,7 @@ public void render(ClientLevel world, PoseStack matrices, Rocket rocket, MultiBu
// VertexConsumer vertexConsumer = vertices.getBuffer(layer.get());
// RenderSystem.setShader(GameRenderer::getPositionColorTexShader);

VertexConsumer consumer = vertices.getBuffer(GCSheets.obj(GCSheets.OBJ_ATLAS));
VertexConsumer consumer = vertices.getBuffer(GCRenderTypes.obj(GCRenderTypes.OBJ_ATLAS));
this.model.get().render(matrices, null, consumer, light, overlay);
// Minecraft.getInstance().getBlockRenderer().getModelRenderer().renderModel(entry, vertexConsumer, null, this.model.get(), 1, 1, 1, light, overlay);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/galacticraft/mod/GalacticraftClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import dev.galacticraft.mod.client.GCKeyBinds;
import dev.galacticraft.mod.client.gui.screen.ingame.*;
import dev.galacticraft.mod.client.model.GCModelLoader;
import dev.galacticraft.mod.client.model.GCRenderTypes;
import dev.galacticraft.mod.client.model.OxygenSealerSpriteProvider;
import dev.galacticraft.mod.client.model.SolarPanelSpriteProvider;
import dev.galacticraft.mod.client.model.types.ObjModel;
Expand Down Expand Up @@ -183,6 +184,8 @@ public void onInitializeClient() {

ModelLoadingPlugin.register(GCModelLoader.INSTANCE);

GCRenderTypes.init();

Constant.LOGGER.info("Client initialization complete. (Took {}ms.)", System.currentTimeMillis() - startInitTime);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public ResourceLocation getFabricId() {
public CompletableFuture<Void> reload(PreparationBarrier synchronizer, ResourceManager resourceManager, ProfilerFiller preparationsProfiler, ProfilerFiller reloadProfiler, Executor backgroundExecutor, Executor gameExecutor) {
preparationsProfiler.startTick();
Map<ResourceLocation, ResourceLocation> atlasMap = new HashMap<>();
atlasMap.put(GCSheets.OBJ_ATLAS, Constant.id("obj"));
atlasMap.put(GCRenderTypes.OBJ_ATLAS, Constant.id("obj"));
TextureManager textureManager = Minecraft.getInstance().getTextureManager();
RocketAtlasCallback.EVENT.invoker().collectAtlases(atlasMap, textureManager);
this.atlases = new AtlasSet(atlasMap, textureManager);
Expand Down Expand Up @@ -267,7 +267,7 @@ public AtlasSet getAtlases() {
}

public TextureAtlasSprite getDefaultSprite() {
return this.atlases.getAtlas(GCSheets.OBJ_ATLAS).getSprite(WHITE_SPRITE);
return this.atlases.getAtlas(GCRenderTypes.OBJ_ATLAS).getSprite(WHITE_SPRITE);
}

public record ReloadState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,32 @@
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.VertexFormat;
import dev.galacticraft.mod.Constant;
import net.fabricmc.fabric.api.client.rendering.v1.CoreShaderRegistrationCallback;
import net.minecraft.Util;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.RenderStateShard;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.function.BiFunction;

public class GCSheets {
public class GCRenderTypes {
public static final ResourceLocation OBJ_ATLAS = Constant.id("textures/atlas/obj.png");

@Nullable
private static ShaderInstance rendertypeBubbleShader;

public static final RenderStateShard.ShaderStateShard BUBBLE_SHADER = new RenderStateShard.ShaderStateShard(
GCRenderTypes::getRendertypeBubbleShader
);

@Nullable
public static ShaderInstance getRendertypeBubbleShader() {
return rendertypeBubbleShader;
}

public static final BiFunction<ResourceLocation, Boolean, RenderType> OBJ = Util.memoize(
(texture, composite) -> {
return RenderType.create("rocket", DefaultVertexFormat.NEW_ENTITY, VertexFormat.Mode.TRIANGLES, RenderType.TRANSIENT_BUFFER_SIZE, true, false, RenderType.CompositeState.builder()
Expand All @@ -48,17 +64,17 @@ public class GCSheets {
}
);

private static final BiFunction<ResourceLocation, Boolean, RenderType> ENTITY_TRANSLUCENT_EMISSIVE = Util.memoize(
private static final BiFunction<ResourceLocation, Boolean, RenderType> BUBBLE = Util.memoize(
((texture, composite) -> {
RenderType.CompositeState compositeState = RenderType.CompositeState.builder()
.setShaderState(RenderType.RENDERTYPE_ENTITY_TRANSLUCENT_EMISSIVE_SHADER)
.setShaderState(BUBBLE_SHADER)
.setTextureState(new RenderStateShard.TextureStateShard(texture, false, false))
.setTransparencyState(RenderType.TRANSLUCENT_TRANSPARENCY)
.setCullState(RenderType.NO_CULL)
.setWriteMaskState(RenderType.COLOR_WRITE)
.setOverlayState(RenderType.OVERLAY)
.createCompositeState(composite);
return RenderType.create("entity_translucent_emissive", DefaultVertexFormat.NEW_ENTITY, VertexFormat.Mode.TRIANGLES, RenderType.TRANSIENT_BUFFER_SIZE, true, true, compositeState);
return RenderType.create("entity_bubble", DefaultVertexFormat.NEW_ENTITY, VertexFormat.Mode.TRIANGLES, RenderType.TRANSIENT_BUFFER_SIZE, true, true, compositeState);
})
);

Expand All @@ -70,11 +86,17 @@ public static RenderType obj(ResourceLocation texture, boolean outline) {
return OBJ.apply(texture, outline);
}

public static RenderType entityTranslucentEmissive(ResourceLocation texture, boolean outline) {
return ENTITY_TRANSLUCENT_EMISSIVE.apply(texture, outline);
public static RenderType bubble(ResourceLocation texture, boolean outline) {
return BUBBLE.apply(texture, outline);
}

public static RenderType bubble(ResourceLocation texture) {
return bubble(texture, true);
}

public static RenderType entityTranslucentEmissive(ResourceLocation texture) {
return entityTranslucentEmissive(texture, true);
public static void init() {
CoreShaderRegistrationCallback.EVENT.register(context -> {
context.register(Constant.id("rendertype_bubble"), DefaultVertexFormat.NEW_ENTITY, shader -> rendertypeBubbleShader = shader);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import dev.galacticraft.mod.client.model.BakedObjModel;
import dev.galacticraft.mod.client.model.GCBakedModel;
import dev.galacticraft.mod.client.model.GCModel;
import dev.galacticraft.mod.client.model.GCSheets;
import dev.galacticraft.mod.client.model.GCRenderTypes;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -70,7 +70,7 @@ public GCBakedModel bake(ResourceManager resourceManager, Function<Material, Tex
List<BakedMaterial> bakedMaterials = new ArrayList<>();
for (Mtl material : materials) {
if (material.getMapKdOptions() != null && material.getMapKdOptions().getFileName() != null)
bakedMaterials.add(new BakedMaterial(material, spriteGetter.apply(new Material(atlas.orElse(GCSheets.OBJ_ATLAS), new ResourceLocation(material.getMapKdOptions().getFileName())))));
bakedMaterials.add(new BakedMaterial(material, spriteGetter.apply(new Material(atlas.orElse(GCRenderTypes.OBJ_ATLAS), new ResourceLocation(material.getMapKdOptions().getFileName())))));
}

return new BakedObjModel(obj, bakedMaterials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,17 @@
import dev.galacticraft.mod.Constant;
import dev.galacticraft.mod.client.model.GCBakedModel;
import dev.galacticraft.mod.client.model.GCModelLoader;
import dev.galacticraft.mod.client.model.GCSheets;
import dev.galacticraft.mod.client.model.GCRenderTypes;
import dev.galacticraft.mod.content.block.entity.machine.OxygenBubbleDistributorBlockEntity;
import dev.galacticraft.mod.content.entity.BubbleEntity;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand Down Expand Up @@ -80,7 +75,7 @@ public void render(BubbleEntity entity, float yaw, float tickDelta, PoseStack ma
matrices.pushPose();
matrices.translate(0.5F, 1.0F, 0.5F);
matrices.scale((float) size, (float) size, (float) size);
VertexConsumer consumer = vertexConsumers.getBuffer(GCSheets.entityTranslucentEmissive(GCSheets.OBJ_ATLAS));
VertexConsumer consumer = vertexConsumers.getBuffer(GCRenderTypes.bubble(GCRenderTypes.OBJ_ATLAS));
// for (BakedQuad quad : bubbleModel.getQuads(null, null, entity.level().random)) {
// consumer.putBulkData(matrices.last(), quad, 1, 1, 1, Integer.MAX_VALUE, OverlayTexture.NO_OVERLAY);
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import dev.galacticraft.mod.client.model.GCBakedModel;
import dev.galacticraft.mod.client.model.GCModelLoader;
import dev.galacticraft.mod.client.model.GCModelState;
import dev.galacticraft.mod.client.model.GCSheets;
import dev.galacticraft.mod.client.model.GCRenderTypes;
import dev.galacticraft.mod.content.entity.Buggy;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRenderer;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void render(Buggy buggy, float yaw, float tickDelta, PoseStack matrices,
if (this.buggyModel == null)
this.buggyModel = GCModelLoader.INSTANCE.getModel(MODEL);
float pitch = Mth.lerp(tickDelta, buggy.xRotO, buggy.getXRot());
VertexConsumer consumer = vertexConsumers.getBuffer(GCSheets.obj(GCSheets.OBJ_ATLAS));
VertexConsumer consumer = vertexConsumers.getBuffer(GCRenderTypes.obj(GCRenderTypes.OBJ_ATLAS));
matrices.pushPose();
matrices.scale(1.0F, 1.0F, 1.0F);
matrices.mulPose(Axis.YP.rotationDegrees(180.0F - yaw));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#version 150

#moj_import <fog.glsl>

uniform sampler2D Sampler0;

uniform vec2 ScreenSize;
uniform vec4 ColorModulator;
uniform float FogStart;
uniform float FogEnd;

in float vertexDistance;
in vec4 vertexColor;
in vec4 overlayColor;
in vec2 texCoord0;
in vec4 normal;
in vec3 normal2;
in vec3 viewDir;

out vec4 fragColor;

void main() {
// - Rim lighting -
vec3 viewAngle = normalize(-viewDir);

// The more orthogonal the camera is to the fragment, the stronger the rim light.
// abs() so that the back faces get treated the same as the front, giving a rim effect.
float rimStrength = 1 - abs(dot(viewAngle, normal2)); // The more orthogonal, the stronger

float rimFactor = pow(rimStrength, 6); // higher power = sharper rim light
vec4 rim = vec4(rimFactor);

vec4 color = texture(Sampler0, texCoord0);
if (color.a < 0.1) {
discard;
}
color *= vertexColor * ColorModulator;
color.rgb = mix(overlayColor.rgb, color.rgb, overlayColor.a);
fragColor = color + rim * linear_fog_fade(vertexDistance, FogStart, FogEnd);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"blend": {
"func": "add",
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "galacticraft:rendertype_bubble",
"fragment": "galacticraft:rendertype_bubble",
"attributes": [
"Position",
"Color",
"UV0",
"UV1",
"UV2",
"Normal"
],
"samplers": [
{ "name": "Sampler0" },
{ "name": "Sampler1" },
{ "name": "Sampler2" }
],
"uniforms": [
{ "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] },
{ "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] },
{ "name": "ScreenSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] },
{ "name": "ColorModulator", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] },
{ "name": "Light0_Direction", "type": "float", "count": 3, "values": [0.0, 0.0, 0.0] },
{ "name": "Light1_Direction", "type": "float", "count": 3, "values": [0.0, 0.0, 0.0] },
{ "name": "FogStart", "type": "float", "count": 1, "values": [ 0.0 ] },
{ "name": "FogEnd", "type": "float", "count": 1, "values": [ 1.0 ] }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#version 150

#moj_import <light.glsl>

in vec3 Position;
in vec4 Color;
in vec2 UV0;
in ivec2 UV1;
in ivec2 UV2;
in vec3 Normal;

uniform sampler2D Sampler1;
uniform sampler2D Sampler2;

uniform mat4 ModelViewMat;
uniform mat4 ProjMat;

uniform vec3 Light0_Direction;
uniform vec3 Light1_Direction;

out float vertexDistance;
out vec4 vertexColor;
out vec4 overlayColor;
out vec2 texCoord0;
out vec4 normal;
out vec3 normal2;
out vec3 viewDir;

void main() {
vec4 viewSpace = ModelViewMat * vec4(Position, 1.0);
viewDir = vec3(viewSpace);
normal2 = Normal;
gl_Position = ProjMat * viewSpace;

vertexDistance = length((ModelViewMat * vec4(Position, 1.0)).xyz);
vertexColor = minecraft_mix_light(Light0_Direction, Light1_Direction, Normal, Color);
overlayColor = texelFetch(Sampler1, UV1, 0);
texCoord0 = UV0;
normal = ProjMat * ModelViewMat * vec4(Normal, 0.0);
}

0 comments on commit 39d466d

Please sign in to comment.