Skip to main content

6. Build the bundle

Unity's job here is small: take the text files from the last steps, make skinned meshes and prefabs the way the game expects, and pack them into one asset bundle. No clicking around in the editor. BuildKitsuneTee.cs does it all in batch mode.

& "C:\Program Files\Unity\Hub\Editor\2022.3.62f3\Editor\Unity.exe" -batchmode -quit `
-projectPath UnityProject -executeMethod BuildKitsuneTee.Build -logFile build/unity-build.log `
-teeRef <ref> -teeOut KitsuneTee/Resources

It builds four prefabs into KitsuneTee/Resources/KitsuneTee.unity3d: gearFemaleKitsuneTee, gearFemaleKitsuneTeeJeans, gearMaleKitsuneTee and gearMaleKitsuneTeeJeans.

What a gear prefab needs​

gearFemaleKitsuneTee the root, named like the Prefab in items.xml
├─ body named like TransformName, with the SkinnedMeshRenderer
└─ Origin the player rig, in its rest pose
└─ Hips
├─ Spine ...
└─ LeftUpLeg ...
  1. A child named exactly like the item's TransformName (body), holding a SkinnedMeshRenderer.
  2. The player rig under the root: Origin, Hips and down. The game finds bones by name when it stitches your part onto the player, so the names must match the player rig exactly.
  3. Bind poses that match the player body's, so the clothes and the body bend the same way.
  4. A shader with a _ClipFPV property, and vertex colour red on whatever should vanish in first person.

The rig and the bind poses​

The rig comes from player_rig.csv, one row per bone with its parent and local transform. We rebuild it as plain GameObjects:

foreach (var r in rig.Skip(1)) // row 0 is the rig prefab's own root
{
var t = new GameObject(r[0]).transform;
t.SetParent(made.TryGetValue(r[1], out var up) ? up : root.transform, false);
t.localPosition = new Vector3(F(r[2]), F(r[3]), F(r[4]));
t.localRotation = new Quaternion(F(r[5]), F(r[6]), F(r[7]), F(r[8]));
t.localScale = new Vector3(F(r[9]), F(r[10]), F(r[11]));
made[r[0]] = t;
}

Then the mesh keeps only the bones it actually uses, and takes the body's own bind matrix for each one, per sex, straight from <sex>_body_bind.csv:

mesh.bindposes = names.Select(b => bind[b]).ToArray();
smr.bones = names.Select(b => made[b]).ToArray();
smr.rootBone = made["Hips"];

That's the whole trick to clothes that follow the body: same bones, same bind poses, weights borrowed from the skin underneath.

Both sides, and sub-meshes​

A T-shirt has openings, and you can see the inside through them. So every piece is added twice: once as is, once with the normals flipped and the triangles wound the other way.

The tee-and-jeans prefab has both pieces in one mesh, as two sub-meshes, each with its own material. One body part, one SkinnedMeshRenderer, two materials. Remember, there's no legs slot, so the jeans have to live in the same part as the shirt.

mesh.subMeshCount = subs.Count;
for (int s = 0; s < subs.Count; s++) mesh.SetTriangles(subs[s], s);
smr.sharedMaterials = pieces.Select(pc => pc.Mat).ToArray();

The first-person shader​

When the player is in first person, the game draws only the body and hands parts. For a body gear part it looks at every material: if the material has a float property called _ClipFPV, the game sets it to 1, then deletes every triangle that has a vertex with red vertex colour above 0. That's how vanilla shirts keep their collars out of your face.

A plain Standard material doesn't have _ClipFPV, so nothing gets cut, and your collar fills the screen. The fix is a shader that just declares the property:

Shader "Kitsune/PlayerGear"
{
Properties
{
_MainTex ("Albedo", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
// ...the usual Standard inputs...
_ClipFPV ("Clip in first person (set by the game)", Float) = 0
}
SubShader
{
CGPROGRAM
#pragma surface surf Standard fullforwardshadows addshadow
// ...a normal Standard surface shader...
ENDCG
}
FallBack "Standard"
}

The shader never reads _ClipFPV or the vertex colours. It only has to have the property. The red marks we set in step 3 do the rest.

Linear, not Gamma​

:::danger The washed-out trap The game renders in the Linear colour space. Build your bundle from a Gamma project and the textures come through byte for byte the same, but the shaders compile for Gamma, and non-metal surfaces get a much stronger reflection. The outfit looks washed out and plasticky in game, and you'll go hunting in the textures for a problem that isn't there.

A project made with -createProject starts in Gamma. The builder fixes that before it does anything else:

if (PlayerSettings.colorSpace != ColorSpace.Linear)
{
PlayerSettings.colorSpace = ColorSpace.Linear;
AssetDatabase.SaveAssets();
}

:::

The bundle​

BuildPipeline.BuildAssetBundles(tmp,
new[] { new AssetBundleBuild { assetBundleName = "kitsunetee.unity3d", assetNames = prefabs.ToArray() } },
BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);

Unity lowercases bundle names, so the builder copies the result to KitsuneTee.unity3d in the mod's Resources folder. Read the log afterwards: each prefab line says how many vertices it has, how many are hidden in first person, and which bones it uses.