Skip to content

Instantly share code, notes, and snippets.

@lowteq
Created March 8, 2020 14:08
Show Gist options
  • Save lowteq/de96e6b2ba34074927edb4cc28a273d7 to your computer and use it in GitHub Desktop.
Save lowteq/de96e6b2ba34074927edb4cc28a273d7 to your computer and use it in GitHub Desktop.
文字をランダムに表示するシェーダー GPUInstancingでオブジェクト毎に表示を変える(カメラの距離で描画が変わってしまうのでだめ)
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
Shader "Unlit/charshader_babel"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_sw("string length in line",int) = 30
_line("line",int) = 20
_source("sourceChars in line",int)=1
_seed("seed",float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
uint instanceID : SV_InstanceID;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
uint instanceID : INSTANCE_ID;
};
sampler2D _MainTex;
float4 _MainTex_ST;
int _sw;
int _line;
int _source;
int _seed;
v2f vert (appdata v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
#if defined(UNITY_INSTANCING_ENABLED)
o.instanceID = v.instanceID;
#endif
return o;
}
float random (float2 p,float seed)
{
return frac(sin(dot(p, fixed2(12.9898,78.233+ seed))) * 43758.5453);
}
float noise(float2 st,float seed)
{
float2 p = floor(st);
return random(p,seed);
}
fixed4 frag (v2f i) : SV_Target
{
int sw = _sw; //列の文字数
int sh = _line; //行の文字数
int w = _source;
//float3 worldpos = mul(unity_ObjectToWorld, float4(0,0,0,1)).xyz;
//float seed = worldpos.x + worldpos.y + worldpos.z;
uint seed = _seed;
#if defined(UNITY_INSTANCING_ENABLED)
seed += i.instanceID;
#endif
float c = noise( i.uv*float2(sw,sh) + 1,seed);
//return fixed4(c,c,c,1);
float x = frac(floor(c*w*w)/w);
float y = floor(c*w)/w;
float dx = frac(i.uv.x*sw)/w;
float dy = frac(i.uv.y*sh)/w;
fixed4 col = tex2D(_MainTex,float2(x+dx,y+dy));
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment