MeshRenderer 旋转时边界错误 [英] MeshRenderer has wrong bounds when rotated

查看:28
本文介绍了MeshRenderer 旋转时边界错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试获取模型的边界(在 Blender 中创建)并在 Inspector 中显示时:

如您所见,当对象未旋转时,边界是正确的.但是当它们是(最左边的对象)时,边界开始变得完全错误.

这是一个显示/获取边界的脚本:

使用 System.Collections;使用 System.Collections.Generic;使用 UnityEngine;公共类 GetBounds : MonoBehaviour{公共 MeshRenderer mesh_renderer = null;public bool show_bounds = false;私有无效 OnDrawGizmos(){如果(!show_bounds)返回;Gizmos.DrawWireCube(mesh_renderer.bounds.center, mesh_renderer.bounds.size);Gizmos.DrawWireSphere(mesh_renderer.bounds.center, 0.3f);}}

我该如何解决这个问题?

解决方案

Unity 不会重新计算

When I try to get the bounds of my models (created in Blender) and show them in Inspector:

As you can see the bounds are correct when the objects are not rotated. But when they are (left-most object) bounds start getting totally wrong.

Here is a script that shows / gets the bounds:

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

public class GetBounds : MonoBehaviour
{
    public MeshRenderer mesh_renderer = null;

    public bool show_bounds = false;

    private void OnDrawGizmos()
    {
        if (!show_bounds) return;

        Gizmos.DrawWireCube(mesh_renderer.bounds.center, mesh_renderer.bounds.size);
        Gizmos.DrawWireSphere(mesh_renderer.bounds.center, 0.3f);
    }
}

How can I fix this?

解决方案

In this thread I have come across this image which explains it pretty much

Unity dos not recalculate the Mesh.bounds all the time except when you add a mesh for the first time or "manually" invoke Mesh.RecalculateBounds.

It then uses this local space Mesh.bounds in order to calculate the translated, scaled and rotated Renderer.bounds in global space based on the Mesh.bounds. This way it always has to iterate a fixed amount of 8 vertices of the bounding box.

There was also a solution provided if you want to get the exact bounds calculated directly from the vertices. I adopted and cleaned it up a bit

public class GetBounds : MonoBehaviour
{
    public MeshRenderer mesh_renderer;
    public bool show_bounds;

    public MeshFilter meshFilter;
    public Mesh mesh;

    private void OnDrawGizmos()
    {
        if (!mesh_renderer) return;
        if (!show_bounds) return;

        if (!meshFilter) meshFilter = mesh_renderer.GetComponent<MeshFilter>();
        if (!meshFilter) return;

        if (!mesh) mesh = meshFilter.mesh;
        if (!mesh) return;

        var vertices = mesh.vertices;
        if (vertices.Length <= 0) return;

        // TransformPoint converts the local mesh vertice dependent on the transform
        // position, scale and orientation into a global position
        var min = transform.TransformPoint(vertices[0]);
        var max = min;

        // Iterate through all vertices
        // except first one
        for (var i = 1; i < vertices.Length; i++)
        {
            var V = transform.TransformPoint(vertices[i]);

            // Go through X,Y and Z of the Vector3
            for (var n = 0; n < 3; n++)
            {
                max[n] = Mathf.Max(V[n], max[n]);
                min[n] = Mathf.Min(V[n], min[n]);
            }
        }

        var bounds = new Bounds();
        bounds.SetMinMax(min, max);

        // ust to compare it to the original bounds
        Gizmos.DrawWireCube(mesh_renderer.bounds.center, mesh_renderer.bounds.size);
        Gizmos.DrawWireSphere(mesh_renderer.bounds.center, 0.3f);

        Gizmos.color = Color.green;
        Gizmos.DrawWireCube(bounds.center, bounds.size);
        Gizmos.DrawWireSphere(bounds.center, 0.3f);
    }
}

Result:

  • In WHITE: The MeshRenderer.bounds

  • In GREEN: The "correct" calculated vertex bounds

这篇关于MeshRenderer 旋转时边界错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