当前位置:网站首页>【Unity Shader】插入Pass实现模型遮挡X光透视效果
【Unity Shader】插入Pass实现模型遮挡X光透视效果
2022-07-07 16:31:00 【InfoQ】
实现效果:

分Pass渲染:
- 透视效果Pass,在渲染的时候使用ZTest Greater,首先渲染的是前面遮挡的Cube,此时深度缓存中有了Cube的深度值。然后再渲染模型,模型未遮挡的部分深度测试不通过,不会渲染,被遮挡的部分深度测试通过,正常显示。
- 正常渲染Pass,首先渲染的是前面遮挡的Cube,此时深度缓存中有了Cube的深度值。然后再渲染模型,模型被遮挡的部分深度测试失败,不会渲染,未被遮挡的部分正常显示。
- ZTest Less:深度小于当前缓存则通过
- ZTest Greater:深度大于当前缓存则通过
- ZTest LEqual:深度小于等于当前缓存则通过
- ZTest GEqual:深度大于等于当前缓存则通过
- ZTest Equal:深度等于当前缓存则通过
- ZTest NotEqual:深度不等于当前缓存则通过
- ZTest Always:不论如何都通过
Shader代码:
//原Pass不变,插入一个Pass,用于显示模型遮挡部分
Shader "Test/Model_XRay"
{
Properties
{
_MainTex ("Diffuse (RGB)", 2D) = "grey" {}
_Color("Color (RGBA)", Color) = (1,1,1,1)
_XRayColor("XRay Color", Color) = (1,1,1,1)
}
SubShader
{
Tags
{
"RenderType"="Opaque"
"Queue"="AlphaTest+1"
"IgnoreProjector"="True"
}
LOD 200
Fog { Mode Off }
//渲染X光透视效果的Pass
Pass
{
Blend SrcAlpha One
ZWrite Off
ZTest Greater
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _XRayColor;
struct v2f
{
float4 pos : SV_POSITION;
float3 normal : normal;
float3 viewDir : TEXCOORD0;
};
v2f vert (appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(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 _XRayColor * rim;
}
ENDCG
}
Pass
{
CGPROGRAM
//定义顶点/片元着色器代码
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
//定义顶点着色器的输入
struct a2v
{
float4 vertex : POSITION;
float2 uv :TEXCOORD0;
};
//定义顶点着色器的输出、片元着色器的输入
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv:TEXCOORD0;
};
v2f vert (a2v v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return tex2D(_MainTex,i.uv);
}
ENDCG
}
}
FallBack "Diffuse"
}
边栏推荐
- 简单几步教你如何看k线图图解
- Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
- 3分钟学会制作动态折线图!
- Chapter 3 business function development (user access project)
- Deep learning machine learning various data sets summary address
- Some key points in the analysis of spot Silver
- 海量数据去重的hash,bitmap与布隆过滤器Bloom Filter
- Tips for this week 131: special member functions and ` = Default`
- Hutool - 轻量级 DB 操作解决方案
- 三仙归洞js小游戏源码
猜你喜欢
Introduction to OTA technology of Internet of things
数学分析_笔记_第11章:Fourier级数
Win11C盘满了怎么清理?Win11清理C盘的方法
zdog. JS rocket turn animation JS special effects
五种网络IO模型
Classification of regression tests
Do you really understand sticky bag and half bag? 3 minutes to understand it
The report of the state of world food security and nutrition was released: the number of hungry people in the world increased to 828million in 2021
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
Chapter 3 business function development (user access project)
随机推荐
Ten thousand words nanny level long article -- offline installation guide for datahub of LinkedIn metadata management platform
[paddleseg source code reading] add boundary IOU calculation in paddleseg validation (1) -- val.py file details tips
Tips of the week 136: unordered containers
保证接口数据安全的10种方案
Chapter 2 build CRM project development environment (database design)
Use onedns to perfectly solve the optimization problem of office network
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
你真的理解粘包与半包吗?3分钟搞懂它
[trusted computing] Lesson 13: TPM extended authorization and key management
go语言的字符串类型、常量类型和容器类型
开发一个小程序商城需要多少钱?
Tips of this week 141: pay attention to implicit conversion to bool
Using stored procedures, timers, triggers to solve data analysis problems
Youth experience and career development
[principle and technology of network attack and Defense] Chapter 1: Introduction
【C语言】字符串函数
socket編程之常用api介紹與socket、select、poll、epoll高並發服務器模型代碼實現
Introduction de l'API commune de programmation de socket et mise en œuvre de socket, select, Poll et epoll
不能忽略的现货白银短线操作小技巧
[OKR target management] value analysis