24

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

D.B
  • 596
  • 4
  • 17
Abhay Shankar
  • 241
  • 1
  • 5
  • 1
    Hi, could you explain what you're trying to achieve? I'm lost after reading your question. – Augmented Jacob Apr 16 '15 at 07:30
  • @AugmentedJacob i am trying to create an app similar to colAR app – Abhay Shankar Apr 17 '15 at 08:23
  • @AbhayShankar Hello, did you managed to get this running? I'm getting the very same problem and would be interested to know what you did. Thanks, and thanks for posting your code and keeping the link alive. – Valryon Jun 14 '15 at 21:09
  • @AbhayShankar Neither did I... but I managed to it in another way as I needed the camera patch to extract colors from a drawing. (Short version: place GameObjects to be displayed, when AR is on, take a screenshot, take each GameObject position and extract color from screenshot) – Valryon Jun 22 '15 at 09:18
  • Can you give us more details about what does not work and what is intended? Can you talk more about what the Camera Patch sample is? All I found is [this](https://developer.vuforia.com/forum/unity-extension-technical-discussion/camera-patch-unity) – Samie Bencherif Feb 26 '20 at 23:22

0 Answers0