- Home /
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?
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
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.
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
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!
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
Follow this Question
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