Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ScrubbyCoding · Nov 08, 2017 at 10:52 PM · c#shader programmingmaskproperties

How to add a disintegrate to a shader?

I have found a force field shader online, and in the video the force field can disintegrate/dissolve. But the source file doesn't add that, and someone commented saying that but they said they weren't going to add it because it is a shader mask and under procedural properties. The person who made it speaks a different language and doesn't usally reply so that's why I'm here. https://www.youtube.com/watch?v=wbL2FCxlCe4. There is the link to the video. So basically I'm asking if someone can help me add a disintegration shader to this current shader. I don't code with shaders and don't plan on learning because other people that I work with are in that area of specialties. They don't understand though. the script is:

 Shader "Unlit/ShieldFX"
 {
     Properties
     {
         _MainColor("MainColor", Color) = (1,1,1,1)
         _MainTex ("Texture", 2D) = "white" {}
         _Fresnel("Fresnel Intensity", Range(0,200)) = 3.0
         _FresnelWidth("Fresnel Width", Range(0,2)) = 3.0
         _Distort("Distort", Range(0, 100)) = 1.0
         _IntersectionThreshold("Highlight of intersection threshold", range(0,1)) = .1 //Max difference for intersections
         _ScrollSpeedU("Scroll U Speed",float) = 2
         _ScrollSpeedV("Scroll V Speed",float) = 0
         //[ToggleOff]_CullOff("Cull Front Side Intersection",float) = 1
         _SliceGuide ("Slice Guide (RGB)", 2D) = "white" {}
         _SliceAmount ("Slice Amount", Range(0.0, 1.0)) = 0.5
     }
         
     SubShader
     { 
         Tags{ "Queue" = "Overlay" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
 
         GrabPass{ "_GrabTexture" }
         Pass
         {
 
             Lighting Off ZWrite On
             Blend SrcAlpha OneMinusSrcAlpha
             Cull Off
 
             CGPROGRAM
             
 
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 fixed4 vertex : POSITION;
                 fixed4 normal: NORMAL;
                 fixed3 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 fixed2 uv : TEXCOORD0;
                 fixed4 vertex : SV_POSITION;
                 fixed3 rimColor :TEXCOORD1;
                 fixed4 screenPos: TEXCOORD2;
             };
 
             sampler2D _MainTex, _CameraDepthTexture, _GrabTexture;
             fixed4 _MainTex_ST,_MainColor,_GrabTexture_ST, _GrabTexture_TexelSize;
             fixed _Fresnel, _FresnelWidth, _Distort, _IntersectionThreshold, _ScrollSpeedU, _ScrollSpeedV;
 
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
 
                 //scroll uv
                 o.uv.x += _Time * _ScrollSpeedU;
                 o.uv.y += _Time * _ScrollSpeedV;
 
                 //fresnel 
                 fixed3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
                 fixed dotProduct = 1 - saturate(dot(v.normal, viewDir));
                 o.rimColor = smoothstep(1 - _FresnelWidth, 1.0, dotProduct) * .5f;
                 o.screenPos = ComputeScreenPos(o.vertex);
                 COMPUTE_EYEDEPTH(o.screenPos.z);//eye space depth of the vertex 
                 return o;
             }
             
             fixed4 frag (v2f i,fixed face : VFACE) : SV_Target
             {
                 //intersection
                 fixed intersect = saturate((abs(LinearEyeDepth(tex2Dproj(_CameraDepthTexture,i.screenPos).r) - i.screenPos.z)) / _IntersectionThreshold);
 
                 fixed3 main = tex2D(_MainTex, i.uv);
                 //distortion
                 i.screenPos.xy += (main.rg * 2 - 1) * _Distort * _GrabTexture_TexelSize.xy;
                 fixed3 distortColor = tex2Dproj(_GrabTexture, i.screenPos);
                 distortColor *= _MainColor * _MainColor.a + 1;
 
                 //intersect hightlight
                 i.rimColor *= intersect * clamp(0,1,face);
                 main *= _MainColor * pow(_Fresnel,i.rimColor) ;
                 
                 //lerp distort color & fresnel color
                 main = lerp(distortColor, main, i.rimColor.r);
                 main += (1 - intersect) * (face > 0 ? .03:.3) * _MainColor * _Fresnel;
                 return fixed4(main,.9);
             }
 
              //if you're not planning on using shadows, remove "addshadow" for better performance
 
             ENDCG
         }
     }
 }
 

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MaxGuernseyIII · Nov 08, 2017 at 11:56 PM 0
Share

