Skip to content

Instantly share code, notes, and snippets.

@sehm
Last active September 21, 2018 14:39
Show Gist options
  • Save sehm/95f5e65f5864c834559d to your computer and use it in GitHub Desktop.
Save sehm/95f5e65f5864c834559d to your computer and use it in GitHub Desktop.
Unity ShaderLab : モザイクシェーダー ピクセル値は単純にブロックの先頭を使う
Shader "Custom/mosaic" {
Properties
{
//[PerRendererData]
_MainTex ("Texture", 2D) = "white" {}
_Width ("Image Width", Float) = 256
_Height ("Image Height", Float) = 256
_BlockWidth ("Block Width", Float) = 2
_BlockHeight ("Block Height", Float) = 2
}
SubShader
{
Tags {
"RenderType"="Opaque"
}
Cull Off Lighting Off Fog { Mode Off }
AlphaTest Off Blend Off
ZWrite Off
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST; // xy : Scale, zw : Offset(Transform)
float _Width;
float _Height;
float _BlockWidth;
float _BlockHeight;
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord + _MainTex_ST.zw;// offset 値だけ反映
//o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);// scale, offset を両方反映するならばマクロあり。(v.texcoord * _MainTex_ST.xy + _MainTex_ST.zw)
return o;
}
fixed4 frag( v2f i ) : COLOR
{
// ブロックの先頭のピクセル値を反映する.
i.uv.x = floor((i.uv.x * _Width) / _BlockWidth) * _BlockWidth / _Width;
i.uv.y = floor((i.uv.y * _Height) / _BlockHeight) * _BlockHeight / _Height;
return tex2D(_MainTex, i.uv);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment