public static TextureBindings BindTextures(Dictionary <string, Material> materials) { var textures = new TextureBindings(); var images = textures.Images; foreach (string mtlName in materials.Keys) { Material material = materials[mtlName]; Asset textureAsset = material.TextureAsset; if (textureAsset == null || textureAsset.Id == 9854798) { var linkedTo = material.LinkedTo; Color color; if (linkedTo.BrickColor != null) { BrickColor bc = linkedTo.BrickColor; color = bc.Color; } else if (linkedTo.Color3uint8 != null) { color = linkedTo.Color3uint8; } else { BrickColor def = BrickColor.FromNumber(-1); color = def.Color; } float r = color.R / 255f, g = color.G / 255f, b = color.B / 255f; if (!images.ContainsKey("BrickColor")) { byte[] rawImg = ResourceUtility.GetResource("Images/BlankWhite.png"); using (MemoryStream imgStream = new MemoryStream(rawImg)) { Image image = Image.FromStream(imgStream); textures.BindTexture("BrickColor", image, false); } } material.UseEnvMap = true; material.VertexColor = new Vector3(r, g, b); textures.BindTextureAlias(mtlName, "BrickColor"); } else { byte[] rawImg = textureAsset.GetContent(); using (MemoryStream imgStream = new MemoryStream(rawImg)) { Image image = Image.FromStream(imgStream); textures.BindTexture(mtlName, image); } } } return(textures); }
public TextureBindings BindTextures(TextureCompositor compositor, Dictionary <string, Material> materials) { 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 .GetChildrenOfClass <CharacterMesh>() .ToDictionary(mesh => mesh.BodyPart); // Compose the textures that will be used var limbOverlays = new Dictionary <Limb, long>(); var limbBitmaps = new Dictionary <long, Bitmap>() { { 0, body } }; foreach (Limb limb in LimbMatcher.Keys) { // Head is already textured, ignore it. if (limb == Limb.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 (Limb 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)) { Material 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); }