Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16] Need help with LayerRenderer
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
Tavi007

[1.16] Need help with LayerRenderer

By Tavi007, Saturday at 07:10 PM in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 107 posts
Posted Saturday at 07:10 PM

Hello!

I've been trying to add a special LayerRenderer, but I ran into a few problems. I want a layer that
1. can be applied to any LivingEntity.
2. is applied for a limited duration.
3. has a green, but also transparent, texture.
4. is the outermost layer.

My ultimate goal is to have a green flash, similar to the red flash, that happens, when the entity gets damaged. If you know of another method besides layers, please let me know.

I already tried to implement the renderer myself and this is my current state:

In RenderEvents.java:

	@SubscribeEvent
	public static void onRenderLivingEventPre(RenderLivingEvent.Pre<LivingEntity, EntityModel<LivingEntity>> event) {
		LivingEntity entityIn = event.getEntity();
		ImmersionData data = (ImmersionData) entityIn.getCapability(ImmersionDataCapability.IMMERSION_DATA_CAPABILITY, null).orElse(new ImmersionData());
		if(entityIn.hurtTime > 0) {
			if (data.disableFlag) {
				data.setHurtTime(entityIn.hurtTime);
				entityIn.hurtTime = 0; //desync client and server hurtTime. Is this a problem?

				//to do: add green overlay texture
				if (flag) {
					TransparentGreenLayer layer = new TransparentGreenLayer(event.getRenderer());
					event.getRenderer().addLayer(layer);
					flag = false;
				}
			}
		}
		else {
			data.disableFlag = false;
		}

	}

TransparentGreenLayer.java:

public class TransparentGreenLayer extends LayerRenderer<LivingEntity, EntityModel<LivingEntity>> {
	
	private final ResourceLocation transparentGreenTexture = new ResourceLocation(ElementalCombat.MOD_ID, "textures/models/transparent_green.png");
	
	public TransparentGreenLayer(IEntityRenderer<LivingEntity, EntityModel<LivingEntity>> p_i226040_1_) {
		super(p_i226040_1_);
	}

	@Override
	public void render(MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int packedLightIn, LivingEntity entitylivingbaseIn, float limbSwing, float limbSwingAmount, float partialTicks, float ageInTicks, float netHeadYaw, float headPitch) {
		if (!entitylivingbaseIn.isInvisible()) {
			renderCutoutModel(this.getEntityModel(), transparentGreenTexture, matrixStackIn, bufferIn, packedLightIn, entitylivingbaseIn, 1.0F, 1.0F, 1.0F);
		}
	}
}

Rest can be found here (https://github.com/Tavi007/ElementalCombat/tree/testingGreenFlash)

 

Currently the layer will be applied, but it's not transparent and once applied will stay permanently. Also it's applied to all entities (of the same type) simultaneously and some layers are applied after mine. The result looks like this:
 

image.png.8fa85914d4596f9f6685a357ac22e190.png


So can anyone help me please? :)

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7688

diesieben07

diesieben07    7688

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7688
  • 56257 posts
Posted Saturday at 07:18 PM

First of all, do not use RenderLivingEvent to add layers, that event fires every frame for every rendered entity. Adding your layer there means it will be added multiple times. Your layer should always be there and then the layer needs to check if it needs to render, or not.

As for making it transparent, have you looked at how the red tint works?

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 107 posts
Posted Saturday at 07:44 PM

Okay, which event would be better suited for adding the layer? And you mean, I need to add a check in TransparentGreenLayer#render ?

As for the red flash, I found the following.
From LivingRenderer:

public void render(T entityIn, float entityYaw, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int packedLightIn) {
      
		...
      
      if (rendertype != null) {
         IVertexBuilder ivertexbuilder = bufferIn.getBuffer(rendertype);
         int i = getPackedOverlay(entityIn, this.getOverlayProgress(entityIn, partialTicks));
         this.entityModel.render(matrixStackIn, ivertexbuilder, packedLightIn, i, 1.0F, 1.0F, 1.0F, flag1 ? 0.15F : 1.0F);
      }
   }

   public static int getPackedOverlay(LivingEntity livingEntityIn, float uIn) {
      return OverlayTexture.getPackedUV(OverlayTexture.getU(uIn), OverlayTexture.getV(livingEntityIn.hurtTime > 0 || livingEntityIn.deathTime > 0));
   }

	...
}

Following the stacktrace to IVertexBuilder I found this part:


   default IVertexBuilder overlay(int overlayUV) {
      return this.overlay(overlayUV & '\uffff', overlayUV >> 16 & '\uffff');
   }

The input in overla() is the same as the return value from getPackedOverlay(). I don't really understand, what the method from IVertexBuilder does here, nor do I not understand how I could use it to change the red color to a green one...

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7688

diesieben07

diesieben07    7688

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7688
  • 56257 posts
Posted Saturday at 07:54 PM
9 minutes ago, Tavi007 said:

Okay, which event would be better suited for adding the layer?

At startup, so probably FMLClientSetupEvent with enqueueWork.

 

10 minutes ago, Tavi007 said:

And you mean, I need to add a check in TransparentGreenLayer#render ?

Yes.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 107 posts
Posted Sunday at 06:06 PM
21 hours ago, diesieben07 said:

At startup, so probably FMLClientSetupEvent with enqueueWork.

I'm a bit puzzled right now. Do I have to add the layer to every LivingEntityRenderer using a loop or are the 'base' classes MobRenderer and PlayerRenderer enough? In both cases I do not understand, how to do this exactly. All I know is Minecraft#getInstance().getRenderManager()...

I do get, that moving the logic to the render function will fix the problem with the infinite duration. But what about the semi transparent texture? Will that even work this way?

Besides what about the vanilla way. Reading through the minecraft code I didn't found a way to use the vanilla OverlayTexture directly. Or did you notice something?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7688

diesieben07

diesieben07    7688

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7688
  • 56257 posts
Posted Sunday at 07:32 PM
1 hour ago, Tavi007 said:

Do I have to add the layer to every LivingEntityRenderer

If you want it to appear on every renderer then yes.

 

I can't really help you with rendering directly, sorry.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 107 posts
Posted Sunday at 10:05 PM
2 hours ago, diesieben07 said:

If you want it to appear on every renderer then yes.

Do you know, how I could do a loop over all registered (Living-)EntityRenderer? You can start from the RenderManager, if it's the right direction...

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7688

diesieben07

diesieben07    7688

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7688
  • 56257 posts
Posted Sunday at 10:06 PM

Loop through ForgeRegistries.ENTITIES to get all entities.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 107 posts
Posted Monday at 08:18 AM
9 hours ago, diesieben07 said:

Loop through ForgeRegistries.ENTITIES to get all entities.

That didn't really help, because I don't know how I would get the renderer for each entity. I might have found an other solution. I added this to my FMLClientSetupEvent subscriber:

		Minecraft.getInstance().getRenderManager().renderers.forEach((entityType, entityRenderer) -> {
			if(entityRenderer instanceof LivingRenderer<?, ?>) {
				LivingRenderer<LivingEntity, EntityModel<LivingEntity>> livingRenderer = (LivingRenderer<LivingEntity, EntityModel<LivingEntity>>) entityRenderer;
				livingRenderer.addLayer(new TransparentGreenLayer(livingRenderer));
			}
		});

and it did add the layer to the vanilla mobs (I tested it with a drowned). However I got 2 warnings doing this ('resource' for Minecraft.getInstance() and 'unchecked' for the cast.) I fear, that this could lead to a crash at some point. Also I'm unsure, if the layer will be added to other modded livingEntities, since I'm not using a forgeRegistry. I would feel much safer, if I could get the same loop via ForgeRegistries.ENTITIES.

On another note. Currently the added layer isn't transparent, even though the texture is. If I change the texture to be completly transparent, it seems to be working, cause I couldn't see any layer, but the code was running (tested with a breakpoint). So are semi-transparent texture simply not working as a layer? That would be bad for my case and it would make my ultimate goal impossible (at least with a layer). @diesieben07 do you know someone, who has more insight on that matter?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7688

diesieben07

diesieben07    7688

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7688
  • 56257 posts
Posted Monday at 08:26 AM

That loop looks fine. The unchecked cast is indeed unchecked - why do you do that cast?

What actual warning message do you get for "Minecraft.getInstance()"? "resource" doesn't mean anything.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 107 posts
Posted Monday at 08:32 AM

For Minecraft.getInstance(): Resource leak: '<unassigned Closeable value>' is not closed at this location
For the cast: Type safety: Unchecked cast from EntityRenderer<capture#2-of ?> to LivingRenderer<LivingEntity,EntityModel<LivingEntity>>

and I casted it, because the normal EntityRenderer does not have the addLayer method.

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7688

diesieben07

diesieben07    7688

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7688
  • 56257 posts
Posted Monday at 08:47 AM
13 minutes ago, Tavi007 said:

For Minecraft.getInstance(): Resource leak: '<unassigned Closeable value>' is not closed at this location

