对于我做过的立方体/盒子:
void CalcPositons(GameObject objectToDrawTransform)
{
Bounds bounds = objectToDrawTransform.GetComponent<MeshFilter>().sharedMesh.bounds;
Vector3 v3Center = bounds.center;
Vector3 v3Extents = bounds.extents;
v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
v3FrontTopLeft = objectToDrawTransform.transform.TransformPoint(v3FrontTopLeft);
v3FrontTopRight = objectToDrawTransform.transform.TransformPoint(v3FrontTopRight);
v3FrontBottomLeft = objectToDrawTransform.transform.TransformPoint(v3FrontBottomLeft);
v3FrontBottomRight = objectToDrawTransform.transform.TransformPoint(v3FrontBottomRight);
v3BackTopLeft = objectToDrawTransform.transform.TransformPoint(v3BackTopLeft);
v3BackTopRight = objectToDrawTransform.transform.TransformPoint(v3BackTopRight);
v3BackBottomLeft = objectToDrawTransform.transform.TransformPoint(v3BackBottomLeft);
v3BackBottomRight = objectToDrawTransform.transform.TransformPoint(v3BackBottomRight);
}
然后我使用LineRenderer绘制线条。 我不想制作大纲着色器。
现在我为测试创建了一个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetBounds : MonoBehaviour
{
public GameObject objecttodraw;
private void Start()
{
CalcPositons(objecttodraw);
}
void CalcPositons(GameObject objectToDrawTransform)
{
Bounds bounds = objectToDrawTransform.GetComponent<MeshFilter>().sharedMesh.bounds;
Vector3 v3Center = bounds.center;
Vector3 v3Extents = bounds.extents;
}
}
这是脚本附加到的3d对象以及我想要获取边界的对象:
我正在获取中心和范围,但我现在如何绘制边界?