Thursday, October 20, 2011

Unity 3D (Pro): View matrix (separately) for CG shaders

As Unity's shader support is largely based on Open GL, there isn't, by default design, access to separate model and view matrices in shader code. Occasionally you might want access to this and to use it you'll need to pass this information in yourself.

Just a quick summary of what Unity does provide in Shader code.


  • UNITY_MATRIX_MVP - Current model * view * project matrix

  • UNITY_MATRIX_MV - Current mode * view matrix

  • UNITY_MATRIX_P - Current project matrix

  • _Object2World - Current model matrix

  • _World2Object - Inverse of current world matrix



Very occasionally I wish there was a UNITY_MATRIX_M and UNITY_MATRIX_V but there isn't - at least - not yet. (Same limitation in GLSL)

In C# we can get the view matrix from Camera.mainCamera.worldToCameraMatrix and for GameObjects the model matrix can be generated by using Matrix4x4.TRS( transform.position, transform.rotation, transform.localScale).

If we wanted to pass in our own modelView matrix we could do


Matrix4x4 modelViewMatrix = Camera.mainCamera.worldToCameraMatrix * Matrix4x4.TRS( transform.position, transform.rotation, transform.localScale);



Then at the appropriate point we can do


material.SetMatrix("modelView", modelViewMatrix);



To pass this model view matrix into our shader.

In our shader, presumably in the vertex shader we can then use this value like so


v2f vert(appdata_base v) 
{
v2f o;
o.pos = mul( mul(UNITY_MATRIX_P, modelView), v.vertex );
return o;
}



The result of the above code is exactly the same as


o.pos = mul( UNITY_MATRIX_MVP, v.vertex );



So now knowing this you can manipulate and/or use the model and view matrices separately in your shaders if you so choose, by first passing them in from script code.

Tuesday, October 11, 2011

Moving iPhone Developer Credentials from one Mac to another

Update: So apparently in Xcode 4 (at least) there is an official way to do this... outlined here

I recently reinstalled OSX on my Mac and upgraded to Lion. Of course I forgot to transfer my iPhone developer credentials before I did it. I had to go back to the provisioning portal and setup my new certificate and get a new developer profile. While it wasn't a huge time waste I should have transferred my certificates and profile to save myself some time.

This is what I should have done.


  • Open Xcode

  • Window->Organizer
  • DEVELOPMENT -> Provisioning Profiles

  • Choose your Provisioning profile, right click and "Reveal in Finder"

  • Save the resulting file

  • Open Keychain Access

  • Export your private and public certificates to files and save them

  • Transfer all the file to your new system

  • Drag and drop Provisioning Profile into Xcode's Organiser on your new system

  • Import the certificates file you exported into Keychain Access on your new system



NOTE: There is a very annoying bug in Keychain Access that means you need to re-import your certificates at the command line. Attempting to you import you certificates using the Keychain Access GUI yields "An error has occurred. Unable to import an item. The contents of this item cannot be retrieved". This is just a blatant bug in Keychain Access you can import the files into Keychain Access at the command line with the following commands.

security import priv_key.p12 -k ~/Library/Keychains/login.keychain
security import pub_key.pem -k ~/Library/Keychains/login.keychain