Skip to content

Commit

Permalink
compatible with optiFine (#306)
Browse files Browse the repository at this point in the history
* compatible with optiFine

* glass, shaders, fusion reactor
  • Loading branch information
Yefancy authored Dec 8, 2021
1 parent a1329de commit 5a65647
Show file tree
Hide file tree
Showing 37 changed files with 380 additions and 74 deletions.
3 changes: 3 additions & 0 deletions src/main/java/gregtech/api/capability/GregtechDataCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public class GregtechDataCodes {
public static final int SYNC_FLUID_CHANGE = 3;
public static final int SYNC_TANK_SHAPE = 4;

// Fusion Reactor
public static final int UPDATE_COLOR = 371;

// Central Monitor
public static final int UPDATE_ALL = 1;
public static final int UPDATE_COVERS = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public RecipeMap<?> getRecipeMap() {
return this.recipeMap;
}

public Recipe getPreviousRecipe() {
return previousRecipe;
}

protected boolean shouldSearchForRecipes() {
return canWorkWithInputs() && canFitNewOutputs();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/cover/CoverBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public <T> T getCapability(Capability<T> capability, T defaultValue) {

@SideOnly(Side.CLIENT)
public boolean canRenderInLayer(BlockRenderLayer renderLayer) {
return renderLayer == BlockRenderLayer.CUTOUT || renderLayer == BloomRenderLayerHooks.BLOOM;
return renderLayer == BlockRenderLayer.CUTOUT || renderLayer == BloomRenderLayerHooks.getRealBloomLayer();
}

@SideOnly(Side.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation,

@SideOnly(Side.CLIENT)
public boolean canRenderInLayer(BlockRenderLayer renderLayer) {
return renderLayer == BlockRenderLayer.CUTOUT_MIPPED || renderLayer == BloomRenderLayerHooks.BLOOM;
return renderLayer == BlockRenderLayer.CUTOUT_MIPPED || renderLayer == BloomRenderLayerHooks.getRealBloomLayer();
}

@SideOnly(Side.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import gregtech.api.gui.Widget;
import gregtech.api.gui.widgets.AdvancedTextWidget;
import gregtech.api.metatileentity.sound.ISoundCreator;
import gregtech.api.metatileentity.sound.PositionedSoundMTE;
import gregtech.api.multiblock.PatternMatchContext;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeMap;
Expand All @@ -29,15 +28,12 @@
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.Style;
Expand Down Expand Up @@ -156,7 +152,7 @@ public void onRemoval() {
protected void updateFormedValid() {
if (!hasMufflerMechanics() || isMufflerFaceFree())
this.recipeMapWorkable.updateWorkable();
boolean state = this.recipeMapWorkable.isWorking() && ConfigHolder.U.clientConfig.casingsActiveEemissiveTextures;
boolean state = this.recipeMapWorkable.isWorking() && ConfigHolder.U.clientConfig.casingsActiveEmissiveTextures;
if (lastActive != state) {
lastActive = state;
replaceVariantBlocksActive(lastActive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public BakedQuad rebake(Builder builder) {
for (int i = 0; i < format.getElementCount(); i++) {
VertexFormatElement ele = format.getElement(i);
//Stuff for Light or UV
if (ele.getUsage() == VertexFormatElement.EnumUsage.UV) {
if (ele.getUsage() == VertexFormatElement.EnumUsage.COLOR) {
unpackedBuilder.put(i, 1, 1, 1, 1);
} else if (ele.getUsage() == VertexFormatElement.EnumUsage.UV) {
if (ele.getIndex() == 1) {
unpackedBuilder.put(i, ((float) blockLight * 0x20) / 0xFFFF, ((float) skyLight * 0x20) / 0xFFFF);
} else if (ele.getIndex() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.*;
import gregtech.core.hooks.BlockHooks;
import gregtech.core.hooks.CTMHooks;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.*;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
Expand Down Expand Up @@ -146,7 +147,7 @@ public List<BakedQuad> getQuads(@Nullable IBlockState state, @Nullable EnumFacin
.collect(Collectors.toList()));
}
}
return ret;
return CTMHooks.getQuadsWithOptiFine(ret, layer, this, state, side, rand);
} catch (ExecutionException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import gregtech.core.hooks.CTMHooks;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.BlockPart;
import net.minecraft.client.renderer.block.model.IBakedModel;
Expand Down Expand Up @@ -49,7 +50,8 @@ public IModel getVanillaParent() {
}

public boolean canRenderInLayer(IBlockState state, BlockRenderLayer layer) {
return (layers < 0 && state.getBlock().getRenderLayer() == layer) || ((layers >> layer.ordinal()) & 1) == 1;
boolean flag = (layers < 0 && state.getBlock().getRenderLayer() == layer) || ((layers >> layer.ordinal()) & 1) == 1;
return CTMHooks.checkLayerWithOptiFine(flag, layers, layer);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/gregtech/api/render/DepthTextureHook.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.api.render;

import gregtech.api.render.shader.Shaders;
import gregtech.api.util.RenderUtil;
import gregtech.common.ConfigHolder;
import gregtech.core.hooks.BloomRenderLayerHooks;
Expand Down Expand Up @@ -37,7 +38,7 @@ public class DepthTextureHook {
private static int lastWidth, lastHeight;

private static boolean shouldRenderDepthTexture() {
return lastBind && ConfigHolder.U.clientConfig.hookDepthTexture && OpenGlHelper.isFramebufferEnabled();
return lastBind && !Shaders.isOptiFineShaderPackLoaded() && ConfigHolder.U.clientConfig.hookDepthTexture && OpenGlHelper.isFramebufferEnabled();
}

public static void onPreWorldRender(TickEvent.RenderTickEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void render(CCRenderState renderState, Matrix4 translation, IVertexOperat

TextureAtlasSprite emissiveSprite = predicate.getEmissiveSprite(isActive, isWorkingEnabled);
if (emissiveSprite != null) {
if (ConfigHolder.U.clientConfig.machinesEemissiveTextures) {
if (ConfigHolder.U.clientConfig.machinesEmissiveTextures) {
IVertexOperation[] lightPipeline = ArrayUtils.add(ops, new LightMapOperation(240, 240));
Textures.renderFaceBloom(renderState, translation, lightPipeline, renderSide, bounds, emissiveSprite);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/render/SimpleCubeRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void registerIcons(TextureMap textureMap) {
public void renderSided(EnumFacing side, Matrix4 translation, Cuboid6 bounds, CCRenderState renderState, IVertexOperation[] pipeline) {
Textures.renderFace(renderState, translation, pipeline, side, bounds, sprite);
if (spriteEmissive != null) {
if (ConfigHolder.U.clientConfig.machinesEemissiveTextures) {
if (ConfigHolder.U.clientConfig.machinesEmissiveTextures) {
IVertexOperation[] lightPipeline = ArrayUtils.add(pipeline, new LightMapOperation(240, 240));
Textures.renderFaceBloom(renderState, translation, lightPipeline, side, bounds, spriteEmissive);
} else Textures.renderFace(renderState, translation, pipeline, side, bounds, spriteEmissive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void render(CCRenderState renderState, Matrix4 translation, IVertexOperat
Textures.renderFace(renderState, translation, ops, frontFacing.rotateY(), bounds, sprites.get(CubeSide.LEFT));
Textures.renderFace(renderState, translation, ops, frontFacing.rotateYCCW(), bounds, sprites.get(CubeSide.RIGHT));

IVertexOperation[] lightPipeline = ConfigHolder.U.clientConfig.machinesEemissiveTextures ?
IVertexOperation[] lightPipeline = ConfigHolder.U.clientConfig.machinesEmissiveTextures ?
ArrayUtils.add(ops, new LightMapOperation(240, 240)) : ops;

if (spritesEmissive.containsKey(CubeSide.TOP)) Textures.renderFaceBloom(renderState, translation, lightPipeline, EnumFacing.UP, bounds, sprites.get(CubeSide.TOP));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void registerIcons(TextureMap textureMap) {
public void renderSided(EnumFacing side, Cuboid6 bounds, CCRenderState renderState, IVertexOperation[] pipeline, Matrix4 translation) {
Textures.renderFace(renderState, translation, pipeline, side, bounds, sprite);
if (spriteEmissive != null) {
if (ConfigHolder.U.clientConfig.machinesEemissiveTextures) {
if (ConfigHolder.U.clientConfig.machinesEmissiveTextures) {
IVertexOperation[] lightPipeline = ArrayUtils.add(pipeline, new LightMapOperation(240, 240));
Textures.renderFaceBloom(renderState, translation, lightPipeline, side, bounds, spriteEmissive);
} else Textures.renderFace(renderState, translation, pipeline, side, bounds, spriteEmissive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void render(CCRenderState renderState, Matrix4 translation, IVertexOperat
Textures.renderFace(renderState, translation, pipeline, renderSide, bounds, renderSprite);
TextureAtlasSprite spriteEmissive = spritesEmissive.get(overlayFace);
if (spriteEmissive != null) {
if (ConfigHolder.U.clientConfig.machinesEemissiveTextures) {
if (ConfigHolder.U.clientConfig.machinesEmissiveTextures) {
IVertexOperation[] lightPipeline = ArrayUtils.add(pipeline, new LightMapOperation(240, 240));
Textures.renderFaceBloom(renderState, translation, lightPipeline, renderSide, bounds, spriteEmissive);
} else Textures.renderFace(renderState, translation, pipeline, renderSide, bounds, spriteEmissive);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/render/Textures.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public static void renderFaceRaw(CCRenderState renderState, Matrix4 translation,
public static void renderFaceBloom(CCRenderState renderState, Matrix4 translation, IVertexOperation[] ops, EnumFacing face, Cuboid6 bounds, TextureAtlasSprite sprite) {
if (ops.length > 0 && ops[0] instanceof GTBlockOperation) {
GTBlockOperation op = (GTBlockOperation) ops[0];
if (op.layer != BloomRenderLayerHooks.BLOOM || !op.shouldSideBeRendered(face, bounds)) {
if (op.layer != BloomRenderLayerHooks.getRealBloomLayer() || !op.shouldSideBeRendered(face, bounds)) {
return;
}
}
Expand Down
32 changes: 27 additions & 5 deletions src/main/java/gregtech/api/render/shader/Shaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import gregtech.common.ConfigHolder;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
Expand All @@ -19,18 +18,18 @@
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.opengl.GL30;

import javax.annotation.Nonnull;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;

import static codechicken.lib.render.shader.ShaderHelper.getStream;
import static codechicken.lib.render.shader.ShaderHelper.readShader;
import static codechicken.lib.render.shader.ShaderObject.ShaderType.FRAGMENT;
import static codechicken.lib.render.shader.ShaderObject.ShaderType.VERTEX;
import static org.lwjgl.opengl.GL11.glGetInteger;

/**
* Created with IntelliJ IDEA.
Expand Down Expand Up @@ -61,15 +60,34 @@ public class Shaders {
public static ShaderObject S_BLUR;
public static ShaderObject COMPOSITE;



// OptiFine
private static BooleanSupplier isShaderPackLoaded;

static {
mc = Minecraft.getMinecraft();
FULL_IMAGE_PROGRAMS = new HashMap<>();
if (allowedShader()) {
initShaders();
}
try { // hook optFine. thanks to Scannable.
final Class<?> clazz = Class.forName("net.optifine.shaders.Shaders");
final Field shaderPackLoaded = clazz.getDeclaredField("shaderPackLoaded");
shaderPackLoaded.setAccessible(true);
isShaderPackLoaded = () -> {
try {
return shaderPackLoaded.getBoolean(null);
} catch (final IllegalAccessException e) {
GTLog.logger.warn("Failed reading field indicating whether shaders are enabled. Shader mod integration disabled.");
isShaderPackLoaded = null;
return false;
}
};
GTLog.logger.info("Find optiFine mod loaded.");
} catch (ClassNotFoundException e) {
GTLog.logger.info("No optiFine mod found.");
} catch (NoSuchFieldException | NoClassDefFoundError e) {
GTLog.logger.warn("Failed integrating with shader mod. Ignoring.");
}
}

public static void initShaders() {
Expand Down Expand Up @@ -110,6 +128,10 @@ public static boolean allowedShader() {
return OpenGlHelper.shadersSupported && ConfigHolder.U.clientConfig.shader.useShader;
}

public static boolean isOptiFineShaderPackLoaded() {
return isShaderPackLoaded != null && isShaderPackLoaded.getAsBoolean();
}

public static Framebuffer renderFullImageInFBO(Framebuffer fbo, ShaderObject frag, Consumer<ShaderProgram.UniformCache> uniformCache) {
if (fbo == null || frag == null || !allowedShader()) return fbo;
// int lastID = glGetInteger(GL30.GL_FRAMEBUFFER_BINDING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ private static void blend(Framebuffer bloom, Framebuffer backgroundFBO, float st
uniformCache.glUniform1I("buffer_a", 0);
uniformCache.glUniform1I("buffer_b", 1);
uniformCache.glUniform1F("intensive", strength);
uniformCache.glUniform1F("threshold_up", (float) ConfigHolder.U.clientConfig.shader.bloom.highBrightnessThreshold);
uniformCache.glUniform1F("threshold_down", (float) ConfigHolder.U.clientConfig.shader.bloom.lowBrightnessThreshold);
});

GlStateManager.setActiveTexture(GL13.GL_TEXTURE1);
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/gregtech/api/util/RenderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import codechicken.lib.texture.TextureUtils;
import codechicken.lib.vec.Matrix4;
import gregtech.api.gui.Widget;
import gregtech.api.gui.resources.TextureArea;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
Expand All @@ -12,8 +11,8 @@
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.item.ItemStack;
import net.minecraft.client.shader.Framebuffer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.GuiIngameForge;
Expand All @@ -31,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.function.Function;

@SideOnly(Side.CLIENT)
public class RenderUtil {
Expand Down Expand Up @@ -274,6 +274,25 @@ public static Matrix4 adjustTrans(Matrix4 translation, EnumFacing side, int laye
return trans;
}

public static Function<Float, Integer> colorInterpolator(int color1, int color2) {
int a = color1 >> 24 & 255;
int r = color1 >> 16 & 255;
int g = color1 >> 8 & 255;
int b = color1 & 255;

int a2 = color2 >> 24 & 255;
int r2 = color2 >> 16 & 255;
int g2 = color2 >> 8 & 255;
int b2 = color2 & 255;
return (f)->{
int A = (int) (a * (1 - f) + a2 * (f));
int R = (int) (r * (1 - f) + r2 * (f));
int G = (int) (g * (1 - f) + g2 * (f));
int B = (int) (b * (1 - f) + b2 * (f));
return A << 24 | R << 16 | G << 8 | B;
};
}

public static void renderRect(float x, float y, float width, float height, float z, int color) {
float f3 = (float)(color >> 24 & 255) / 255.0F;
float f = (float)(color >> 16 & 255) / 255.0F;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/gregtech/common/ConfigHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ public static class ClientConfig {
public double resolution = 2;

@Config.Comment("Whether or not to enable Emissive Textures for GregTech Machines. Default: true")
public boolean machinesEemissiveTextures = true;
public boolean machinesEmissiveTextures = true;

@Config.Comment("Whether or not to enable Emissive Textures for GregTech Casings when the multi-block is working. Default: true")
public boolean casingsActiveEemissiveTextures = true;
public boolean casingsActiveEmissiveTextures = true;

@Config.Comment("Shader Settings")
public Shader shader = new Shader();
Expand All @@ -324,6 +324,14 @@ public static class Bloom {
@Config.RangeInt(min = 0, max = 2)
public int bloomStyle = 2;

@Config.Comment("The brightness after bloom should not exceed this value. It can be used to limit the brightness of highlights (e.g., daytiome. and this value should be greater than lowBrightnessThreshold)")
@Config.RangeDouble(min = 0)
public double highBrightnessThreshold = 1.3;

@Config.Comment("The brightness after bloom should not smaller than this value. It can be used to limit the brightness of dusky parts (e.g., night/caves. and this value should be smaller than highBrightnessThreshold)")
@Config.RangeDouble(min = 0)
public double lowBrightnessThreshold = 0.3;

@Config.Comment("Mipmap Size. (2-5)=>(fast-slow)=>(low quality-high quality)")
@Config.RangeInt(min = 2, max = 5)
public int nMips = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
public class BlockTransparentCasing extends VariantActiveBlock<BlockTransparentCasing.CasingType> {
public class BlockGlassCasing extends VariantActiveBlock<BlockGlassCasing.CasingType> {

public BlockTransparentCasing() {
public BlockGlassCasing() {
super(Material.IRON);
setTranslationKey("transparent_casing");
setHardness(5.0f);
Expand All @@ -37,7 +37,7 @@ public boolean canCreatureSpawn(IBlockState state, IBlockAccess world, BlockPos
@Override
@Nonnull
public BlockRenderLayer getRenderLayer() {
return BlockRenderLayer.TRANSLUCENT;
return BlockRenderLayer.CUTOUT; // The glasses here dont have the transparency as an ice block, and should not be rendered in TRANSLUCENT.
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gregtech/common/blocks/MetaBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private MetaBlocks() {
public static BlockMachineCasing MACHINE_CASING;
public static BlockSteamCasing STEAM_CASING;
public static BlockMultiblockCasing MULTIBLOCK_CASING;
public static BlockTransparentCasing TRANSPARENT_CASING;
public static BlockGlassCasing TRANSPARENT_CASING;
public static BlockWireCoil WIRE_COIL;
public static BlockWireCoil2 WIRE_COIL2;
public static BlockFusionCasing FUSION_CASING;
Expand Down Expand Up @@ -147,7 +147,7 @@ public static void init() {
STEAM_CASING.setRegistryName("steam_casing");
MULTIBLOCK_CASING = new BlockMultiblockCasing();
MULTIBLOCK_CASING.setRegistryName("multiblock_casing");
TRANSPARENT_CASING = new BlockTransparentCasing();
TRANSPARENT_CASING = new BlockGlassCasing();
TRANSPARENT_CASING.setRegistryName("transparent_casing");
WIRE_COIL = new BlockWireCoil();
WIRE_COIL.setRegistryName("wire_coil");
Expand Down
Loading

0 comments on commit 5a65647

Please sign in to comment.