最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

unity game engine - Using shaders to create reveal map effect - Stack Overflow

programmeradmin2浏览0评论

I am trying to create explored map effect something similar to classic RTS or RPG games where the map is revealed as the player/unit moves across the map.

I am trying out shaders to achieve this, brand new to them so probably doing something wrong.

This is what I have done so far:

I have a grid , with two tilemap one for the surface(everything) and the other for unexplored area(fog)

Tilemap_ground has a Tilemap and Tilemap renderer, with the default 2d sprite material:

Tilemap fog has a TileMap and tilemap renderer but a material from a custom shader that I created, I have put this map on a differnt layer so its above the ground layer, also made the fog color as red for testing:

this is what the shader looks like:

Shader "CustomRenderTexture/reveal"
{
 Properties
    {
        _MainTex ("Tile Texture", 2D) = "white" {}
        _RevealPosition ("Reveal Position", Vector) = (0, 0, 0, 0)
        _RevealRadius ("Reveal Radius", Float) = 5.0
        _FogColor ("Fog Color", Color) = (0, 0, 0, 1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                float3 worldPos : TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _RevealPosition;
            float _RevealRadius;
            float4 _FogColor;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                float distance1 = distance(i.worldPos.xy, _RevealPosition.xy);

                // Reveal tiles based on distance
                float alpha = saturate((distance1 - _RevealRadius) / _RevealRadius);

                _FogColor[3] = alpha;
                return _FogColor;
            }
            ENDCG
        }
    }
}

When I run this the expected reveal area is never revealed:

Although for some reason the mini map(which is a camera projecting on texture) gets the revealed area(kinda)

also something to note not sure if it makes a difference but I add tiles to the maps dynamically i.e. on Monobehaviour.Start -> drawGrass(), drawFog()

   public void drawGrass(int width, int height) {
        int tileOffset = 0;
        int startI = -width / 2 - tileOffset;
        int startJ = -height / 2 - tileOffset;
        for (int i = startI; i < width / 2; i++) {
            for (int j = startJ; j < height / 2; j++) {
                tileMap.SetTile(new Vector3Int(i-1, j-1, 0), tile);
            }
        }
    }

    public void drawFog(int width, int height) {
        int tileOffset = 0;
        int startI = -width / 2 - tileOffset;
        int startJ = -height / 2 - tileOffset;
        for (int i = startI; i < width / 2; i++) {
            for (int j = startJ; j < height / 2; j++) {
                tileMapFog.SetTile(new Vector3Int(i, j, 0), fogTile);
            }
        }
    }
发布评论

评论列表(0)

  1. 暂无评论