I struggled to get the sample camera patch to work in unity.
I finally managed to get it to work in the editor, but I can't get it to work on my Samsung Galaxy S3 (Android).
The only difference that I see is cameraTextureRatio, which in the editor is 1.0 1.0, the texture and image size are identical. On my phone it is:
_CAMERATEXTURE_RATIO_X:0.625 _CAMERATEXTURE_RATIO_Y:0.9375
with permissions:
VideoTextureInfo 1024 512 640 480
I count it like this:
cameraTextureRatio.x = (float)texInfo.imageSize.x / (float)texInfo.textureSize.x ; cameraTextureRatio.y = (float)texInfo.imageSize.y / (float)texInfo.textureSize.y ;
My shader:
Shader "Custom/VidTexPatch" { Properties { _MainTex("Texture", 2D) = "white" { } } SubShader{ Pass{ Cull Back CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; float4x4 _MATRIX_MVP; float _CAMERATEXTURE_RATIO_X ; float _CAMERATEXTURE_RATIO_Y ; struct v2f{ float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata_base v){ v2f o; float2 targetSize = float2(15,15); float2 cameraTextureRatio = float2 (_CAMERATEXTURE_RATIO_X, _CAMERATEXTURE_RATIO_Y); float4 targetPoint = float4(targetSize.x * (v.texcoord.x - 0.5), targetSize.y * (v.texcoord.y - 0.5), 0.0, 1.0); float4 clipCoords = mul(_MATRIX_MVP, targetPoint); float3 normalizedDeviceCoords = clipCoords.xyz / clipCoords.w; float2 cameraTexCoords = float2((normalizedDeviceCoords.x + 1.0) / 2.0, ((normalizedDeviceCoords.y * -1.0) + 1.0) / 2.0); float2 finalCoords = float2(cameraTexCoords.x * cameraTextureRatio.x, cameraTexCoords.y * cameraTextureRatio.y);
The buffer size is different from the texture size. I searched and found that this is due to the power of two equipment limitations.
At startup, the captured patch is displayed in front of the camera. when I tried in my moto g (android) resolution of 1280 * 720 (16: 9), a buffer of 2048 * 1024. and display is what is right.
the ratio of 1280/2048 = 0.62 and 720/1024 = 0.70 is somewhat closer compared to the 4: 3 resolution:
The buffer size of 640 * 480 (4: 3) is 1024 * 512, and the ratio of 640/1024 = 0.62 and 480/512 = 0.93. There should be some scaling in the x direction, which you can observe when viewing the plane (RenderTargetView) in your device. I tried to resize the texture, but unity will not allow this.
I did a clean unity project here: DropBox
the marker used is in Textures / ar.png