Advertisement
YuraSuper2048

Billboarding Shader

Sep 26th, 2022 (edited)
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. Shader "Sprites/Billboard" {
  2.    Properties {
  3.       _MainTex ("Texture Image", 2D) = "white" {}
  4.     _Color ("Main Color", Color) = (1,1,1,1)
  5.       _ScaleX ("Scale X", Float) = 1.0
  6.       _ScaleY ("Scale Y", Float) = 1.0
  7.    }
  8.    SubShader {
  9.       Pass {  
  10.          Tags{"QUEUE" = "AlphaTest" "RenderType" = "TransparentCutout" }
  11.          AlphaToMask On
  12.         ColorMask RGB
  13.          Cull Off
  14.          
  15.          CGPROGRAM
  16.  
  17.          #pragma vertex vert  
  18.          #pragma fragment frag
  19.  
  20.          // User-specified uniforms            
  21.          uniform sampler2D _MainTex;  
  22.          fixed4 _Color;      
  23.          uniform float _ScaleX;
  24.          uniform float _ScaleY;
  25.  
  26.          struct vertexInput {
  27.             float4 vertex : POSITION;
  28.             float2 uv : TEXCOORD0;
  29.             fixed4 color : COLOR;
  30.          };
  31.          struct vertexOutput {
  32.             float2 uv : TEXCOORD0;
  33.             float4 pos : SV_POSITION;
  34.             fixed4 color : COLOR;
  35.          };
  36.  
  37.          vertexOutput vert(vertexInput input)
  38.          {
  39.             vertexOutput o;
  40.            
  41.             o.pos = UnityObjectToClipPos(input.vertex);
  42.             o.uv = input.uv.xy;
  43.  
  44.             // billboard mesh towards camera
  45.             float3 vpos = mul((float3x3)unity_ObjectToWorld, input.vertex.xyz);
  46.             float4 worldCoord = float4(unity_ObjectToWorld._m03, unity_ObjectToWorld._m13, unity_ObjectToWorld._m23, 1);
  47.             float4 viewPos = mul(UNITY_MATRIX_V, worldCoord) + float4(vpos, 0);
  48.             float4 outPos = mul(UNITY_MATRIX_P, viewPos);
  49.  
  50.             o.pos = outPos;
  51.             o.color = input.color;
  52.  
  53.             return o;
  54.          }
  55.  
  56.          float4 frag(vertexOutput input) : COLOR
  57.          {
  58.             // sample the texture
  59.             fixed4 col = tex2D(_MainTex, input.uv);
  60.             return col;
  61.          }
  62.  
  63.          ENDCG
  64.       }
  65.    }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement