2026-02-27 22:54:56 +09:00

94 lines
2.8 KiB
Plaintext

Shader "StudioMaron/DotShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_EmissionIntensity("Intensity", Range(0, 10)) = 1
_DotSize("DotSize", Range(0, 1)) = 0.5
_DotNumber_X("Dot Number X", Int) = 160
_DotNumber_Y("Dot Number X", Int) = 90
_Noise("Avoid Moire", Range(0, 0.2)) = 0.05
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
//****************************************
//距離関数 (円)
//****************************************
float circle(float2 p, float radius)
{
return length(p) - radius;
}
//****************************************
//・2次元 疑似ランダム(入力は2次元です)
//****************************************
float2 random2(fixed2 st) {
st = fixed2(dot(st, float2(127.1, 311.7)),
dot(st, fixed2(269.5, 183.3)));
return -1.0 + 2.0 * frac(sin(st) * 43758.5453123);
}
sampler2D _MainTex;
float4 _MainTex_ST;
float _EmissionIntensity;
float _DotSize;
float _Noise;
int _DotNumber_X;
int _DotNumber_Y;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex); // _MainTex_STの処理
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
float4 frag (v2f i) : SV_Target
{
// sample the texture
float4 col = tex2D(_MainTex, frac(i.uv)); // _MainTex_STの処理のためにfrac()を使用
col.rgb *= _EmissionIntensity;
float2 uv = i.uv * float2(_DotNumber_X, _DotNumber_Y);
float2 moire = (random2(floor(uv)) - 0.5) * _Noise; // モアレ対策にドット毎にちょっとだけずらす。僅かに効果あり
float2 p = frac(uv) - 0.5 + moire;
col.rgb *= circle(p, _DotSize * 0.7) < 0 ? 1 : 0;
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}