public TextureBindings BindTextures(TextureCompositor compositor, Dictionary <string, ValveMaterial> materials) { Contract.Requires(compositor != null && materials != null); TextureBindings textureBinds = new TextureBindings(); Bitmap uvMap = compositor.BakeTextureMap(); Rbx2Source.SetDebugImage(uvMap); foreach (string matName in materials.Keys) { Rbx2Source.Print("Building Material {0}", matName); ValveMaterial material = materials[matName]; Image image = null; if (material.UseAvatarMap) { if (Enum.TryParse(matName, out BodyPart limb)) { Rectangle cropRegion = UVCrops[limb]; image = TextureCompositor.CropBitmap(uvMap, cropRegion); } } else { Asset texture = material.TextureAsset; if (texture != null) { byte[] textureData = texture.GetContent(); MemoryStream textureStream = new MemoryStream(textureData); image = Image.FromStream(textureStream); textureStream.Dispose(); } } textureBinds.BindTexture(matName, image); } return(textureBinds); }
public TextureBindings BindTextures(TextureCompositor compositor, Dictionary <string, ValveMaterial> materials) { Contract.Requires(compositor != null && materials != null); TextureBindings textures = new TextureBindings(); Bitmap core = compositor.BakeTextureMap(); Rbx2Source.SetDebugImage(core); Bitmap head = TextureCompositor.CropBitmap(core, RECT_HEAD); textures.BindTexture("Head", head); Bitmap body = TextureCompositor.CropBitmap(core, RECT_BODY); Folder characterAssets = compositor.CharacterAssets; Rbx2Source.Print("Processing Package Textures..."); Rbx2Source.IncrementStack(); // Collect CharacterMeshes var packagedLimbs = characterAssets .GetChildrenOfType <CharacterMesh>() .ToDictionary(mesh => mesh.BodyPart); // Compose the textures that will be used var limbOverlays = new Dictionary <BodyPart, long>(); var limbBitmaps = new Dictionary <long, Bitmap>() { { 0, body } }; foreach (BodyPart limb in LimbMatcher.Keys) { // Head is already textured, ignore it. if (limb == BodyPart.Head) { continue; } // Is there a CharacterMesh for this limb? if (packagedLimbs.ContainsKey(limb)) { // Check the CharacterMesh textures. CharacterMesh mesh = packagedLimbs[limb]; if (mesh.OverlayTextureId > 0) { // Use the overlay texture for this limb. long overlayId = mesh.OverlayTextureId; limbOverlays.Add(limb, overlayId); // Compose this overlay texture with the body texture if it doesn't exist yet. if (!limbBitmaps.ContainsKey(overlayId)) { Asset overlayAsset = Asset.Get(overlayId); TextureCompositor overlayCompositor = new TextureCompositor(AvatarType.R6, RECT_FULL); overlayCompositor.SetContext("Overlay Texture " + overlayId); overlayCompositor.AppendTexture(overlayAsset, RECT_BODY, 1); overlayCompositor.AppendTexture(body, RECT_BODY); Bitmap overlayTex = overlayCompositor.BakeTextureMap(RECT_BODY); limbBitmaps.Add(overlayId, overlayTex); } continue; } else if (mesh.BaseTextureId > 0) { // Use the base texture for this limb. long baseId = mesh.BaseTextureId; limbOverlays.Add(limb, baseId); // Compose the base texture if it doesn't exist yet. if (!limbBitmaps.ContainsKey(baseId)) { Asset baseAsset = Asset.Get(baseId); TextureCompositor baseCompositor = new TextureCompositor(AvatarType.R6, RECT_FULL); baseCompositor.SetContext("Base Texture " + baseId); baseCompositor.AppendTexture(baseAsset, RECT_BODY); Bitmap baseTex = baseCompositor.BakeTextureMap(RECT_BODY); limbBitmaps.Add(baseId, baseTex); } continue; } } // If no continue statement is reached, fallback to using the body texture. // This occurs if the limb has no package, or the package limb has no textures. limbOverlays.Add(limb, 0); } // Add the images into the texture assembly. foreach (long id in limbBitmaps.Keys) { Bitmap bitmap = limbBitmaps[id]; string matName = GetBodyMatName(id); textures.BindTexture(matName, bitmap, false); } // Link the limbs to their textures. foreach (BodyPart limb in limbOverlays.Keys) { long id = limbOverlays[limb]; string matName = GetBodyMatName(id); string limbName = Rbx2Source.GetEnumName(limb); textures.BindTextureAlias(limbName, matName); } // Handle the rest of the materials foreach (string matName in materials.Keys) { if (!textures.MatLinks.ContainsKey(matName)) { ValveMaterial material = materials[matName]; Asset texture = material.TextureAsset; TextureCompositor matComp = new TextureCompositor(AvatarType.R6, RECT_ITEM); matComp.SetContext("Accessory Texture " + matName); matComp.AppendTexture(texture, RECT_ITEM); Bitmap bitmap = matComp.BakeTextureMap(); textures.BindTexture(matName, bitmap); } } Rbx2Source.DecrementStack(); return(textures); }
public TextureAssembly AssembleTextures(TextureCompositor compositor, Dictionary <string, Material> materials) { TextureAssembly assembly = new TextureAssembly(); assembly.Images = new Dictionary <string, Image>(); assembly.MatLinks = new Dictionary <string, string>(); Bitmap uvMap = compositor.BakeTextureMap(); Rbx2Source.SetDebugImage(uvMap); ImageAttributes blankAtt = new ImageAttributes(); foreach (string materialName in materials.Keys) { Rbx2Source.Print("Building Material {0}", materialName); Material material = materials[materialName]; Image image = null; if (material.UseAvatarMap) { Limb limb; if (Enum.TryParse(materialName, out limb)) { Rectangle cropRegion = UVCrops[limb]; Size size = cropRegion.Size; int w = size.Width; int h = size.Height; Point origin = cropRegion.Location; int x = origin.X; int y = origin.Y; Bitmap newImg = new Bitmap(w, h); Graphics graphics = Graphics.FromImage(newImg); Rectangle dest = new Rectangle(Point.Empty, size); graphics.DrawImage(uvMap, dest, x, y, w, h, GraphicsUnit.Pixel, blankAtt); graphics.Dispose(); image = newImg; } } else { Asset texture = material.TextureAsset; if (texture != null) { byte[] textureData = texture.GetContent(); MemoryStream textureStream = new MemoryStream(textureData); image = Image.FromStream(textureStream); } } if (image != null) { assembly.Images.Add(materialName, image); assembly.MatLinks.Add(materialName, materialName); } else { Rbx2Source.Print("Missing Image for Material {0}?", materialName); } } return(assembly); }