Skip to content

Instantly share code, notes, and snippets.

@baobao
Created December 18, 2016 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baobao/0ea3c01bf671f73e2df9597691918b69 to your computer and use it in GitHub Desktop.
Save baobao/0ea3c01bf671f73e2df9597691918b69 to your computer and use it in GitHub Desktop.
// 参考 http://glslsandbox.com/e#17334.0
Shader "Lightning"
{
Properties
{
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
half s = 0.0;
// "line"関数名は予約後らしく使用不可
void CreateLine (half2 currentTempPos, half2 currentStraightPos, half size, half2 fragCoord)
{
// 0~1
half2 uv = fragCoord.xy;
// 本来の雷ポジションから補正後のポジションへのベクトル
half2 lab = currentTempPos - currentStraightPos;
// 補正後ポジションから処理中のuv座標へのベクトル
half2 la = uv - currentTempPos;
// 本来のポジションから処理中uv座標へのベクトル
half2 lb = uv - currentStraightPos;
// +1だと点になる
half d = (length (la) + length (lb) - length (lab)) * min (length (la), length (lb ));
// pow(x,y) xのy乗を返す
s = max ( size - pow ( d * 4e8 , 0.07 ), s);
}
// ランダムな小数値を返却
half rand(half2 co)
{
return frac(sin(dot(co.xy ,half2(12.9898, 78.233))) * 43758.5453);
}
void split ( half2 startPos , half2 endPos, half2 fragCoord)
{
int num = 20;
// 同時に表示する雷の本数
int lightningNum = 8;
for (int j = 1 ; j < lightningNum ; j++)
{
// 雷の先端ポジション
half2 temp = startPos;
// 分割した雷の1つ分の距離
half2 d = (endPos - startPos) / num;
for (int i = 1; i < num; i++)
{
half2 currentPos = startPos + d * i;
if(i == j * 2)
{
// 雷の動きを激しくさせるランダム要素
d += (rand(half2(j, j) + currentPos) - 0.5) * rand(currentPos) * 0.025;
}
currentPos += (rand(currentPos) - 0.5) * d.y;
CreateLine (temp, currentPos, j * 0.25 + 2.0, fragCoord);
temp = currentPos;
}
}
}
fixed4 frag (v2f_img i) : SV_Target
{
split (
// 雷の開始位置
// UV座標の原点は左下なので、(0.2, 1.0)は左上より少し右の位置
half2(0.2, 1.0),
// 雷の終了位置
half2(clamp(cos(_Time.x), 0, 0.8), clamp(sin(_Time.x), 0, 0.3)),
// uv座標
i.uv);
// 雷の色をセット
// スター・ウォーズ新シリーズもリリースされたってことでの赤にしました
return fixed4(s, s - 1, s - 1, 1);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment