在shader 中判断定点与传送平面的位置关系,改变其透明度
问题:所有会被用到的shader都要改
算法:
1,需要 面上一点,和面的法向量
2,(目标点-面上点)点乘 法向量 即 cos夹角
3,d>0 :在法向指向的一侧
d=0: 点在面上
d<0:点在法向的所指方向的另一侧
透明度要做渐变的话可以计算在法向量的投影 ,法向量为单位法向量即可
基础知识
向量a·向量b=| a |*| b |*cosΘ
Θ为两向量夹角
| b |*cosΘ叫做向量b在向量a上的投影
| a |*cosΘ叫做向量a在向量b上的投影
效果

代码
Shader"Cerulean/CanBeTransfer"{Properties{_BaseTex("Base Texture",2D)="white"{}_PlanePoint("Plane Point",Vector)=(0,0,0,0)_PlaneNormal("Plane Normal",Vector)=(1,0,0,0)}SubShader{Tags{"Queue"="Transparent""RenderType"="Transparent"}BlendSrcAlphaOneMinusSrcAlphaLightingOffZTestOnLOD 100Pass{CGPROGRAM#pragma vertex vert#pragma fragment frag#include"UnityCG.cginc"sampler2D _BaseTex;float4_BaseTex_ST;fixed3 _PlanePoint;fixed3 _PlaneNormal;//计算某个点在某平面的法向量上的投影fixed Projection2PlaneNormal(fixed3 planePoint, fixed3 normal, fixed3 pos){fixed3 dis = pos - planePoint;return dot(dis, normal);}struct appdata{float4 vertex : POSITION;float2 uv : TEXCOORD0;};struct v2f{float4 vertex : SV_POSITION;float2 baseuv : TEXCOORD0;float4 global : TEXCOORD1;};v2f vert (appdata v){v2f o;o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);o.baseuv = TRANSFORM_TEX(v.uv,_BaseTex);o.global = mul(unity_ObjectToWorld, v.vertex);return o;}fixed4 frag (v2f i): SV_Target{fixed3 baseCol = tex2D(_BaseTex, i.baseuv);fixed alpha = saturate(Projection2PlaneNormal(_PlanePoint,_PlaneNormal, i.global));return fixed4(baseCol, alpha);}ENDCG}}}
传送门脚本
//找到要被传送的物体设置shader的变量material.SetVector("_PlanePoint", transform.position);material.SetVector("_PlaneNormal", transform.forward);