Search Unity

Crop an UI Image with a material

Discussion in 'Editor & General Support' started by LabMatti, May 21, 2018.

  1. LabMatti

    LabMatti

    Joined:
    Jan 19, 2018
    Posts:
    58
    Hi all!

    I've a plane with a webcam stream on it (WebCamTexture). I apply a chromaKey shader to it and I copy this material to an image in my UI.

    Now I would want to crop this image and take only a portion of it. I tried to mask the image but if the image has a material as in my case it doesn't work.

    What can I do to crop the image?

    This is the shader:

    Code (CSharp):
    1.  
    2. Shader "Unlit/Transparent Chroma" {
    3.          Properties {
    4.                 _MainTex ("Base (RGB)", 2D) = "white" {}
    5.                 _MaskCol ("Mask Color", Color)  = (1.0, 0.0, 0.0, 1.0)
    6.                 _Sensitivity ("Threshold Sensitivity", Range(0,1)) = 0.5
    7.                 _Smooth ("Smoothing", Range(0,1)) = 0.1
    8.         }
    9.         SubShader {
    10.                 Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    11.                 LOD 100
    12.                 ZTest Always Cull Back ZWrite On Lighting Off Fog { Mode off }
    13.                 CGPROGRAM
    14.                 #pragma surface surf Lambert alpha
    15.  
    16.                  struct Input {
    17.                     float2 uv_MainTex;
    18.                 };
    19.  
    20.                 sampler2D _MainTex;
    21.                 float4 _MaskCol;
    22.                 float _Sensitivity;
    23.                  float _Smooth;
    24.  
    25.                 void surf (Input IN, inout SurfaceOutput o) {
    26.                         half4 c = tex2D (_MainTex, IN.uv_MainTex);
    27.  
    28.                         float maskY = 0.2989 * _MaskCol.r + 0.5866 * _MaskCol.g + 0.1145 * _MaskCol.b;
    29.                         float maskCr = 0.7132 * (_MaskCol.r - maskY);
    30.                          float maskCb = 0.5647 * (_MaskCol.b - maskY);
    31.  
    32.                         float Y = 0.2989 * c.r + 0.5866 * c.g + 0.1145 * c.b;
    33.                          float Cr = 0.7132 * (c.r - Y);
    34.                          float Cb = 0.5647 * (c.b - Y);
    35.  
    36.                          float blendValue = smoothstep(_Sensitivity, _Sensitivity + _Smooth, distance(float2(Cr, Cb), float2(maskCr, maskCb)));
    37.                         o.Alpha = 1.0 * blendValue;
    38.                         o.Emission = c.rgb * blendValue;              
    39.                 }
    40.                 ENDCG
    41.         }
    42.         FallBack "Diffuse"  
    43. }
    This is the script that put the material to the image.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class streamCam : MonoBehaviour {
    7.     public WebCamDevice[] devices;
    8.     public int index;
    9.     public RawImage rawImage;
    10.     public Image image;
    11.     public Rect rect;
    12.     public Texture2D text2D;
    13.     public Texture outText;
    14.     public RenderTexture inText;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         devices = WebCamTexture.devices;
    19.         WebCamTexture webcamTexture = new WebCamTexture ();
    20.         int i = 0;
    21.         foreach(WebCamDevice cam in devices){
    22.             print(cam.name+" "+i);
    23.             i++;
    24.         }
    25.  
    26.         Renderer renderer = GetComponent<Renderer>();
    27.         webcamTexture.deviceName = devices[index].name;
    28.  
    29.         renderer.material.mainTexture = webcamTexture;
    30.         rawImage.material = renderer.material;
    31.         image.material = renderer.material;
    32.         webcamTexture.Play();
    33.         //renderer.material.SetColor("_MaskCol",new Color(0f,0.69f,.25f));
    34.  
    35.  
    36.  
    37.     }
    38.    
    39.     // Update is called once per frame
    40.     void Update () {
    41.         rect = new Rect (Vector2.zero,new Vector2(100f,100f));
    42.         Renderer renderer = GetComponent<Renderer>();
    43.         /*rawImage.material = renderer.material;
    44.         rawImage.texture = renderer.material.mainTexture;*/
    45.         image.material = renderer.material;
    46.         text2D = renderer.material.mainTexture as Texture2D;
    47.         image.sprite = Sprite.Create (text2D, rect, new Vector2(0f,0f));      
    48.     }
    49. }
    50.