Skip to content

Commit efccb54

Browse files
committed
emissive trims
remember to document
1 parent f46eeb6 commit efccb54

File tree

8 files changed

+140
-17
lines changed

8 files changed

+140
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ _Here be dragons_
5656

5757
# Credits
5858
- [Benjamin Norton](https://github.com/Benjamin-Norton) for their mod All the Trims, of which this is heavily inspired and referenced from.
59+
- [ManuelXXVI](https://github.com/ManuelXXVI) for their Emissive Trim Shader, of which they kindly let me implement into this mod.

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
99
loader_version=0.14.21
1010

1111
# Mod Properties
12-
mod_version=0.1.1
12+
mod_version=0.2.0
1313
maven_group=net.diamonddev
1414
archives_base_name=simpletrims
1515

src/main/java/net/diamonddev/simpletrims/data/PaletteEncoderDecoder.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ public class PaletteEncoderDecoder {
1111
public static class EncodedPalette {
1212
public Identifier loc;
1313
public byte[] bytes;
14+
15+
// properties
16+
public boolean emissive;
1417
}
1518

16-
public static EncodedPalette encode(Identifier loc, InputStream stream) {
19+
public static EncodedPalette encode(Identifier loc, InputStream stream, boolean emissive) {
1720

1821
try {
1922
EncodedPalette palette = new EncodedPalette();
2023

2124
palette.loc = loc;
2225
palette.bytes = stream.readAllBytes();
26+
palette.emissive = emissive;
2327

2428
return palette;
2529
} catch (IOException e) {
@@ -29,9 +33,23 @@ public static EncodedPalette encode(Identifier loc, InputStream stream) {
2933

3034
public static NativeImage openDecode(EncodedPalette palette) {
3135
try {
32-
return NativeImage.read(palette.bytes);
36+
NativeImage image = NativeImage.read(palette.bytes);
37+
38+
// Apply Properties if Needed
39+
if (palette.emissive) {
40+
// palettes are 8px wide by 1px tall
41+
for (int i = 0; i < 8; i++) {
42+
image.setColor(i, 0, bitwiseShiftOpacity(image.getColor(i, 0), 254)); // the shader for emissive trims wants opacity of 254 to become emissive
43+
}
44+
}
45+
46+
return image;
3347
} catch (IOException ioe) {
3448
throw new RuntimeException("Could not decode EncodedPalette", ioe);
3549
}
3650
}
51+
52+
private static int bitwiseShiftOpacity(int color, int alpha) {
53+
return (color & 0xffffff) | (alpha << 24); // ty stack overflow https://stackoverflow.com/a/6715482
54+
}
3755
}

src/main/java/net/diamonddev/simpletrims/data/SimpleTrimsDataLoader.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import net.minecraft.registry.Registries;
99
import net.minecraft.resource.ResourceManager;
1010
import net.minecraft.util.Identifier;
11-
import net.minecraft.util.Language;
1211
import org.jetbrains.annotations.Nullable;
1312

1413
import java.io.InputStream;
1514
import java.io.InputStreamReader;
1615
import java.nio.charset.StandardCharsets;
1716
import java.util.ArrayList;
1817
import java.util.HashMap;
18+
import java.util.Optional;
19+
import java.util.function.Function;
1920

2021
import static net.diamonddev.simpletrims.data.SimpleTrimsDataLoader.MaterialKeys.*;
2122

@@ -33,13 +34,18 @@ static class MaterialKeys {
3334
static final String
3435
KEY_ENCODED_PALETTE = "encoded_palette",
3536
KEY_ASSET_NAME = "asset_name",
37+
KEY_INGREDIENT = "ingredient",
3638
KEY_DESC = "description",
39+
KEY_PROPERTIES = "properties",
40+
3741
KEY_DESC_COLOR = "color",
3842
KEY_DESC_TRANSLATIONKEY = "translate",
39-
KEY_TRANSLATIONS = "translations",
43+
KEY_DESC_TRANSLATIONS = "translations",
44+
45+
KEY_PROPS_EMISSIVE = "emissive",
46+
4047
KEY_TRANSLATIONS_LANG = "lang",
41-
KEY_TRANSLATIONS_STRING = "string",
42-
KEY_INGREDIENT = "ingredient";
48+
KEY_TRANSLATIONS_STRING = "string";
4349
}
4450

4551
public static class MaterialBean {
@@ -50,17 +56,20 @@ public static class LangBean {
5056
@SerializedName(KEY_TRANSLATIONS_STRING)
5157
public String translation;
5258
}
53-
5459
public static class DesciptionBean {
5560
@SerializedName(KEY_DESC_COLOR)
5661
public String matColorHexcode;
5762

5863
@SerializedName(KEY_DESC_TRANSLATIONKEY)
5964
public String matNameTranslationKey = NOT_A_TRANSLATION_KEY_LOL;
6065

61-
@SerializedName(KEY_TRANSLATIONS)
66+
@SerializedName(KEY_DESC_TRANSLATIONS)
6267
public ArrayList<LangBean> translations;
6368
}
69+
public static class PropsBean {
70+
@SerializedName(KEY_PROPS_EMISSIVE)
71+
public boolean emissive = false;
72+
}
6473

6574
@SerializedName(KEY_ENCODED_PALETTE)
6675
public boolean encodedPalette = true;
@@ -71,10 +80,13 @@ public static class DesciptionBean {
7180
@SerializedName(KEY_DESC)
7281
public DesciptionBean desc;
7382

83+
@SerializedName(KEY_PROPERTIES)
84+
public PropsBean properties = null;
85+
7486
@SerializedName(KEY_INGREDIENT)
7587
public String ingredient;
76-
7788
}
89+
7890
public static class MaterialBeanWrapper {
7991
private final MaterialBean bean;
8092
private final Identifier filepath;
@@ -91,7 +103,6 @@ public Item getIngredientAsItem() {
91103
}
92104
return ingredient;
93105
}
94-
95106
public String getIngredientAsId() {
96107
return bean.ingredient;
97108
}
@@ -107,7 +118,6 @@ public String getAssetName() {
107118
public String getDescTranslationKey() {
108119
return bean.desc.matNameTranslationKey;
109120
}
110-
111121
public String getReferrableTranslationKey() {
112122
return String.format("%s.%s.simpletrims.referable.material", getNamespace(), getAssetName());
113123
}
@@ -126,6 +136,9 @@ public HashMap<String, String> getTranslationHashmap() {
126136
}
127137
return translationHash;
128138
}
139+
public boolean usingTranslationMap() {
140+
return getDescTranslationKey().equals(NOT_A_TRANSLATION_KEY_LOL);
141+
}
129142

130143
public String getNamespace() {
131144
return filepath.getNamespace();
@@ -136,8 +149,10 @@ public Identifier getPathToPalette() {
136149
} else return new Identifier(filepath.getNamespace(), "trims/color_palettes/" + getAssetName());
137150
}
138151

139-
public boolean usingTranslationMap() {
140-
return getDescTranslationKey().equals(NOT_A_TRANSLATION_KEY_LOL);
152+
public boolean shouldBeEmissive() {
153+
if (bean.properties != null) {
154+
return bean.properties.emissive;
155+
} else return false;
141156
}
142157
}
143158

@@ -178,7 +193,8 @@ public void reload(ResourceManager manager) { // could put template loading here
178193
if (manager.getResource(id).isPresent()) {
179194
try (InputStream stream = manager.getResource(id).get().getInputStream()) {
180195

181-
ENCODED_PALETTES.add(PaletteEncoderDecoder.encode(id, stream));
196+
ENCODED_PALETTES.add(PaletteEncoderDecoder.encode(id, stream,
197+
getBeanWrapperFromIdAndIfPresent(id.getNamespace(), isolateFileName(id), MaterialBeanWrapper::shouldBeEmissive, false)));
182198

183199
} catch (Exception e) {
184200
SimpleTrims.LOGGER.error("Error occurred reading palette image at id [{}] - {}", id.toString(), e);
@@ -187,6 +203,15 @@ public void reload(ResourceManager manager) { // could put template loading here
187203
}
188204
}
189205

206+
public static <T> T getBeanWrapperFromIdAndIfPresent(String namespace, String assetName, Function<MaterialBeanWrapper, T> function, T notPresent) {
207+
for (var bean : SIMPLE_TRIM_MATERIALS) {
208+
if (bean.getNamespace().equals(namespace) && bean.getAssetName().equals(assetName)) {
209+
return function.apply(bean);
210+
}
211+
}
212+
return notPresent;
213+
}
214+
190215
public static String isolateFileName(Identifier resId) {
191216
String[] split = resId.getPath().split("/");
192217
return split[split.length-1].split("\\.")[0]; // isolates the filename ("namespace:path/to/file.json" -> "file")

src/main/java/net/diamonddev/simpletrims/network/SendEncodedPalettes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ public static PaletteEncoderDecoder.EncodedPalette[] read(PacketByteBuf buf) {
5656
private static void writeSingle(PaletteEncoderDecoder.EncodedPalette palette, PacketByteBuf buf) {
5757
buf.writeIdentifier(palette.loc);
5858
buf.writeByteArray(palette.bytes);
59+
60+
buf.writeBoolean(palette.emissive);
5961
}
6062
private static PaletteEncoderDecoder.EncodedPalette readSingle(PacketByteBuf buf) {
6163
PaletteEncoderDecoder.EncodedPalette data = new PaletteEncoderDecoder.EncodedPalette();
6264

6365
data.loc = buf.readIdentifier();
6466
data.bytes = buf.readByteArray();
6567

68+
data.emissive = buf.readBoolean();
69+
6670
return data;
6771
}
6872

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#version 150
2+
3+
#moj_import <fog.glsl>
4+
5+
uniform sampler2D Sampler0;
6+
7+
uniform vec4 ColorModulator;
8+
uniform float FogStart;
9+
uniform float FogEnd;
10+
uniform vec4 FogColor;
11+
12+
in float vertexDistance;
13+
in vec4 vertexColor;
14+
in vec2 texCoord0;
15+
in vec2 texCoord1;
16+
in vec4 normal;
17+
18+
out vec4 fragColor;
19+
20+
void main() {
21+
vec4 color = texture(Sampler0, texCoord0) * vertexColor * ColorModulator;
22+
vec4 emissiveColor = texture(Sampler0, texCoord0); // USED FOR ARMOR TRIMS
23+
24+
fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor);
25+
26+
if (color.a < 0.1) {discard;}
27+
28+
float opacity = ceil(color.a * 255);
29+
if(opacity == 254) // im guessing this is how it can choose which trims glow and which dont, if i set the opacity to 254 it glows?
30+
{
31+
fragColor = linear_fog(emissiveColor, vertexDistance, FogStart, FogEnd, FogColor);
32+
}
33+
}
34+
35+
// Credits:
36+
// This shader is a modified version of a shader created by ManuelXXVI.
37+
// Credits for the emissive trims go to him, and thank you for letting me use your shader!
38+
// Original Resource Pack by ManuelXXVI: https://www.planetminecraft.com/texture-pack/xxvi-s-shiny-armor-trims/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#version 150
2+
3+
#moj_import <fog.glsl>
4+
5+
uniform sampler2D Sampler0;
6+
7+
uniform vec4 ColorModulator;
8+
uniform float FogStart;
9+
uniform float FogEnd;
10+
uniform vec4 FogColor;
11+
12+
in float vertexDistance;
13+
in vec4 vertexColor;
14+
in vec2 texCoord0;
15+
in vec2 texCoord1;
16+
in vec4 normal;
17+
18+
out vec4 fragColor;
19+
20+
void main() {
21+
vec4 color = texture(Sampler0, texCoord0) * vertexColor * ColorModulator;
22+
vec4 emissiveColor = texture(Sampler0, texCoord0); // USED FOR ARMOR TRIMS
23+
24+
fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor);
25+
26+
if (color.a < 0.1) {discard;}
27+
28+
float opacity = ceil(color.a * 255);
29+
if(opacity == 254)
30+
{
31+
fragColor = linear_fog(emissiveColor, vertexDistance, FogStart, FogEnd, FogColor);;
32+
}
33+
}
34+
35+
// Credits:
36+
// This shader is a modified version of a shader created by ManuelXXVI.
37+
// Credits for the emissive trims go to him, and thank you for letting me use your shader!
38+
// Original Resource Pack by ManuelXXVI: https://www.planetminecraft.com/texture-pack/xxvi-s-shiny-armor-trims/

src/main/resources/fabric.mod.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"name": "Simple Trims",
66
"description": "Simplifying the process of adding new trims and materials.",
77
"authors": [
8-
"DiamondDev",
9-
"Bawnorton (Author of AllTheTrims, of which some code was reused from)"
8+
"DiamondDev"
109
],
1110
"contact": {
1211
"homepage": "https://diamonddevv.github.io/",

0 commit comments

Comments
 (0)