推荐坪地网站建设,c2c有哪些电商平台,酒店网站建设方案策划,宁波城乡建设网站文章目录 前言一、抓屏开启1、Unity下开启抓屏2、Shader中开启抓屏 二、抓屏使用1、设置为半透明渲染队列#xff0c;关闭深度写入2、申明纹理和采样器3、在片元着色器使用请添加图片描述 三、测试代码 前言
我们在这篇文章中看一下#xff0c;URP下怎么开启抓屏。 一、抓屏… 文章目录 前言一、抓屏开启1、Unity下开启抓屏2、Shader中开启抓屏 二、抓屏使用1、设置为半透明渲染队列关闭深度写入2、申明纹理和采样器3、在片元着色器使用请添加图片描述 三、测试代码 前言
我们在这篇文章中看一下URP下怎么开启抓屏。 一、抓屏开启
1、Unity下开启抓屏
在URP下打开下面这个选项 Frame Debugger下可以看见 CopyColor 就是我们上一帧的内容 Opaque Downsampling改变截取的帧精度
2、Shader中开启抓屏
使用不透明渲染队列才可以使用深度图Render Queue 2500 时才可以使用深度图 Tags{“Queue”“Geometry}” 开启Zwrite Zwrite On 二、抓屏使用
我们这里创建一个面片来测试一下
1、设置为半透明渲染队列关闭深度写入
因为要用面片显示抓取的内容。所以我们得让抓屏不把该面片抓进去需要抓取的其他物体开启抓屏。 Tags{“Queue”“Transparent”} ZWrite Off 2、申明纹理和采样器 TEXTURE2D(_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture); 也可以直接使用hlsl内部定义 #define REQUIRE_OPAQUE_TEXTURE 3、在片元着色器使用
使用模型uv采样 float4 opaqueMap SAMPLE_TEXTURE2D(_CameraOpaqueTexture,sampler_CameraOpaqueTexture,i.uv); return opaqueMap; 使用屏幕uv采样为了使其效果明显我们给输出结果加上0.3) float2 uv i.positionCS.xy/ _ScreenParams.xy; float4 opaqueMap SAMPLE_TEXTURE2D(_CameraOpaqueTexture,sampler_CameraOpaqueTexture,uv); return opaqueMap0.3; 三、测试代码
Shader MyShader/URP/P4_2
{Properties {_Color(Color,Color) (0,0,0,0)_MainTex(MainTex,2D) white{}}SubShader{Tags{//告诉引擎该Shader只用于 URP 渲染管线RenderPipelineUniversalPipeline//渲染类型RenderTypeTransparent//渲染队列QueueTransparent}//Blend One OneZWrite OffPass{Name UnlitHLSLPROGRAM#pragma vertex vert#pragma fragment frag// Pragmas#pragma target 2.0// Includes#include Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl#include Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl#include Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlslCBUFFER_START(UnityPerMaterial)half4 _Color;CBUFFER_END//纹理的定义如果是编译到GLES2.0平台则相当于sample2D _MainTex;否则相当于 Texture2D _MainTex;TEXTURE2D(_MainTex);SAMPLER(SamplerState_linear_mirrorU_ClampV); float4 _MainTex_ST;TEXTURE2D(_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture);TEXTURE2D(_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture);//struct appdata//顶点着色器的输入struct Attributes{float3 positionOS : POSITION;float2 uv : TEXCOORD0;};//struct v2f//片元着色器的输入struct Varyings{float4 positionCS : SV_POSITION;float2 uv : TEXCOORD0;float4 screenPos : TEXCOORD1;};//v2f vert(Attributes v)//顶点着色器Varyings vert(Attributes v){Varyings o (Varyings)0;float3 positionWS TransformObjectToWorld(v.positionOS);o.positionCS TransformWorldToHClip(positionWS);o.uv TRANSFORM_TEX(v.uv,_MainTex);o.screenPos ComputeScreenPos(o.positionCS);return o;}//fixed4 frag(v2f i) : SV_TARGET//片元着色器half4 frag(Varyings i) : SV_TARGET{half4 c;float4 mainTex SAMPLE_TEXTURE2D(_MainTex,SamplerState_linear_mirrorU_ClampV,i.uv);//c _Color * mainTex;//深度图//float2 uv i.screenPos.xy / i.screenPos.w;//float2 uv i.positionCS.xy/ _ScreenParams.xy;//float4 cameraDepthTex SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,uv);//float depthTex Linear01Depth(cameraDepthTex,_ZBufferParams);//抓屏float2 uv i.positionCS.xy/ _ScreenParams.xy;float4 opaqueMap SAMPLE_TEXTURE2D(_CameraOpaqueTexture,sampler_CameraOpaqueTexture,uv);return opaqueMap;}ENDHLSL}}SubShader{Tags{//渲染类型RenderTypeTransparent//渲染队列QueueTransparent}//Blend One OneZWrite OffPass{CGPROGRAM#pragma vertex vert#pragma fragment frag// Pragmas#pragma target 2.0// Includes#include UnityCG.cginchalf4 _Color;sampler2D _MainTex;float4 _MainTex_ST;sampler2D _CameraDepthTexture;struct appdata{float3 positionOS : POSITION;float2 uv : TEXCOORD0;};struct v2f{float4 positionCS : SV_POSITION;float2 uv : TEXCOORD0;float4 screenPos : TEXCOORD1;};v2f vert(appdata v){v2f o;o.positionCS UnityObjectToClipPos(v.positionOS);o.uv TRANSFORM_TEX(v.uv,_MainTex);o.screenPos ComputeScreenPos(o.positionCS);return o;}//fixed4 frag(v2f i) : SV_TARGET//片元着色器half4 frag(v2f i) : SV_TARGET{half4 c;float4 mainTex tex2D(_MainTex,i.uv);//c _Color * mainTex;//深度图//float2 uv i.screenPos.xy / i.screenPos.w;float2 uv i.positionCS/ _ScreenParams.xy;float4 cameraDepthTex tex2D(_CameraDepthTexture,uv);float depthTex Linear01Depth(cameraDepthTex);return depthTex;}ENDCG}}
}