Please clean up your code snippet. Use the button that looks like 1s and 0s.

avatar image ScrubbyCoding MaxGuernseyIII · Nov 09, 2017 at 12:23 AM 0
Share

@$$anonymous$$axGuernseyIII Sorry about that I cleaned it up.

avatar image MaxGuernseyIII ScrubbyCoding · Nov 09, 2017 at 12:26 AM 0
Share

Thank you. I have had a terrible time getting help with shaders via this medium.

Having a complete, readable code snippet can't hurt your chances.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Nov 09, 2017 at 12:45 AM

The effect you see in the video is nothing in the shader. He uses procedurally generated textures and that mask slider is part of the texture generator and not of the shader. So this is actually done on the CPU. The texture is generated from hexagonal shapes and the mask simply deactivates some of them based on the slider.


You can't really get the same result just by some shader logic (unless you actually generate this pattern inside the shader itself). You can get a similar effect by using a predefined masking texture with several random "hot spots" which have a gradient to "0". By using this mask texture along with a bias offset you can get a "1" everywhere when the slider is at "1" (so the mask is basically "overbright" and just 1 everywhere) or you get a "0" everywhere when the slider is at "0" (which would be like "darkening" the mask so it's 0 everywhere). In between you would have a gradient between your hotspots so the texture doesn't fade out at once


edit
I just got home and quickly created this script to generate a random mask texture, and this is the modified ShieldFx shader.

To sum up the changes: I add two new properties to the shader:

 _MaskTex ("MaskTexture", 2D) = "white" {}
 _Disolve ("Disolve", Range(0,1)) = 0.0


_MaskTex willl hold our generated mask texture and _Disolve controls if the field is visible (0) or completely disolved (1)


Inside the CG code we have to declare our properties. I just added the _MaskTex sampler and the _Disolve value simply at the end here:

 sampler2D _MainTex, _CameraDepthTexture, _GrabTexture, _MaskTex;
 fixed _Fresnel, _FresnelWidth, _Distort, _IntersectionThreshold, _ScrollSpeedU, _ScrollSpeedV, _Disolve;


Finally inside the Fragment shader we calculate a masking multiplier like this:

 fixed mask = clamp(tex2D(_MaskTex, i.uv).a+0.004,0,1) - _Disolve>0?1:0;

which is finally used to scale the alpha value of our resulting color:

 return fixed4(main,.9 * mask);


Note that the masking texture is a pure alpha texture. So only the alpha channel is actually used. Of course instead of the random generated mask texture you can create your own mask texture. Generally alpha values closer to "0" will disolve first and alpha values close to 1 will disolve last.

The random generated mask texture simply uses all 255 values randomly spreaded across the texture. So while the _Disolve value ramps from 0 to 1 random "parts" of the texture will disappear. Keep in mind when you use a small mask size the "areas" will be larger. Also keep in mind that the minimum texture size would be 16x16 pixels, otherwise you don't get the full spectrum. That means when you use an 8x8 mask the shield will be completely disolved after just "0.25" and not at "1.0"


Finally here you can see how the result looks like as animated GIF:

ps: Just in case it's not obvious, you can modify the _Disolve property of the material through script like that:

 Material mat; // needs to reference your material that is using the shader
 
 mat.SetFloat("_Disolve", yourValue)



Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ScrubbyCoding · Nov 09, 2017 at 01:03 AM 0
Share

@Bunny83 Can you please elaborate on how I would go about doing this? I'm new to masks and textures and stuff like that.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

417 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Shader: Mask Without a Texture? 2 Answers

Masked Advanced Minimap 1 Answer

Stencil Shaders with depth testing 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges