help with rendertexture

hello!
i’ll try to explain what i have…
my “field” camera with this script (used to keep the aspetct)

using UnityEngine;

[RequireComponent(typeof(Camera))]
public class CameraCrop : MonoBehaviour {

    // Set this to your target aspect ratio, eg. (16, 9) or (4, 3).
    public Vector2 targetAspect = new Vector2(16, 9);
    Camera _camera;

    void Start () {
        _camera = GetComponent<Camera>();
        UpdateCrop();
    }

    // Call this method if your window size or target aspect change.
    public void UpdateCrop() {
        // Determine ratios of screen/window & target, respectively.
        float screenRatio = Screen.width / (float)Screen.height;
        float targetRatio = targetAspect.x / targetAspect.y;

        if(Mathf.Approximately(screenRatio, targetRatio)) {
            // Screen or window is the target aspect ratio: use the whole area.
            _camera.rect = new Rect(0, 0, 1, 1);
        }
        else if(screenRatio > targetRatio) {
            // Screen or window is wider than the target: pillarbox.
            float normalizedWidth = targetRatio / screenRatio;
            float barThickness = (1f - normalizedWidth)/2f;
            _camera.rect = new Rect(barThickness, 0, normalizedWidth, 1);
        }
        else {
            // Screen or window is narrower than the target: letterbox.
            float normalizedHeight = screenRatio / targetRatio;
            float barThickness = (1f - normalizedHeight) / 2f;
            _camera.rect = new Rect(0, barThickness, 1, normalizedHeight);
        }
    }
}

and one more camera inside my canvas used to who 3d character on UI and a background image ( used to show a screenshot got from the field camera) so what i have on my ui is 3d model with a background spriterenderer with a screenshot texture got from “field” camera ( using rendertexture )

the above script is on the ui camera too, everything is working well, only the background image on 4:3 for example doesn’t fit the entire screen there is any way to fix this?

Could you post a screenshot showing your problem?

Stevens

This script appears to be some kind of a “contain within” approach. Lines 26 to 29 handle when the camera is wider than the desired view (add lateral black bands on each side), and lines 32 through 35 handle when the camera is narrower than the desired view (add top and bottom black bands).

Also, just to be clear, this has NOTHING to do with a RenderTexture, which is a very specific type of Unity3D object that is used to render to intermediate surfaces. The camera involved might have a RenderTexture on it, but that is not relevant to the above code.

That is a good point. In debugging this kind of thing, it can be very helpful to strip off the parts that aren’t necessary to making the problem happen.

Stevens

on cap1 is what i have on my “field” camera then i take a screenshot and on cap2 is what i have after i get a screenshot with the rendertexture then convert it to a texture2d and create a new sprite from that texture ( to a sprite render inside the canvas camera)

so as you can see the screenshot doesn’t fit the entire screen, without the above script it does fit perfectly.

this is what i do to resize it

    void ResizeSpriteToScreen()
    {
        SpriteRenderer sr = sprite.GetComponent<SpriteRenderer>();

        sprite.transform.position = _myCamera.transform.position + Vector3.forward * sr.transform.position.z;

        sprite.transform.localScale = new Vector3(1, 1, 1);
        Vector3 lossyScale = sprite.transform.lossyScale;

        float width = sr.sprite.bounds.size.x;
        float height = sr.sprite.bounds.size.y;

        float worldScreenHeight = _myCamera.orthographicSize * 2f;
        float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;

        Vector3 xWidth = sprite.transform.localScale;
        xWidth.x = worldScreenWidth / width;
        sprite.transform.localScale = xWidth;
        //transform.localScale.x = worldScreenWidth / width;
        Vector3 yHeight = sprite.transform.localScale;
        yHeight.y = worldScreenHeight / height;
        sprite.transform.localScale = yHeight;

        Vector3 newLocalScale = new Vector3(transform.localScale.x / lossyScale.x,
            sprite.transform.localScale.y / lossyScale.y,
            sprite.transform.localScale.z / lossyScale.z
        );

        sprite.transform.localScale = newLocalScale;
    }

6403224--714432--cap1.PNG
6403224--714435--cap2.PNG