Skip to content

Instantly share code, notes, and snippets.

@zaneclaes
Last active November 6, 2017 14:43
Show Gist options
  • Save zaneclaes/b56ecaa33ab8382ca01f17f9b598d225 to your computer and use it in GitHub Desktop.
Save zaneclaes/b56ecaa33ab8382ca01f17f9b598d225 to your computer and use it in GitHub Desktop.
UV Boxes for creature
MeshRenderBoneComposition comp =
creatureAsset.creature_manager.target_creature.render_composition;
List<MeshRenderRegion> regions = comp.getRegions();
foreach (MeshRenderRegion region in regions) {
Rect r = new Rect();
int uvIndex = region.getUVsIndex();
for (int i = 0; i < region.getNumPts(); i++) {
Vector2 p = new Vector2(region.store_uvs[uvIndex], region.store_uvs[uvIndex + 1]);
if (i == 0) {
r.xMax = r.xMin = (float)p.x;
r.yMax = r.yMin = (float)p.y;
} else {
r.xMin = Mathf.Min(r.xMin, (float)p.x);
r.xMax = Mathf.Max(r.xMax, (float)p.x);
r.yMin = Mathf.Min(r.yMin, (float)p.y);
r.yMax = Mathf.Max(r.yMax, (float)p.y);
}
uvIndex += 2;
}
// Now, r should be the UV box for the region...
// To use it, I do something like this, where "_texture" is the output texture and "sprite" is some source-asset.
Texture2D t = sprite.texture;
Graphics.CopyTexture(
t, 0, 0, (int)(r.x * t.width), (int)(r.y * t.height), (int)(r.width * t.width), (int)(r.height * t.height),
_texture, 0, 0, (int)(r.x * _texture.width), (int)(r.y * _texture.height)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment