IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity 如何制作锁定目标模型的效果 -> 正文阅读

[游戏开发]Unity 如何制作锁定目标模型的效果

计算包围盒
在这里插入图片描述
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NaughtyAttributes;

public class MeshBoundingBoxGenerator : MonoBehaviour
{
    public bool ShowMeshGizmos;
    public GameObject mainBody;
    public bool useCombine;
    [ReadOnly]
    public MeshBoundingBox boundingBox;

    //public Bounds bounds;

    private BoxCollider boxCollider;

    [Button("GenerateMeshBoundingBox")]
    private void GenerateMeshBoundingBox()
    {
        Mesh mesh = GetBoundingMesh();
        boundingBox = new MeshBoundingBox(mainBody.transform, mesh.bounds.center, mesh.bounds.size / 2);
    }

    [Button("GenerateBoxCollider")]
    private void GenerateBoxCollider()
    {
        Mesh mesh = GetBoundingMesh();
        boxCollider = gameObject.AddAndGetComponent<BoxCollider>();
        boxCollider.isTrigger = true;
        boxCollider.center = mesh.bounds.center - mainBody.transform.position;
        boxCollider.size = mesh.bounds.size;
    }

    private Mesh GetBoundingMesh() 
    {
        Mesh mesh;
        if (useCombine)
        {
            mesh = GetWorldCombineMesh(gameObject);
        }
        else
        {
            mesh = GetWorldMesh(mainBody);
        }
        return mesh;
    }

    public Mesh GetWorldCombineMesh(GameObject go)
    {
        SkinnedMeshRenderer[] skinRenderers = go.GetComponentsInChildren<SkinnedMeshRenderer>();
        MeshFilter[] meshFilters = go.GetComponentsInChildren<MeshFilter>();
        CombineInstance[] combineInstances = new CombineInstance[skinRenderers.Length + meshFilters.Length];

        int index = 0;
        for (int i = 0; i < skinRenderers.Length; i++)
        {
            var render = skinRenderers[i];
            var mesh = new Mesh();
            render.BakeMesh(mesh);
            combineInstances[index] = new CombineInstance
            {
                mesh = mesh,
                transform = render.gameObject.transform.localToWorldMatrix,
                subMeshIndex = 0
            };

            index++;
        }

        for (int i = 0; i < meshFilters.Length; i++)
        {
            var render = meshFilters[i];
            var temp = (render.sharedMesh != null) ? render.sharedMesh : render.mesh;
            var mesh = (Mesh)Instantiate(temp);
            combineInstances[index] = new CombineInstance
            {
                mesh = mesh,
                transform = render.gameObject.transform.localToWorldMatrix,
                subMeshIndex = 0
            };

            index++;
        }

        Mesh combinedMesh = new Mesh();
        combinedMesh.CombineMeshes(combineInstances, true, true);

        return combinedMesh;
    }

    public Mesh GetWorldMesh(GameObject meshGO)
    {
        Mesh mesh = null;
        if (meshGO != null)
        {
            SkinnedMeshRenderer skinnedMeshRenderer = meshGO.GetComponent<SkinnedMeshRenderer>();
            if (skinnedMeshRenderer != null)
            {
                mesh = new Mesh();
                skinnedMeshRenderer.BakeMesh(mesh);
            }
            else
            {
                MeshFilter meshFilter = meshGO.GetComponent<MeshFilter>();
                mesh = (Mesh)Instantiate((meshFilter.sharedMesh != null) ? meshFilter.sharedMesh : meshFilter.mesh);
            }
        }

        if (meshGO == null || mesh == null) { return null; }

        CombineInstance combineInstance;
        combineInstance = new CombineInstance { mesh = mesh, transform = meshGO.transform.localToWorldMatrix, subMeshIndex = 0 };
        Mesh combinedMesh = new Mesh();
        combinedMesh.CombineMeshes(new CombineInstance[] { combineInstance }, true, true);
        return combinedMesh;
    }

    private void OnDrawGizmos()
    {
        if (ShowMeshGizmos) 
        {
            if (boundingBox != null) 
            {
                boundingBox.DrawBoundingBox();
            }
        }
    }
}

public static class MonoBehaviourExtensionMethods
{
    public static T AddAndGetComponent<T>(this GameObject gameObject) where T : Component
    {
        T component = gameObject.GetComponent<T>();
        if (component == null)
        {
            component = gameObject.AddComponent<T>();
        }

        return component;
    }
}

包围盒

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class MeshBoundingBox
{
    public Transform body;
    public Vector3 centerOffset;
    public Vector3 halfSize;
    public Vector3[] localPoints;

    public MeshBoundingBox(Transform body, Vector3 center, Vector3 halfSize)
    {
        this.body = body;
        this.centerOffset = center - body.position;
        this.halfSize = halfSize;
        this.localPoints = new Vector3[]
            {
                halfSize,
                new Vector3(-halfSize.x, halfSize.y , halfSize.z),
                new Vector3(halfSize.x, -halfSize.y , halfSize.z),
                new Vector3(halfSize.x, halfSize.y , -halfSize.z),
                new Vector3(-halfSize.x, -halfSize.y , halfSize.z),
                new Vector3(halfSize.x, -halfSize.y , -halfSize.z),
                new Vector3(-halfSize.x, halfSize.y , -halfSize.z),
                halfSize,
            };
    }

    public Vector3[] GetWorldBoundingPoints() 
    {
        Vector3[] worldVertex = new Vector3[8];
        for (int i = 0; i < 8; i++)
        {
            worldVertex[i] = body.position + centerOffset + localPoints[i];
        }
        return worldVertex;
    }

    public void DrawBoundingBox()
    {
        Vector3[] worldVertex = GetWorldBoundingPoints();
        for (int i = 0; i < worldVertex.Length; i++)
        {
            Gizmos.DrawSphere(worldVertex[i], 0.05f);
        }

        Gizmos.DrawWireCube(body.position + centerOffset, halfSize * 2);
    }
}

锁定目标
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LockTarget : MonoBehaviour
{
    public Camera cam;

    public MeshBoundingBoxGenerator target;

    public int glCount = 5;

    public Material glMaterial;

    private void OnPostRender()
    {
        if (target != null)
        {
            DrawLockRange(GetScreenRect(target.boundingBox.GetWorldBoundingPoints()));
        }
    }

    private Rect GetScreenRect(Vector3[] worldVertex)
    {
        Rect rect = new Rect();
        float xMin = float.MaxValue, xMax = float.MinValue, yMin = float.MaxValue, yMax = float.MinValue;
        for (int i = 0; i < worldVertex.Length; i++)
        {
            Vector3 screenVertex = cam.WorldToScreenPoint(worldVertex[i]);
            if (screenVertex.x < xMin)
            {
                xMin = screenVertex.x;
            }
            if (screenVertex.x > xMax)
            {
                xMax = screenVertex.x;
            }
            if (screenVertex.y < yMin)
            {
                yMin = screenVertex.y;
            }
            if (screenVertex.y > yMax)
            {
                yMax = screenVertex.y;
            }
        }

        rect = Rect.MinMaxRect(xMin, yMin, xMax, yMax);

        return rect;
    }

    private void DrawLockRange(Rect rect) 
    {
        GL.PushMatrix();
        if (!glMaterial)
            return;
        glMaterial.SetPass(0);
        GL.LoadPixelMatrix();

        GL.Begin(GL.LINES);
        GL.Color(new Color(1, 0, 0, 1));

        GL.Vertex3(rect.xMin, rect.yMin, 0);
        GL.Vertex3(rect.xMin, rect.yMin + glCount, 0);
        GL.Vertex3(rect.xMin, rect.yMin, 0);
        GL.Vertex3(rect.xMin + glCount, rect.yMin, 0);

        GL.Vertex3(rect.xMax, rect.yMin, 0);
        GL.Vertex3(rect.xMax, rect.yMin + glCount, 0);
        GL.Vertex3(rect.xMax, rect.yMin, 0);
        GL.Vertex3(rect.xMax - glCount, rect.yMin, 0);

        GL.Vertex3(rect.xMin, rect.yMax, 0);
        GL.Vertex3(rect.xMin, rect.yMax - glCount, 0);
        GL.Vertex3(rect.xMin, rect.yMax, 0);
        GL.Vertex3(rect.xMin + glCount, rect.yMax, 0);

        GL.Vertex3(rect.xMax, rect.yMax, 0);
        GL.Vertex3(rect.xMax, rect.yMax - glCount, 0);
        GL.Vertex3(rect.xMax, rect.yMax, 0);
        GL.Vertex3(rect.xMax - glCount, rect.yMax, 0);

        GL.End();

        GL.PopMatrix();
    }
}

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-03-08 22:54:07  更:2022-03-08 22:54:20 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 17:55:18-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码