首页 > 代码库 > shader中的广告板技术

shader中的广告板技术

Shader "CM/Billboard1"{	Properties	{		_MainTex ("Texture", 2D) = "white" {}	}	SubShader	{		Pass		{			CGPROGRAM			#pragma vertex vert			#pragma fragment frag			#include "UnityCG.cginc"			struct appdata			{				float4 vertex : POSITION;				float2 uv : TEXCOORD0;			};			struct v2f			{				float4 pos : SV_POSITION;				float2 uv : TEXCOORD0;			};			sampler2D _MainTex;			float4 _MainTex_ST;						float4 Billboard(float4 vertex)			{				float4 ori=mul(UNITY_MATRIX_MV, float4(0,0,0,1));				float4 vt = vertex;				float2 r1=float2(_Object2World[0][0],_Object2World[0][2]);				float2 r2=float2(_Object2World[2][0],_Object2World[2][2]);				vt.xy = vt.x*r1 + vt.z*r2;				vt.z = 0;				vt.xyz += ori.xyz;				return mul(UNITY_MATRIX_P, vt);			}						v2f vert (appdata_base v) 			{				v2f o;				o.pos = Billboard(v.vertex);				return o;			}			fixed4 frag(v2f i) : COLOR 			{				return tex2D(_MainTex, i.uv);			}			ENDCG		}	}}

跟着相机转的plane:

技术分享

 

广告板技术之二:   来自wiki ----------- https://en.wikibooks.org/wiki/Cg_Programming/Unity/Billboards            不过这种技术不能缩放广告板

Shader "Cg  shader for billboards" {   Properties {      _MainTex ("Texture Image", 2D) = "white" {}   }   SubShader {      Pass {            CGPROGRAM          #pragma vertex vert           #pragma fragment frag          // User-specified uniforms                     uniform sampler2D _MainTex;                  struct vertexInput {            float4 vertex : POSITION;            float4 tex : TEXCOORD0;         };         struct vertexOutput {            float4 pos : SV_POSITION;            float4 tex : TEXCOORD0;         };          vertexOutput vert(vertexInput input)          {            vertexOutput output;            output.pos = mul(UNITY_MATRIX_P,               mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0))              - float4(input.vertex.x, input.vertex.y, 0.0, 0.0));             output.tex = input.tex;            return output;         }          float4 frag(vertexOutput input) : COLOR         {            return tex2D(_MainTex, float2(input.tex.xy));            }          ENDCG      }   }}

  

广告板技术三 : 草         ---------------------- 待定  

 

shader中的广告板技术