Skip to content

Instantly share code, notes, and snippets.

@marcteys
Last active July 6, 2017 18:42
Show Gist options
  • Save marcteys/f280359954be3f52e4d3 to your computer and use it in GitHub Desktop.
Save marcteys/f280359954be3f52e4d3 to your computer and use it in GitHub Desktop.
Unity Shader Trick
using UnityEngine;
using System.Collections;
public class BlinkShader : MonoBehaviour
{
public float fadeSpeed = 2f;
public float highIntensity = 2f;
public float lowIntensity = 0.5f;
public float changeMargin = 0.2f;
public bool blinkOn;
float currentIntensity;
private float targetIntensity;
private MaterialPropertyBlock materialProperty;
private Renderer renderer;
void Awake()
{
currentIntensity = 0f;
targetIntensity = highIntensity;
materialProperty = new MaterialPropertyBlock();
renderer = this.GetComponent<Renderer>();
}
void Update()
{
// If the light is on...
if (blinkOn)
{
currentIntensity = Mathf.Lerp(currentIntensity, targetIntensity, fadeSpeed * Time.deltaTime);
CheckTargetIntensity();
}
else
currentIntensity = Mathf.Lerp(currentIntensity, 0f, fadeSpeed * Time.deltaTime);
SetMaterial();
}
void SetMaterial()
{
materialProperty.Clear();
materialProperty.AddFloat("_Shininess", currentIntensity);
renderer.SetPropertyBlock(materialProperty);
}
void CheckTargetIntensity()
{
if (Mathf.Abs(targetIntensity - currentIntensity) < changeMargin)
{
if (targetIntensity == highIntensity)
targetIntensity = lowIntensity;
else
targetIntensity = highIntensity;
}
}
}
Shader "Hologram" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_RimColor ("Rim Color", Color) = (1, 1, 1, 1)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_Outline ("Outline width", Range (0.0, 0.03)) = .005
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent+1" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
Pass {
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 color : COLOR;
};
uniform float4 _MainTex_ST;
uniform float4 _RimColor;
float _Shininess;
uniform float _Outline;
v2f vert (appdata_base v) {
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
//float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
//float2 offset = TransformViewToProjection(norm.xy);
//o.pos.xy += offset * o.pos.z * _Outline;
float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
float2 offset = TransformViewToProjection(norm.xy);
o.pos.xy += offset * _Outline;
float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
float dotProduct =( 1 - dot(v.normal, viewDir))*_Shininess;
float rimWidth = 0.7;
o.color = smoothstep(1 - rimWidth, 1.0, dotProduct);
o.color *= _RimColor;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
uniform sampler2D _MainTex;
uniform float4 _Color;
float4 frag(v2f i) : COLOR {
float4 texcol = tex2D(_MainTex, i.uv);
texcol *= _Color;
texcol.rgb += i.color;
texcol.a = _Color.a;
return texcol;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment