Setting pixel color out of (clamped) texture area

Node-based Shader Editor

Setting pixel color out of (clamped) texture area

Postby Jayme65 » Sat Nov 18, 2017 2:07 pm

Hi,

I have to display a texture with its correct aspect ratio on a plane, then instead of having the border pixels stretched out over the rest of the surface have it replaced by a color.
The correct aspect ration is given that way:
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class InitScript : MonoBehaviour
{
    public GameObject screen;
    public Material bmp;
    void Start()
    {
        SetTexture(@"C:\Users\JM\Desktop\169.jpg");
    }

    void SetTexture(string path)
    {
        // Create new texture2d
        Texture2D tex = LoadPNG(path);
        if (tex == null)
        {
            return;
        }
       
        // Set width/height scale/offset for UV's
        float twidth = 1.0f;
        float theight = 1.0f;
        float owidth = 0.0f;
        float oheight = 0.0f;
        if (tex.width >= tex.height)
        {
            theight = (float)tex.width / tex.height; // Tiling
            oheight = (theight - 1) / -2; // Offset
        }
        else
        {
            twidth = (float)tex.height / tex.width;
            owidth = (twidth - 1) / -2;
        }

        // Set texture
        bmp.SetTexture("_TextureSnap", tex);

        // Scale object UVs to fit texture
        bmp.SetTextureScale("_TextureSnap", new Vector2(twidth, theight));
        bmp.SetTextureOffset("_TextureSnap", new Vector2(owidth, oheight));
    }

    public static Texture2D LoadPNG(string filePath)
    {
        Texture2D tex = null;
        byte[] fileData;
        if (File.Exists(filePath))
        {
            fileData = File.ReadAllBytes(filePath);
            tex = new Texture2D(2, 2, TextureFormat.ARGB32, false);
            tex.filterMode = FilterMode.Point;
            tex.wrapMode = TextureWrapMode.Clamp;
            tex.LoadImage(fileData);
        }
        return tex;
    }
}


I end up with something like this (left) but now want to fill the "stretched" area with a color (right)
Image

Would you please help me understand how I should do to achieve this final result?
Thanks a lot!

My diagram at the moment
Image
Jayme65
 
Posts: 6
Joined: Wed Apr 19, 2017 6:18 pm

Re: Setting pixel color out of (clamped) texture area

Postby Amplify_Borba » Mon Nov 20, 2017 7:34 pm

Hello Jayme65!

Thank you for getting in touch!

I've built the following shader, which has also been uploaded to this post for your convenience, that provides you means to achieve the intended result without resorting to an external script, you may use it or edit it to suit your needs.

scale.jpeg
scale.jpeg (190.33 KiB) Viewed 2984 times


ScaleShader.zip
(1.56 KiB) Downloaded 271 times


Please let me know if you have any further questions, thanks!
Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Amplify_Borba
 
Posts: 1239
Joined: Mon Jul 24, 2017 9:50 am

Re: Setting pixel color out of (clamped) texture area

Postby Jayme65 » Tue Nov 21, 2017 9:36 am

It works! ;)
Image
Thanks for your help, your support is greatly appreciated!

One last think..if you don't mind!?

The final goal for this shader is to be able to blend 2 textures (with one being a render texture), setting correct aspect-ratio and filling with transparency when out of it (this step was the subject of this question).
This is what I ended up with, is that correct and optimized from your point of view?
Image

Thanks a lot
Last edited by Jayme65 on Tue Nov 21, 2017 2:47 pm, edited 1 time in total.
Jayme65
 
Posts: 6
Joined: Wed Apr 19, 2017 6:18 pm

Re: Setting pixel color out of (clamped) texture area

Postby Amplify_Borba » Tue Nov 21, 2017 2:23 pm

Hello!

The following simplified sample will blend between two textures at a 16:9 scale which you may change by editing the the floats, but you may also use the texel size node like before. The render texture is also properly filled out with transparency.

It is best that you maintain the same aspect ratio on all the images you're intending to use in order for the following example to work properly. You will also be required to adjust the render texture's size for this.

SimpleScale.jpeg
SimpleScale.jpeg (207.38 KiB) Viewed 2969 times

ScaleShader.zip
(2.05 KiB) Downloaded 266 times


It will require further calculations in order for the shader to interpret and adjust images with different scales if that is the intended use for it, but this example should point you in the right way!

Please let me know if this was helpful, and if you have any further questions, thanks!
Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Amplify_Borba
 
Posts: 1239
Joined: Mon Jul 24, 2017 9:50 am

Re: Setting pixel color out of (clamped) texture area

Postby Jayme65 » Tue Nov 21, 2017 3:21 pm

Thanks very much for the reply!

Unfortunately, the 2 images won't share the same aspect ratio!
Even without that restriction, the shader you kindly posted doesn't properly display the render texture (there's a 16/9 ratio applied over the 16/9 movie) ;)

Image

I need to blend between images and render textures with both a different aspect ratio!
Jayme65
 
Posts: 6
Joined: Wed Apr 19, 2017 6:18 pm

Re: Setting pixel color out of (clamped) texture area

Postby Amplify_Borba » Tue Nov 21, 2017 4:57 pm

Jayme65 wrote:Thanks very much for the reply!

Even without that restriction, the shader you kindly posted doesn't properly display the render texture (there's a 16/9 ratio applied over the 16/9 movie) ;)


Would you please elaborate on what you mean by having a 16:9 ratio applied over the 16:9 movie? The shader posted above was meant to serve as an example for a texture and a render texture with a resolution on the same scale.

Jayme65 wrote:I need to blend between images and render textures with both a different aspect ratio!


What is the intended use for this shader? Would it be viable to simply alternate between planes/materials? In any case, you should be able to use the previous example and tweak it to perform a specific uv calculation for each different scale you're using.

Looking forward to learning more about your requirements in order to best help you!
Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Amplify_Borba
 
Posts: 1239
Joined: Mon Jul 24, 2017 9:50 am


Return to Amplify Shader Editor

Who is online

Users browsing this forum: Bing [Bot] and 42 guests