Gotta love eclipse and its bogus useless warnings. Minecraft inherits from AutoCloseable, so eclipse wants you to call close here. For... zero reason. Ignore this warning.

 

15 minutes ago, Tavi007 said:

and I casted it, because the normal EntityRenderer does not have the addLayer method.

You casted it to something you cannot guarantee. You checked if its a LivingRenderer<?, ?>, but thats not what you casted it to.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 107 posts
Posted Monday at 01:06 PM
3 hours ago, diesieben07 said:

You casted it to something you cannot guarantee. You checked if its a LivingRenderer<?, ?>, but thats not what you casted it to.

LivingRenderer has the following definition:

public abstract class LivingRenderer<T extends LivingEntity, M extends EntityModel<T>> extends EntityRenderer<T> implements IEntityRenderer<T, M>
So I should be safe to cast it to LivingRenderer<LivingEntity, EntityModel<LivingEntity>> and will never crash, right?

Also what about my other questions? To summarize my open questions:
- Do semi-transparent texture work as a layer?
- Will the layer be added to all LivingEntities (vanilla and modded) using the current method? If not, how can I get the LivingRenderer from the LivingEntity?
- How can I make my layer be the outermost layer?

If you do not know the answere to some of these, it's not a problem. But maybe you know someone, who might know the solution.

Maybe a better solution would be to have another event hook added/move the .pre-event right before this.entityModel.render() is called in LivingRenderer#render. This way one could make last second changes to the rendering (and I could change the used OverlayTexture). Just to throw this idea in...

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7688

diesieben07

diesieben07    7688

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7688
  • 56257 posts
Posted Monday at 01:44 PM
36 minutes ago, Tavi007 said:

LivingRenderer has the following definition:

public abstract class LivingRenderer<T extends LivingEntity, M extends EntityModel<T>> extends EntityRenderer<T> implements IEntityRenderer<T, M>
So I should be safe to cast it to LivingRenderer<LivingEntity, EntityModel<LivingEntity>> and will never crash, right?

No.

First: The cast will never crash (that's why it's unchecked, generics are erased), but it can cause problems.

Example:

List<String> listOfStrings = new ArrayList<String>();
List<Object> listOfObjects = (List<Object>) listOfStrings; // I mean, every String is an object, right?
listOfObjects.add(123); // fine, every Integer is an object, right?
String firstElement = listOfStrings.get(0); // oops, this crashes, because your List<String> contains an Integer

 

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 107 posts
Posted Monday at 02:00 PM

I know that my question is a general java problem and that this forum should not be used as a java tutorial...

 

I thought I would be fine, since T extends LivingEntity and M extends EntityModel<LivingEntity>. So if my renderer is an instance of LivingRenderer, shouldn't be the types T and M at least be LivingEntity and EntityModel ?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7688

diesieben07

diesieben07    7688

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7688
  • 56257 posts
Posted Monday at 02:12 PM
9 minutes ago, Tavi007 said:

I know that my question is a general java problem and that this forum should not be used as a java tutorial...

 

I thought I would be fine, since T extends LivingEntity and M extends EntityModel<LivingEntity>. So if my renderer is an instance of LivingRenderer, shouldn't be the types T and M at least be LivingEntity and EntityModel ?

Yes, exactly.

But LivingRenderer<LivingEntity, EntityModel<LivingEntity>> means "A LivingRenderer with exactly LivingEntity and EntityModel<LivingEntity>", it does not mean "A LivingRenderer with at least LivingEntity and EntityModel<LivingEntity>". If you want at least, you need wildcards with bounds:

LivingRenderer<? extends LivingEntity, ? extends EntityModel<? extends LivingEntity>>.

  • Quote

Share this post


Link to post
Share on other sites

Tavi007    2

Tavi007

Tavi007    2

  • Creeper Killer
  • Tavi007
  • Members
  • 2
  • 107 posts
Posted yesterday at 10:30 AM

Well TIL.

Before I fix this problem, I would like to know, if my idea with the layer will even work. I've added this to my TextureLayer class:

	protected static <T extends LivingEntity> void renderCutoutModel(EntityModel<T> modelIn, ResourceLocation textureLocationIn, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int packedLightIn, T entityIn, float red, float green, float blue) {
		IVertexBuilder ivertexbuilder = bufferIn.getBuffer(RenderType.getEntityCutoutNoCull(textureLocationIn));
		modelIn.render(matrixStackIn, ivertexbuilder, packedLightIn, LivingRenderer.getPackedOverlay(entityIn, 0.0F), red, green, blue, 0.01F);
	}

so i can change the alpha value and see if this will affect the tranparency of my layer. Sadly that didn't do much.

I've also read further into the vanilla render to see when exactly the color changes (and how). I don't really get how that packedOverlay integer changes the color. Normally it has the value 655360 and if hurttime>0 it has the value 196608. Adding this:

ResourceLocation texturLocation = event.getRenderer().getEntityTexture(entityIn);
RenderType rendertype = event.getRenderer().getEntityModel().getRenderType(texturLocation);
IVertexBuilder ivertexbuilder = event.getBuffers().getBuffer(rendertype);
event.getRenderer().getEntityModel().render(event.getMatrixStack(),
	ivertexbuilder, 
	event.getLight(), 
	655360,
	0.0F, 1.0F, 0.0F, 1.0F);

to my LvingRender hook I was able to play around with packedOverlay value a bit more (tho the actual entity looks messed up. But I was only interessted in the overlay color). I could create a white overlay if I set the value to 1655360, but not any other color. Again, I don't understand how these numbers are used while rendering. I could however make the entity look greenish, by reducing the red and blue color (currently both are set to 0).
Not that it matters, because I can't change them on the real model render anyway. :(

I'm losing hope, that this feature will work :/
Thank god, it isn't something fundamental, but I would have been really nice to have this included.
 

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Centmap
      Forge Mods Folder

      By Centmap · Posted 56 minutes ago

      so i created a "1.16.5" folder in the folder "mod" as usual but it doesn't use the mods. Can anyone help me ?
    • Skyriis
      [SOLVED][1.16.5] Adding a Button to KeyBindings

      By Skyriis · Posted 1 hour ago

      That worked.   Here is my solution @SubscribeEvent public static void onOpenGui(final GuiScreenEvent.InitGuiEvent.Post event) { if (!(event.getGui() instanceof ControlsScreen)) return; final ControlsScreen controlsScreen = (ControlsScreen) event.getGui(); final KeyBindingList replacement = new KeyBindingListReplacement(controlsScreen, event.getGui().getMinecraft()); final KeyBindingList old = ObfuscationReflectionHelper.getPrivateValue(ControlsScreen.class, controlsScreen, "field_146494_r"); controlsScreen.getEventListeners().remove(old); ObfuscationReflectionHelper.setPrivateValue(ControlsScreen.class, controlsScreen, replacement, "field_146494_r"); try { Method addChildMethod = ObfuscationReflectionHelper.findMethod(Screen.class, "func_230481_d_", IGuiEventListener.class); addChildMethod.setAccessible(true); addChildMethod.invoke(controlsScreen, replacement); addChildMethod.setAccessible(false); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } }  
    • DrCowiber
      Failed To Start Minecraft Server

      By DrCowiber · Posted 2 hours ago

      im using this run.sh file, which contains: java -Xmx6G -Xms6G -jar minecraft_server.1.16.5.jar --nogui Edit: When I first posted about this I was using the serverRun.jar, which the first log file I posted is what the output was
    • Tez
      [1.15.2] Couln't not resolve dependency: net.minecraftforge:forge:1.15.2-31.2.0:userdev

      By Tez · Posted 2 hours ago

      I'm new to mod coding so i've watch tutorial in youtube in setup workspace step i got this error , i've try many solutions to fix but it still not working try update gradle to 6.8.3 but still not working try update java and still not working too here's my stacktrace: Stacktrace
    • diesieben07
      Failed To Start Minecraft Server

      By diesieben07 · Posted 2 hours ago

      Please show how exactly you are starting the server.
  • Topics

    • Centmap
      0
      Forge Mods Folder

      By Centmap
      Started 56 minutes ago

    • Skyriis
      8
      [SOLVED][1.16.5] Adding a Button to KeyBindings

      By Skyriis
      Started 23 hours ago

    • DrCowiber
      8
      Failed To Start Minecraft Server

      By DrCowiber
      Started 21 hours ago

    • Tez
      0
      [1.15.2] Couln't not resolve dependency: net.minecraftforge:forge:1.15.2-31.2.0:userdev

      By Tez
      Started 2 hours ago

    • Mysterious minecrafter
      1
      game keeps crashing while initializings

      By Mysterious minecrafter
      Started 4 hours ago

  • Who's Online (See full list)

    • Zeher_Monkey
    • Uncreative
    • NullDev
    • PyRoTheLifeLess
    • Centmap
    • redlynx
    • GermanBucket
    • smitokyo
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16] Need help with LayerRenderer
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community