赞
踩
前些时间接到一个需求,要求摄像相机位置来判断,当前是不是需要透明化,需要从半透到最后最后透明,在崩坏3的角色界面里面有这样的效果,当你镜头推进的时候, 头发可以又半透到透明状态变化。
关键点在、在于用镜头位置与物体所在世界位置坐标 做比较
- shader "Scene/Alpha Blend " {
- Properties {
- _Color ("Color Tint", Color) = (1, 1, 1, 1)
- _MainTex ("Main Tex", 2D) = "white" {}
- _AlphaScale ("Alpha Scale", Range(0, 1)) = 1
- _Cutoff ("Alpha Cutoff", Range(0, 1)) = 0.5
- }
- SubShader {
- Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}//"RenderType"="TransparentCutout"
- //第一个Pass,关闭深度写入,处理AlphaTest部分
- Pass {
- Tags { "LightMode"="ForwardBase" }
- Cull off
- ZWrite Off
- //ZTest on
- Blend SrcAlpha OneMinusSrcAlpha
-
- CGPROGRAM
-
- #pragma multi_compile_fwdbase
-
- #pragma vertex vert
- #pragma fragment frag
-
- #include "Lighting.cginc"
- #include "AutoLight.cginc"
-
- fixed4 _Color;
- sampler2D _MainTex;
- float4 _MainTex_ST;
- fixed _AlphaScale;
- float _Cutoff;
-
- struct a2v {
- float4 vertex : POSITION;
- float3 normal : NORMAL;
- float4 texcoord : TEXCOORD0;
- };
-
- struct v2f {
- float4 vertex : SV_POSITION;
- float3 worldNormal : TEXCOORD0;
- float3 worldPos : TEXCOORD1;
- float2 uv : TEXCOORD2;
- float lengthInCamera : TEXCOORD3;
- SHADOW_COORDS(4)
- };
-
- v2f vert(a2v v) {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
-
- o.worldNormal = UnityObjectToWorldNormal(v.normal);
-
-
-
- float4 wpos = mul(unity_ObjectToWorld, v.vertex);
-
- o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
-
- o.lengthInCamera = length(_WorldSpaceCameraPos - wpos.xyz);
-
-
- // Pass shadow coordinates to pixel shader
- TRANSFER_SHADOW(o);
-
- return o;
- }
-
- fixed4 frag(v2f i) : SV_Target {
- fixed3 worldNormal = normalize(i.worldNormal);
- fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
-
- fixed4 texColor = tex2D(_MainTex, i.uv);
-
- clip (texColor.a - _Cutoff);
- fixed3 albedo = texColor.rgb * _Color.rgb;
-
- fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
-
- fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(worldNormal, worldLightDir));
-
-
- // UNITY_LIGHT_ATTENUATION not only compute attenuation, but also shadow infos
- UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
-
- float Start =5;//设定开始值
- float End = 3;//设定结束值
-
- texColor.a = saturate((Start-i.lengthInCamera)/(End-Start)) * 0.5 ;
-
-
- return fixed4(ambient + diffuse * atten, texColor.a * _AlphaScale);
-
- }
-
- ENDCG
- }
-
- }
- }
下面是当前效果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。