Skip to content

Commit

Permalink
Fix lighting on flat-shaded models when semi-flat option enabled. Pro…
Browse files Browse the repository at this point in the history
…bably fix for #111
  • Loading branch information
grondag committed Oct 30, 2020
1 parent 8ec78b3 commit 19cf2d3
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/main/java/grondag/canvas/light/AoCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@
import grondag.canvas.apiimpl.util.ColorHelper;
import grondag.canvas.light.AoFace.Vertex2Float;
import grondag.canvas.light.AoFace.WeightFunction;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper;

import static grondag.canvas.apiimpl.util.GeometryHelper.AXIS_ALIGNED_FLAG;
import static grondag.canvas.apiimpl.util.GeometryHelper.CUBIC_FLAG;
import static grondag.canvas.apiimpl.util.GeometryHelper.LIGHT_FACE_FLAG;
import static grondag.canvas.light.AoFaceData.OPAQUE;
import static grondag.canvas.terrain.RenderRegionAddressHelper.cacheIndexToXyz5;
import static grondag.canvas.terrain.RenderRegionAddressHelper.fastOffsetRelativeCacheIndex;
import static grondag.canvas.terrain.RenderRegionAddressHelper.offsetMainChunkBlockIndex;

import net.minecraft.client.util.math.Vector3f;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;

import static grondag.canvas.apiimpl.util.GeometryHelper.*;
import static grondag.canvas.light.AoFaceData.OPAQUE;
import static grondag.canvas.terrain.RenderRegionAddressHelper.*;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper;

/**
* Adaptation of inner, non-static class in BlockModelRenderer that serves same
Expand Down Expand Up @@ -101,7 +107,7 @@ static int blendIndex(int face, float depth) {
protected abstract boolean isOpaque(int cacheIndex);

private boolean checkBlendDirty(int blendIndex) {
if ((blendIndex & 63) == 0) {
if (blendIndex < 64) {
final long mask = 1L << blendIndex;

if ((blendCacheCompletionLowFlags & mask) == 0) {
Expand Down Expand Up @@ -185,8 +191,9 @@ public void compute(MutableQuadViewImpl quad) {
}
}

// PERF: quite bad - essentially pay whole cost of AO on flat lighting
// needs specialized routines or segregate brightness/AO computation
public void computeFlat(MutableQuadViewImpl quad) {

final int flags = quad.geometryFlags();

quad.hdLight = null;
Expand Down Expand Up @@ -285,8 +292,8 @@ private AoFaceCalc blendedInsetData(QuadViewImpl quad, int vertexIndex, int ligh
if (checkBlendDirty(blendIndex)) {
final float w0 = 1 - w1;
result.weightedMean(
gatherFace(lightFace, true).calc, w0,
gatherFace(lightFace, false).calc, w1);
gatherFace(lightFace, true).calc, w0,
gatherFace(lightFace, false).calc, w1);
}

return result;
Expand Down Expand Up @@ -411,7 +418,7 @@ private void irregularFace(MutableQuadViewImpl quad) {

aoResult[i] = (ao + maxAo) * (0.5f * DIVIDE_BY_255);
quad.lightmap(i, ColorHelper.maxBrightness(quad.lightmap(i), (((int) ((sky + maxSky) * 0.5f) & 0xFF) << 16)
| ((int) ((block + maxBlock) * 0.5f) & 0xFF)));
| ((int) ((block + maxBlock) * 0.5f) & 0xFF)));
}
}

Expand All @@ -433,7 +440,7 @@ private void irregularFaceFlat(MutableQuadViewImpl quad) {
* parameterization, the logic itself is practically identical to vanilla.
*/
private AoFaceData gatherFace(final int lightFace, boolean isOnBlockFace) {
final int faceDataIndex = isOnBlockFace ? lightFace : lightFace + 6;
final int faceDataIndex = isOnBlockFace ? lightFace : (lightFace + 6);
final int mask = 1 << faceDataIndex;
final AoFaceData fd = faceData[faceDataIndex];

Expand Down

0 comments on commit 19cf2d3

Please sign in to comment.