Start a new topic
Solved

WikitudeCamera Change Aspect ratio Unity

Hi


Unity 2019.2.17

I need to change the WikitudeCamera Aspect ratio to fit inside a video box (small box inside the screen), but didn't work using the WikitudeCamera inside a RectTransform with anchors. 

I found in the documentations that "The WikitudeCamera prefab takes care about rendering the live camera stream fullscreen behind all your augmentations."


I'm using the Input Plugins API to supply the frame data.


So, I wonder if it is possible to resize it and how do it.


Thanks.


Hmm, hard to tell without seeing more of the project. Did you use render textures in your approach?

You could definitely try using a fullscreen camera and just overlaying it with an overlay with your desired "cutout". Not the most performant way I admit, but the fastet achievable way for sure.


Kind regards,
Gökhan

Hi Gökhan


I'm using the sample Plane Detection feeding the frames via Input Plugin.

Its possible to add the video to texture, like the screen shot shows, but what I need is to resize the video with the augmentation  (which is behind the texture).




image


Hi Leo,


Thats unfortunately not possible since the background camera is generated on runtime and is a hidden object in the hierarchy in our professional edition SDK. In expert edition we introduced the CameraFrameRenderer gameObject therefore.


Nevertheless, there is a workaround to make it visible in professional edition. Just add this script to the WikitudeCamera object.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BackgroundCameraEnable : MonoBehaviour
{
    public Camera BackgroundCamera;

    void Update() {
        if(!BackgroundCamera) {
            BackgroundCamera = gameObject.transform.Find("BackgroundCamera").GetComponent<Camera>();
            BackgroundCamera.gameObject.hideFlags = HideFlags.None;
        }
    }
}


It should instantly find the object and "unhide" it on entering play mode. You could then use the camera component to resize and modify the camera to your likings.


Kind regards,

Gökhan



Hi Gökhan


It worked, but another problem occurs. When the frame width and height are equal of the screen, ok. 

Although, the video freezes/stop working on Unity play mode.

I'm using the Input Plugins API to supply the frame data, as shown in the documentation. 

Any suggestion?


Thanks


Hi Leo,
Could you share, how you are using the Input Plugins API?


Kind regards,

Gökhan

Hi Gökhan


The Input Plugins API is used exactly like the sample shows. The problem described before no longer appears (miraculously). But, instead as I sad before, the solution proposed ONLY works on Unity Play mode, not being useful when the Android apk is built and installed in the smartphone. The grid is resized to a small rectangle inside the screen, but the video is still full screen.

Even if I apply the solution proposed to samples without Input Plugins API (using the camera from the device), the video does not resize as set.


Any suggestion?

Thanks

Leo


Hi Leo,


i did a quick test and summarized what you can do in order to get a scaled, aspect ratio changed background + ar camera view:


Create two render textures. One will hold the AR Cameras output ("WikitudeCamera") and a second one for the background camera.

They are set up like this in my case. Sizes of the render textures should be set during runtime.

You have to set the WikitudeCamera camera components clear flags to "Solid Color" with a transparent color by script on runtime, because it will be set to "Depth Only" on startup. But for the render textures you need this to be set. You can modify the BackgroundCameraEnable script i posted before (which has to be added to the WikitudeCamera gameobject). This way you can get access to both camera components on runtime.


Also add this two renderTextures as public variables to the script. Create your UI and add two RawImage UI elements to the canvas. I did it like this for instance (keep in mind -> the background camera image has to be behind the AR camera or in this case higher in the hierarchy):

The Raw Images will hold the render textures of the cameras. I set their sizes to their native sizes (by script) and scaled them down to 0.5. The Box element wrapping the raw images has a Rect Mask 2D component, which will cut off everything outside of its borders. The size of the box was set to 500 by 500 in this case, as an example.



Then you should be able to get from this:

toooooooooo this:

You can change the shape of the box anyway you want and position it anywhere you want. Keep in mind to alter the  render texture and ui sizes on device orientation changes.


Let me know if this solution works for you!

Kind regards,
Gökhan


Hi Gökhan


I've implemented the suggestion, but it only works on Unity Play mode again. When installing the Android APK, the texture became black. I'm testing on Android 10, Samsung Galaxy A50.


Unity Play mode screen shot: The textures renders the video correctly 

image


Android 10 device screen shot: The textures doesn't renders the video, and the camera appears int the back still in full screen 

image


Tkanks

Leo

Hi Leo,

Thats probably because the Render Texture for the background camera wasn't set. I would check for this in the update loop and apply the changes if needed. Here a code snippet (untested):

...

void Update() {
   if (backgroundCamera.renderTexture == null) {
      backgroundCamera.renderTexture = backgroundCameraRenderTexture;
   }

   if (backgroundCamera.clearFlags != ClearFlags.SolidColor) {
      backgroundCamera.clearFlags = CameraClearFlags.SolidColor;
      backgroundCamera.backgroundColor = new Color(0,0,0,0);
   }
   ...
}

...




Kind regards,
Gökhan

Hi Gökhan


Correct! The problem was not setting the BackgroundCamera targetTexture:


 if (BackgroundCamera.targetTexture == null){

     BackgroundCamera.targetTexture = backgroundCameraRenderTexture;

}


Now it works!

Thanks

Leo


1 person likes this

Hi Gökhan


Developing the tool, using the suggestions above, we've faced another problem. The AR target texture has a different size in relation to the screen, so the AR objects are not placed correctly. I think it is because the Tracker.ConvertScreenCoordinate, which takes the Input.mousePosition, converts the coordinates to the screen size references not the new AR target texture resolution.


For instance, the ScenePickingController sample:

/* If we register a screen tap while we're tracking, convert the screen tap to a coordinate in the map. */

if (_isTracking && Input.GetMouseButtonUp(0)) {

   /* The result of this operation is not instantaneous, so we need to wait for

    * the OnScreenConversionComputed callback to get the results. */

   Tracker.ConvertScreenCoordinate(Input.mousePosition);

   }



Any suggestion?

Thanks

Leo


Hi Leo,


I guess you also have to adjust the inputs position and send the adjusted position to the Tracker where you would have touched, if the view would have been fullscreen. Hmmm, let me come of with some pseudo code (not tested) for you...



if (_isTracking && Input.GetMouseButtonUp(0)) {
   if (ARCameraViewRect.Contains(Input.mousePosition) {
      Vector2 adjustedPosition = new Vector2();
      adjustedPostition.x = Input.mousePosition.x - ARCameraViewRect.position.x * (Screen.width / ARCameraViewRect.size.width);
      adjustedPostition.y = Input.mousePosition.y - ARCameraViewRect.position.y * (Screen.height / ARCameraViewRect.size.height);  
    
     Tracker.ConvertScreenCoordinate(adjustedPostition);
   }
}


Kind regards,
Gökhan


Hi Gökhan


Unfortunately it doesn't work.  The background camera and the AR Camera are not in full screen, as I pointed out before. I have to add pillar boxes to fit the video resolution in different aspect ratio devices, so they have the same video size shown (despite the aspect ratio, there is not width or height frame cut).


Any other suggestion?

Thanks

Leo

Login or Signup to post a comment