I've been struggling to get the camera patch sample to work in unity.
I finally succeeded to make it work in the editor but for the life of me can't make it work on my samsung galaxy s3(android).
The only difference I can see is cameraTextureRatio which is 1.0 1.0 in the editor the texture and image size being identical. On my phone it is:
_CAMERATEXTURE_RATIO_X:0.625
_CAMERATEXTURE_RATIO_Y:0.9375
with resolutions of:
VideoTextureInfo 1024 512 640 480
I'm calculating it like so:
cameraTextureRatio.x = (float)texInfo.imageSize.x / (float)texInfo.textureSize.x ;
cameraTextureRatio.y = (float)texInfo.imageSize.y / (float)texInfo.textureSize.y ;
My shader is:
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);
// float2 finalCoords = float2(cameraTexCoords.x * cameraTextureRatio.y, cameraTexCoords.y * cameraTextureRatio.y);
o.uv = finalCoords;
//o.uv=cameraTexCoords;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
half4 frag(v2f i) : COLOR{
half4 texcol = tex2D(_MainTex, i.uv);
return texcol;
}
ENDCG
}
}}
The buffer size is different to the texture size.I searched and found it is related to power of two restriction of hardware.
When run the captured patch gets displayed in front of the camera. when i tried in my moto g(android) the resolution is 1280*720 (16:9) the buffer is of 2048*1024. and mapping is some what correct.
the ratio 1280/2048 =0.62 and 720/1024=0.70 is somewhat closer with respect to when resolution is 4:3:-
640*480 (4:3) the buffer is of size 1024*512 and the ratio 640/1024=0.62 and 480/512=0.93. There must be some scaling in x direction which you can observe when you view the plane(RenderTargetView) in your device. I tried to resize the texture but unity wont allow it.
I've made a clean unity project here: DropBox
the marker used is in Textures/ar.png