Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is it posible to add X/Y offset based on screenspace tot his shader?

Discussion in 'Shaders' started by Rusoski, Jan 20, 2020.

  1. Rusoski

    Rusoski

    Joined:
    Nov 6, 2013
    Posts:
    63
    Good, day, I got this modified shader from official Unity site, this renders a billboard (material must be applied to a cube), the difference is that this shader supports transparency, I added a "Width Scale" and "Height Scale" parameter which work fine, and I have also added a "X Offset" and a "Y Offset" parameter to the shader with the intention of shifting the texture to left/right and up/down, but currently it is based on world space, and the result is not the expected, I would like the offsets to be based on camera or screen space, so when rotating the camera around the object, the result would be displayed correctly, but I am kind of stuck, here is the code:

    Code (CSharp):
    1. Shader "Custom/Alpha Billboard"
    2. {
    3.     Properties
    4.     {
    5.         _TintColor("Tint Color", Color) = (0.5,0.5,0.5,0.5)
    6.         _WidthScale("Width Scale", Range(0.01,1.0)) = 1.0
    7.         _HeightScale("Height Scale", Range(0.01,1.0)) = 1.0
    8.         _XOff("X Offset", Range(-1.0,1.0)) = 0.0
    9.         _YOff("Y Offset", Range(-1.0,1.0)) = 0.0
    10.         _MainTex("Particle Texture", 2D) = "white" {}
    11.         _Scale("Scale", float) = 1.0
    12.     }
    13.  
    14.         Category
    15.         {
    16.             Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
    17.             Blend SrcAlpha OneMinusSrcAlpha
    18.             AlphaTest Greater .01
    19.             Cull Off
    20.  
    21.             // important
    22.             ZWrite off
    23.  
    24.                         SubShader
    25.                         {
    26.                             Pass
    27.                             {
    28.                                 CGPROGRAM
    29.                                 #pragma vertex vert
    30.                                 #pragma fragment frag
    31.  
    32.                                 #include "UnityCG.cginc"
    33.  
    34.                                 sampler2D _MainTex;
    35.                                 fixed4 _TintColor;
    36.                                 float _Scale;
    37.                                 float _WidthScale;
    38.                                 float _HeightScale;
    39.                                 float _XOff;
    40.                                 float _YOff;
    41.  
    42.                                 struct appdata_t
    43.                                 {
    44.                                     float4 vertex : POSITION;
    45.                                     fixed4 color : COLOR;
    46.                                     float2 texcoord : TEXCOORD0;
    47.                                 };
    48.  
    49.                                 struct v2f
    50.                                 {
    51.                                     float4 vertex : POSITION;
    52.                                     fixed4 color : COLOR;
    53.                                     float2 texcoord : TEXCOORD0;
    54.                                 };
    55.  
    56.                                 v2f vert(appdata_t v)
    57.                                 {
    58.                                     v2f o;
    59.                                     o.vertex = mul(UNITY_MATRIX_P,
    60.                                          mul(UNITY_MATRIX_MV, float4(0.0, _XOff, _YOff, 1.0))
    61.                                         + float4(v.vertex.x * _HeightScale, v.vertex.y *_WidthScale, 0.0, 0.0) * _Scale);
    62.  
    63.                                     o.color = v.color;
    64.                                     o.texcoord = float2(v.vertex.x + 0.5, v.vertex.y + 0.5);
    65.                                     return o;
    66.                                 }
    67.  
    68.                                 fixed4 frag(v2f i) : COLOR
    69.                                 {
    70.                                     return 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord);
    71.                                 }
    72.                                 ENDCG
    73.                             }
    74.                         }
    75.         }
    76. }
     
  2. Rusoski

    Rusoski

    Joined:
    Nov 6, 2013
    Posts:
    63
    The shader is now working a expected:


    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Custom/Built in/Particles/Alpha Blended (3000)" {
    4. Properties {
    5.     _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
    6.     _MainTex ("Particle Texture", 2D) = "white" {}
    7.     _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
    8. }
    9.  
    10. Category {
    11.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
    12.     Blend SrcAlpha OneMinusSrcAlpha
    13.     ColorMask RGB
    14.     Cull Off Lighting Off ZWrite Off
    15.  
    16.     SubShader {
    17.         Pass {
    18.  
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #pragma target 2.0
    23.             #pragma multi_compile_particles
    24.             #pragma multi_compile_fog
    25.  
    26.             #include "UnityCG.cginc"
    27.  
    28.             sampler2D _MainTex;
    29.             fixed4 _TintColor;
    30.  
    31.             struct appdata_t {
    32.                 float4 vertex : POSITION;
    33.                 fixed4 color : COLOR;
    34.                 float2 texcoord : TEXCOORD0;
    35.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    36.             };
    37.  
    38.             struct v2f {
    39.                 float4 vertex : SV_POSITION;
    40.                 fixed4 color : COLOR;
    41.                 float2 texcoord : TEXCOORD0;
    42.                 UNITY_FOG_COORDS(1)
    43.                 #ifdef SOFTPARTICLES_ON
    44.                 float4 projPos : TEXCOORD2;
    45.                 #endif
    46.                 UNITY_VERTEX_OUTPUT_STEREO
    47.             };
    48.  
    49.             float4 _MainTex_ST;
    50.  
    51.             v2f vert (appdata_t v)
    52.             {
    53.                 v2f o;
    54.                 UNITY_SETUP_INSTANCE_ID(v);
    55.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    56.                 o.vertex = UnityObjectToClipPos(v.vertex);
    57.                 #ifdef SOFTPARTICLES_ON
    58.                 o.projPos = ComputeScreenPos (o.vertex);
    59.                 COMPUTE_EYEDEPTH(o.projPos.z);
    60.                 #endif
    61.                 o.color = v.color * _TintColor;
    62.                 o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    63.                 UNITY_TRANSFER_FOG(o,o.vertex);
    64.                 return o;
    65.             }
    66.  
    67.             UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
    68.             float _InvFade;
    69.  
    70.             fixed4 frag (v2f i) : SV_Target
    71.             {
    72.                 #ifdef SOFTPARTICLES_ON
    73.                 float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
    74.                 float partZ = i.projPos.z;
    75.                 float fade = saturate (_InvFade * (sceneZ-partZ));
    76.                 i.color.a *= fade;
    77.                 #endif
    78.  
    79.                 fixed4 col = 2.0f * i.color * tex2D(_MainTex, i.texcoord);
    80.                 col.a = saturate(col.a); // alpha should not have double-brightness applied to it, but we can't fix that legacy behaior without breaking everyone's effects, so instead clamp the output to get sensible HDR behavior (case 967476)
    81.  
    82.                 UNITY_APPLY_FOG(i.fogCoord, col);
    83.                 return col;
    84.             }
    85.             ENDCG
    86.         }
    87.     }
    88. }
    89. }
    90.