LEDディスプレイ - saitocastel1900/UnityShader GitHub Wiki

  • 仕組みとしては、画素のテクスチャをループして並べて基のテクスチャに上書きしている
Shader "Unlit/LedUnlitShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _PixelTex("Pixel Texture", 2D) = "white" {}
        _PixelSize("Pixel Size", Range(1, 100)) = 200
        _Intensity("intensity", float) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex, _PixelTex;
            float _PixelSize, _Intensity;
            
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex,i.uv);
                float4 pix = tex2D(_PixelTex, i.uv * _PixelSize);
                
                return col * pix * _Intensity;
            }
            ENDCG
        }
    }
}

  • このままでもいいけど、ディスプレイは画素単位で描画してい流ので、単純にテクスチャを重ねるのではなくて、画素一つに対して一色にする
Shader "Unlit/LedUnlitShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _PixelTex("Pixel Texture", 2D) = "white" {}
        _PixelSize("Pixel Size", Range(1, 100)) = 200
        _Intensity("intensity", float) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex, _PixelTex;
            float _PixelSize, _Intensity;
            
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

                float2 posterize(float2 In, float frameRate)
            {
                return floor(In * frameRate) / (frameRate);
            }

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, posterize(i.uv, _PixelSize));
                float4 pix = tex2D(_PixelTex, i.uv * _PixelSize);
                
                return col * pix * _Intensity;
            }
            ENDCG
        }
    }
}