C for Graphic:遮罩显示(一)

2024-05-14 18:36
文章标签 显示 遮罩 graphic

本文主要是介绍C for Graphic:遮罩显示(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

      模板缓冲一般用于遮罩渲染的功能,其原理很以前聊过(模板缓冲原理),就不再啰嗦了。
      现在实现一个功能:使用一个长方体(或任意物体)遮罩渲染对象(比如一个球)。
      首先在官网下载内置着色器,找到DefaultResourcesExtra中的Standard.shader,改造一下支持stencil操作,如下:

Shader "Standard-Stencil"
{Properties{_StencilVal("Stencil Value", int) = 1_StencilComp("Stencil CompFunction (Disable,0,Never,1,Less,2,Equal,3,LessEqual,4,Greater,5,NotEqual,6,GreaterEqual,7,Always,8)", int) = 3_StencilPass("StencilPassFunciton(Keep,0,Zero,1,Replace,2,IncrSat,3,DecrSat,4,Invert,5,IncrWrap,6,DecrWrap,7)", int) = 0[Enum(Less,0,Greater,1,LEqual,2,GEqual,3,Equal,4,NotEqual,5,Always,6)]_ZTest("ZTest",int) = 0[Enum(Off,0,Front,1,Back,2)]_Cull("Cull", int) = 2_Color("Color", Color) = (1,1,1,1)_MainTex("Albedo", 2D) = "white" {}_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5_Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5_GlossMapScale("Smoothness Scale", Range(0.0, 1.0)) = 1.0[Enum(Metallic Alpha,0,Albedo Alpha,1)] _SmoothnessTextureChannel ("Smoothness texture channel", Float) = 0[Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0_MetallicGlossMap("Metallic", 2D) = "white" {}[ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0[ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0_BumpScale("Scale", Float) = 1.0_BumpMap("Normal Map", 2D) = "bump" {}_Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02_ParallaxMap ("Height Map", 2D) = "black" {}_OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0_OcclusionMap("Occlusion", 2D) = "white" {}_EmissionColor("Color", Color) = (0,0,0)_EmissionMap("Emission", 2D) = "white" {}_DetailMask("Detail Mask", 2D) = "white" {}_DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}_DetailNormalMapScale("Scale", Float) = 1.0_DetailNormalMap("Normal Map", 2D) = "bump" {}[Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0// Blending state[HideInInspector] _Mode ("__mode", Float) = 0.0[HideInInspector] _SrcBlend ("__src", Float) = 1.0[HideInInspector] _DstBlend ("__dst", Float) = 0.0[HideInInspector] _ZWrite ("__zw", Float) = 1.0}CGINCLUDE#define UNITY_SETUP_BRDF_INPUT MetallicSetupENDCGSubShader{Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }LOD 300// ------------------------------------------------------------------//  Base forward pass (directional light, emission, lightmaps, ...)Pass{Name "FORWARD"Tags { "LightMode" = "ForwardBase" }Blend [_SrcBlend] [_DstBlend]ZWrite [_ZWrite]ZWrite [_ZTest]Stencil{Ref [_StencilVal]Comp [_StencilComp]Pass [_StencilPass]}Cull [_Cull]CGPROGRAM#pragma target 3.0// -------------------------------------#pragma shader_feature _NORMALMAP#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON#pragma shader_feature _EMISSION#pragma shader_feature _METALLICGLOSSMAP#pragma shader_feature ___ _DETAIL_MULX2#pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A#pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF#pragma shader_feature _ _GLOSSYREFLECTIONS_OFF#pragma shader_feature _PARALLAXMAP#pragma multi_compile_fwdbase#pragma multi_compile_fog#pragma multi_compile_instancing// Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.//#pragma multi_compile _ LOD_FADE_CROSSFADE#pragma vertex vertBase#pragma fragment fragBase#include "UnityStandardCoreForward.cginc"ENDCG}// ------------------------------------------------------------------//  Additive forward pass (one light per pass)Pass{Name "FORWARD_DELTA"Tags { "LightMode" = "ForwardAdd" }Blend [_SrcBlend] OneFog { Color (0,0,0,0) } // in additive pass fog should be blackZWrite OffZTest LEqualCGPROGRAM#pragma target 3.0// -------------------------------------#pragma shader_feature _NORMALMAP#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON#pragma shader_feature _METALLICGLOSSMAP#pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A#pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF#pragma shader_feature ___ _DETAIL_MULX2#pragma shader_feature _PARALLAXMAP#pragma multi_compile_fwdadd_fullshadows#pragma multi_compile_fog// Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.//#pragma multi_compile _ LOD_FADE_CROSSFADE#pragma vertex vertAdd#pragma fragment fragAdd#include "UnityStandardCoreForward.cginc"ENDCG}// ------------------------------------------------------------------//  Shadow rendering passPass {Name "ShadowCaster"Tags { "LightMode" = "ShadowCaster" }ZWrite On ZTest LEqualCGPROGRAM#pragma target 3.0// -------------------------------------#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON#pragma shader_feature _METALLICGLOSSMAP#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A#pragma shader_feature _PARALLAXMAP#pragma multi_compile_shadowcaster#pragma multi_compile_instancing// Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.//#pragma multi_compile _ LOD_FADE_CROSSFADE#pragma vertex vertShadowCaster#pragma fragment fragShadowCaster#include "UnityStandardShadow.cginc"ENDCG}// ------------------------------------------------------------------//  Deferred passPass{Name "DEFERRED"Tags { "LightMode" = "Deferred" }CGPROGRAM#pragma target 3.0#pragma exclude_renderers nomrt// -------------------------------------#pragma shader_feature _NORMALMAP#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON#pragma shader_feature _EMISSION#pragma shader_feature _METALLICGLOSSMAP#pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A#pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF#pragma shader_feature ___ _DETAIL_MULX2#pragma shader_feature _PARALLAXMAP#pragma multi_compile_prepassfinal#pragma multi_compile_instancing// Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.//#pragma multi_compile _ LOD_FADE_CROSSFADE#pragma vertex vertDeferred#pragma fragment fragDeferred#include "UnityStandardCore.cginc"ENDCG}// ------------------------------------------------------------------// Extracts information for lightmapping, GI (emission, albedo, ...)// This pass it not used during regular rendering.Pass{Name "META"Tags { "LightMode"="Meta" }Cull OffCGPROGRAM#pragma vertex vert_meta#pragma fragment frag_meta#pragma shader_feature _EMISSION#pragma shader_feature _METALLICGLOSSMAP#pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A#pragma shader_feature ___ _DETAIL_MULX2#pragma shader_feature EDITOR_VISUALIZATION#include "UnityStandardMeta.cginc"ENDCG}}SubShader{Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }LOD 150// ------------------------------------------------------------------//  Base forward pass (directional light, emission, lightmaps, ...)Pass{Name "FORWARD"Tags { "LightMode" = "ForwardBase" }Blend [_SrcBlend] [_DstBlend]ZWrite [_ZWrite]CGPROGRAM#pragma target 2.0#pragma shader_feature _NORMALMAP#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON#pragma shader_feature _EMISSION#pragma shader_feature _METALLICGLOSSMAP#pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A#pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF#pragma shader_feature _ _GLOSSYREFLECTIONS_OFF// SM2.0: NOT SUPPORTED shader_feature ___ _DETAIL_MULX2// SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP#pragma skip_variants SHADOWS_SOFT DIRLIGHTMAP_COMBINED#pragma multi_compile_fwdbase#pragma multi_compile_fog#pragma vertex vertBase#pragma fragment fragBase#include "UnityStandardCoreForward.cginc"ENDCG}// ------------------------------------------------------------------//  Additive forward pass (one light per pass)Pass{Name "FORWARD_DELTA"Tags { "LightMode" = "ForwardAdd" }Blend [_SrcBlend] OneFog { Color (0,0,0,0) } // in additive pass fog should be blackZWrite OffZTest LEqualCGPROGRAM#pragma target 2.0#pragma shader_feature _NORMALMAP#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON#pragma shader_feature _METALLICGLOSSMAP#pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A#pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF#pragma shader_feature ___ _DETAIL_MULX2// SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP#pragma skip_variants SHADOWS_SOFT#pragma multi_compile_fwdadd_fullshadows#pragma multi_compile_fog#pragma vertex vertAdd#pragma fragment fragAdd#include "UnityStandardCoreForward.cginc"ENDCG}// ------------------------------------------------------------------//  Shadow rendering passPass {Name "ShadowCaster"Tags { "LightMode" = "ShadowCaster" }ZWrite On ZTest LEqualCGPROGRAM#pragma target 2.0#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON#pragma shader_feature _METALLICGLOSSMAP#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A#pragma skip_variants SHADOWS_SOFT#pragma multi_compile_shadowcaster#pragma vertex vertShadowCaster#pragma fragment fragShadowCaster#include "UnityStandardShadow.cginc"ENDCG}// ------------------------------------------------------------------// Extracts information for lightmapping, GI (emission, albedo, ...)// This pass it not used during regular rendering.Pass{Name "META"Tags { "LightMode"="Meta" }Cull OffCGPROGRAM#pragma vertex vert_meta#pragma fragment frag_meta#pragma shader_feature _EMISSION#pragma shader_feature _METALLICGLOSSMAP#pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A#pragma shader_feature ___ _DETAIL_MULX2#pragma shader_feature EDITOR_VISUALIZATION#include "UnityStandardMeta.cginc"ENDCG}}FallBack "VertexLit"//CustomEditor "StandardShaderGUI"
}

      PS:本来想把CompareFunction和PassFunction做成Enum标签,结果发现shader的Enum标签不支持超过7个,算是新发现。
      一般情况standard材质够场景制作了,这个材质赋予球体。
      接下来按照以前的做法写一个StencilMaskShader

Shader "StencilDemo/StencilMaskShader"
{Properties{_MainTex ("Texture", 2D) = "white" {}}SubShader{LOD 100Pass{Tags { "RenderType"="Opaque" }ColorMask 0Stencil{Ref 1Comp Always    //(Disable,0,Never,1,Less,2,Equal,3,LessEqual,4,Greater,5,NotEqual,6,GreaterEqual,7,Always,8)Pass Replace   //(Keep,0,Zero,1,Replace,2,IncrSat,3,DecrSat,4,Invert,5,IncrWrap,6,DecrWrap,7)}ZWrite OffZTest Off}}
}

      这个用于立方体,置换立方体蒙版区域的stencilvalue,用于显示球体。设置好球体的renderqueue>立方体的renderqueue。效果如下:
在这里插入图片描述

      因为模板测试位于图形流水线片段处理阶段,所以看到的效果就如同球体投影到平面上遮罩显示一样,我们想要立体感的话可以用两种方案。

      一.填充立方体剖面

Shader "StencilDemo/ZTestUnlitShader"
{Properties{_MainTex ("Texture", 2D) = "white" {}}SubShader{Tags { "RenderType"="Opaque+20" }LOD 100Pass{ZTest GreaterCGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"struct appdata{float4 vertex : POSITION;float2 uv : TEXCOORD0;};struct v2f{float2 uv : TEXCOORD0;float4 vertex : SV_POSITION;};sampler2D _MainTex;float4 _MainTex_ST;v2f vert (appdata v){v2f o;o.vertex = UnityObjectToClipPos(v.vertex);o.uv = TRANSFORM_TEX(v.uv, _MainTex);return o;}fixed4 frag (v2f i) : SV_Target{fixed4 col = tex2D(_MainTex, i.uv);return col;}ENDCG}}
}

      这里设置渲染立方体剖面材质的renderqueue>球体renderqueue,同时设定ZTest Greater,这样就能显示出立方体被球体遮挡的区域,就能形成填充剖面。效果如下:
在这里插入图片描述

      这个技术和以前切水果游戏一样,无非切水果的剖面纹理使用了相应的水果内部纹理。
      PS:当然用Texture3D来进行填充剖面,也是很好的想法。
      第二种方法性能要求高一点,但是也有很多应用的地方,后面有时间再写。

这篇关于C for Graphic:遮罩显示(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/989528

相关文章

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

遮罩,在指定元素上进行遮罩

废话不多说,直接上代码: ps:依赖 jquer.js 1.首先,定义一个 Overlay.js  代码如下: /*遮罩 Overlay js 对象*/function Overlay(options){//{targetId:'',viewHtml:'',viewWidth:'',viewHeight:''}try{this.state=false;//遮罩状态 true 激活,f

lvgl8.3.6 控件垂直布局 label控件在image控件的下方显示

在使用 LVGL 8.3.6 创建一个垂直布局,其中 label 控件位于 image 控件下方,你可以使用 lv_obj_set_flex_flow 来设置布局为垂直,并确保 label 控件在 image 控件后添加。这里是如何步骤性地实现它的一个基本示例: 创建父容器:首先创建一个容器对象,该对象将作为布局的基础。设置容器为垂直布局:使用 lv_obj_set_flex_flow 设置容器

C# dateTimePicker 显示年月日,时分秒

dateTimePicker默认只显示日期,如果需要显示年月日,时分秒,只需要以下两步: 1.dateTimePicker1.Format = DateTimePickerFormat.Time 2.dateTimePicker1.CustomFormat = yyyy-MM-dd HH:mm:ss Tips:  a. dateTimePicker1.ShowUpDown = t

小程序button控件上下边框的显示和隐藏

问题 想使用button自带的loading图标功能,但又不需要button显示边框线 button控件有一条淡灰色的边框,在控件上了样式 border:none; 无法让button边框隐藏 代码如下: <button class="btn">.btn{border:none; /*一般使用这个就是可以去掉边框了*/} 解决方案 发现button控件有一个伪元素(::after

MFC中Spin Control控件使用,同时数据在Edit Control中显示

实现mfc spin control 上下滚动,只需捕捉spin control 的 UDN_DELTAPOD 消息,如下:  OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult) {  LPNMUPDOWN pNMUpDown = reinterpret_cast(pNMHDR);  // TODO: 在此添加控件通知处理程序代码    if

微信小程序uniappvue3版本-控制tabbar某一个的显示与隐藏

1. 首先在pages.json中配置tabbar信息 2. 在代码根目录下添加 tabBar 代码文件 直接把微信小程序文档里面的四个文件复制到自己项目中就可以了   3. 根据自己的需求更改index.js文件 首先我这里需要判断什么时候隐藏某一个元素,需要引入接口 然后在切换tabbar时,改变tabbar当前点击的元素 import getList from '../

gazebo 已加载模型但无法显示

目录 写在前面的话问题一:robot_state_publisher 发布机器人信息失败报错一 Error: Error document empty.报错二 .xcaro 文件中有多行注释成功启动 问题二:通过 ros2 启动 gazebo 失败成功启动 问题三:gazebo 崩溃和无法显示模型问题四: 缺少 robot_description 等话题正确的输出 写在前面的话