Scaling part of an image to fit a desired rectangle with overflow









Here’s the deal where this maths helps:-

Lets say I have a large image or webcam view, and can find a face in it and have the x,y,width,height rectangle coordinates for that face in that image, and I want to take that face and make it fit a template (fit in another rectangle in a second image).

Simply just copy the rectangle pixels from image 1 and paste it to the new rectangle in image 2, right..

however what if I want to take some of the image surrounding the face so I can use a feathered mask on image 2 as such, i.e. image 2 is 512px x 512px and I want the face to go into a rectangle somewhere in the lower middle of this second image, but wish fill the entire of image2 with available data (obviously if the face is right at the border of an image some will be left blank).

Well the below helped me with some javascript I was doing with canvas and webcams etc, but is applicable across the board hence the maths section. Firstly I found this:-

So taking that I created this code, javascript/pseudocode.


Hope that helps with someone’s brain mangling, if indeed you stumble across this page.

By |October 11th, 2014|Math|Comments Off on Scaling part of an image to fit a desired rectangle with overflow

Unity 3D Split APK for Android Google Play – OBB File

When I found myself having to split an android application (APK file) to allow it to be uploaded onto the Google Play store, I found a serious lack of current information and much misdirection possibly due to the recent changes within Unity and the actual Play store itself.

So here is a rough guide that works with unity 4.3 or up, in this example I am only catering for devices running android 4.0 ICS or up ( API level 16), not quite sure what to do with earlier Android API versions at this very moment.


Firstly ignore the asset store OBB plug-in  (it was created for earlier versions of unity, or possibly for catering for earlier Android API’s) going down this route will burn hours of your life.


If you haven’t done so already go to EDIT>PROJECT SETTINGS>PLAYER SETTINGS select the android role out and go to the PUBLISHING SETTINGS roll-out and do the following:-

1) Create a new keystore by selecting a keystore name and password (confirm the password)
2) Select “Create a new key” under Key Alias
3) A new window opens; enter the necessary information.
4) Select the newly created key.

n.b. Remember the passwords; they are not stored with the project (for security reasons), every time you load the project you will need to enter this password from now on.

5) Tick Split Application Binary at the bottom of this roll out!


Again if you haven’t already done so create a primary loading scene with very little content, this will allow unity to split most of your app data off to the OBB data/zip file and keep the APK file size small. As an example I created a scene called Android Loader, so that I could exclude […]

Chroma Key / Key Matte Shader for Textures – Vuforia Video Textures

I was going to place this on the asset store, but someone already has put something on there, so here you go for free, this shader simply removes a set colour with tolerance from a texture and makes its transparent. It is really useful when using video/movie textures with Qualcomm Vuforia plugin to cut out a person for instance on a blue screen /  keyable background to then plant that character in an Augmented Reality setting:

As advised below, this shader should possibly tagged with the keywords IOS, Android, Alpha, Channel. Hope this helps.

Unity3d XML Object/Class/Type Serialisation Attributes

In an earlier post I post the basics of parsing xml to a class/object of type, below are the correct attributes to help the parser with coercion of values, type and values to ignore etc, which helps speed things up dramatically obviously, in this example we take some arbitrary XML as below:-

And in unity for this xml would have the class set with the following xml attributes (note the use of List ‘using system.collections.generic;’ ):-

And to take the xml text and parse it to the object:-



By |November 17th, 2013|Unity 3D|Comments Off on Unity3d XML Object/Class/Type Serialisation Attributes

Unity3d XML Object/Class Serialisation (the basics)

A friend of mine was asking about how you can save the scene hierarchy and object/script properties  in Unity, he had written most of the code instantiating prefabs and was already busy setting variables via interactions in his scripts. I explained that although I do have a generic reflection based method, that can save/restore the hierarchy, gameobjects, transforms, materials & script variables ( used on the space application ), it would be too verbose and unfitting for this task, plus if his script and prefab scripts were not written the right way this approach may not work.

With simpler configuration apps a much simpler class based object oriented approach is all that is required and most cases it is better to start with that system in mind than to retro fit. It is also much better if bringing in xml /json from other sources to reflect the xml/json structure with a c# class which you can then attach methods to manipulate the data, rather than hand parse each node (after learning the hard way), something that might be appropriate with collada objects perhaps.

Update: The methods below how to correctly set xml attributes in c# to coerce your values from the xml to your class see the latest post:-

Unity3d XML Object/Class/Type Serialisation Attributes

Below is an older very simple one script demonstration of instantiation and recording values to and from XML in Unity, apologies for any errors or messy logic I think I only had an hour to write it one afternoon, but hopefully it will convey the basics, but I would use the above attributes in c# see the link above over the method below for helping the xml parser […]

By |September 2nd, 2013|CodeBlog, Unity 3D|Comments Off on Unity3d XML Object/Class Serialisation (the basics)