Jump to content

[1.18.2] Make the player model translucent (ghost-like effect)


bl4d3tv

Recommended Posts

Could anyone point me in the right direction in how might I achieve this? Been looking through the forum and the internet and couldn't find and updated answer. Most answers I found talked about GlStateManager which seems to no longer be used.

Link to comment
Share on other sites

17 minutes ago, ChampionAsh5357 said:

The player model already supports translucent textures, so you would mainly just need to change the final parameter of ModelPart#render to be a different alpha value. You could accomplish this by replacing the player rendering via PlayerRenderEvent$Pre.

Do you mind writing an small code example? ModelPart#render seems to need a lot of arguments and I don't really know where to get them from. Appreciate it!

Link to comment
Share on other sites

1 hour ago, ChampionAsh5357 said:

I would suggest just following the call references using your IDE to figure out where to get the parameters from. In general, it is just from the EntityRenderer with the last four floats being RGBA scaled from 0 to 1.

I've managed to get the torso rendering with transparency but it appears stuck to the ground.

Is there a way to render the whole model with transparency or do I have to do it part by part?

How can I put the new torso in place of the old one?

I'm guessing I have to cancel the event and replace the render with my own but I'm having a hard time figuring out where to get some arguments from.

Link to comment
Share on other sites

11 hours ago, bl4d3tv said:

I've managed to get the torso rendering with transparency but it appears stuck to the ground.

Is there a way to render the whole model with transparency or do I have to do it part by part?

How can I put the new torso in place of the old one?

I'm guessing I have to cancel the event and replace the render with my own but I'm having a hard time figuring out where to get some arguments from.

are you using parchment mappings yet? And every model in the game has different parts so that they do different animations. The ones I can think of the top of my head is Body, Legs, Right Arm, Left Arm, Head, Hat. I know some other models in the game have more custom model parts but since you are editing the player you just need to work with those parts

Link to comment
Share on other sites

11 hours ago, bl4d3tv said:

I've managed to get the torso rendering with transparency but it appears stuck to the ground.

Send a picture of what you see and the code

 

Just now, sFXprt said:

Is there a way to render the whole model with transparency or do I have to do it part by part?

No, the way models work is they have different parts so that they do different animations. The ones I can think of the top of my head is Body, Legs, Right Arm, Left Arm, Head, Hat. I know some other models in the game have more custom model parts but since you are editing the player you just need to work with those parts

 

Just now, sFXprt said:

How can I put the new torso in place of the old one?

If you use your own custom renderer maybe, but I am very unsure... You are gonna have to wait for a much more advanced forum member like @ChampionAsh5357 or @warjort to assist you further.

Link to comment
Share on other sites

6 hours ago, sFXprt said:

are you using parchment mappings yet?

Yes, I'm using parchment mappings.

6 hours ago, sFXprt said:

Send a picture of what you see and the code

This is what I see when rendering the body with less transparency:

WWDEH7v.png

As you can see, the rotation and placement is wrong.

And this is the code snippet from the RenderPlayerEvent.Pre event:

@SubscribeEvent
    public static void onRenderPlayerPre(RenderPlayerEvent.Pre event) {
		// Commeted this out to see if the body renders in the correct position but didn't work.
        //event.setCanceled(true);

        PlayerRenderer renderer = event.getRenderer();
        PlayerModel<AbstractClientPlayer> playerModel = renderer.getModel();
        AbstractClientPlayer abstractPlayer = (AbstractClientPlayer) event.getPlayer();

  		// This is the part where I think I'm getting the wrong arguments to send to the render function. Or maybe I don't have to use this function at all...
        playerModel.body.render(event.getPoseStack(), event.getMultiBufferSource().getBuffer(RenderType.entityTranslucentCull(renderer.getTextureLocation((AbstractClientPlayer) event.getPlayer()))), event.getPackedLight(), 1, 1.0f, 1.0f, 1.0f, 0.5f);
    }

 

Link to comment
Share on other sites

You should not be using entityTranslucentCull first, it should just be entityTranslucent.

Second, you should be setting up the animation and then calling the render method like any other model implementation. Otherwise, no animation data will be set on the model itself. Look at how LivingEntityRenderer#render works.

Link to comment
Share on other sites

21 hours ago, ChampionAsh5357 said:

You should not be using entityTranslucentCull first, it should just be entityTranslucent.

Second, you should be setting up the animation and then calling the render method like any other model implementation. Otherwise, no animation data will be set on the model itself. Look at how LivingEntityRenderer#render works.

