You are browsing as a guest. Sign up (or log in) to start making projects!

2h 14m 45s logged

MOB TEXTURE RANDOMIZATION

(phase 1)

What’s new

I have successfully been able to randomize the mob texture of my custom entity to select out of a list of random textures I made myself.
While I initially had the idea to create one large “mega texture”, and randomly choose where in the large file to look for each spawned mob’s texture, this isn’t easily possible with the fabric’s (and minecraft source code’s) mob rendering system. I wanted to see if I could at least randomize the texture files and pivot from there.

Technical Stuff

I speant a while reading through the source code of the panda mob to understand how to do this. Pandas in the vanilla game have a small chance of spawning with a “brown panda” texture, and after I read through the code I understood a method.

Minecraft Source Code Secrets

First it’s important to understand how minecraft handles entities. The game has a server and client (yes, even on singleplayer) always running in a world. Rendering is all handled on the client, while mob data is handled on the server. Pandas are assigned their “isBrown” property in the Panda class in the server.
In the client, there are two important files: the pandaRenderer, which handles rendering, and pandaRenderState, which handles entity information. The pandaRenderer extends mobRenderer, which has a method called “extract renderState” which takes the server-side entity class and renderState class as arguments, allowing variables from the server-side class to be synced with the renderState class(i.e. state.isBrown = entity.isBrown), which can be used by the entityRenderer. The entityRenderer also has a method that returns a texture path, so that for each panda that has a True isBrown value, a path to the brown panda texture can be returned.

The result

Once I understood how the game handles texture rendering, I added a String in the constructor for my custom mob, the chaosEntity, which is randomly assigned the path of one of the texture files. This is read by the renderer class to render the mob’s texture.

Why the other thing failed

I initially wanted to randomly offset the location in the texture png that the game looks for each body part of the entity. For example, in a 600x600 png image, one mob’s head might be taken from the pixels at coordinates starting at (0, 10) on the png, while another’s might start at (520, 50). However, this coordinate offset is defined in the entityModel class, which is registered into the game’s rendering system when the client is initialized. This means when the game starts, the model can only be registered once and not edited. I tried a lot of ways to change the model after initialization, but every time got a crash because of how the game handles models. As far as I can tell right now, there is no way to adjust the model of an entity after it has spawned without writing my own rendering system from scratch.

Future Steps

While this is a working example (finally) of each spawned mob in the world having a random texture, I don’t have the energy to make enough png textures for it to be interesting. I’m experimenting with procedural image generation to try and randomly generate mob texture files for every chaosEntity, and then use those files for that mob’s texture. Wish me luck!

0
4

Comments 0

No comments yet. Be the first!