Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the special effect of blinds with Unityshader

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to achieve blinds in Unityshader". In daily operation, I believe many people have doubts about how to achieve blinds in Unityshader. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to achieve blinds in Unityshader". Next, please follow the editor to study!

1. Divide the picture into horizontal N columns, the code is as follows:

Shader "Unlit/BYCShader" {Properties {[PerRendererData] _ MainTex ("Sprite Texture", 2D) = "white" {} _ Color ("Tint", Color) = (1 Stencil Comparison, Float) = 8 _ Stencil ("Stencil ID", Float) = 0 _ StencilOp ("Stencil Operation", Float) = 0 _ StencilWriteMask ("StencilWriteMask", Float) = 255 _ StencilReadMask ("StencilReadMask", Float) = 255 _ ColorMask ("ColorMask") Float) = 15 _ Lan ("Lan", Float) = 10 [Toggle (UNITY_UI_ALPHACLIP)] _ UseUIAlphaClip ("Use AlphaClip" Float) = 0} SubShader {Tags {"Queue" = "Transparent"IgnoreProjector" = "True"RenderType" = "Transparent"PreviewType" = "Plane"CanUseSpriteAtlas" = "True"} Stencil {Ref [_ Stencil] Comp [_ StencilComp] Pass [_ StencilOp] ReadMask [_ StencilReadMask] WriteMask [_ StencilWriteMask]} Cull Off Lighting Off ZWrite Off ZTest [unity_GUIZTestMode] Blend SrcAlpha OneMinusSrcAlpha ColorMask [_ ColorMask] Pass {Name "Default" CGPROGRAM # pragma vertex vert # pragma fragment frag # pragma target 2.0 # include "UnityCG.cginc" # include "UnityUI.cginc" # pragma multi_compile _ _ UNITY_UI_CLIP_RECT # pragma multi_compile _ _ UNITY_UI_ALPHACLIP struct appdata_t {float4 vertex: POSITION Float4 color: COLOR; float2 texcoord: TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID}; struct v2f {float4 vertex: SV_POSITION; fixed4 color: COLOR; float2 texcoord: TEXCOORD0; float4 worldPosition: TEXCOORD1; UNITY_VERTEX_OUTPUT_STEREO}; v2f vert (appdata_t v) {v2f OUT; UNITY_SETUP_INSTANCE_ID (v); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO (OUT); OUT.worldPosition = v.vertex OUT.vertex = UnityObjectToClipPos (OUT.worldPosition); OUT.texcoord = v.texcoord.OUT.color = v.color.return OUT;} sampler2D _ MainTex; float _ Lan; float4 frag (V2f IN): SV_Target {/ / start here float2 uv = IN.texcoord; uv.x*= _ Lan; uv.x = frac (uv.x); return float4 (uv.xy,1.0,1.0) } ENDCG}

two。 Color subtraction is performed on each column at the same time (the control threshold can be achieved by c # code)

The code is as follows:

Shader "Unlit/BYCShader" {Properties {[PerRendererData] _ MainTex ("Sprite Texture", 2D) = "white" {} _ Color ("Tint", Color) = (1 Stencil Comparison, Float) = 8 _ Stencil ("Stencil ID", Float) = 0 _ StencilOp ("Stencil Operation", Float) = 0 _ StencilWriteMask ("StencilWriteMask", Float) = 255 _ StencilReadMask ("StencilReadMask", Float) = 255 _ ColorMask ("ColorMask") Float) = 15 _ Lan ("Lan", Float) = 10 _ StepX ("StepX", Range (0.0jue 1.0)) = 1.0 [Toggle (UNITY_UI_ALPHACLIP)] _ UseUIAlphaClip ("Use AlphaClip" Float) = 0} SubShader {Tags {"Queue" = "Transparent"IgnoreProjector" = "True"RenderType" = "Transparent"PreviewType" = "Plane"CanUseSpriteAtlas" = "True"} Stencil {Ref [_ Stencil] Comp [_ StencilComp] Pass [_ StencilOp] ReadMask [_ StencilReadMask] WriteMask [_ StencilWriteMask]} Cull Off Lighting Off ZWrite Off ZTest [unity_GUIZTestMode] Blend SrcAlpha OneMinusSrcAlpha ColorMask [_ ColorMask] Pass {Name "Default" CGPROGRAM # pragma vertex vert # pragma fragment frag # pragma target 2.0 # include "UnityCG.cginc" # include "UnityUI.cginc" # pragma multi_compile _ _ UNITY_UI_CLIP_RECT # pragma multi_compile _ _ UNITY_UI_ALPHACLIP struct appdata_t {float4 vertex: POSITION Float4 color: COLOR; float2 texcoord: TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID}; struct v2f {float4 vertex: SV_POSITION; fixed4 color: COLOR; float2 texcoord: TEXCOORD0; float4 worldPosition: TEXCOORD1; UNITY_VERTEX_OUTPUT_STEREO}; v2f vert (appdata_t v) {v2f OUT; UNITY_SETUP_INSTANCE_ID (v); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO (OUT); OUT.worldPosition = v.vertex OUT.vertex = UnityObjectToClipPos (OUT.worldPosition); OUT.texcoord = v.texcoord.OUT.color = v.color.return OUT;} sampler2D _ MainTex; float _ Lan; float _ StepX; float4 frag (V2f IN): SV_Target {float2 uv = IN.texcoord; uv.x*= _ Lan; uv.x = frac (uv.x) / / when the color value is greater than the specified value stepx, the elimination int needDiscard = step (_ StepX,uv.x); if (needDiscard = = 1) {discard;} return float4 (uv.xy,1.0,1.0);} ENDCG}

3. Coupled with shear, the shutter changes in perspective when it is closed and opened. Shear can be used to approximately simulate perspective, because perspective is very expensive to implement, so shear is used.

Add a picture and shear it

The code is as follows:

Shader "Unlit/BYCShader" {Properties {[PerRendererData] _ MainTex ("Sprite Texture", 2D) = "white" {} _ Color ("Tint", Color) = (1 Stencil Comparison, Float) = 8 _ Stencil ("Stencil ID", Float) = 0 _ StencilOp ("Stencil Operation", Float) = 0 _ StencilWriteMask ("StencilWriteMask", Float) = 255 _ StencilReadMask ("StencilReadMask", Float) = 255 _ ColorMask ("ColorMask") Float) = 15 _ Lan ("Lan", Float) = 10 _ StepX ("StepX", Range (0.0jue 1.0)) = 1.0 [Toggle (UNITY_UI_ALPHACLIP)] _ UseUIAlphaClip ("Use AlphaClip" Float) = 0} SubShader {Tags {"Queue" = "Transparent"IgnoreProjector" = "True"RenderType" = "Transparent"PreviewType" = "Plane"CanUseSpriteAtlas" = "True"} Stencil {Ref [_ Stencil] Comp [_ StencilComp] Pass [_ StencilOp] ReadMask [_ StencilReadMask] WriteMask [_ StencilWriteMask]} Cull Off Lighting Off ZWrite Off ZTest [unity_GUIZTestMode] Blend SrcAlpha OneMinusSrcAlpha ColorMask [_ ColorMask] Pass {Name "Default" CGPROGRAM # pragma vertex vert # pragma fragment frag # pragma target 2.0 # include "UnityCG.cginc" # include "UnityUI.cginc" # pragma multi_compile _ _ UNITY_UI_CLIP_RECT # pragma multi_compile _ _ UNITY_UI_ALPHACLIP struct appdata_t {float4 vertex: POSITION Float4 color: COLOR; float2 texcoord: TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID}; struct v2f {float4 vertex: SV_POSITION; fixed4 color: COLOR; float2 texcoord: TEXCOORD0; float4 worldPosition: TEXCOORD1; UNITY_VERTEX_OUTPUT_STEREO}; v2f vert (appdata_t v) {v2f OUT; UNITY_SETUP_INSTANCE_ID (v); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO (OUT); OUT.worldPosition = v.vertex OUT.vertex = UnityObjectToClipPos (OUT.worldPosition); OUT.texcoord = v.texcoord.OUT.color = v.color.return OUT;} sampler2D _ MainTex; float _ Lan; float _ StepX; float4 frag (V2f IN): SV_Target {/ / crop here float2 uv = IN.texcoord; uv.x*= _ Lan; uv.x = frac (uv.x); int needDiscard = step (_ StepX,uv.x) If (needDiscard = = 1) {discard;} / / Shear float x1 = uv.x; uv = IN.texcoord; uv+=float2 (- 0.5); uv.x-=x1 / / when cutting, first adjust the center of gravity to the center, and then shear around the starting point of each column (here it is similar to rotating around a certain point, so you have to reverse the operation, add and subtract, subtract, add) float2x2 qiebian = float2x2 (1 1.0-_StepX 0, (1.0-_StepX), 1); uv = mul (qiebian,uv); uv-=float2 (- 0.5); uv.x+=x1; float4 color= tex2D (_ MainTex, uv) Return color;} ENDCG}} at this point, the study on "how to achieve blinds in Unityshader" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report