Thank you, I got really far by looking at how LivingEntityRenderer#render works, though I'm not sure if the way I did it is the most optimal one.

There is one problem that I'm having a hard time figuring out, the render just does a lot of popping. I attached a video below (ignore the rotation, I already fixed it):

On another note, how would I go about rendering this event conditionally? Should I use packets or is there another way?

Link to comment
Share on other sites

19 hours ago, bl4d3tv said:

There is one problem that I'm having a hard time figuring out, the render just does a lot of popping.

Did you remember to cancel the event? I have no idea what the error is when I can't see the code.

19 hours ago, bl4d3tv said:

On another note, how would I go about rendering this event conditionally? Should I use packets or is there another way?

Add an if statement? If you need data from the server, send a value via a packet to the client.

Link to comment
Share on other sites

On 5/12/2023 at 11:15 AM, ChampionAsh5357 said:

Did you remember to cancel the event? I have no idea what the error is when I can't see the code.

Add an if statement? If you need data from the server, send a value via a packet to the client.

Hey, I've managed to fix most issues. The approach I took was to copy most of the PlayerRenderer code and adjust it to my needs in the RenderPlayerEvent.Pre. Now, the issue I have is that I can't get the armor to render. I see that in the PlayerRenderer constructor the armor layers are set up from something called EntityRendererProvider.Context, but I can't seem to find where to get this context from.

I did try to extend the PlayerRenderer class into my own with my needed modifications, but I can't seem to register it as a replacement to the vanilla PlayerRenderer. Altough I'm thinking this might not be the best approach because it might make other mods incompatible.

This is the relevant code of the RenderEvent if needed:

@SubscribeEvent
  public static void onPlayerRender(RenderPlayerEvent.Pre event) {
  // Note: conditions are not yet setup, I'll do it once I get the render right

  event.setCanceled(true);

  final PoseStack poseStack = event.getPoseStack();
  final MultiBufferSource buffer = event.getMultiBufferSource();
  final PlayerRenderer renderer = event.getRenderer();
  final PlayerModel<AbstractClientPlayer> playerModel = renderer.getModel();
  final Entity entity = event.getEntity();
  final float ticks = event.getPartialTick();
  final LivingEntity livingEntity = event.getEntityLiving();
  final AbstractClientPlayer abstractPlayer = (AbstractClientPlayer) event.getPlayer();

  event.getPoseStack().pushPose();

  // Set the model attack time
  playerModel.attackTime = event.getEntityLiving().attackAnim;

  boolean shouldSit = entity.isPassenger() && (entity.getVehicle() != null && entity.getVehicle().shouldRiderSit());
  playerModel.riding = shouldSit;
  playerModel.young = false;
  float f = Mth.rotLerp(ticks, livingEntity.yBodyRotO, livingEntity.yBodyRot);
  float f1 = Mth.rotLerp(ticks, livingEntity.yHeadRotO, livingEntity.yHeadRot);
  float f2 = f1 - f;
  if (shouldSit && entity.getVehicle() instanceof LivingEntity) {
    LivingEntity livingentity = (LivingEntity)entity.getVehicle();
    f = Mth.rotLerp(ticks, livingentity.yBodyRotO, livingentity.yBodyRot);
    f2 = f1 - f;
    float f3 = Mth.wrapDegrees(f2);
    if (f3 < -85.0F) {
      f3 = -85.0F;
    }

    if (f3 >= 85.0F) {
      f3 = 85.0F;
    }

    f = f1 - f3;
    if (f3 * f3 > 2500.0F) {
      f += f3 * 0.2F;
    }

    f2 = f1 - f;
  }

  float f6 = Mth.lerp(ticks, livingEntity.xRotO, livingEntity.getXRot());
  if (LivingEntityRenderer.isEntityUpsideDown(livingEntity)) {
    f6 *= -1.0F;
    f2 *= -1.0F;
  }

  if (livingEntity.getPose() == Pose.SLEEPING) {
    Direction direction = livingEntity.getBedOrientation();
    if (direction != null) {
      float f4 = livingEntity.getEyeHeight(Pose.STANDING) - 0.1F;
      poseStack.translate((double)((float)(-direction.getStepX()) * f4), 0.0D, (double)((float)(-direction.getStepZ()) * f4));
    }
  }

  float f7 = livingEntity.tickCount + ticks;
  setupRotations(livingEntity, poseStack, f7, f, ticks);
  poseStack.scale(-1.0F, -1.0F, 1.0F);
  poseStack.translate(0.0D, (double)-1.501F, 0.0D);
  float f8 = 0.0F;
  float f5 = 0.0F;
  if (!shouldSit && livingEntity.isAlive()) {
    f8 = Mth.lerp(ticks, livingEntity.animationSpeedOld, livingEntity.animationSpeed);
    f5 = livingEntity.animationPosition - livingEntity.animationSpeed * (1.0F - ticks);
    if (livingEntity.isBaby()) {
      f5 *= 3.0F;
    }

    if (f8 > 1.0F) {
      f8 = 1.0F;
    }
  }

  playerModel.prepareMobModel((AbstractClientPlayer) livingEntity, f5, f8, ticks);
  playerModel.setupAnim((AbstractClientPlayer) livingEntity, f5, f8, f7, f2, f6);

  // This is the part responsible for making the player translucent, specifically the RenderType#itemEntityTranslucentCull
  event.getRenderer().getModel().renderToBuffer(
    event.getPoseStack(),
    event.getMultiBufferSource()
    .getBuffer(
      RenderType.itemEntityTranslucentCull(
        event.getRenderer().getTextureLocation((AbstractClientPlayer) event.getPlayer()))), event.getPackedLight(), 0, 1.0f, 1.0f, 1.0f, 0.8f);

  event.getPoseStack().popPose();
}

