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!