Unity中实现一个类似x光扫描效果
发表于2019-01-23
要在unity开发过程中实现一个类似x光扫描效果很简单,首先获取顶点到摄像机的观察方向,然后与顶点的法线方向进行点积算出投影的长度最后再乘以我们设置的颜色值即为最终要输出的颜色。这里要注意观察方向与法线方向要进行归一化。
实现效果如下:
shader代码:
Shader "Custom/XLight" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} } SubShader { Tags{"IgnoreProjector"="True" "LightMode" = "ForwardBase" "Queue"="Transparent" "RenderType"="Transparent"} Pass { Blend SrcAlpha OneMinusSrcAlpha ZWrite Off CGPROGRAM #include "Lighting.cginc" #pragma vertex vert #pragma fragment frag fixed4 _Color; struct v2f { float4 pos : SV_POSITION; float3 normal : normal; float3 viewDir : TEXCOORD0; }; v2f vert (appdata_base v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.viewDir = ObjSpaceViewDir(v.vertex); o.normal = v.normal; return o; } fixed4 frag(v2f i) : SV_Target { float3 normal = normalize(i.normal); float3 viewDir = normalize(i.viewDir); float rim = 1 - dot(normal, viewDir); return _Color * rim; } ENDCG } } FallBack "Diffuse" }