Hi, I try to create a fixed-size(ignore view distance) billboard shader for quad. Does anyone know how to achieve it? Here is my billboard shader code.
Code (CSharp): Shader "Unlit/Constant Scale Quad" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; sampler2D _MainTex; v2f vert (appdata_base v) { v2f o; // extract world pivot position from object to world transform matrix float3 worldPos = unity_ObjectToWorld._m03_m13_m23; // extract x and y scale from object to world transform matrix float2 scale = float2( length(unity_ObjectToWorld._m00_m10_m20), length(unity_ObjectToWorld._m01_m11_m21) ); // transform pivot position into view space float4 viewPos = mul(UNITY_MATRIX_V, float4(worldPos, 1.0)); // apply transform scale to xy vertex positions float2 vertex = v.vertex.xy * scale; // multiply by view depth for constant view size scaling vertex *= -viewPos.z; // divide by perspective projection matrix [1][1] if you don't want camera FOV to displayed size // the * 0.5 is to make a default quad with a scale of 1 be exactly the height of the view // vertex /= UNITY_MATRIX_P._m11 * 0.5; // along with the perspective projection divide by screen height if you want the scale to be in screen pixels // vertex /= _ScreenParams.y; // add vertex positions to view position pivot viewPos.xy += vertex; // transform into clip space o.pos = mul(UNITY_MATRIX_P, viewPos); o.uv = v.texcoord; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); return col; } ENDCG } } }
Yes! World canvas is one of solutions. But I want to achieve it by shader in this case. Thank you for the prompt reply.
Please explain why do you want to achieve this by a shader, but not a sprite in specific world pos (projected to screen)? I'm curious of the use case. If it's of fixed-size then it's UI element, as I undestand.
If you want something to interact with the world, like be occluded by objects or otherwise be sort with transparencies, or if you're doing VR, UI elements aren't that useful. Canvases can come with some performance overhead that's unnecessary.
In HDRP 10.x ,Unity 2020.2f , I use this shader to 3D TextMesh or SpriteRenderer .It did work as billboard and kepp fixed-size,but x/y uv looks like reversed.
I search this shader for three days, so excited to find this thread after changing tons of search keywords....