Skip to content

Commit

Permalink
feat: add Illumar Smart Lamp
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTJP committed May 11, 2024
1 parent 16ec44a commit c058610
Show file tree
Hide file tree
Showing 37 changed files with 1,219 additions and 33 deletions.
12 changes: 12 additions & 0 deletions core/src/main/java/mrtjp/projectred/core/BundledSignalsLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ public static void applyChangeMask(byte[] source, byte[] dest, int mask) {
}
}

public static int applyAndGetChangeMask(@Nullable byte[] source, byte[] dest) {
int mask = 0;
for (int i = 0; i < 16; i++) {
byte newVal = source == null ? 0 : source[i];
if (dest[i] != newVal) {
dest[i] = newVal;
mask |= 1 << i;
}
}
return mask;
}

public static byte[] raiseSignal(@Nullable byte[] signal, @Nullable byte[] source) {
if (signal == null) signal = new byte[16];
if (source == null) return signal;
Expand Down
132 changes: 119 additions & 13 deletions core/src/main/java/mrtjp/projectred/core/client/HaloRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,25 @@ public void onMovingPostRender() {
//endregion

//region World renderer
public static void addLight(BlockPos pos, int colour, Cuboid6 box) {
addLight(new Translation(pos), colour, box);
public static void addLight(BlockPos pos, Cuboid6 box, int colourIndex) {
addLight(new Translation(pos), box, colourIndex);
}

public static void addLight(Transformation t, int colour, Cuboid6 box) {
public static void addLight(Transformation t, Cuboid6 box, int colourIndex) {
Transformation t2 = new TransformationList(t, new Translation(offset));
levelLights.add(new LevelLight(t2, colour, box));
levelLights.add(new SingleColourLight(t2, box, colourIndex));
if (Configurator.lightHaloMax > -1 && levelLights.size() > Configurator.lightHaloMax) {
levelLights.poll();
}
}

public static void addMultiLight(BlockPos pos, Cuboid6 box, byte[] alphas) {
addMultiLight(new Translation(pos), box, alphas);
}

public static void addMultiLight(Transformation t, Cuboid6 box, byte[] alphas) {
Transformation t2 = new TransformationList(t, new Translation(offset));
levelLights.add(new MultiLight(t2, box, alphas));
if (Configurator.lightHaloMax > -1 && levelLights.size() > Configurator.lightHaloMax) {
levelLights.poll();
}
Expand Down Expand Up @@ -153,19 +165,19 @@ public static void onRenderWorldStageEvent(final RenderLevelStageEvent event) {
// Render to normal render target for primary visuals
ccrs.bind(HALO_GLOW_RENDER_TYPE, buffers, stack);
for (LevelLight light : lightList) {
renderToCCRS(ccrs, light.box, light.colour, light.t, HaloContext.LEVEL_RENDERER);
renderToCCRS(ccrs, light.box, light.t, light.getColour(HaloContext.LEVEL_RENDERER));
}

// Update depth buffer
ccrs.bind(HALO_FABULOUS_DEPTH_RENDER_TYPE, buffers, stack);
for (LevelLight light : lightList) {
renderToCCRS(ccrs, light.box, light.colour, light.t, HaloContext.LEVEL_RENDERER);
renderToCCRS(ccrs, light.box, light.t, light.getColour(HaloContext.LEVEL_RENDERER));
}

// Render to post chain for post-processing effects
ccrs.bind(HALO_FABULOUS_BLOOM_RENDER_TYPE, buffers, stack);
for (LevelLight light : lightList) {
renderToCCRS(ccrs, light.box, light.colour, light.t, HaloContext.BLOOM_RENDERER);
renderToCCRS(ccrs, light.box, light.t, light.getColour(HaloContext.LEVEL_RENDERER));
}
postChainFlushPending = true;

Expand Down Expand Up @@ -200,7 +212,7 @@ public static void onRenderWorldLastEvent(final RenderLevelLastEvent event) {
// Render to normal render target for primary visuals
ccrs.bind(HALO_GLOW_RENDER_TYPE, buffers, stack);
for (LevelLight light : lightList) {
renderToCCRS(ccrs, light.box, light.colour, light.t, HaloContext.LEVEL_RENDERER);
renderToCCRS(ccrs, light.box, light.t, light.getColour(HaloContext.LEVEL_RENDERER));
}

// Finish render
Expand Down Expand Up @@ -255,24 +267,78 @@ private static boolean isFabulous() {
//endregion

//region Render functions
private static void renderToCCRS(CCRenderState ccrs, Cuboid6 cuboid, int colour, Transformation t, HaloContext context) {
private static void renderToCCRS(CCRenderState ccrs, Cuboid6 cuboid, Transformation t, int rgba) {
ccrs.setPipeline(t);
ccrs.baseColour = getBaseColour(colour, context);
ccrs.baseColour = rgba;
BlockRenderer.renderCuboid(ccrs, cuboid, 0);
}

public static void renderInventoryHalo(CCRenderState ccrs, PoseStack mStack, MultiBufferSource buffers, Cuboid6 cuboid, int colour, Vector3 pos) {
public static void renderInventoryHalo(CCRenderState ccrs, PoseStack mStack, MultiBufferSource buffers, Cuboid6 cuboid, Vector3 pos, int colourIndex) {
RenderType type = isFabulous() ? HALO_FABULOUS_ITEM_ENTITY_RENDER_TYPE : HALO_GLOW_RENDER_TYPE;
ccrs.reset();
ccrs.bind(type, buffers, mStack);
renderToCCRS(ccrs, cuboid, colour, pos.translation(), HaloContext.ITEM_RENDERER);
renderToCCRS(ccrs, cuboid, pos.translation(), getBaseColour(colourIndex, HaloContext.ITEM_RENDERER));
}

public static void renderInventoryMultiHalo(CCRenderState ccrs, PoseStack mStack, MultiBufferSource buffers, Cuboid6 cuboid, Vector3 pos, byte[] alphas) {
RenderType type = isFabulous() ? HALO_FABULOUS_ITEM_ENTITY_RENDER_TYPE : HALO_GLOW_RENDER_TYPE;
ccrs.reset();
ccrs.bind(type, buffers, mStack);
renderToCCRS(ccrs, cuboid, pos.translation(), getBlendedColour(alphas, HaloContext.ITEM_RENDERER));
}
//endregion

//region Colour calculations
private static int getBaseColour(int colorIndex, HaloContext context) {
return LightColours.byIndex(colorIndex).rgbaByContext(context);
}

/**
* Mix all 16 colours given input array of alpha values. Colours are additively blended
* using same calculations as GL_SRC_ALPHA/GL_ONE_MINUS_SRC_ALPHA blending.
*
* @param alphas 16-element array of alpha values
* @return Blended colour (rgba)
*/
private static int getBlendedColour(byte[] alphas, HaloContext context) {
// Find the total alpha
int aTotal = 0;
int aMax = 0;
for (int i = 0; i < 16; i++) {
aTotal += alphas[i] & 0xFF;
aMax = Math.max(aMax, alphas[i] & 0xFF);
}

if (aTotal == 0) {
return 0;
}

// Normalize alpha values
float[] aNorm = new float[16];
for (int i = 0; i < 16; i++) {
aNorm[i] = (alphas[i] & 0xFF) / (float) aTotal * aMax / 255f;
}

float r = 0, g = 0, b = 0, a = 0;
for (int i = 0; i < 16; i++) {
if (alphas[i] == 0) continue;

int colour = getBaseColour(i, context);
float rsrc = ((colour >> 24) & 0xFF) / 255f;
float gsrc = ((colour >> 16) & 0xFF) / 255f;
float bsrc = ((colour >> 8) & 0xFF) / 255f;
float asrc = aNorm[i];

r = rsrc * asrc + r * (1 - asrc);
g = gsrc * asrc + g * (1 - asrc);
b = bsrc * asrc + b * (1 - asrc);
a = asrc * asrc + a * (1 - asrc);
}

// Note: Below alpha controls how halo renderer blends it into the render target, not alpha used to blend colours together
return (int) (r * 255) << 24 | (int) (g * 255) << 16 | (int) (b * 255) << 8 | 0xA0;
}

private enum HaloContext {
ITEM_RENDERER,
LEVEL_RENDERER,
Expand Down Expand Up @@ -372,8 +438,48 @@ public static LightColours byIndex(int index) {
return values()[index];
}
}
//endregion

private static abstract class LevelLight {

public final Transformation t;
public final Cuboid6 box;

public LevelLight(Transformation t, Cuboid6 box) {
this.t = t;
this.box = box;
}

abstract int getColour(HaloContext context);
}

private static class SingleColourLight extends LevelLight {

private final int colourIndex;

public SingleColourLight(Transformation t, Cuboid6 box, int colourIndex) {
super(t, box);
this.colourIndex = colourIndex;
}

@Override
int getColour(HaloContext context) {
return getBaseColour(colourIndex, context);
}
}

private static class MultiLight extends LevelLight {

private record LevelLight(Transformation t, int colour, Cuboid6 box) {
private final byte[] colourAlphas;

public MultiLight(Transformation t, Cuboid6 box, byte[] colourAlphas) {
super(t, box);
this.colourAlphas = colourAlphas;
}

@Override
int getColour(HaloContext context) {
return getBlendedColour(colourAlphas, context);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.19.2 2024-03-05T10:08:22.223009 ProjectRed-Illumination Item Models
// 1.19.2 2024-05-06T17:00:08.454488 ProjectRed-Illumination Item Models
a163c9ce1e63e428f3fa804b66e491b455f18540 assets/projectred_illumination/models/item/black_cage_light.json
a163c9ce1e63e428f3fa804b66e491b455f18540 assets/projectred_illumination/models/item/black_fallout_light.json
a163c9ce1e63e428f3fa804b66e491b455f18540 assets/projectred_illumination/models/item/black_fixture_light.json
Expand Down Expand Up @@ -59,6 +59,7 @@ a163c9ce1e63e428f3fa804b66e491b455f18540 assets/projectred_illumination/models/i
3b57b1bd43ea0d8e0b5691177b1431a5c5b8e6fa assets/projectred_illumination/models/item/green_inverted_illumar_lamp.json
a163c9ce1e63e428f3fa804b66e491b455f18540 assets/projectred_illumination/models/item/green_inverted_lantern.json
a163c9ce1e63e428f3fa804b66e491b455f18540 assets/projectred_illumination/models/item/green_lantern.json
f08f45f7fd93ac70ed2e01d17acf536a88abe652 assets/projectred_illumination/models/item/illumar_smart_lamp.json
a163c9ce1e63e428f3fa804b66e491b455f18540 assets/projectred_illumination/models/item/light_blue_cage_light.json
a163c9ce1e63e428f3fa804b66e491b455f18540 assets/projectred_illumination/models/item/light_blue_fallout_light.json
a163c9ce1e63e428f3fa804b66e491b455f18540 assets/projectred_illumination/models/item/light_blue_fixture_light.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.19.2 2024-03-05T10:08:22.240051 ProjectRed-Illumination Recipes
// 1.19.2 2024-05-08T21:36:25.799111 ProjectRed-Illumination Recipes
bab0e0ace8eafa4f132dd7aa9ea77ce3743620f1 data/projectred_illumination/recipes/black_cage_light.json
7346715a74ba4257b555898569b21950b6c69747 data/projectred_illumination/recipes/black_fallout_light.json
408cec0af00793db8e5895042fe84e4120c17b33 data/projectred_illumination/recipes/black_fixture_light.json
Expand Down Expand Up @@ -59,6 +59,7 @@ bd0ba40afb2e62423d4e81e30adba54004eb2483 data/projectred_illumination/recipes/gr
6afac6792ea3525e6a20140fec5038fcf60066ce data/projectred_illumination/recipes/green_inverted_illumar_lamp.json
3cbcf89e8fca3b497f11a8b47f3e169b5559e1f9 data/projectred_illumination/recipes/green_inverted_lantern.json
c849335acf420adbd919fd18bf0828690eabba30 data/projectred_illumination/recipes/green_lantern.json
4186608e2b704efdf4cb473b603a5cca3623617a data/projectred_illumination/recipes/illumar_smart_lamp.json
343c00444ba713d21c9d7800377363b528676b1f data/projectred_illumination/recipes/light_blue_cage_light.json
250e310dadd501be6ab7a84d0f3204fb5200eda2 data/projectred_illumination/recipes/light_blue_fallout_light.json
c1be8144957ba247e87114e8473c6e4f34557d04 data/projectred_illumination/recipes/light_blue_fixture_light.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.19.2 2024-03-05T10:08:22.239755 ProjectRed-Illumination Block Loot Tables
// 1.19.2 2024-05-08T16:20:21.395168 ProjectRed-Illumination Block Loot Tables
ca2aba13d5b4bcbedec5753a41db442358432327 data/projectred_illumination/loot_tables/blocks/black_illumar_lamp.json
6f70dd57e208bacc8c06e451db975c57cfa5a3a6 data/projectred_illumination/loot_tables/blocks/black_inverted_illumar_lamp.json
df6b2b1652062f4dbcabe57c04201f9c03b655ca data/projectred_illumination/loot_tables/blocks/blue_illumar_lamp.json
Expand All @@ -11,6 +11,7 @@ bf9d87f4ec56c5a82548eca928f2a00e2f47048b data/projectred_illumination/loot_table
c7d386cf69bfcf351d594c19416129dcdb92a50c data/projectred_illumination/loot_tables/blocks/gray_inverted_illumar_lamp.json
93ce38abe30aa4f1d205453c89f750226d037031 data/projectred_illumination/loot_tables/blocks/green_illumar_lamp.json
44bca255835e3cd52a14a0d7ae64ca5c2af0f794 data/projectred_illumination/loot_tables/blocks/green_inverted_illumar_lamp.json
02a0f482e29ef8f4f2f4e4e36592d07d2af1332e data/projectred_illumination/loot_tables/blocks/illumar_smart_lamp.json
2cf051942144dd90ce174df0886e0c693bd603ad data/projectred_illumination/loot_tables/blocks/light_blue_illumar_lamp.json
f2802e525768f1e0f7dc0f7726b2ee283dff792e data/projectred_illumination/loot_tables/blocks/light_blue_inverted_illumar_lamp.json
dcc7947bad8775d1ff68dc0c0d20b06ab6b902fd data/projectred_illumination/loot_tables/blocks/light_gray_illumar_lamp.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.19.2 2024-03-05T10:08:22.238879 ProjectRed-Illumination Block Models
// 1.19.2 2024-05-07T15:02:14.42821 ProjectRed-Illumination Block Models
0746294f4d3dd435feb6f198d7131a6fe4be71fa assets/projectred_illumination/blockstates/black_illumar_lamp.json
0746294f4d3dd435feb6f198d7131a6fe4be71fa assets/projectred_illumination/blockstates/black_inverted_illumar_lamp.json
14312633b8d330d240fa130218c89f36a16579c1 assets/projectred_illumination/blockstates/blue_illumar_lamp.json
Expand All @@ -11,6 +11,7 @@ eccf141f058ae9adb65b77fd11c82ccbc44e5108 assets/projectred_illumination/blocksta
eccf141f058ae9adb65b77fd11c82ccbc44e5108 assets/projectred_illumination/blockstates/gray_inverted_illumar_lamp.json
d49ec0eee75ebf8b26b19bd7947521c052fab378 assets/projectred_illumination/blockstates/green_illumar_lamp.json
d49ec0eee75ebf8b26b19bd7947521c052fab378 assets/projectred_illumination/blockstates/green_inverted_illumar_lamp.json
d6ebdb5e8eadaba44c9200e53c4982b04dd77359 assets/projectred_illumination/blockstates/illumar_smart_lamp.json
3c01dccff2ba9c4e93ed0fc989f5b93af0edc1ad assets/projectred_illumination/blockstates/light_blue_illumar_lamp.json
3c01dccff2ba9c4e93ed0fc989f5b93af0edc1ad assets/projectred_illumination/blockstates/light_blue_inverted_illumar_lamp.json
e31dd0781aa8551edcdad8f3d0f51d02f69ae9d5 assets/projectred_illumination/blockstates/light_gray_illumar_lamp.json
Expand Down Expand Up @@ -43,6 +44,8 @@ f81f1a943995009a435634ce66184a37b91142ef assets/projectred_illumination/models/b
b74a5d560b10ff6da6302100aa2a3576859567e8 assets/projectred_illumination/models/block/gray_illumar_lamp_on.json
c2ea0630753528faa979169f4896680edd444740 assets/projectred_illumination/models/block/green_illumar_lamp.json
f8618a27bdb622b34d2159a0971a1ecc82eb7830 assets/projectred_illumination/models/block/green_illumar_lamp_on.json
a10bc3eccc0d66e962e91821236deadc48cff693 assets/projectred_illumination/models/block/illumar_smart_lamp.json
ef2683c5a198564370912026bb7d58ee36cdbf2f assets/projectred_illumination/models/block/illumar_smart_lamp_on.json
05aa22099bae4f9fa31127b214f3ce3aa62e36ee assets/projectred_illumination/models/block/light_blue_illumar_lamp.json
a7d29aab07598cbd6028e141475541aabcd62979 assets/projectred_illumination/models/block/light_blue_illumar_lamp_on.json
672ed59c46c3acc2baf1825ec0696da87620c095 assets/projectred_illumination/models/block/light_gray_illumar_lamp.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.2 2024-03-05T10:08:22.238073 Languages: en_us
548cfaf60e6a16b400fd90c0cc5da98891f425cf assets/projectred_illumination/lang/en_us.json
// 1.19.2 2024-05-07T15:11:08.498971 Languages: en_us
bd4c754665d90437f9a3014a701298a4082d18cc assets/projectred_illumination/lang/en_us.json
Loading

0 comments on commit c058610

Please sign in to comment.