Jump to content

[1.16] Need help with LayerRenderer


Tavi007

Recommended Posts

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? :)

Link to comment
Share on other sites

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...

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.
 

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.