Skip to content

Commit

Permalink
Add emission support and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Aug 21, 2024
1 parent 431e5b4 commit fe35e5c
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,9 @@ public TextureAtlasSprite getSprite() {
public Direction getLightFace() {
return this.direction;
}

@Override
public int getMaxLightQuad(int idx) {
return getLight(idx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,11 @@ default int getAccurateNormal(int i) {

return normal == 0 ? getFaceNormal() : normal;
}

/**
* Gets the maximum light value for this vertex.
* @param idx The vertex index.
* @return Lightmap value.
*/
int getMaxLightQuad(int idx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -723,13 +723,13 @@ public boolean isSectionBuilt(int x, int y, int z) {
}

public void onChunkAdded(int x, int z) {
for (int y = this.level.getMinSectionY(); y < this.level.getMaxSectionY(); y++) {
for (int y = this.level.getMinSectionY(); y <= this.level.getMaxSectionY(); y++) {
this.onSectionAdded(x, y, z);
}
}

public void onChunkRemoved(int x, int z) {
for (int y = this.level.getMinSectionY(); y < this.level.getMaxSectionY(); y++) {
for (int y = this.level.getMinSectionY(); y <= this.level.getMaxSectionY(); y++) {
this.onSectionRemoved(x, y, z);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ private void init(Visitor visitor,
// below the level
this.initOutsideWorldHeight(queue, viewport, searchDistance, frame,
this.level.getMinSectionY(), GraphDirection.DOWN);
} else if (origin.getY() >= this.level.getMaxSectionY()) {
} else if (origin.getY() > this.level.getMaxSectionY()) {
// above the level
this.initOutsideWorldHeight(queue, viewport, searchDistance, frame,
this.level.getMaxSectionY() - 1, GraphDirection.UP);
this.level.getMaxSectionY(), GraphDirection.UP);
} else {
this.initWithinWorld(visitor, queue, viewport, useOcclusionCulling, frame);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ public Direction getLightFace() {
return lightFace();
}

@Override
public int getMaxLightQuad(int idx) {
return lightmap(idx);
}

@Override
public int getFlags() {
return geometryFlags();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@

public class CloudRenderer {
private static final ResourceLocation CLOUDS_TEXTURE_ID = ResourceLocation.withDefaultNamespace("textures/environment/clouds.png");
public static final ShaderProgram CLOUDS = new ShaderProgram(ResourceLocation.fromNamespaceAndPath("sodium", "clouds"), DefaultVertexFormat.POSITION_COLOR, ShaderDefines.builder().build());

private CloudTextureData textureData;
private CompiledShaderProgram shaderProgram;

private @Nullable CloudRenderer.CloudGeometry cachedGeometry;

Expand Down Expand Up @@ -110,34 +110,27 @@ public void render(Camera camera,
RenderSystem.disableCull();
}

if (fabulous) {
Minecraft.getInstance().levelRenderer.getCloudsTarget().bindWrite(false);
}

RenderSystem.setShaderColor(ARGB.from8BitChannel(ARGB.red(color)), ARGB.from8BitChannel(ARGB.green(color)), ARGB.from8BitChannel(ARGB.blue(color)), 1.0F);

vertexBuffer.bind();

RenderSystem.enableBlend();
RenderSystem.enableDepthTest();
RenderSystem.blendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA,
GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
RenderType.clouds().setupRenderState();
RenderSystem.depthFunc(GL32C.GL_LESS);

vertexBuffer.drawWithShader(modelViewMatrix, projectionMatrix, this.shaderProgram);
RenderSystem.setShader(CLOUDS);

vertexBuffer.drawWithShader(modelViewMatrix, projectionMatrix, RenderSystem.getShader());

RenderSystem.depthFunc(GL32C.GL_LEQUAL);
RenderSystem.disableBlend();

VertexBuffer.unbind();

if (fastClouds) {
RenderSystem.enableCull();
}

if (fabulous) {
Minecraft.getInstance().getMainRenderTarget().bindWrite(false);
}
RenderType.clouds().clearRenderState();


RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);

Expand Down Expand Up @@ -408,16 +401,9 @@ public void reloadTextures(ResourceProvider resourceProvider) {
this.destroy();

this.textureData = loadTextureData();

this.shaderProgram = Minecraft.getInstance().getShaderManager().getProgram(new ShaderProgram(ResourceLocation.fromNamespaceAndPath("sodium", "clouds"), DefaultVertexFormat.POSITION_COLOR, ShaderDefines.builder().build()));
}

public void destroy() {
if (this.shaderProgram != null) {
this.shaderProgram.close();
this.shaderProgram = null;
}

if (this.cachedGeometry != null) {
var vertexBuffer = this.cachedGeometry.vertexBuffer();
vertexBuffer.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void writeQuadVertices(VertexBufferWriter writer, PoseStack.Pose m
float y = quad.getY(i);
float z = quad.getZ(i);

int newLight = mergeLighting(quad.getLight(i), light);
int newLight = mergeLighting(quad.getMaxLightQuad(i), light);

// The packed transformed normal vector
int normal = MatrixHelper.transformNormal(matNormal, matrices.trustedNormals, quad.getAccurateNormal(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void renderClouds(ResourceHandle resourceHandle, int color, CloudStatus c
ClientLevel level = Objects.requireNonNull(this.level);
Camera camera = this.minecraft.gameRenderer.getMainCamera();

this.cloudRenderer.render(camera, level, projectionMatrix, modelView, this.ticks, tickDelta, color);
this.cloudRenderer.render(camera, level, projectionMatrix, modelView, this.ticks, this.minecraft.getDeltaTracker().getGameTimeDeltaPartialTick(false), color);
}

@Inject(method = "onResourceManagerReload(Lnet/minecraft/server/packs/resources/ResourceManager;)V", at = @At("RETURN"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.caffeinemc.mods.sodium.client.model.quad.properties.ModelQuadFacing;
import net.caffeinemc.mods.sodium.client.model.quad.properties.ModelQuadFlags;
import net.caffeinemc.mods.sodium.client.util.ModelQuadUtil;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.core.Direction;
Expand All @@ -16,7 +17,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(BakedQuad.class)
public class BakedQuadMixin implements BakedQuadView {
public abstract class BakedQuadMixin implements BakedQuadView {
@Shadow
@Final
protected int[] vertices;
Expand All @@ -37,6 +38,9 @@ public class BakedQuadMixin implements BakedQuadView {
@Final
private boolean shade;

@Shadow
public abstract int getLightEmission();

@Unique
private int flags;

Expand Down Expand Up @@ -124,6 +128,11 @@ public Direction getLightFace() {
return this.direction;
}

@Override
public int getMaxLightQuad(int idx) {
return LightTexture.lightCoordsWithEmission(getLight(idx), getLightEmission());
}

@Override
@Unique(silent = true) // The target class has a function with the same name in a remapped environment
public boolean hasShade() {
Expand Down

0 comments on commit fe35e5c

Please sign in to comment.