// Direct copy of PlayerRenderer#setupRotations
private static void setupRotations(LivingEntity entityLiving, PoseStack pMatrixStack, float pAgeInTicks, float pRotationYaw, float pPartialTicks) {
  if (entityLiving.isFullyFrozen()) {
    pRotationYaw += (float)(Math.cos((double)entityLiving.tickCount * 3.25D) * Math.PI * (double)0.4F);
  }

  Pose pose = entityLiving.getPose();
  if (pose != Pose.SLEEPING) {
    pMatrixStack.mulPose(Vector3f.YP.rotationDegrees(180.0F - pRotationYaw));
  }

  if (entityLiving.deathTime > 0) {
    float f = ((float)entityLiving.deathTime + pPartialTicks - 1.0F) / 20.0F * 1.6F;
    f = Mth.sqrt(f);
    if (f > 1.0F) {
      f = 1.0F;
    }

    pMatrixStack.mulPose(Vector3f.ZP.rotationDegrees(f * 90.0F));
  } else if (entityLiving.isAutoSpinAttack()) {
    pMatrixStack.mulPose(Vector3f.XP.rotationDegrees(-90.0F - entityLiving.getXRot()));
    pMatrixStack.mulPose(Vector3f.YP.rotationDegrees(((float)entityLiving.tickCount + pPartialTicks) * -75.0F));
  } else if (pose == Pose.SLEEPING) {
    Direction direction = entityLiving.getBedOrientation();
    float f1 = direction != null ? sleepDirectionToRotation(direction) : pRotationYaw;
    pMatrixStack.mulPose(Vector3f.YP.rotationDegrees(f1));
    pMatrixStack.mulPose(Vector3f.ZP.rotationDegrees(90.0F));
    pMatrixStack.mulPose(Vector3f.YP.rotationDegrees(270.0F));
  } else if (LivingEntityRenderer.isEntityUpsideDown(entityLiving)) {
    pMatrixStack.translate(0.0D, (double)(entityLiving.getBbHeight() + 0.1F), 0.0D);
    pMatrixStack.mulPose(Vector3f.ZP.rotationDegrees(180.0F));
  }

}

// Direct copy of PlayerRenderer#sleepDirectionToRotation
private static float sleepDirectionToRotation(Direction pFacing) {
  switch(pFacing) {
    case SOUTH:
      return 90.0F;
    case WEST:
      return 0.0F;
    case NORTH:
      return 270.0F;
    case EAST:
      return 180.0F;
    default:
      return 0.0F;
  }
}
}

 

Link to comment
Share on other sites

The context is constructed when the renderers are to avoid duplication, but since you have access to the renderer in the event, you should just be able to access the layers (whether you'll need an AT or not will depend).

Though, looking at how complicated this is getting, it may just be worthwhile to use a mixin instead into LivingEntityRenderer#render and inject into where #renderToBuffer is called and replace the last local variable with whatever value you want at a given time. Though, we do not support mixins on this forum yet, so you would be on your own (though we do support it on the discord channel in #non-api-modding).

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.



×
×
  • Create New...

Important Information

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