zoukankan      html  css  js  c++  java
  • unity 快速创建小地图

    1. 先写一个纹理遮罩shader

    Shader "Unlit/TexMask"
    {
    	Properties
    	{
    		_MainTex ("Texture", 2D) = "white" {}
    		_MaskTex("Mask Texture",2D) = "white"{}
    	}
    	SubShader
    	{
    		Tags {"Queue"="Transparent" "RenderType"="Transparent" }
    		LOD 100
    
    		Pass
    		{
    			Blend SrcAlpha OneMinusSrcAlpha
    			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;
    			sampler2D _MaskTex;
    
    			v2f vert (appdata v)
    			{
    				v2f o;
    				o.vertex = mul(UNITY_MATRIX_MVP, 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)*tex2D(_MaskTex,i.uv);
    				// apply fog
    				UNITY_APPLY_FOG(i.fogCoord, col);
    				return col;
    			}
    			ENDCG
    		}
    	}
    }
    

    原理就是:初始图片*遮罩图片,需要隐藏的地方,遮罩图片的alpha值为0,需要显示的地方为白色,这里我设置的是一张圆形遮罩,后面就是一张圆形的小地图


    2.写一个小地图脚本,用来显示小地图,原理就是在目标头上生成一个摄像机,让摄像机跟随目标,并将看到的俯视图转为小地图的纹理

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    
    public class MiniMap : MonoBehaviour {
        public Transform target;
        private Camera miniMapCamera;
        private RenderTexture renderTex;
        public Image miniMapTex;
    	// Use this for initialization
    	void Start () {
            miniMapCamera = new GameObject("MiniMapCamera", typeof(Camera)).GetComponent<Camera>();
            renderTex = new RenderTexture((int)miniMapCamera.pixelWidth, (int)miniMapCamera.pixelHeight, 1);
            miniMapCamera.targetTexture = renderTex;
            miniMapTex.material.SetTexture("_MainTex", renderTex);
        }
    	
    	// Update is called once per frame
    	void Update () {
            miniMapCamera.transform.position = target.position + new Vector3(0, 30, 0);
            miniMapCamera.transform.LookAt(target.position);
    
        }
    }
    

    3.显示小地图


    4.这里功能都写得特别简单粗糙,适当在上面添加一些功能和美术,应该还是蛮好看的


  • 相关阅读:
    Junit单元测试
    win7的6个网络命令
    WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS
    WOJ1022 Competition of Programming 贪心 WOJ1023 Division dp
    woj1019 Curriculum Schedule 输入输出 woj1020 Adjacent Difference 排序
    woj1018(HDU4384)KING KONG 循环群
    woj1016 cherry blossom woj1017 Billiard ball 几何
    woj1013 Barcelet 字符串 woj1014 Doraemon's Flashlight 几何
    woj1012 Thingk and Count DP好题
    woj1010 alternate sum 数学 woj1011 Finding Teamates 数学
  • 原文地址:https://www.cnblogs.com/liang123/p/6325892.html
Copyright © 2011-2022 走看看