Skip to content

Commit

Permalink
Nobody Else Shall Wonder
Browse files Browse the repository at this point in the history
- Allow materials to specify the cardinal lighting mode
- Hard-code the cardinal lighting mode to be one of 3
  - Off
  - Chunk
  - Entity (default)
- This gives artists some control over how they want their models to
  appear in-game. Kryppers in particular noticed lighting discrepancies
  between a flywheel model and a block model
- Remove "useLightDirections" config, command, and uniform
- Remove "diffuse" flag from Material
  • Loading branch information
Jozufozu committed Jan 5, 2025
1 parent e57702f commit a89756a
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.engine_room.flywheel.api.material;

public enum CardinalLightingMode {
/**
* No normal-based darkening will be applied.
*/
OFF,

/**
* World-space normal based darkening will be applied.
*
* <p>This mode matches the appearance of chunk geometry.
*/
CHUNK,

/**
* World-space normal based darkening will be applied in accordance to the "light directions" specified in RenderSystem.
*
* <p>This mode matches the appearance of entities.
*
* @see com.mojang.blaze3d.systems.RenderSystem#setShaderLights
*/
ENTITY,
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public interface Material {
boolean useLight();

/**
* Should this material be rendered with diffuse lighting?
* How should this material receive cardinal lighting?
*
* @return {@code true} if this material should be rendered with diffuse lighting.
* @return The cardinal lighting mode.
*/
boolean diffuse();
CardinalLightingMode cardinalLightingMode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ public interface BackendConfig {
* @return The current light smoothness setting.
*/
LightSmoothness lightSmoothness();

boolean useLightDirections();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.engine_room.flywheel.backend.engine;

import dev.engine_room.flywheel.api.material.CardinalLightingMode;
import dev.engine_room.flywheel.api.material.DepthTest;
import dev.engine_room.flywheel.api.material.Material;
import dev.engine_room.flywheel.api.material.Transparency;
Expand All @@ -19,7 +20,7 @@ public final class MaterialEncoder {
private static final int WRITE_MASK_LENGTH = Mth.ceillog2(WriteMask.values().length);
private static final int USE_OVERLAY_LENGTH = 1;
private static final int USE_LIGHT_LENGTH = 1;
private static final int DIFFUSE_LENGTH = 1;
private static final int CARDINAL_LIGHTING_MODE_LENGTH = Mth.ceillog2(CardinalLightingMode.values().length);

// The bit offset of each property
private static final int BLUR_OFFSET = 0;
Expand All @@ -31,7 +32,7 @@ public final class MaterialEncoder {
private static final int WRITE_MASK_OFFSET = TRANSPARENCY_OFFSET + TRANSPARENCY_LENGTH;
private static final int USE_OVERLAY_OFFSET = WRITE_MASK_OFFSET + WRITE_MASK_LENGTH;
private static final int USE_LIGHT_OFFSET = USE_OVERLAY_OFFSET + USE_OVERLAY_LENGTH;
private static final int DIFFUSE_OFFSET = USE_LIGHT_OFFSET + USE_LIGHT_LENGTH;
private static final int CARDINAL_LIGHTING_MODE_OFFSET = USE_LIGHT_OFFSET + USE_LIGHT_LENGTH;

// The bit mask for each property
private static final int BLUR_MASK = bitMask(BLUR_LENGTH, BLUR_OFFSET);
Expand All @@ -43,7 +44,7 @@ public final class MaterialEncoder {
private static final int WRITE_MASK_MASK = bitMask(WRITE_MASK_LENGTH, WRITE_MASK_OFFSET);
private static final int USE_OVERLAY_MASK = bitMask(USE_OVERLAY_LENGTH, USE_OVERLAY_OFFSET);
private static final int USE_LIGHT_MASK = bitMask(USE_LIGHT_LENGTH, USE_LIGHT_OFFSET);
private static final int DIFFUSE_MASK = bitMask(DIFFUSE_LENGTH, DIFFUSE_OFFSET);
private static final int CARDINAL_LIGHTING_MODE_MASK = bitMask(CARDINAL_LIGHTING_MODE_LENGTH, CARDINAL_LIGHTING_MODE_OFFSET);

private MaterialEncoder() {
}
Expand All @@ -59,7 +60,7 @@ public static int packUberShader(Material material) {
}

// Packed format:
// diffuse[1] | useLight[1] | useOverlay[1] | writeMask[2] | transparency[3] | depthTest[4] | polygonOffset[1] | backfaceCulling[1] | mipmap[1] | blur[1]
// cardinalLightingMode[2] | useLight[1] | useOverlay[1] | writeMask[2] | transparency[3] | depthTest[4] | polygonOffset[1] | backfaceCulling[1] | mipmap[1] | blur[1]
public static int packProperties(Material material) {
int bits = 0;

Expand All @@ -72,7 +73,8 @@ public static int packProperties(Material material) {
bits |= (material.writeMask().ordinal() << WRITE_MASK_OFFSET) & WRITE_MASK_MASK;
if (material.useOverlay()) bits |= USE_OVERLAY_MASK;
if (material.useLight()) bits |= USE_LIGHT_MASK;
if (material.diffuse()) bits |= DIFFUSE_MASK;
bits |= (material.cardinalLightingMode()
.ordinal() << CARDINAL_LIGHTING_MODE_OFFSET) & CARDINAL_LIGHTING_MODE_MASK;

return bits;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import org.joml.Vector3f;

import dev.engine_room.flywheel.api.RenderContext;
import dev.engine_room.flywheel.backend.BackendConfig;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;

public final class LevelUniforms extends UniformWriter {
private static final int SIZE = 16 * 4 + 4 * 13;
private static final int SIZE = 16 * 4 + 4 * 12;
static final UniformBuffer BUFFER = new UniformBuffer(Uniforms.LEVEL_INDEX, SIZE);

public static final Vector3f LIGHT0_DIRECTION = new Vector3f();
Expand Down Expand Up @@ -55,8 +54,6 @@ public static void update(RenderContext context) {

ptr = writeInt(ptr, level.effects().constantAmbientLight() ? 1 : 0);

ptr = writeInt(ptr, BackendConfig.INSTANCE.useLightDirections() ? 1 : 0);

// TODO: use defines for custom dimension ids
int dimensionId;
ResourceKey<Level> dimension = level.dimension();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ flat in uint _flw_instanceID;
out vec4 _flw_outputColor;

float _flw_diffuseFactor() {
if (flw_material.diffuse) {
if (flw_useLightDirections == 1u) {
return diffuseFromLightDirections(flw_vertexNormal);
if (flw_material.cardinalLightingMode == 2u) {
return diffuseFromLightDirections(flw_vertexNormal);
} else if (flw_material.cardinalLightingMode == 1u) {
if (flw_constantAmbientLight == 1u) {
return diffuseNether(flw_vertexNormal);
} else {
if (flw_constantAmbientLight == 1u) {
return diffuseNether(flw_vertexNormal);
} else {
return diffuse(flw_vertexNormal);
}
return diffuse(flw_vertexNormal);
}
} else {
return 1.;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const uint FLW_MAT_WRITE_MASK_COLOR_DEPTH = 0u;
const uint FLW_MAT_WRITE_MASK_COLOR = 1u;
const uint FLW_MAT_WRITE_MASK_DEPTH = 2u;

const uint FLW_MAT_CARDINAL_LIGHTING_MODE_OFF = 0u;
const uint FLW_MAT_CARDINAL_LIGHTING_MODE_CHUNK = 1u;
const uint FLW_MAT_CARDINAL_LIGHTING_MODE_ENTITY = 2u;

struct FlwMaterial {
bool blur;
bool mipmap;
Expand All @@ -29,5 +33,5 @@ struct FlwMaterial {
uint writeMask;
bool useOverlay;
bool useLight;
bool diffuse;
uint cardinalLightingMode;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const uint _FLW_TRANSPARENCY_LENGTH = 3u;
const uint _FLW_WRITE_MASK_LENGTH = 2u;
const uint _FLW_USE_OVERLAY_LENGTH = 1u;
const uint _FLW_USE_LIGHT_LENGTH = 1u;
const uint _FLW_DIFFUSE_LENGTH = 1u;
const uint _FLW_CARDINAL_LIGHTING_MODE_LENGTH = 2u;

// The bit offset of each property
const uint _FLW_BLUR_OFFSET = 0u;
Expand All @@ -20,7 +20,7 @@ const uint _FLW_TRANSPARENCY_OFFSET = _FLW_DEPTH_TEST_OFFSET + _FLW_DEPTH_TEST_L
const uint _FLW_WRITE_MASK_OFFSET = _FLW_TRANSPARENCY_OFFSET + _FLW_TRANSPARENCY_LENGTH;
const uint _FLW_USE_OVERLAY_OFFSET = _FLW_WRITE_MASK_OFFSET + _FLW_WRITE_MASK_LENGTH;
const uint _FLW_USE_LIGHT_OFFSET = _FLW_USE_OVERLAY_OFFSET + _FLW_USE_OVERLAY_LENGTH;
const uint _FLW_DIFFUSE_OFFSET = _FLW_USE_LIGHT_OFFSET + _FLW_USE_LIGHT_LENGTH;
const uint _FLW_CARDINAL_LIGHTING_MODE_OFFSET = _FLW_USE_LIGHT_OFFSET + _FLW_USE_LIGHT_LENGTH;

// The bit mask for each property
const uint _FLW_BLUR_MASK = ((1u << _FLW_BLUR_LENGTH) - 1u) << _FLW_BLUR_OFFSET;
Expand All @@ -32,10 +32,10 @@ const uint _FLW_TRANSPARENCY_MASK = ((1u << _FLW_TRANSPARENCY_LENGTH) - 1u) << _
const uint _FLW_WRITE_MASK_MASK = ((1u << _FLW_WRITE_MASK_LENGTH) - 1u) << _FLW_WRITE_MASK_OFFSET;
const uint _FLW_USE_OVERLAY_MASK = ((1u << _FLW_USE_OVERLAY_LENGTH) - 1u) << _FLW_USE_OVERLAY_OFFSET;
const uint _FLW_USE_LIGHT_MASK = ((1u << _FLW_USE_LIGHT_LENGTH) - 1u) << _FLW_USE_LIGHT_OFFSET;
const uint _FLW_DIFFUSE_MASK = ((1u << _FLW_DIFFUSE_LENGTH) - 1u) << _FLW_DIFFUSE_OFFSET;
const uint _FLW_CARDINAL_LIGHTING_MODE_MASK = ((1u << _FLW_CARDINAL_LIGHTING_MODE_LENGTH) - 1u) << _FLW_CARDINAL_LIGHTING_MODE_OFFSET;

// Packed format:
// diffuse[1] | useLight[1] | useOverlay[1] | writeMask[2] | transparency[3] | depthTest[4] | polygonOffset[1] | backfaceCulling[1] | mipmap[1] | blur[1]
// cardinalLightingMode[2] | useLight[1] | useOverlay[1] | writeMask[2] | transparency[3] | depthTest[4] | polygonOffset[1] | backfaceCulling[1] | mipmap[1] | blur[1]
void _flw_unpackMaterialProperties(uint p, out FlwMaterial m) {
m.blur = (p & _FLW_BLUR_MASK) != 0u;
m.mipmap = (p & _FLW_MIPMAP_MASK) != 0u;
Expand All @@ -46,7 +46,7 @@ void _flw_unpackMaterialProperties(uint p, out FlwMaterial m) {
m.writeMask = (p & _FLW_WRITE_MASK_MASK) >> _FLW_WRITE_MASK_OFFSET;
m.useOverlay = (p & _FLW_USE_OVERLAY_MASK) != 0u;
m.useLight = (p & _FLW_USE_LIGHT_MASK) != 0u;
m.diffuse = (p & _FLW_DIFFUSE_MASK) != 0u;
m.cardinalLightingMode = (p & _FLW_CARDINAL_LIGHTING_MODE_MASK) >> _FLW_CARDINAL_LIGHTING_MODE_OFFSET;
}

void _flw_unpackUint2x16(uint s, out uint hi, out uint lo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ layout(std140) uniform _FlwLevelUniforms {
float flw_skyDarken;

uint flw_constantAmbientLight;
uint flw_useLightDirections;

/** Use FLW_DIMENSION_* ids to determine the dimension. May eventually be implemented for custom dimensions. */
uint flw_dimension;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.engine_room.flywheel.lib.material;

import dev.engine_room.flywheel.api.material.CardinalLightingMode;
import dev.engine_room.flywheel.api.material.CutoutShader;
import dev.engine_room.flywheel.api.material.DepthTest;
import dev.engine_room.flywheel.api.material.FogShader;
Expand Down Expand Up @@ -29,7 +30,7 @@ public class SimpleMaterial implements Material {

protected final boolean useOverlay;
protected final boolean useLight;
protected final boolean diffuse;
protected final CardinalLightingMode cardinalLightingMode;

protected SimpleMaterial(Builder builder) {
shaders = builder.shaders();
Expand All @@ -46,7 +47,7 @@ protected SimpleMaterial(Builder builder) {
writeMask = builder.writeMask();
useOverlay = builder.useOverlay();
useLight = builder.useLight();
diffuse = builder.diffuse();
cardinalLightingMode = builder.cardinalLightingMode();
}

public static Builder builder() {
Expand Down Expand Up @@ -128,8 +129,8 @@ public boolean useLight() {
}

@Override
public boolean diffuse() {
return diffuse;
public CardinalLightingMode cardinalLightingMode() {
return cardinalLightingMode;
}

public static class Builder implements Material {
Expand All @@ -150,7 +151,7 @@ public static class Builder implements Material {

protected boolean useOverlay;
protected boolean useLight;
protected boolean diffuse;
protected CardinalLightingMode cardinalLightingMode;

public Builder() {
shaders = StandardMaterialShaders.DEFAULT;
Expand All @@ -167,7 +168,7 @@ public Builder() {
writeMask = WriteMask.COLOR_DEPTH;
useOverlay = true;
useLight = true;
diffuse = true;
cardinalLightingMode = CardinalLightingMode.ENTITY;
}

public Builder(Material material) {
Expand All @@ -189,7 +190,7 @@ public Builder copyFrom(Material material) {
writeMask = material.writeMask();
useOverlay = material.useOverlay();
useLight = material.useLight();
diffuse = material.diffuse();
cardinalLightingMode = material.cardinalLightingMode();
return this;
}

Expand Down Expand Up @@ -264,7 +265,11 @@ public Builder useLight(boolean value) {
}

public Builder diffuse(boolean value) {
this.diffuse = value;
return cardinalLightingMode(value ? CardinalLightingMode.ENTITY : CardinalLightingMode.OFF);
}

public Builder cardinalLightingMode(CardinalLightingMode value) {
this.cardinalLightingMode = value;
return this;
}

Expand Down Expand Up @@ -339,8 +344,8 @@ public boolean useLight() {
}

@Override
public boolean diffuse() {
return diffuse;
public CardinalLightingMode cardinalLightingMode() {
return cardinalLightingMode;
}

public SimpleMaterial build() {
Expand Down
6 changes: 5 additions & 1 deletion docs/shader-api/material.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const uint FLW_MAT_WRITE_MASK_COLOR_DEPTH = 0u;
const uint FLW_MAT_WRITE_MASK_COLOR = 1u;
const uint FLW_MAT_WRITE_MASK_DEPTH = 2u;

const uint FLW_MAT_CARDINAL_LIGHTING_MODE_OFF = 0u;
const uint FLW_MAT_CARDINAL_LIGHTING_MODE_CHUNK = 1u;
const uint FLW_MAT_CARDINAL_LIGHTING_MODE_ENTITY = 2u;

struct FlwMaterial {
bool blur;
bool mipmap;
Expand All @@ -29,5 +33,5 @@ struct FlwMaterial {
uint writeMask;
bool useOverlay;
bool useLight;
bool diffuse;
uint cardinalLightingMode;
};
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,16 @@ public JsonObject toJson() {

public static class FabricBackendConfig implements BackendConfig {
public static final LightSmoothness LIGHT_SMOOTHNESS_DEFAULT = LightSmoothness.SMOOTH;
public static final boolean USE_LIGHT_DIRECTIONS_DEFAULT = true;

public LightSmoothness lightSmoothness = LIGHT_SMOOTHNESS_DEFAULT;
public boolean useLightDirections = USE_LIGHT_DIRECTIONS_DEFAULT;

@Override
public LightSmoothness lightSmoothness() {
return lightSmoothness;
}

@Override
public boolean useLightDirections() {
return useLightDirections;
}

public void fromJson(JsonObject object) {
readLightSmoothness(object);
readUseLightDirections(object);
}

private void readLightSmoothness(JsonObject object) {
Expand Down Expand Up @@ -232,23 +224,9 @@ private void readLightSmoothness(JsonObject object) {
lightSmoothness = LIGHT_SMOOTHNESS_DEFAULT;
}

private void readUseLightDirections(JsonObject object) {
var useLightDirectionsJson = object.get("useLightDirections");

if (useLightDirectionsJson instanceof JsonPrimitive primitive && primitive.isBoolean()) {
useLightDirections = primitive.getAsBoolean();
return;
} else if (useLightDirectionsJson != null) {
FlwBackend.LOGGER.warn("'useLightDirections' value must be a boolean");
}

useLightDirections = USE_LIGHT_DIRECTIONS_DEFAULT;
}

public JsonObject toJson() {
JsonObject object = new JsonObject();
object.addProperty("lightSmoothness", lightSmoothness.getSerializedName());
object.addProperty("useLightDirections", useLightDirections);
return object;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,6 @@ public static void registerClientCommands(CommandDispatcher<FabricClientCommandS
return Command.SINGLE_SUCCESS;
})));

command.then(ClientCommandManager.literal("useLightDirections")
.executes(context -> {
if (FabricFlwConfig.INSTANCE.backendConfig.useLightDirections) {
context.getSource()
.sendFeedback(Component.translatable("command.flywheel.use_light_directions.get.on"));
} else {
context.getSource()
.sendFeedback(Component.translatable("command.flywheel.use_light_directions.get.off"));
}
return Command.SINGLE_SUCCESS;
})
.then(ClientCommandManager.literal("on")
.executes(context -> {
FabricFlwConfig.INSTANCE.backendConfig.useLightDirections = true;
FabricFlwConfig.INSTANCE.save();
context.getSource()
.sendFeedback(Component.translatable("command.flywheel.use_light_directions.set.on"));
return Command.SINGLE_SUCCESS;
}))
.then(ClientCommandManager.literal("off")
.executes(context -> {
FabricFlwConfig.INSTANCE.backendConfig.useLightDirections = false;
FabricFlwConfig.INSTANCE.save();
context.getSource()
.sendFeedback(Component.translatable("command.flywheel.use_light_directions.set.off"));
return Command.SINGLE_SUCCESS;
})));

command.then(createDebugCommand());

dispatcher.register(command);
Expand Down
Loading

0 comments on commit a89756a

Please sign in to comment.