Skip to content

Instantly share code, notes, and snippets.

@olokobayusuf
Last active July 7, 2017 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olokobayusuf/1dbfa84397dce4330d942355671484e3 to your computer and use it in GitHub Desktop.
Save olokobayusuf/1dbfa84397dce4330d942355671484e3 to your computer and use it in GitHub Desktop.
/**
* In this example, we rotate the Texture2D on the main thread (which will block)
*/
void OnPhoto (Texture2D photo, Orientation orientation) {
// Rotate the image
Utilities.RotateImage(photo, orientation, null, null, (rotated, unused) => preview.texture = rotated);
}
/**
* In this example, we rotate the Texture2D on a worker thread then invoke the callback on the main thread (required as Unity is not thread safe)
*/
void OnPhoto (Texture2D photo, Orientation orientation) {
// Create IDispatches for a worker thread and the main thread
IDispatch worker = new ConcurrentDispatch(), main = new MainDispatch();
// Rotate the image
Utilities.RotateImage(photo, orientation, worker, main, (rotated, unused) => preview.texture = rotated);
}
/**
* In this example, we rotate the pixels then update the Texture2D with it
* Since the rotation is done on the main thread, the call to RotateImage will block
*/
void OnPhoto (Texture2D photo, Orientation orientation) {
// Get the pixels
var pixels = photo.GetPixels32();
// Allocate a destination buffer for the rotated pixels
var rotated = new Color32[pixels.Length];
// Rotate the image
Utilities.RotateImage(rotated, orientation, photo.width, photo.height, (byte)orientation);
// If we are rotating by 90 or 270 degrees, then we must resize the texture itself, swapping the width and height
if ((orientation & Orientation.Rotation_90) == Orientation.Rotation_90 || (orientation & Orientation.Rotation_270) == Orientation.Rotation_270) photo.Resize(photo.height, photo.width);
// Set pixels and apply
photo.SetPixels32(rotated);
photo.Apply();
// Display the photo
preview.texture = photo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment