Guys the solution I used was to apply a transform matrix which is processed in the shader.
Just use what you need from here:
You can just add “matrix [_Rotation]” to your shader pass in your existing shader if you don’t want to build your own shader at run-time as they did in this example.
The full transformation matrix is built for you so you don’t need to understand how the 4x4 Matrix works internally, just plug in your offset, scale and rotation values. Unity - Scripting API: Matrix4x4.TRS
The real problem I have at the moment is HOW DO YOU DETECT THE RESOLUTION OF THE CAMERA??? You can’t scale the resulting image to give the correct aspect ratio without knowing this making the feature fairly useless. Is there something I missed here? I need to build for iOS and Android, have only just hit this problem so will post back if I find a solution.
I’m seeing exactly the same thing. However, if you’re simply rendering with the texture, don’t manually manipulate the pixel data, instead construct your own orthographic camera, and a custom plane mesh with different UVs on the various platforms.
However, I agree with your general observation that handling of webcams isn’t quite as cross-platform as you might like.
Code attached below which I’m currently using to generate a 2D plane with identical positions but different UVs which works on iOS and Mac (haven’t yet tested windows or android):
videoPlaneMesh = new Mesh ();
videoPlaneMesh.vertices = new Vector3[] { new Vector3 (-1, -1, 0), new Vector3 (1, -1, 0), new Vector3 (-1, 1, 0), new Vector3 (1, 1, 0) };
//
// We have to use different UVs for OpenCV versus unity Webcam, because the y-axes of the image we get from the camera
// are opposite.
//
if (useUnityWebcam)
{
#if UNITY_IPHONE && !UNITY_EDITOR
// This version is for iPad, and seems to permute X and Z
videoPlaneMesh.uv = new Vector2[] { new Vector2 (1, 1), new Vector2 (1, 0), new Vector2 (0, 1), new Vector2 (0, 0) };
#else
videoPlaneMesh.uv = new Vector2[] { new Vector2 (0, 0), new Vector2 (1, 0), new Vector2 (0, 1), new Vector2 (1, 1) };
#endif //UNITY_IPHONE
The solution I used was to apply a transform matrix which is processed in the shader.
Just use what you need from here:
You can just add “matrix [_Rotation]” to your shader pass in your existing shader if you don’t want to build your own shader at run-time as they did in this example.
The full transformation matrix is built for you so you don’t need to understand how the 4x4 Matrix works internally, just plug in your offset, scale and rotation values. Unity - Scripting API: Matrix4x4.TRS
The real problem I have at the moment is HOW DO YOU DETECT THE RESOLUTION OF THE CAMERA??? You can’t scale the resulting image to give the correct aspect ratio without knowing this making the feature fairly useless. Is there something I missed here? I need to build for iOS and Android, have only just hit this problem so will post back if I find a solution.
Hi Iamdain,
I’m having the same problem but am stuck in trying to create the correct inputs for the TRS function. What scale did you use to get the proper flip of the rawdata from the iOS device?
Thanks in advance for your help!
-Amy
I am new to Unity IOS. I have tried to do the matrix bit to the best of my abilities with no luck. Iamdain could you please explain your solution in more detail? From a newbies point of view maybe?