本文是视频课程《Unity技术美术TA:Shader篇》,算是对自己学习的总结,也希望分享下所学知识~~
有啥用
可以做屏幕效果,如:热扭曲等
如何开启?
1.设置面板
Opaque Downsamping 选项
None:原封不动
2xBilinear:小2倍
4xBox:小4倍(降采样模式不一样)
4xBilinear:小4倍(降采样模式不一样)
2.FrameDebug 能看见 DrawProcedural
(对应Buildin管线就是 自己定义Grab )
不透明的物体不会被渲染到这里(人家都叫Opaque了。。)
如何在 Shader 里面获取
Shader实现如下:
Shader "Custom/TA_URP/GrabViewer"
{
Properties {}
SubShader
{
Tags
{
"RenderPipeline"="UniversalPipeline"
"RenderType"="Transparent"
//自己要变成不透明物体,否则会把自己抓进去
"Queue"="Transparent"
}
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
//简写方式(之前的深度图也有类似的)
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"
//相当于 引入了 DeclareOpaqueTexture 文件
// #define REQUIRE_OPAQUE_TEXTURE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"
struct Attributes
{
float3 positionOS : POSITION;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
};
Varyings vert(Attributes v)
{
Varyings o;
o.positionCS = TransformObjectToHClip(v.positionOS);
return o;
}
half4 frag(Varyings i) : SV_Target
{
half2 screenUV = i.positionCS.xy / _ScreenParams.xy;
//简写 效果和下面一样
float3 opaqueTex = SampleSceneColor(screenUV);
// half4 opaqueTex = SAMPLE_DEPTH_TEXTURE(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, screenUV);
return half4(opaqueTex, 1);
}
ENDHLSL
}
}
FallBack "Hidden/Shader Graph/FallbackError"
}
DeclareOpaqueTexture.hlsl 代码如下:
#ifndef UNITY_DECLARE_OPAQUE_TEXTURE_INCLUDED
#define UNITY_DECLARE_OPAQUE_TEXTURE_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D_X(_CameraOpaqueTexture);
SAMPLER(sampler_CameraOpaqueTexture);
float3 SampleSceneColor(float2 uv)
{
return SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, UnityStereoTransformScreenSpaceTex(uv)).rgb;
}
float3 LoadSceneColor(uint2 uv)
{
return LOAD_TEXTURE2D_X(_CameraOpaqueTexture, uv).rgb;
}
#endif
效果如图: