Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by vargata · Dec 29, 2013 at 09:19 PM · collisionshaderalpha

shader change alpha around collision

Hi peepz, I wrote a little shader which receive the collisioncoordinates from an oncollisionenter and changes the alpha. It would be great, but the effect works with world coordinates, so if my object moves away, the effect stays there here the code is:

 Shader "Custom/Shieldeffect" {
     Properties {
         _Position ("Collision", Vector) = (0, 0, 0, 0)
         _MaxDistance ("Effect Size", float) = 40
         _Amount ("Extrusion Amount", Range(-1,1)) = 0.5
         _Color ("Color (RGBA)", Color) = (0.7, 1, 1, 0.2)
     }
     SubShader {
         Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
         LOD 2000
         Cull Off
       
         CGPROGRAM
           #pragma surface surf Lambert vertex:vert alpha
           #pragma target 3.0
       
          struct Input {
             float4 mycolor : COLOR;
             float3 worldPos;
           };
       
           float _Amount;
           float4 _Color;  
           float4 _Position;
           float _MaxDistance;  
           float myDist;  
       
           void vert (inout appdata_full v) {
               //if(distance(_Position.xyz, v.vertex.xyz)<_MaxDistance){
                   v.vertex.xyz += v.normal * _Amount * sin(_Time);
             //}
           }
       
           void surf (Input IN, inout SurfaceOutput o) {
             o.Albedo = _Color.rgb;
             myDist = distance(_Position.xyz, IN.worldPos);
             if(myDist < _MaxDistance){
                   o.Alpha = abs(sin(_Time * 8)) - (myDist / _MaxDistance);
                   if(o.Alpha < 0.2){
                       o.Alpha = 0.2;
                   }
               }
               else {
                   o.Alpha = 0.2;
               }
           }
       
           ENDCG
     } 
     Fallback "Transparent/Diffuse"
 }

The problem is that the surface shader doesnt have other than worldcoords (am I wrong?). How to make that effect happen in local space?

Comment
Add comment · Show 3
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 tanoshimi · Dec 29, 2013 at 09:53 PM 0
Share

local to what?

avatar image sparkzbarca · Dec 29, 2013 at 10:08 PM 0
Share

to the object the shader is attached to. He wants the shield to light up when hit by enemy fire . I'm no expert i'm not sure if there is a way to reference the object the shader is attached to.

i've got a couple of ideas (I haven't yet had to shader program this could all be bullshit)

use properties to pass a reference to something (the object, the position)

check the shader documentation to see if there is a keyword to reference the object its attached to kind of like

this.gameobject

this is a working shield script

http://wiki.unity3d.com/index.php?title=Shield

check it to see if things like

input.screenposition can help you

avatar image vargata · Dec 29, 2013 at 11:37 PM 0
Share

I've checked them but none of them seems to do what i would need sadly :( Anyway yes, u understood well what i would need.

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by vargata · Dec 30, 2013 at 06:26 PM

well. thanks to meh11 here is a solution just in case if somebody would look for a simple nice energy shield effect with collisiondetection

shader:

 Shader "Custom/Shieldeffect" {
     Properties {
         _Position ("Collision", Vector) = (-1, -1, -1, -1)        
         _MaxDistance ("Effect Size", float) = 40        
         _ShieldColor ("Color (RGBA)", Color) = (0.7, 1, 1, 0)
         _EmissionColor ("Emission color (RGBA)", Color) = (0.7, 1, 1, 0.01)        
         _EffectTime ("Effect Time (ms)", float) = 0
     }
     
     SubShader {
         Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
         LOD 2000
         Cull Off
       
         CGPROGRAM
           #pragma surface surf Lambert vertex:vert alpha
           #pragma target 3.0
       
          struct Input {
              float customDist;
           };
       
           float4 _Position;          
           float _MaxDistance;          
           float4 _ShieldColor;
           float4 _EmissionColor;          
           float _EffectTime;
           
           float _Amount;
       
           void vert (inout appdata_full v, out Input o) {
               o.customDist = distance(_Position.xyz, v.vertex.xyz);
           }
       
           void surf (Input IN, inout SurfaceOutput o) {
             o.Albedo = _ShieldColor.rgb;
             o.Emission = _EmissionColor;
             
             if(_EffectTime > 0)
             {
                 if(IN.customDist < _MaxDistance){
                       o.Alpha = _EffectTime/500 - (IN.customDist / _MaxDistance) + _ShieldColor.a;
                       if(o.Alpha < _ShieldColor.a){
                           o.Alpha = _ShieldColor.a;
                       }
                   }
                   else {
                       o.Alpha = _ShieldColor.a;
                   }
               }
               else{
                   o.Alpha = o.Alpha = _ShieldColor.a;
               }
           }
       
           ENDCG
     } 
     Fallback "Transparent/Diffuse"
 }

script:

 #pragma strict
 
 var EffectTime : float;

 function Update(){
     if(EffectTime>0){
         if(EffectTime < 450 && EffectTime > 400){
             renderer.sharedMaterial.SetVector("_ShieldColor", Vector4(0.7, 1, 1, 0));
         }        
         
         EffectTime-=Time.deltaTime * 1000;
         
         renderer.sharedMaterial.SetFloat("_EffectTime", EffectTime);
     }
 }
     
 function OnCollisionEnter(collision : Collision) {
         for (var contact : ContactPoint in collision.contacts) {
         
             renderer.sharedMaterial.SetVector("_ShieldColor", Vector4(0.7, 1, 1, 0.05));
             
             renderer.sharedMaterial.SetVector("_Position", transform.InverseTransformPoint(contact.point));
             
             EffectTime=500;
         }
 }

and it looks like this: http://www.youtube.com/watch?v=v4suSVfhLf4

Comment
Add comment · Show 2 · 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 MiraiTunga · May 31, 2014 at 01:00 PM 0
Share

awesome stuff! i have been trying to figure this out for days !was trying using contact points to get a triangle and then , trying to change the material of triangles next to it !! which was too complicated i realize i was heading in wrong direction .your example really helped to put me in the right direction!!, you should consider putting this on the app store!

avatar image Shrikky23 · Nov 04, 2016 at 09:09 PM 0
Share

Thank you, your Shader is exactly the reference that I needed to make my own similar shader. I was wondering if there is a really good advanced shader tutorial that we can learn from?

And why is it else { o.Alpha = o.Alpha = _ShieldColor.a; }

??

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

22 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

Related Questions

Cancel out mesh alpha 1 Answer

Is it possible to create a blur effect based on the alpha of a transparent texture on a plane? 0 Answers

Depth mask with alpha / adding alpha to frag grabpass shader 0 Answers

Shader with "alpha" parameter no longer writes to Z-Buffer 2 Answers

How to make projector/light shader "alpha enabled"? 2 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