Skip to content

Instantly share code, notes, and snippets.

@haltyt
Last active September 6, 2017 14:07
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 haltyt/2ef99dea2f15d06635734a302e4e9389 to your computer and use it in GitHub Desktop.
Save haltyt/2ef99dea2f15d06635734a302e4e9389 to your computer and use it in GitHub Desktop.
Shader "Custom/VertexColorWave" {
Properties
{
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_Color("Main Color", Color) = (0.2, 0.5, 1.0)
_Speed("Speed", Range(0.01, 10)) = 1
_TrianglesScale("Triangles Scale", Range(0.01, 10)) = 1
_RangeScale("Range Scale", Range(0.01, 10)) = 1
_Center("Center", Vector) = (0.0, -1.0, 3.0, 1.0)
_Radius("Radius", Range(0.0, 1000)) = 0
_PointSize("Point Size", Float) = 5.0
_WaveScaleW("Wave Scale Width", Range(0.0, 1.0)) = 0.1
_WaveScaleH("Wave Scale Height", Range(0.0, 1.0)) = 0.1
_WaveDecay("Wave Decay", Range(0.0, 1.0)) = 0.1
_ZoomScale("Zoom Scale", Range(0.0, 1.0)) = 0.1
//_RotateScale("Rotate Scale", Range(0.0, 1.0)) = 0.1
_BaseColor ("Base Color", Color) = (0.0, 1.0, 0.0)
_WaveFactorX("Wave Factor X", Range(0.0, 2.0)) = 0.0
_WaveFactorY("Wave Factor Y", Range(0.0, 2.0)) = 0.0
_WaveFactorZ("Wave Factor Z", Range(0.0, 2.0)) = 0.0
//_WaveCorrection("Wave Correction", float) = 0.3
//_Pitch("Wave Pitch", float) = 1.0
//_Delay("Delay by Distance", float) = 0.02
//_Bend("Bend", float) = 0.3
//_MainTex ("Base (RGB)", 2D) = "white" {}
_SpectrumVolume("SpectrumVolume", Range(0.0, 2.0)) = 0.0
}
SubShader
{
//Tags{ "RenderType" = "Opaque" }
Tags { "RenderType" = "Transparent" "Queue"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#define PI 3.14159265358979
//#define reciproce_sqrt3 0.57735026918962576450914878050196
sampler2D _MainTex;
half3 _Color;
float _Speed;
float _TrianglesScale;
float _RangeScale;
float4 _Center;
float _Radius;
float _PointSize;
float _WaveScaleW;
float _WaveScaleH;
float _WaveDecay;
float _ZoomScale;
// float _RotateScale;
float _WaveFactorX;
float _WaveFactorZ;
float _WaveFactorY;
// float _WaveCorrection;
// float _Pitch;
// float _Delay;
// float _Bend;
float _SpectrumVolume;
float4 _BaseColor;
float4x4 depthCameraTUnityWorld;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 color: COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
float4 uv : TEXCOORD0;
float4 color: COLOR;
float size : PSIZE;
};
//Wave Effect
float4 wave(float4 vertex)
{
float bend = sin((PI * _Time * 1000 / 45 + length(vertex.xz)) / _WaveScaleW) * _WaveDecay / length(vertex.xz);
vertex.y += _SpectrumVolume * _WaveScaleH * bend;
return vertex;
}
//Zoom Effect
float4 zoom(float4 vertex)
{
float zoom = _SpectrumVolume * 10;
vertex.xyz += _ZoomScale * zoom * vertex.xyz;
return vertex;
}
v2f vert(appdata v)
{
//Wave Effect
v.vertex = wave(v.vertex);
//Zoom Effect
v.vertex = zoom(v.vertex);
v2f o;
//ローカル座標系→ワールド座標系→ビュー座標系→プロジェクション座標系へ変換
//Transform local coordinate to projection coordinate
o.vertex = UnityObjectToClipPos(v.vertex);
//ローカル座標系→ワールド座標系へ変換
//Transform local coordinate to world coordinate
o.uv = mul(unity_ObjectToWorld, v.vertex);
//o.uv = v.uv;
o.color = v.color;
//o.color = mul(depthCameraTUnityWorld, v.vertex);
o.size = _PointSize;
//ローカル座標系→ビュー座標系へ変換
//Transform local coordinate to view coordinate
float4 view = mul(UNITY_MATRIX_MV, v.vertex);
//ビュー座標の深度に応じてPointサイズ変更
//Change point size based on depth
//o.size = 20 / fmod(length(mul(depthCameraTUnityWorld, view).xyz), 10);
if(o.size > 20)
o.size = 20;
return o;
}
//float planeDistance = 0.2;
//float offset;
float r(float n)
{
return frac(abs(sin(n*55.753)*367.34));
}
float r(float2 n)
{
return r(dot(n, float2(2.46, -1.21)));
}
float3 trianglesColor(float3 pos)
{
float variant = _Radius; //_Time.y;
float a = (radians(60.0));
float zoom = 0.125;
float2 c = (pos.xy + float2(0.0, pos.z)) * float2(sin(a), 1.0) / _TrianglesScale;//scaled coordinates
c = ((c + float2(c.y, 0.0)*cos(a)) / zoom) + float2(floor((c.x - c.y*cos(a)) / zoom*4.0) / 4.0, 0.0);//Add rotations
float type = (r(floor(c*4.0))*0.2 + r(floor(c*2.0))*0.3 + r(floor(c))*0.5);//Randomize type
type += 0.3 * sin(variant*5.0*type);
float l = min(min((1.0 - (2.0 * abs(frac((c.x - c.y)*4.0) - 0.5))),
(1.0 - (2.0 * abs(frac(c.y * 4.0) - 0.5)))),
(1.0 - (2.0 * abs(frac(c.x * 4.0) - 0.5))));
l = smoothstep(0.06, 0.04, l);
return lerp(type, l, 0.3);
}
//---------------------------------------------------------------
// Material
//
// Defines the material (colors, shading, pattern, texturing) of the model
// at every point based on its position and normal. In this case, it simply
// returns a constant yellow color.
//------------------------------------------------------------------------
float3 doMaterial(in float4 vertex, in float4 center, in float4 color)
{
float3 pos = vertex.xyz;
float3 midPos = center.xyz;
//float d = length(pos.xz - midPos.xz) + pos.y - midPos.y;
float d = length(pos - midPos);
d /= _RangeScale;
float border = fmod(_Time.y * _Speed, _SpectrumVolume * 10);
// Small rim
float3 c = float3(1.0, 1.0, 1.0) * smoothstep(border - 0.4, border, d);
// Small triangle slightly after front
c += trianglesColor(pos) * smoothstep(border - 0.4, border - 0.6, d);
// Cutoff front
c *= smoothstep(border, border - 0.05, d);
// Cutoff back
c *= smoothstep(border - 3.0, border - 0.5, d);
// fade out
c *= smoothstep(5.0, 4.0, border);
return color.rgb * 0.7 + _BaseColor * 0.2 + mul(depthCameraTUnityWorld, vertex) * c * 0.3;
}
fixed4 frag(v2f i) : SV_Target
{
return float4(doMaterial(i.uv, _Center, i.color), 1.0);
// float4 tex = tex2D(_MainTex, i.uv);
//
// float3 color = doMaterial(tex, _Center, i.color);
// //float alpha = pow(max(0,dot(In.viewDir,In.normal)),3);
//
// tex.rgb *= color;
// tex.a *= i.color.a;
// return tex;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment