Friday 25 March 2011

This is the radegast bake code

The following is the radegast bake code. Note that this is not all the code you need because there is a call to Baker.Bake() and Baker.AddTexture() et cetera.
That said, here it is for posterity:


/// Blocking method to create and upload baked textures for all of the
/// missing bakes
///
/// True on success, otherwise false
private bool CreateBakes()
{
bool success = true;
List pendingBakes = new List();

// Check each bake layer in the Textures array for missing bakes
for (int bakedIndex = 0; bakedIndex < BAKED_TEXTURE_COUNT; bakedIndex++) { AvatarTextureIndex textureIndex = BakeTypeToAgentTextureIndex((BakeType)bakedIndex); if (Textures[(int)textureIndex].TextureID == UUID.Zero) { // If this is the skirt layer and we're not wearing a skirt then skip it if (bakedIndex == (int)BakeType.Skirt && !Wearables.ContainsKey(WearableType.Skirt)) continue; pendingBakes.Add((BakeType)bakedIndex); } } if (pendingBakes.Count > 0)
{
DownloadTextures(pendingBakes);

Parallel.ForEach(Math.Min(MAX_CONCURRENT_UPLOADS, pendingBakes.Count), pendingBakes,
delegate(BakeType bakeType)
{
if (!CreateBake(bakeType))
success = false;
}
);
}

// Free up all the textures we're holding on to
for (int i = 0; i < Textures.Length; i++) { Textures[i].Texture = null; } // We just allocated and freed a ridiculous amount of memory while // baking. Signal to the GC to clean up GC.Collect(); return success; } ///
/// Blocking method to create and upload a baked texture for a single
/// bake layer
///

/// Layer to bake /// True on success, otherwise false
private bool CreateBake(BakeType bakeType)
{
List textureIndices = BakeTypeToTextures(bakeType);
Baker oven = new Baker(bakeType);

for (int i = 0; i < textureIndices.Count; i++) { AvatarTextureIndex textureIndex = textureIndices[i]; TextureData texture = Textures[(int)textureIndex]; texture.TextureIndex = textureIndex; oven.AddTexture(texture); } int start = Environment.TickCount; oven.Bake(); Logger.DebugLog("Baking " + bakeType + " took " + (Environment.TickCount - start) + "ms"); UUID newAssetID = UUID.Zero; int retries = UPLOAD_RETRIES; while (newAssetID == UUID.Zero && retries > 0)
{
newAssetID = UploadBake(oven.BakedTexture.AssetData);
--retries;
}

Textures[(int)BakeTypeToAgentTextureIndex(bakeType)].TextureID = newAssetID;

if (newAssetID == UUID.Zero)
{
Logger.Log("Failed uploading bake " + bakeType, Helpers.LogLevel.Warning);
return false;
}

return true;
}

///
/// Blocking method to upload a baked texture
///

/// Five channel JPEG2000 texture data to upload /// UUID of the newly created asset on success, otherwise UUID.Zero
private UUID UploadBake(byte[] textureData)
{
UUID bakeID = UUID.Zero;
AutoResetEvent uploadEvent = new AutoResetEvent(false);

Client.Assets.RequestUploadBakedTexture(textureData,
delegate(UUID newAssetID)
{
bakeID = newAssetID;
uploadEvent.Set();
}
);

// FIXME: evalute the need for timeout here, RequestUploadBakedTexture() will
// timout either on Client.Settings.TRANSFER_TIMEOUT or Client.Settings.CAPS_TIMEOUT
// depending on which upload method is used.
uploadEvent.WaitOne(UPLOAD_TIMEOUT, false);

return bakeID;
}

No comments:

Post a Comment