Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the TextureResizer #1

Open
ZeevAbrams opened this issue Jan 9, 2022 · 0 comments
Open

Simplify the TextureResizer #1

ZeevAbrams opened this issue Jan 9, 2022 · 0 comments

Comments

@ZeevAbrams
Copy link

Hi,
Thanks for posting the code for tfLite - but it's massive overkill. Though I tried many ways to go around it, the best was just to copy the code within the TextureResizer into a new Script file, and just use it directly. It has almost no dependencies on the tfLite package - except for the Dispose - which I remarked out below:
(Also - I copied the required Shader to a new Shader file under Custom/ResizeShader - it's easy to find

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

public class TextureResizer : MonoBehaviour
{
// taking the code from : https://github.com/paulorenanmelo/MonocularDepthSensing/blob/master/Assets/Scripts/tf-lite-unity-sample/Common/TextureResizer.cs
// and redoing it

RenderTexture resizeTexture;
Material _blitMaterial;

static readonly int _VertTransform = Shader.PropertyToID("_VertTransform");
static readonly int _UVRect = Shader.PropertyToID("_UVRect");


public enum AspectMode
{
    None,
    Fit,
    Fill,
}
public struct ResizeOptions
{
    public int width;
    public int height;
    public float rotationDegree;
    public bool flipX;
    public bool flipY;
    public AspectMode aspectMode;
}

public Material material
{
    get
    {
        if (_blitMaterial == null)
        {
            _blitMaterial = new Material(Shader.Find("Custom/ResizeShader"));  //changed
        }
        return _blitMaterial;
    }
}

public Vector4 UVRect
{
    get => material.GetVector(_UVRect);
    set => material.SetVector(_UVRect, value);
}

public Matrix4x4 VertexTransfrom
{
    get => material.GetMatrix(_VertTransform);
    set => material.SetMatrix(_VertTransform, value);
}

public TextureResizer()
{

}

//public void Dispose()
//{
//    DisposeUtil.TryDispose(resizeTexture);
//    DisposeUtil.TryDispose(_blitMaterial);
//}
public ResizeOptions ModifyOptionForWebcam(ResizeOptions options, WebCamTexture texture)
{
    options.rotationDegree += texture.videoRotationAngle;
    if (texture.videoVerticallyMirrored)
    {
        options.flipX = !options.flipX;
    }
    return options;
}


public RenderTexture Resize(Texture texture, ResizeOptions options)
{
    // Set options
    if (texture is WebCamTexture)
    {
        options = ModifyOptionForWebcam(options, (WebCamTexture)texture);
    }

    VertexTransfrom = GetVertTransform(options.rotationDegree, options.flipX, options.flipY);
    UVRect = GetTextureST(texture, options);
    return ApplyResize(texture, options.width, options.height);
}


public RenderTexture ApplyResize(Texture texture, int width, int height)
{
    if (resizeTexture == null
        || resizeTexture.width != width
        || resizeTexture.height != height)
    {
        //DisposeUtil.TryDispose(resizeTexture);
        resizeTexture = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32);
    }
    Graphics.Blit(texture, resizeTexture, material, 0);
    return resizeTexture;
}

public static Vector4 GetTextureST(float srcAspect, float dstAspect, AspectMode mode)
{
    switch (mode)
    {
        case AspectMode.None:
            return new Vector4(1, 1, 0, 0);
        case AspectMode.Fit:
            if (srcAspect > dstAspect)
            {
                float s = srcAspect / dstAspect;
                return new Vector4(1, s, 0, (1 - s) / 2);
            }
            else
            {
                float s = dstAspect / srcAspect;
                return new Vector4(s, 1, (1 - s) / 2, 0);
            }
        case AspectMode.Fill:
            if (srcAspect > dstAspect)
            {
                float s = dstAspect / srcAspect;
                return new Vector4(s, 1, (1 - s) / 2, 0);
            }
            else
            {
                float s = srcAspect / dstAspect;
                return new Vector4(1, s, 0, (1 - s) / 2);
            }
    }
    throw new System.Exception("Unknown aspect mode");
}

public static Vector4 GetTextureST(Texture sourceTex, ResizeOptions options)
{
    return GetTextureST(
        (float)sourceTex.width / (float)sourceTex.height, // src
        (float)options.width / (float)options.height, // dst
        options.aspectMode);
}

private static readonly Matrix4x4 PUSH_MATRIX = Matrix4x4.Translate(new Vector3(0.5f, 0.5f, 0));
private static readonly Matrix4x4 POP_MATRIX = Matrix4x4.Translate(new Vector3(-0.5f, -0.5f, 0));
public static Matrix4x4 GetVertTransform(float rotation, bool invertX, bool invertY)
{
    Vector3 scale = new Vector3(
        invertX ? -1 : 1,
        invertY ? -1 : 1,
        1);
    Matrix4x4 trs = Matrix4x4.TRS(
        Vector3.zero,
        Quaternion.Euler(0, 0, rotation),
        scale
    );
    return PUSH_MATRIX * trs * POP_MATRIX;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant