Search Unity

Is there any way to make the one of two textures always to face camera with unity shader?

Discussion in 'Shaders' started by wechat_os_Qy0yuIXc8nH08PQhu0hMZuRMU, Jan 17, 2022.

  1. wechat_os_Qy0yuIXc8nH08PQhu0hMZuRMU

    wechat_os_Qy0yuIXc8nH08PQhu0hMZuRMU

    Joined:
    Aug 6, 2021
    Posts:
    17
    I'm already made a shader which can mix two textures, but I have no idea how I can change the properties of one of two textures such as making it always face to camera. Is there any way to do title's do?
    Code (CSharp):
    1. Shader "Unlit/testing"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("MainTexture", 2D) = "white" {}
    6.         _SecondTex ("SecondTexture", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque"}
    11.         LOD 100
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             // make fog work
    19.             #pragma multi_compile_fog
    20.  
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uvmain: TEXCOORD0;
    27.                 float2 uvsecond: TEXCOORD1;
    28.             };
    29.  
    30.             struct v2f
    31.             {
    32.                 float2 uvmain : TEXCOORD0;
    33.                 float2 uvsecond : TEXCOORD1;
    34.                 UNITY_FOG_COORDS(1)
    35.                 float4 vertex : POSITION;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.             sampler2D _SecondTex;
    40.             float4 _MainTex_ST;
    41.             float4 _SecondTex_ST;
    42.  
    43.             v2f vert (appdata v)
    44.             {
    45.                 v2f o;
    46.                 o.vertex = UnityObjectToClipPos(v.vertex);
    47.                 o.uvmain = TRANSFORM_TEX(v.uvmain, _MainTex);
    48.                 o.uvsecond = TRANSFORM_TEX(v.uvsecond, _SecondTex);
    49.                 UNITY_TRANSFER_FOG(o,o.vertex);
    50.                 return o;
    51.             }
    52.             fixed4 frag (v2f i) : SV_Target
    53.             {
    54.                 fixed4 colmix = lerp(tex2D(_MainTex, i.uvmain),tex2D(_SecondTex,i.uvsecond),0.5);
    55.                 UNITY_APPLY_FOG(i.fogCoord, col);
    56.                 return colmix;
    57.             }
    58.          
    59.             ENDCG
    60.         }
    61.     }
    62. }
    63.  
     
    Last edited: Jan 17, 2022
  2. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    345
    search for "screen space uvs"
    in the official documenation I could only find an example for surface shaders (https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html) but you are writing frag/vert yourself.

    you can compute screen uvs using o.uvsecond = ComputeScreenPos(v.vertex); in the vertex shader
     
  3. wechat_os_Qy0yuIXc8nH08PQhu0hMZuRMU

    wechat_os_Qy0yuIXc8nH08PQhu0hMZuRMU

    Joined:
    Aug 6, 2021
    Posts:
    17
    Thank you for giving tip, I'm new to coding shader, I found the solution from Screenspace UV - Unity Forum, this help me solve this problem.
     
    Last edited: Jan 20, 2022