内置渲染管线
Shader "SunY/C11-Billboard" { Properties { _MainTex("Main Tex", 2D) = "white"{} _Color("Color Tint", Color) = (1, 1, 1, 1) _VerticalBillboarding("Vertical, Restraints", Range(0, 1)) = 1 } SubShader { Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "DisableBatching" = "True" } Pass { Tags { "LightMode" = "ForwardBase" } ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off CGPROGRAM #include "UnityCG.cginc" #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float4 _MainTex_ST; fixed4 _Color; fixed _VerticalBillboarding; struct a2v { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(a2v v) { v2f o; float3 viewer = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1)); // 转换视角方向到模型空间 float3 center = float3(0, 0, 0); // 定义模型空间原点为旋转锚点 float3 normalDir = viewer - center; // 得到法线方向 normalDir.y *= _VerticalBillboarding; // 根据属性值确定Y方向是永远向上还是可变化 normalDir = normalize(normalDir); float3 upDir = abs(normalDir.y) > 0.999 ? float3(0, 0, 1) : float3(0, 1, 0); // 根据法线的Y值确定向上的方向,避免法线和向上方向平行 float3 rightDir = normalize(cross(upDir, normalDir)); // 叉积计算向右方向 upDir = normalize(cross(normalDir, rightDir)); // 叉积计算真正的向上方向 // 根据正交基向量计算及相对锚点的偏移量得到新顶点位置 float3 centerOffs = v.vertex.xyz - center; float3 localPos = center + rightDir * centerOffs.x + upDir * centerOffs.y + normalDir * centerOffs.z; o.pos = UnityObjectToClipPos(float4(localPos, 1)); o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); return o; } fixed4 frag(v2f i) : SV_Target { fixed4 color = tex2D(_MainTex, i.uv); color.rgb *= _Color.rgb; return color; } ENDCG } } FallBack "Transparent/VertexLit" }
URP渲染管线
Shader "SunY/C11-U-Billboard" { Properties { _MainTex("Main Tex", 2D) = "white"{} _Color("Color Tint", Color) = (1, 1, 1, 1) _VerticalBillboarding("Vertical, Restraints", Range(0, 1)) = 1 } SubShader { Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparnet" "DisableBatching" = "True" "RenderPipeline" = "UniversalPipeline" } Pass { Tags { "LightMode" = "UniversalForward" } ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off HLSLPROGRAM #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #pragma vertex vert #pragma fragment frag CBUFFER_START(UnityPerMaterial) float4 _MainTex_ST; half4 _Color; half _VerticalBillboarding; CBUFFER_END TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex); struct a2v { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(a2v v) { v2f o; float3 viewer = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1)); float3 center = float3(0, 0, 0); float3 normalDir = viewer - center; normalDir.y = normalDir.y * _VerticalBillboarding; normalDir = normalize(normalDir); float3 upDir = abs(normalDir.y) > 0.999 ? float3(0, 0, 1) : float3(0, 1, 0); float3 rightDir = normalize(cross(upDir, normalDir)); upDir = normalize(cross(normalDir, rightDir)); float3 centerOffs = v.vertex.xyz - center; float3 localPos = center + rightDir * centerOffs.x + upDir * centerOffs.y + normalDir * centerOffs.z; o.pos = TransformObjectToHClip(float4(localPos, 1)); o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); return o; } half4 frag(v2f i) : SV_Target { half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv); color.rgb *= _Color.rgb; return color; } ENDHLSL } } }
浙公网安备 33010602011771号