Nobody Else Shall Wonder

- 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
This commit is contained in:
Jozufozu 2025-01-05 11:18:53 -08:00
parent e57702f742
commit a89756a709
15 changed files with 71 additions and 122 deletions

View file

@ -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,
}

View file

@ -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();
}

View file

@ -11,6 +11,4 @@ public interface BackendConfig {
* @return The current light smoothness setting.
*/
LightSmoothness lightSmoothness();
boolean useLightDirections();
}

View file

@ -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;
@ -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;
@ -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);
@ -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() {
}
@ -59,7 +60,7 @@ public final class MaterialEncoder {
}
// 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;
@ -72,7 +73,8 @@ public final class MaterialEncoder {
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;
}

View file

@ -3,14 +3,13 @@ package dev.engine_room.flywheel.backend.engine.uniform;
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();
@ -55,8 +54,6 @@ public final class LevelUniforms extends UniformWriter {
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();

View file

@ -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.;

View file

@ -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;
@ -29,5 +33,5 @@ struct FlwMaterial {
uint writeMask;
bool useOverlay;
bool useLight;
bool diffuse;
uint cardinalLightingMode;
};

View file

@ -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;
@ -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;
@ -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;
@ -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) {

View file

@ -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;

View file

@ -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;
@ -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();
@ -46,7 +47,7 @@ public class SimpleMaterial implements Material {
writeMask = builder.writeMask();
useOverlay = builder.useOverlay();
useLight = builder.useLight();
diffuse = builder.diffuse();
cardinalLightingMode = builder.cardinalLightingMode();
}
public static Builder builder() {
@ -128,8 +129,8 @@ public class SimpleMaterial implements Material {
}
@Override
public boolean diffuse() {
return diffuse;
public CardinalLightingMode cardinalLightingMode() {
return cardinalLightingMode;
}
public static class Builder implements Material {
@ -150,7 +151,7 @@ public class SimpleMaterial implements Material {
protected boolean useOverlay;
protected boolean useLight;
protected boolean diffuse;
protected CardinalLightingMode cardinalLightingMode;
public Builder() {
shaders = StandardMaterialShaders.DEFAULT;
@ -167,7 +168,7 @@ public class SimpleMaterial implements Material {
writeMask = WriteMask.COLOR_DEPTH;
useOverlay = true;
useLight = true;
diffuse = true;
cardinalLightingMode = CardinalLightingMode.ENTITY;
}
public Builder(Material material) {
@ -189,7 +190,7 @@ public class SimpleMaterial implements Material {
writeMask = material.writeMask();
useOverlay = material.useOverlay();
useLight = material.useLight();
diffuse = material.diffuse();
cardinalLightingMode = material.cardinalLightingMode();
return this;
}
@ -264,7 +265,11 @@ public class SimpleMaterial implements Material {
}
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;
}
@ -339,8 +344,8 @@ public class SimpleMaterial implements Material {
}
@Override
public boolean diffuse() {
return diffuse;
public CardinalLightingMode cardinalLightingMode() {
return cardinalLightingMode;
}
public SimpleMaterial build() {

View file

@ -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;
@ -29,5 +33,5 @@ struct FlwMaterial {
uint writeMask;
bool useOverlay;
bool useLight;
bool diffuse;
uint cardinalLightingMode;
};

View file

@ -185,24 +185,16 @@ public class FabricFlwConfig implements FlwConfig {
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) {
@ -232,23 +224,9 @@ public class FabricFlwConfig implements FlwConfig {
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;
}
}

View file

@ -100,34 +100,6 @@ public final class FlwCommands {
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);

View file

@ -98,29 +98,6 @@ public final class FlwCommands {
return Command.SINGLE_SUCCESS;
})));
var useLightDirectionsValue = ForgeFlwConfig.INSTANCE.client.backendConfig.useLightDirections;
command.then(Commands.literal("useLightDirections")
.executes(context -> {
if (useLightDirectionsValue.get()) {
sendMessage(context.getSource(), Component.translatable("command.flywheel.use_light_directions.get.on"));
} else {
sendMessage(context.getSource(), Component.translatable("command.flywheel.use_light_directions.get.off"));
}
return Command.SINGLE_SUCCESS;
})
.then(Commands.literal("on")
.executes(context -> {
useLightDirectionsValue.set(true);
sendMessage(context.getSource(), Component.translatable("command.flywheel.use_light_directions.set.on"));
return Command.SINGLE_SUCCESS;
}))
.then(Commands.literal("off")
.executes(context -> {
useLightDirectionsValue.set(false);
sendMessage(context.getSource(), Component.translatable("command.flywheel.use_light_directions.set.off"));
return Command.SINGLE_SUCCESS;
})));
command.then(createDebugCommand());
event.getDispatcher().register(command);

View file

@ -101,24 +101,15 @@ public class ForgeFlwConfig implements FlwConfig {
public static class ForgeBackendConfig implements BackendConfig {
public final ForgeConfigSpec.EnumValue<LightSmoothness> lightSmoothness;
public final ForgeConfigSpec.BooleanValue useLightDirections;
public ForgeBackendConfig(ForgeConfigSpec.Builder builder) {
lightSmoothness = builder.comment("How smooth flywheel's shader-based lighting should be. May have a large performance impact.")
.defineEnum("lightSmoothness", LightSmoothness.SMOOTH);
useLightDirections = builder.comment("If true, diffuse lighting is accurate to vanilla entities and block entities. If false, diffuse lighting is accurate to vanilla chunks. Zero performance impact, just a matter of visual preference.")
.define("useLightDirections", true);
}
@Override
public LightSmoothness lightSmoothness() {
return lightSmoothness.get();
}
@Override
public boolean useLightDirections() {
return useLightDirections.get();
}
}
}