当前位置:网站首页>Unity Shader 透视效果/XRay
Unity Shader 透视效果/XRay
2022-08-09 15:00:00 【qq_21315789】
当人物被建筑遮挡时,为了继续显示人物,一种方法是将人物透过建筑显示为一种颜色。基本原理是在一个pass通道中进行深度测试,当发现被遮挡时使用该pass通道显示一个颜色。
Shader "Test"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}//纹理贴图
_Diffuse("Color", Color) = (1,1,1,1)//漫反射
_XRayColor("XRayColor", Color) =(1,1,1,1)//透视显示颜色
_XRayPower("XRayPower", Range(0.0001,3)) = 1//透视显示系数
}
SubShader
{
Tags {"Queue"= "Geometry+1000" "RenderType"="Opaque" }//Geometry+1000是为了在其它不透明物体之后再渲染,"+"不要有空格。
LOD 100
Pass//透视
{
Name "XRay"
Tags{ "ForceNoShadowCasting" = "true" }//为true表示不受其它物体投掷阴影影响
Blend SrcAlpha One//颜色混合模式。当该pass通道返回颜色为0时完全使用原颜色缓冲的颜色
ZWrite Off //关闭深度写入
ZTest Greater //深度测试,选择在深度更大时渲染本pass。并且因为已经关闭了深度写入,通过本深度测试时不会进行深度写入。
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
fixed4 _XRayColor;
float _XRayPower;
struct v2f
{
float4 vertex : SV_POSITION;
float3 viewDir : TEXCOORD0;
float3 normal : TEXCOORD1;
};
v2f vert(appdata_base v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.normal = v.normal;
o.viewDir = ObjSpaceViewDir(v.vertex);
return o;
}
fixed4 frag(v2f i):SV_Target
{
float3 normal = normalize(i.normal);//模型空间法线方向
float3 viewDir = normalize(i.viewDir);//模型空间视野方向
float rim = 1 - dot(normal,viewDir);//使得在边缘时rim接近1,产生边缘颜色更深的一个效果
return fixed4(_XRayColor.rgb * pow(rim, 1/_XRayPower),1);//用_XRayPower或 1/_XRayPower都行
}
ENDCG
}
Pass//普通显示
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Diffuse;
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
fixed3 worldNormal:TEXCOORD1;
float3 worldPos: TEXCOORD2;//世界空间顶点
};
v2f vert (appdata_base v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;//环境光
fixed4 albedo = tex2D(_MainTex, i.uv);//纹理图片颜色值
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));//世界空间光源方向
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
//漫反射
fixed3 diffuse = _LightColor0.rgb * albedo * _Diffuse.rgb ;
return float4( ambient + diffuse,1);
}
ENDCG
}
}
FallBack "Diffuse"
}

边栏推荐
- 时间序列分析
- 类定义中class和className中间的修饰词的作用有关问题
- 【力扣】1995. 统计特殊四元组
- 【力扣】114. 二叉树展开为链表
- Vim实用技巧_1.vim解决问题的方式
- 【工具使用】Modsim32软件使用详解
- Stetman的读paper小记:Deep Learning Backdoor Survey (Shaofeng Li, Shiqing Ma, Minhui Xue)
- Matlab做分布拟合及绘制频率分布直方图
- Face recognition sample code analysis (1) - program parameter analysis
- Vim实用技巧_6.复制和粘贴原理(寄存器)
猜你喜欢
随机推荐
Basic Concepts of Software Security
R-CNN Fast R-CNN Faster R-CNN总结
Stetman的读paper小记:Deep Learning Backdoor Survey (Shaofeng Li, Shiqing Ma, Minhui Xue)
图像质量指标:峰值信噪比PSNR和结构相似性SSIM
蓝桥杯嵌入式第十三届模拟题做题笔记
【剑指 Offer】 37. 序列化二叉树
Face recognition sample code analysis (2) - face recognition analysis
GCC编译过程
多元回归分析
保存数据
配置 vscode 让它变得更好用
【力扣】98. 验证二叉搜索树
【力扣】593. 有效的正方形
【知识分享】知识链路-Modbus通信知识链路
【力扣】617. 合并二叉树
【力扣】516. 最长回文子序列
Vim实用技巧_7.模式匹配和查找
图解转置卷积原理
hugging face tutorial - Chinese translation - fine-tuning a pre-trained model
godot编写一个节点实时属性显示系统









