you ever just grayscale

This commit is contained in:
yrsegal@gmail.com 2022-06-02 10:57:18 -04:00
parent 28404a6593
commit d188a20366
23 changed files with 294 additions and 20 deletions

View file

@ -6,6 +6,7 @@ archivesBaseName = getArtifactID("common")
minecraft {
version(minecraftVersion)
accessWideners 'src/main/resources/hexplat.accesswidener'
}
repositories {

View file

@ -14,6 +14,7 @@ import com.mojang.math.Matrix4f
import com.mojang.math.Vector3f
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.screens.Screen
import net.minecraft.client.renderer.MultiBufferSource
import net.minecraft.core.BlockPos
import net.minecraft.util.FastColor
import net.minecraft.util.Mth
@ -285,9 +286,11 @@ fun transferMsToGl(ms: PoseStack, toRun: Runnable) {
RenderSystem.applyModelViewMatrix()
}
@JvmOverloads
fun renderEntity(
ms: PoseStack, entity: Entity, world: Level, x: Float, y: Float, rotation: Float,
renderScale: Float, offset: Float
renderScale: Float, offset: Float,
bufferTransformer: (MultiBufferSource) -> MultiBufferSource = { it -> it }
) {
entity.level = world
ms.pushPose()
@ -299,7 +302,7 @@ fun renderEntity(
val erd = Minecraft.getInstance().entityRenderDispatcher
val immediate = Minecraft.getInstance().renderBuffers().bufferSource()
erd.setRenderShadow(false)
erd.render(entity, 0.0, 0.0, 0.0, 0.0f, 1.0f, ms, immediate, 15728880)
erd.render(entity, 0.0, 0.0, 0.0, 0.0f, 1.0f, ms, bufferTransformer(immediate), 0xf000f0)
erd.setRenderShadow(true)
immediate.endBatch()
ms.popPose()

View file

@ -0,0 +1,32 @@
package at.petrak.hexcasting.client.shader;
import at.petrak.hexcasting.mixin.accessor.client.AccessorCompositeRenderType;
import at.petrak.hexcasting.mixin.accessor.client.AccessorEmptyTextureStateShard;
import at.petrak.hexcasting.mixin.accessor.client.AccessorRenderStateShard;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderStateShard;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.function.Function;
public record FakeBufferSource(MultiBufferSource parent, Function<ResourceLocation, RenderType> mapper) implements MultiBufferSource {
@Override
@SuppressWarnings("ConstantConditions")
public @NotNull VertexConsumer getBuffer(@NotNull RenderType renderType) {
if (((AccessorRenderStateShard) renderType).hex$name().equals("entity_cutout_no_cull")
&& renderType instanceof RenderType.CompositeRenderType) {
RenderType.CompositeState state = ((AccessorCompositeRenderType) renderType).hex$state();
RenderStateShard.EmptyTextureStateShard shard = state.textureState;
Optional<ResourceLocation> texture = ((AccessorEmptyTextureStateShard) shard).hex$cutoutTexture();
if (texture.isPresent()) {
return parent.getBuffer(mapper.apply(texture.get()));
}
}
return parent.getBuffer(renderType);
}
}

View file

@ -0,0 +1,43 @@
package at.petrak.hexcasting.client.shader;
import at.petrak.hexcasting.api.HexAPI;
import at.petrak.hexcasting.mixin.accessor.client.AccessorRenderType;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.Util;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import java.util.function.Function;
// https://github.com/VazkiiMods/Botania/blob/3a43accc2fbc439c9f2f00a698f8f8ad017503db/Common/src/main/java/vazkii/botania/client/core/helper/RenderHelper.java
public final class HexRenderTypes extends RenderType {
private HexRenderTypes(String string, VertexFormat vertexFormat, VertexFormat.Mode mode, int i, boolean bl, boolean bl2, Runnable runnable, Runnable runnable2) {
super(string, vertexFormat, mode, i, bl, bl2, runnable, runnable2);
throw new UnsupportedOperationException("Should not be instantiated");
}
private static RenderType makeLayer(String name, VertexFormat format, VertexFormat.Mode mode,
int bufSize, boolean hasCrumbling, boolean sortOnUpload, RenderType.CompositeState glState) {
return AccessorRenderType.hex$create(name, format, mode, bufSize, hasCrumbling, sortOnUpload, glState);
}
private static final Function<ResourceLocation, RenderType> GRAYSCALE_PROVIDER = Util.memoize(texture -> {
CompositeState glState = RenderType.CompositeState.builder()
.setShaderState(new ShaderStateShard(HexShaders::grayscale))
.setTextureState(new TextureStateShard(texture, false, false))
.setTransparencyState(NO_TRANSPARENCY)
.setCullState(NO_CULL)
.setLightmapState(LIGHTMAP)
.setOverlayState(OVERLAY)
.createCompositeState(true);
return makeLayer(HexAPI.MOD_ID + ":grayscale", DefaultVertexFormat.NEW_ENTITY, VertexFormat.Mode.QUADS, 256, true, false, glState);
});
public static RenderType getGrayscaleLayer(ResourceLocation texture) {
return GRAYSCALE_PROVIDER.apply(texture);
}
}

View file

@ -0,0 +1,25 @@
package at.petrak.hexcasting.client.shader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.datafixers.util.Pair;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.server.packs.resources.ResourceManager;
import java.io.IOException;
import java.util.function.Consumer;
// https://github.com/VazkiiMods/Botania/blob/3a43accc2fbc439c9f2f00a698f8f8ad017503db/Common/src/main/java/vazkii/botania/client/core/helper/CoreShaders.java
public class HexShaders {
private static ShaderInstance grayscale;
public static void init(ResourceManager resourceManager,
Consumer<Pair<ShaderInstance, Consumer<ShaderInstance>>> registrations) throws IOException {
registrations.accept(Pair.of(
new ShaderInstance(resourceManager, "hexcasting__grayscale", DefaultVertexFormat.NEW_ENTITY),
inst -> grayscale = inst)
);
}
public static ShaderInstance grayscale() {
return grayscale;
}
}

View file

@ -0,0 +1,11 @@
package at.petrak.hexcasting.mixin.accessor.client;
import net.minecraft.client.renderer.RenderType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
@Mixin(RenderType.CompositeRenderType.class)
public interface AccessorCompositeRenderType {
@Invoker("state")
RenderType.CompositeState hex$state();
}

View file

@ -0,0 +1,14 @@
package at.petrak.hexcasting.mixin.accessor.client;
import net.minecraft.client.renderer.RenderStateShard;
import net.minecraft.resources.ResourceLocation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
import java.util.Optional;
@Mixin(RenderStateShard.EmptyTextureStateShard.class)
public interface AccessorEmptyTextureStateShard {
@Invoker("cutoutTexture")
Optional<ResourceLocation> hex$cutoutTexture();
}

View file

@ -0,0 +1,11 @@
package at.petrak.hexcasting.mixin.accessor.client;
import net.minecraft.client.renderer.RenderStateShard;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(RenderStateShard.class)
public interface AccessorRenderStateShard {
@Accessor("name")
String hex$name();
}

View file

@ -0,0 +1,17 @@
package at.petrak.hexcasting.mixin.accessor.client;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.renderer.RenderType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
// https://github.com/VazkiiMods/Botania/blob/13b7bcd9cbb6b1a418b0afe455662d29b46f1a7f/Xplat/src/main/java/vazkii/botania/mixin/client/AccessorRenderType.java
@Mixin(RenderType.class)
public interface AccessorRenderType {
@Invoker("create")
static RenderType.CompositeRenderType hex$create(String string, VertexFormat vertexFormat,
VertexFormat.Mode mode, int bufSize, boolean hasCrumbling, boolean sortOnUpload,
RenderType.CompositeState compositeState) {
throw new IllegalStateException();
}
}

View file

@ -0,0 +1,32 @@
#version 150
#moj_import <fog.glsl>
uniform sampler2D Sampler0;
uniform vec4 ColorModulator;
uniform float FogStart;
uniform float FogEnd;
uniform vec4 FogColor;
in float vertexDistance;
in vec4 vertexColor;
in vec4 lightMapColor;
in vec4 overlayColor;
in vec2 texCoord0;
in vec4 normal;
out vec4 fragColor;
void main() {
vec4 color = texture(Sampler0, texCoord0);
if (color.a < 0.1) {
discard;
}
color *= vertexColor * ColorModulator;
color.rgb = mix(overlayColor.rgb, color.rgb, overlayColor.a);
color *= lightMapColor;
float lum = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
color = vec4(lum, lum, lum, color.a);
fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor);
}

View file

@ -0,0 +1,34 @@
{
"blend": {
"func": "add",
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "rendertype_entity_cutout_no_cull",
"fragment": "hexcasting__grayscale",
"attributes": [
"Position",
"Color",
"UV0",
"UV1",
"UV2",
"Normal"
],
"samplers": [
{ "name": "Sampler0" },
{ "name": "Sampler1" },
{ "name": "Sampler2" }
],
"uniforms": [
{ "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] },
{ "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] },
{ "name": "IViewRotMat", "type": "matrix3x3", "count": 9, "values": [ 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 ] },
{ "name": "ColorModulator", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] },
{ "name": "Light0_Direction", "type": "float", "count": 3, "values": [0.0, 0.0, 0.0] },
{ "name": "Light1_Direction", "type": "float", "count": 3, "values": [0.0, 0.0, 0.0] },
{ "name": "FogStart", "type": "float", "count": 1, "values": [ 0.0 ] },
{ "name": "FogEnd", "type": "float", "count": 1, "values": [ 1.0 ] },
{ "name": "FogColor", "type": "float", "count": 4, "values": [ 0.0, 0.0, 0.0, 0.0 ] },
{ "name": "FogShape", "type": "int", "count": 1, "values": [ 0 ] }
]
}

View file

@ -0,0 +1,5 @@
accessWidener v1 named
accessible class net/minecraft/client/renderer/RenderType$CompositeRenderType
accessible class net/minecraft/client/renderer/RenderType$CompositeState
accessible field net/minecraft/client/renderer/RenderType$CompositeState textureState Lnet/minecraft/client/renderer/RenderStateShard$EmptyTextureStateShard;
accessible class net/minecraft/client/renderer/RenderStateShard$EmptyTextureStateShard

View file

@ -18,6 +18,10 @@
"accessor.CriteriaTriggersAccessor"
],
"client": [
"accessor.client.AccessorCompositeRenderType",
"accessor.client.AccessorEmptyTextureStateShard",
"accessor.client.AccessorRenderStateShard",
"accessor.client.AccessorRenderType",
"client.MixinClientLevel"
]
}

View file

@ -7,7 +7,7 @@ archivesBaseName = getArtifactID("fabric")
loom {
mixin.defaultRefmapName = "hexcasting.mixins.refmap.json"
accessWidenerPath = file("src/main/resources/hexcasting.accesswidener")
accessWidenerPath = file("src/main/resources/fabricasting.accesswidener")
runs {
client {

View file

@ -1,6 +1,8 @@
package at.petrak.hexcasting.fabric.interop.emi;
import at.petrak.hexcasting.client.ClientTickCounter;
import at.petrak.hexcasting.client.shader.FakeBufferSource;
import at.petrak.hexcasting.client.shader.HexRenderTypes;
import at.petrak.hexcasting.common.recipe.ingredient.VillagerIngredient;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
@ -133,7 +135,8 @@ public class VillagerEmiStack extends EmiStack {
RenderSystem.enableBlend();
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
renderEntity(poseStack, villager, level, x + 8, y + 16, ClientTickCounter.total, 8, 0);
renderEntity(poseStack, villager, level, x + 8, y + 16, ClientTickCounter.total, 8, 0,
mindless ? (it) -> new FakeBufferSource(it, HexRenderTypes::getGrayscaleLayer) : it -> it);
}
}

View file

@ -0,0 +1,38 @@
package at.petrak.hexcasting.fabric.mixin.client;
import at.petrak.hexcasting.client.shader.HexShaders;
import com.mojang.blaze3d.shaders.Program;
import com.mojang.datafixers.util.Pair;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.server.packs.resources.ResourceManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;
// https://github.com/VazkiiMods/Botania/blob/b1f1cf80e10b1c739b0188171b367d9cefc4d3c7/Fabric/src/main/java/vazkii/botania/fabric/mixin/client/FabricMixinGameRenderer.java
@Mixin(GameRenderer.class)
public class FabricMixinGameRenderer {
@SuppressWarnings("InvalidInjectorMethodSignature")
@Inject(
method = "reloadShaders",
at = @At(
value = "INVOKE_ASSIGN",
target = "Lcom/google/common/collect/Lists;newArrayListWithCapacity(I)Ljava/util/ArrayList;",
remap = false
),
locals = LocalCapture.CAPTURE_FAILHARD
)
private void loadShaders(ResourceManager resourceManager, CallbackInfo ci,
List<Program> _programsToClose,
List<Pair<ShaderInstance, Consumer<ShaderInstance>>> shadersToLoad)
throws IOException {
HexShaders.init(resourceManager, shadersToLoad::add);
}
}

View file

@ -1,12 +0,0 @@
package at.petrak.hexcasting.fabric.mixin.client.accessor;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.resources.ResourceLocation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
@Mixin(GameRenderer.class)
public interface FabricAccessorGameRenderer {
@Invoker("loadEffect")
void hex$loadEffect(ResourceLocation resourceLocation);
}

View file

@ -41,7 +41,7 @@
"hexplat.mixins.json",
"fabricasting.mixins.json"
],
"accessWidener": "hexcasting.accesswidener",
"accessWidener": "fabricasting.accesswidener",
"depends": {
"fabricloader": ">=0.13",

View file

@ -6,5 +6,7 @@ accessible class net/minecraft/world/item/crafting/Ingredient$Value
accessible method net/minecraft/world/item/crafting/Ingredient$ItemValue <init> (Lnet/minecraft/world/item/ItemStack;)V
accessible method net/minecraft/world/item/crafting/Ingredient$TagValue <init> (Lnet/minecraft/tags/TagKey;)V
accessible method net/minecraft/world/item/crafting/Ingredient <init> (Ljava/util/stream/Stream;)V
accessible class net/minecraft/client/renderer/RenderType$CompositeRenderType
accessible class net/minecraft/client/renderer/RenderType$CompositeState
accessible field net/minecraft/client/renderer/RenderType$CompositeState textureState Lnet/minecraft/client/renderer/RenderStateShard$EmptyTextureStateShard;
accessible class net/minecraft/client/renderer/RenderStateShard$EmptyTextureStateShard

View file

@ -16,6 +16,6 @@
"client.FabricAbstractTextureMixin",
"client.FabricMouseHandlerMixin",
"client.FabricParticleEngineMixin",
"client.accessor.FabricAccessorGameRenderer"
"client.FabricMixinGameRenderer"
]
}

View file

@ -24,6 +24,7 @@ apply from: 'https://raw.githubusercontent.com/thedarkcolour/KotlinForForge/site
minecraft {
mappings channel: 'official', version: minecraftVersion
accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
if (project.hasProperty('forge_ats_enabled') && project.findProperty('forge_ats_enabled').toBoolean()) {
// This location is hardcoded in Forge and can not be changed.

View file

@ -4,6 +4,7 @@ import at.petrak.hexcasting.client.ClientTickCounter;
import at.petrak.hexcasting.client.HexAdditionalRenderers;
import at.petrak.hexcasting.client.RegisterClientStuff;
import at.petrak.hexcasting.client.ShiftScrollListener;
import at.petrak.hexcasting.client.shader.HexShaders;
import net.minecraftforge.client.event.*;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;
@ -11,6 +12,8 @@ import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import java.io.IOException;
// This is Java because I can't kotlin-fu some of the consumers
public class ForgeHexClientInitializer {
@SubscribeEvent
@ -48,6 +51,11 @@ public class ForgeHexClientInitializer {
});
}
@SubscribeEvent
public static void registerShaders(RegisterShadersEvent evt) throws IOException {
HexShaders.init(evt.getResourceManager(), p -> evt.registerShader(p.getFirst(), p.getSecond()));
}
@SubscribeEvent(priority = EventPriority.LOWEST)
public static void registerParticles(ParticleFactoryRegisterEvent evt) {
RegisterClientStuff.registerParticles();

View file

@ -0,0 +1,2 @@
public net.minecraft.client.renderer.RenderType$CompositeRenderType
public net.minecraft.client.renderer.RenderType$CompositeState f_110576_ # textureState