Help us understand the problem. What is going on with this article?

[Unity] 画面全体を覆う四角形

はじめに

画面の一番奥に画面いっぱいに画像を貼りたかった。
https://github.com/jnhtt/FullscreenQuad

概要

  • Awakeで四角形描画の準備 -- MeshRenderer/MeshFilter/Mesh/Material
  • Shaderで画面いっぱいに画像を貼る

Awakeで四角形の準備

            meshRenderer = gameObject.AddComponent<MeshRenderer>();
            meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
            meshRenderer.receiveShadows = false;
            meshRenderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
            meshRenderer.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
            meshRenderer.sharedMaterial = material;
            meshFilter = gameObject.AddComponent<MeshFilter>();

            Mesh mesh = new Mesh();
            mesh.name = "FullscreenQuad";
            Vector3[] verts = new Vector3[] {
                new Vector3( 1f,  1f, 0f),
                new Vector3(-1f, -1f, 0f),
                new Vector3(-1f,  1f, 0f),
                new Vector3( 1f, -1f, 0f),
            };
            int[] tri = new int[] {
                0, 1, 2,
                3, 1, 0,
            };
            Vector2[] uv = new Vector2[] {
                new Vector2(1f, 1f),
                new Vector2(0f, 0f),
                new Vector2(0f, 1f),
                new Vector2(1f, 0f),
            };
            mesh.vertices = verts;
            mesh.triangles = tri;
            mesh.uv = uv;
            mesh.RecalculateNormals();
            meshFilter.sharedMesh = mesh;
            mesh.RecalculateBounds();

Shader

画像を貼りたい場合は下記を有効にする

fixed4 col = tex2D(_MainTex, i.uv);
Shader "Custom/FullscreenQuadShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Background" }
        Cull Off
        ZTest Off
        ZWrite Off

        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;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = v.vertex;
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                //fixed4 col = tex2D(_MainTex, i.uv);
                fixed4 col = float4(i.uv, 0, 1);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

さいごに

スクロールできるようにするのもいいかも

jnhtt
簡単なゲームを作ってます。 https://smartphone-games.net/
https://smartphone-games.net/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした