Skip to content

Instantly share code, notes, and snippets.

@phi16
Created August 1, 2020 07:36
Show Gist options
  • Save phi16/8b3eb8df7570a989342b8de165eb4340 to your computer and use it in GitHub Desktop.
Save phi16/8b3eb8df7570a989342b8de165eb4340 to your computer and use it in GitHub Desktop.
original
Shader "Amebient/Air"
{
Properties
{
_Color ("Color", Color) = (0.5,0.8,1,1)
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent-525" "LightMode"="ForwardBase" "DisableBatching"="True" }
LOD 100
Blend SrcAlpha One
ZWrite Off
Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#include "UnityCG.cginc"
#include "Water.cginc"
struct appdata {
float4 vertex : POSITION;
};
appdata vert (appdata v) {
return v;
}
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldPos : TEXCOORD1;
float3 color : TEXCOORD2;
float4 projPos : TEXCOORD3;
};
float noise(float i, float t) {
float ut = floor(t), ft = frac(t);
float v0 = rand(float2(i,ut+0));
float v1 = rand(float2(i,ut+1));
return lerp(v0,v1,smoothstep(0,1,ft));
}
float TransitTime, Time;
[maxvertexcount(4)]
void geom (point appdata IN[1], inout TriangleStream<v2f> stream) {
int ix = IN[0].vertex.x;
v2f o;
float2 uvs[4] = { float2(-1,-1), float2(-1,1), float2(1,-1), float2(1,1) };
float2 seed = float2(ix/1000.0, 0);
float animTime = Time + smoothstep(120, 132, TransitTime) * 24;
float3 rep = ix < 32768 ? float3(40,20,40) : float3(20,10,20);
float3 rs = float3(rand(seed+0), rand(seed+1), rand(seed+2));
float3 center = rs * rep;
float t = animTime;
center.y += t * rep.y / 80 + noise(seed.x+0, t/4 + rs.x) * 0.1;
center.x += noise(seed.x+1, t/2 + rs.z) * 0.1;
center.z += noise(seed.x+2, t/2 + rs.y) * 0.1;
center += rep / 2 - _WorldSpaceCameraPos;
center -= floor(center/rep) * rep;
center -= rep / 2 - _WorldSpaceCameraPos;
float3 normal = normalize(center - _WorldSpaceCameraPos);
float3 tangent = normalize(mul(transpose((float3x3)UNITY_MATRIX_V), float3(1,0,0)));
float3 binormal = normalize(cross(tangent, normal));
tangent = cross(normal, binormal);
float size = 0.003 * lerp(1, 5, pow(rand(seed+3),4));
size *= smoothstep(1, 0.5, distance(_WorldSpaceCameraPos.xz, center.xz)/rep.x*2);
size *= smoothstep(1, 0.5, distance(_WorldSpaceCameraPos.y, center.y)/rep.y*2);
if(center.y < oceanLevel()+0.05) size *= smoothstep(0, 0.05, center.y-oceanLevel());
if(center.y < oceanLevel()) return;
for(int i=0;i<4;i++) {
o.worldPos = center + (uvs[i].x * binormal + uvs[i].y * tangent) * size;
o.vertex = mul(UNITY_MATRIX_VP, float4(o.worldPos, 1));
o.uv = uvs[i];
o.projPos = ComputeScreenPos(o.vertex);
o.projPos.z = - o.vertex.z;
o.color = lerp(_Color.xyz, float3(1,1,1), rand(seed+4));
stream.Append(o);
}
stream.RestartStrip();
}
float4 frag (v2f i) : SV_Target
{
float d = length(i.uv);
clip(1-d);
float3 viewDir = normalize(i.worldPos - _WorldSpaceCameraPos);
float3 forward = normalize(mul(transpose((float3x3)UNITY_MATRIX_V), float3(0,0,-1)));
float3 eyeViewDir = viewDir / dot(viewDir, forward);
float eyeDepth = LinearEyeDepth(tex2D(_CameraDepthTexture, i.projPos.xy / i.projPos.w));
float3 collision = _WorldSpaceCameraPos + eyeViewDir * eyeDepth;
float depth = distance(collision, i.worldPos);
float worldDist = distance(i.worldPos, _WorldSpaceCameraPos);
float3 color = i.color * lerp(float3(0.8,0.9,1), float3(1,1,1), i.uv.x);
float alpha = smoothstep(1,0.9,d) * smoothstep(0,1,depth) * 0.1;
alpha *= smoothstep(0.1, 0.2, worldDist);
return float4(color, alpha);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment