Skip to main content

CosmeticPacker

A class in Root.Township, Alta.Customization.

Client: merges a character's cosmetic parts into a few meshes and one packed texture, then applies them to the character's clothes. Packing is queued on CosmeticPackScheduler.

Constructors​

MemberWhat it is
CosmeticPacker()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

27 private and internal fields
MemberWhat it is
private static readonly int _AdditionalShader property ID for _Additional.
private static readonly int _BumpMapShader property ID for the normal map texture.
private static readonly int _MainTexShader property ID for the diffuse texture.
private Material blendMaterialMaterial for alpha-blended parts, made from material with blendShader when any input needs blending.
private Shader blendShaderShader used for the transparent (alpha-blended) clothing material.
private BonesBaking bonesBakingBakes bone data into the packed meshes on Android.
private Dictionary<CosmeticPacker.PartId, CosmeticPacker.Input> cacheInputsCopy of inputs taken at the start of a pack, so changes during the pack don't affect it.
private Matrix4x4[] defaultBindposesBind poses passed to the mesh packer for the combined meshes.
private Texture2D defaultNormalFallback normal map used when packing textures.
private RenderTexture diffuseThe packed diffuse render texture for all parts.
private CharacterDummy dummyThe character the packed clothes are applied to.
private Mesh[] headMeshesPacked opaque head mesh, used when the head is separate (first person).
private Dictionary<CosmeticPacker.PartId, CosmeticPacker.Input> inputsThe current cosmetic parts to pack, keyed by part ID. Exposed as Inputs.
private bool isPackedWhilePausedSet when PackAndApply is called while paused, so Unpause packs then.
private bool isPackingAtTheEndOfTheFrameDeclared but not used in this class.
private bool isPausedWhile true, PackAndApply only marks a pack as pending.
private static Logger loggerNLog logger for packing errors.
private Material materialThe opaque clothing material using the packed textures; made on first pack.
private int maxLODToBakeBodyTypeHighest LOD (exclusive) baked on Android for third-person characters.
private Mesh[] meshesPacked opaque body meshes, one per LOD.
private RenderTexture normalThe packed normal map render texture.
private IEnumerator packDeclared but not used in this class.
private Dictionary<CosmeticPacker.PartId, ICosmeticMeshPartBase> partMapLoaded mesh part for each part ID, filled by FillPartMap and cleared after each pack.
private Shader shaderShader used for the opaque clothing material.
private Mesh[] transparentHeadMeshesPacked transparent head mesh for first person.
private Mesh[] transparentMeshesPacked transparent body meshes, one per LOD.
private Dictionary<CosmeticPacker.PartId, int> workingIndexesIndex for each part during a pack; parts that copy another part share its index.

Properties​

MemberWhat it is
float AndroidTextureSizeMultiplier { get; set; }Scale applied to the packed texture size on Android.
int BodyTypeIndex { get; set; }The body type used when baking bones on Android.
Dictionary<CosmeticPacker.PartId, CosmeticPacker.Input> Inputs { get; }The parts to pack, by part ID. Change these, then call PackAndApply.
bool IsTextureSharingAllowed { get; set; }Whether parts may share texture space when packing.
2 private and internal properties
MemberWhat it is
private int StartingLOD { get; }First LOD to build: 1 on Android, 0 elsewhere.
private float TextureSizeMultiplier { get; }Scale for the packed textures: AndroidTextureSizeMultiplier on Android, 1 elsewhere.

Events​

MemberWhat it is
event Action AndroidPreBonesBakingRaised once on Android before bones are baked into the packed meshes. Cleared after it fires.
event Action FinishedPackingRaised once when packing finishes and the result is applied. Cleared after it fires.

Methods​

MemberWhat it is
void Clean()Cancels any queued pack and frees the inputs, textures, materials and meshes. Called on destroy.
void PackAndApply()Queues a pack for the next frame. While paused, it waits until Unpause.
async Task PackFromScheduler(AddressableSession session, CancellationToken cancellation)Runs the pack. Called by the scheduler, not by your code.
void Pause()Holds back PackAndApply until Unpause.
void Unpause()Ends a pause, and packs if a pack was asked for meanwhile.
6 private and internal methods
MemberWhat it is
private IEnumerable<Mesh> BakingMeshes(int startingLOD) (iterator)Lists the packed meshes to bake bones into on Android: all LODs from the start for first person, up to maxLODToBakeBodyType otherwise.
private async Task FillPartMap(AddressableSession session, CancellationToken cancellationToken)Loads each input's cosmetic definition by hash and stores its mesh part in partMap; logs and skips parts that fail.
private IEnumerable<T> GetSafeEnumerable<T>(T[] elements, int start, int end) (iterator)Yields array elements from a start index up to an end index, clamped to the array length.
private void OnDestroy()Calls Clean to free the packed textures, materials and meshes.
private async Task PackMeshes(int startingLOD, bool isHeadSeparate, CancellationToken cancellation)Combines the part meshes into opaque and transparent meshes, with the head split out in first person. Logs the part list on failure.
private async Task PackTask(AddressableSession session, CancellationToken cancellation)Does one full pack: snapshots inputs, resolves copied colours, loads parts, packs textures and meshes, then applies them to the dummy's clothes.

Used in​

Members of other types that take, return or hold a CosmeticPacker: