Skip to content

Instantly share code, notes, and snippets.

@mtytheone
Last active July 8, 2020 11:20
Show Gist options
  • Save mtytheone/e0b6e5beb9bd783ee150b4ce02d7a912 to your computer and use it in GitHub Desktop.
Save mtytheone/e0b6e5beb9bd783ee150b4ce02d7a912 to your computer and use it in GitHub Desktop.
割とシンプルに収めたGPUパーティクルのコード
Shader "HC/Unlit/SimpleGPUParticle"
{
Properties
{
_Size("ParticleSize", Float) = 0.1
[NoScaleOffset] _MainTex ("Texture", 2D) = "white" {}
[HDR] _EmissionColor("Emission", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" }
LOD 100
Cull Off
//加算ブレンド
Blend One One
//パーティクル用のテクスチャを反映した時にDepthを書き込むと透明部分が前に出てきて変な見え方になるため、Offにする
ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
#define PI acos(-1.0)
#define PI2 PI*2.0
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2g
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
struct g2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
};
float _Size;
sampler2D _MainTex;
fixed4 _EmissionColor;
v2g vert (appdata v)
{
v2g o;
o.vertex = mul(unity_ObjectToWorld, v.vertex);
o.uv = v.uv;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
[maxvertexcount(4)]
void geom(triangle v2g input[3], uint id : SV_PrimitiveID, inout TriangleStream<g2f> outStream)
{
g2f o;
float size = _Size;
float4x4 BillboardMatrix = UNITY_MATRIX_V;
BillboardMatrix._m03 = BillboardMatrix._m13 = BillboardMatrix._m23 = BillboardMatrix._m33 = 0;
// フワフワするための乱数
float w0 = (cos(_Time.y * 0.37 + id * 13) + 1) * 0.5;
float w1 = (cos(_Time.y * 0.23 + id * 19) + 1) * 0.5;
float w2 = (cos(_Time.y * 0.29 + id * 31) + 1) * 0.5;
// 重心
float4 gravityPoint = (w0 * input[0].vertex + w1 * input[1].vertex + w2 * input[2].vertex) / (w0 + w1 + w2);
[unroll]
for (int x = 0; x < 2; x++)
{
[unroll]
for (int y = 0; y < 2; y++)
{
o.uv = float2(x, y);
// 範囲調整したものをビルーボードに
o.vertex = mul(float4((o.uv - 0.5) * size, 0, 1), BillboardMatrix);
// 重心の分の加算
o.vertex += gravityPoint;
// プロジェクション変換まで一気に行う
o.vertex = mul(UNITY_MATRIX_VP, o.vertex);
outStream.Append(o);
}
}
outStream.RestartStrip();
}
fixed4 frag (g2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
col *= _EmissionColor;
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment