Jump to content

[Solved] Options for altering alpha of vanilla / other mods' mobs


Laike_Endaril

Recommended Posts

Have a mod which could benefit greatly from fading mobs in and out, alpha-wise, but haven't found a solution I like yet.  If it were just my own mobs it would be easy, ofc, but I'd need it for *all* mobs.

 

The obvious answer (if you're familiar with openGL) is to set the openGL rendering settings correctly, but I tried this in RenderLivingEvent.Pre and did not get any results (enabled alpha and set color to (1f, 1f, 1f, 0.4f)).  If someone has tips on hacking this method to work I'd appreciate that.

 

I know a shader would work, but I'm trying to avoid that so as not to break compatibility with other shader mods.

 

Replacing vertex data in existing models would also work in theory, but would probably be extremely inefficient, since I'd need to generate (currentModelCount * alphaSteps) models, right?

 

In any case, looking for the most "normal" way to accomplish this, if there is such a thing.

 

 

 

Edit: I made a dumb mistake and used enableAlpha/disableAlpha instead of enableBlend/disableBlend.  This code works fine:
 

Spoiler

@SubscribeEvent
public static void preRender(RenderLivingEvent.Pre event)
{
    GlStateManager.enableBlend();
    GlStateManager.color(1, 1, 1, 0.4f);
}

@SubscribeEvent
public static void postRender(RenderLivingEvent.Post event)
{
    GlStateManager.color(1, 1, 1, 1);
    GlStateManager.disableBlend();
}

 

 

Edited by Laike_Endaril
Solved
Link to comment
Share on other sites

Does running your event subscriber with LOWEST priority fix it?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

1 hour ago, Cadiboo said:

Does running your event subscriber with LOWEST priority fix it?

When I tested using the event mentioned, my mod was the only one loaded, so I'm automatically both LOWEST and HIGHEST, since there are no other mods loaded to add event subscribers.  But yeah, I've had to solve some mod compatibility stuff using priorities before, and it is something I will keep in mind for compat if I get this working on vanilla first.

Link to comment
Share on other sites

The event is probably technically working, but I suspect that most rendering code overwrites whatever you do there. The rendering events are good for ADDING something else to be rendered, or fully replacing what is rendered, but not really good for changing what is rendered. 

 

It is actually usually good practice for any rendering code to sort of "reset" some of the GL stuff to a known state, exactly so other code running before it can't mess it up.

 

Now Minecraft code is quite inconsistent so you might get lucky and find some places where a judiciously placed alpha change will affect the next thing rendered but it would be hard to count on it, as you found out.

 

If you're really good at GL, you might be able to push/pop a matrix to apply the alpha but you'd have to take care to handle the events symmetrically so that you don't end up overflowing the matrix stack.

 

If you're ambitious you can replace all the renderers for the vanilla entities with ones how you want, but doing it for other mobs would be tough.

 

Sorry, maybe someone has a clever idea to do what you're looking for. Otherwise, I'd trace through the various rendering code to see if there is a place to surgically insert an alpha change.

 

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Yeah, what I tried before was a temporary test; I'm decent with openGL so I (usually *cough*) make sure to push/pop or reset parameters when I'm done.

 

Dang, was hoping I had missed something handy, but so far it seems about as much trouble as I thought it would be...I may poke around like you said and see if there's anything global-ish that I can intercept (if it doesn't do *all mobs* or at least nearly all mobs then it wouldn't be worth doing).

 

I'll be sure to post if I find anything workable that doesn't involve loads of incompatibility issues :P

Link to comment
Share on other sites

I can think of a dirty hack (asside from a coremod)

use GLStateManager to set the alpha to what it’s going to be set in the render call (so the state manager will ignore the next call) then directly set you values with GL11.

In the post event set it back with GL11/change it to something else with GLStateManager

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

17 hours ago, Cadiboo said:

use GLStateManager to set the alpha to what it’s going to be set in the render call (so the state manager will ignore the next call) then directly set you values with GL11.

 

I actually considered this, but was iffy about it because I have no way of knowing for sure what it will be set to for some random mod's mobs (though surely in most cases it will be 1; fully opaque).  One other issue would be if the alpha settings are set more than once between when I set them and when the entity is rendered, because there's no way for me to prevent GLStateManager from passing it if it changes twice.

 

HOWEVER, by suggesting it you got me to go look at the GLStateManager class again, and made me realize something extremely useful, so thanks, Cadiboo!

 

Specifically this, which I am quite excited about now:
 

Spoiler

    private static final FloatBuffer BUF_FLOAT_16 = BufferUtils.createFloatBuffer(16);
    private static final FloatBuffer BUF_FLOAT_4 = BufferUtils.createFloatBuffer(4);
    private static final GlStateManager.AlphaState alphaState = new GlStateManager.AlphaState();
    private static final GlStateManager.BooleanState lightingState = new GlStateManager.BooleanState(2896);
    private static final GlStateManager.BooleanState[] lightState = new GlStateManager.BooleanState[8];
    private static final GlStateManager.ColorMaterialState colorMaterialState;
    private static final GlStateManager.BlendState blendState;
    private static final GlStateManager.DepthState depthState;
    private static final GlStateManager.FogState fogState;
    private static final GlStateManager.CullState cullState;
    private static final GlStateManager.PolygonOffsetState polygonOffsetState;
    private static final GlStateManager.ColorLogicState colorLogicState;
    private static final GlStateManager.TexGenState texGenState;
    private static final GlStateManager.ClearState clearState;
    private static final GlStateManager.StencilState stencilState;
    private static final GlStateManager.BooleanState normalizeState;
    private static int activeTextureUnit;
    private static final GlStateManager.TextureState[] textureState;
    private static int activeShadeModel;
    private static final GlStateManager.BooleanState rescaleNormalState;
    private static final GlStateManager.ColorMask colorMaskState;
    private static final GlStateManager.Color colorState;

 

 

All the cached openGL states it uses are *OBJECT INSTANCES!*

 

I can most likely extend them and reflect-replace to accomplish my goal.  I also doubt this would have many (if any) compatibility issues with other mods, since I wouldn't expect many people to be messing with these (I can accept the off-chance of 1 or 2 incompatible mods).


I'll tinker with this after I take care of a couple other things, and post back here after I try it!

Link to comment
Share on other sites

But unfortunately I hadn't realized GlStateManager.AlphaState only has a private constructor, and so I cannot extend it.  So that's about as far as I got with my bright idea lol

 

While I was at it, I also tried what you suggested @Cadiboo, but had no luck there either:
 

    @SubscribeEvent
    public static void preRender(RenderLivingEvent.Pre event)
    {
        GlStateManager.disableAlpha();
        GlStateManager.color(1, 1, 1, 1);
        GL11.glEnable(GL11.GL_ALPHA);
        GL11.glColor4f(1, 1, 1, 0.4f);
    }

    @SubscribeEvent
    public static void postRender(RenderLivingEvent.Post event)
    {
        GlStateManager.enableAlpha();
        GlStateManager.color(1, 1, 1, 0.4f);
        GlStateManager.disableAlpha();
        GlStateManager.color(1, 1, 1, 1);
    }

 

This code had no visible effect (and yes, that class was on the event bus; other, unrelated events in the class were firing on the client)

 

I'll probably put this feature on hold for now and maybe come back to it later and try one of the other approaches

Link to comment
Share on other sites

I wouldn't usually recommend this, but as it has a private constructor you probably won't mess with anything. You could use an Access Transformer

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Alright, so I made a dumb mistake and used enableAlpha / disableAlpha instead of enableBlend / disableBlend for one of the first, most simple methods of accomplishing this.

 

The following works just fine... :P
 

Quote

@SubscribeEvent
public static void preRender(RenderLivingEvent.Pre event)
{
    GlStateManager.enableBlend();
    GlStateManager.color(1, 1, 1, 0.4f);
}

@SubscribeEvent
public static void postRender(RenderLivingEvent.Post event)
{
    GlStateManager.color(1, 1, 1, 1);
    GlStateManager.disableBlend();
}

 

 

Yep...on the bright side, now I know how to use ATs due to my dumb mistake.  Thanks to Tama on the MMD discord for smacking me in the face

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.