【m】11.PointCloudTools 上传

This commit is contained in:
2025-09-02 10:42:11 +08:00
parent 421b516485
commit 66443003d1
31 changed files with 1578 additions and 0 deletions

View File

@@ -0,0 +1,131 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxColliderGizmo : MonoBehaviour {
void Start() {
}
// Update is called once per frame
void OnRenderObject() {
var colliders = gameObject.GetComponents<BoxCollider >();
if (colliders == null) {
return;
}
CreateLineMaterial();
lineMaterial.SetPass(0);
GL.PushMatrix();
GL.MultMatrix(transform.localToWorldMatrix);
for (int i = 0; i < colliders.Length; i++) {
var col = colliders[i];
var c = col.center;
var size = col.size;
float rx = size.x / 2f;
float ry = size.y / 2f;
float rz = size.z / 2f;
Vector3 p0, p1, p2, p3;
Vector3 p4, p5, p6, p7;
p0 = c + new Vector3(-rx,-ry,rz);
p1 = c + new Vector3(rx, -ry, rz);
p2 = c + new Vector3(rx, -ry, -rz);
p3 = c + new Vector3(-rx, -ry, -rz);
p4 = c + new Vector3(-rx, ry, rz);
p5 = c + new Vector3(rx, ry, rz);
p6 = c + new Vector3(rx, ry, -rz);
p7 = c + new Vector3(-rx, ry, -rz);
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p0);
GL.Vertex(p1);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p1);
GL.Vertex(p2);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p2);
GL.Vertex(p3);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p0);
GL.Vertex(p3);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p4);
GL.Vertex(p5);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p5);
GL.Vertex(p6);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p6);
GL.Vertex(p7);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p4);
GL.Vertex(p7);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p0);
GL.Vertex(p4);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p1);
GL.Vertex(p5);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p2);
GL.Vertex(p6);
GL.End();
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex(p3);
GL.Vertex(p7);
GL.End();
}
GL.PopMatrix();
}
static Material lineMaterial;
static void CreateLineMaterial() {
if (!lineMaterial) {
// Unity has a built-in shader that is useful for drawing
// simple colored things.
Shader shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// Turn on alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// Turn backface culling off
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", 0);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 67b37331afa91f6418b142c26b0ba6fa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,108 @@
using UnityEditor;
using UnityEngine;
[RequireComponent(typeof(CapsuleCollider))]
public class CapsuleColliderGizmo : MonoBehaviour
{
[Tooltip("Gizmo显示颜色")] public Color gizmoColor = new Color(0, 1, 0, 0.3f);
[Tooltip("是否始终显示Gizmo")] public bool alwaysShow = true;
private CapsuleCollider capsuleCollider;
private void Awake()
{
capsuleCollider = GetComponent<CapsuleCollider>();
}
private void OnDrawGizmos()
{
# if UNITY_EDITOR
if (!alwaysShow && !Selection.Contains(gameObject)) return;
#endif
DrawCapsuleGizmo();
}
private void OnDrawGizmosSelected()
{
if (alwaysShow) return;
DrawCapsuleGizmo();
}
private void DrawCapsuleGizmo()
{
if (capsuleCollider == null)
{
capsuleCollider = GetComponent<CapsuleCollider>();
if (capsuleCollider == null) return;
}
Gizmos.color = gizmoColor;
// 获取胶囊体的参数
Vector3 center = transform.TransformPoint(capsuleCollider.center);
float radius = capsuleCollider.radius * Mathf.Max(transform.lossyScale.x, transform.lossyScale.z);
float height = capsuleCollider.height * transform.lossyScale.y;
int direction = capsuleCollider.direction;
// 计算胶囊体的两个半球中心
Vector3 halfHeight = Vector3.zero;
switch (direction)
{
case 0: // X轴
halfHeight = Vector3.right * (height / 2 - radius);
radius = capsuleCollider.radius * Mathf.Max(transform.lossyScale.y, transform.lossyScale.z);
break;
case 1: // Y轴
halfHeight = Vector3.up * (height / 2 - radius);
radius = capsuleCollider.radius * Mathf.Max(transform.lossyScale.x, transform.lossyScale.z);
break;
case 2: // Z轴
halfHeight = Vector3.forward * (height / 2 - radius);
radius = capsuleCollider.radius * Mathf.Max(transform.lossyScale.x, transform.lossyScale.y);
break;
}
Vector3 topCenter = center + halfHeight;
Vector3 bottomCenter = center - halfHeight;
// 绘制胶囊体的圆柱部分
if (height > radius * 2)
{
DrawCylinder(topCenter, bottomCenter, radius, 16);
}
// 绘制上下两个半球
Gizmos.DrawSphere(topCenter, radius);
Gizmos.DrawSphere(bottomCenter, radius);
}
// 添加自定义圆柱绘制方法
private void DrawCylinder(Vector3 topCenter, Vector3 bottomCenter, float radius, int segments)
{
Vector3 up = (topCenter - bottomCenter).normalized;
Vector3 right = Vector3.Cross(up, Vector3.forward).normalized;
if (right.sqrMagnitude < 0.01f)
right = Vector3.Cross(up, Vector3.right).normalized;
Vector3 forward = Vector3.Cross(up, right).normalized;
for (int i = 0; i < segments; i++)
{
float angle = (i / (float)segments) * Mathf.PI * 2;
float nextAngle = ((i + 1) / (float)segments) * Mathf.PI * 2;
Vector3 topPoint = topCenter + (Mathf.Cos(angle) * right + Mathf.Sin(angle) * forward) * radius;
Vector3 topNextPoint = topCenter + (Mathf.Cos(nextAngle) * right + Mathf.Sin(nextAngle) * forward) * radius;
Vector3 bottomPoint = bottomCenter + (Mathf.Cos(angle) * right + Mathf.Sin(angle) * forward) * radius;
Vector3 bottomNextPoint =
bottomCenter + (Mathf.Cos(nextAngle) * right + Mathf.Sin(nextAngle) * forward) * radius;
// 绘制侧面竖线
Gizmos.DrawLine(topPoint, bottomPoint);
// 绘制顶部环线
Gizmos.DrawLine(topPoint, topNextPoint);
// 绘制底部环线
Gizmos.DrawLine(bottomPoint, bottomNextPoint);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8094e78c655752e4b8ed71f3f146a258
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,151 @@
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(MeshCollider))]
public class MeshColliderGizmo : MonoBehaviour
{
[Tooltip("Gizmo显示颜色")]
public Color lineColor = new Color(0, 1, 1, 0.8f);
[Tooltip("线框宽度")]
public float lineWidth = 1.0f;
[Tooltip("是否在运行时显示")]
public bool showInPlayMode = true;
[Tooltip("是否只显示边缘")]
public bool showEdgesOnly = true;
private MeshCollider _meshCollider;
private Mesh _mesh;
private List<Vector3> _vertices = new List<Vector3>();
private List<int> _triangles = new List<int>();
private List<Vector3> _linePoints = new List<Vector3>();
private Material _glMaterial;
private Matrix4x4 _localToWorldMatrix;
private void Awake()
{
_meshCollider = GetComponent<MeshCollider>();
InitializeMeshData();
InitializeGLMaterial();
}
private void InitializeMeshData()
{
// 尝试获取网格数据
MeshFilter meshFilter = GetComponent<MeshFilter>();
if (meshFilter != null && meshFilter.sharedMesh != null)
{
_mesh = meshFilter.sharedMesh;
}
else if (_meshCollider.sharedMesh != null)
{
_mesh = _meshCollider.sharedMesh;
}
if (_mesh != null)
{
_vertices.Clear();
_triangles.Clear();
_mesh.GetVertices(_vertices);
_mesh.GetTriangles(_triangles, 0);
GenerateLinePoints();
}
else
{
Debug.LogWarning("未找到有效的Mesh资源", this);
}
}
private void InitializeGLMaterial()
{
// 创建用于GL绘制的材质
_glMaterial = new Material(Shader.Find("Hidden/Internal-Colored"));
_glMaterial.hideFlags = HideFlags.HideAndDontSave;
_glMaterial.SetInt("ZWrite", 0);
_glMaterial.SetInt("ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
}
private void GenerateLinePoints()
{
if (showEdgesOnly)
{
// 只显示边缘线(去重处理)
HashSet<long> edgeSet = new HashSet<long>();
_linePoints.Clear();
for (int i = 0; i < _triangles.Count; i += 3)
{
AddEdge(_triangles[i], _triangles[i + 1], edgeSet);
AddEdge(_triangles[i + 1], _triangles[i + 2], edgeSet);
AddEdge(_triangles[i + 2], _triangles[i], edgeSet);
}
}
else
{
// 显示所有三角形线
_linePoints.Clear();
for (int i = 0; i < _triangles.Count; i += 3)
{
_linePoints.Add(_vertices[_triangles[i]]);
_linePoints.Add(_vertices[_triangles[i + 1]]);
_linePoints.Add(_vertices[_triangles[i + 1]]);
_linePoints.Add(_vertices[_triangles[i + 2]]);
_linePoints.Add(_vertices[_triangles[i + 2]]);
_linePoints.Add(_vertices[_triangles[i]]);
}
}
}
private void AddEdge(int v1, int v2, HashSet<long> edgeSet)
{
// 创建唯一的边ID避免重复
long edgeId = (long)Mathf.Min(v1, v2) << 32 | Mathf.Max(v1, v2);
if (edgeSet.Contains(edgeId)) return;
edgeSet.Add(edgeId);
_linePoints.Add(_vertices[v1]);
_linePoints.Add(_vertices[v2]);
}
private void Update()
{
_localToWorldMatrix = transform.localToWorldMatrix;
}
private void OnRenderObject()
{
if (!showInPlayMode || _mesh == null || _linePoints.Count == 0 || _glMaterial == null)
return;
// 设置材质和颜色
_glMaterial.SetPass(0);
GL.PushMatrix();
GL.MultMatrix(_localToWorldMatrix);
GL.Color(lineColor);
// 设置线宽
GL.Begin(GL.LINES);
for (int i = 0; i < _linePoints.Count; i++)
{
GL.Vertex(_linePoints[i]);
}
GL.End();
GL.PopMatrix();
}
// 编辑器下的Gizmo预览
#if UNITY_EDITOR
private void OnDrawGizmos()
{
if (!Application.isPlaying && _mesh != null)
{
Gizmos.color = lineColor;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawWireMesh(_mesh);
}
}
#endif
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1be297c9ec995924f872cde2f236e42a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,75 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SphereColliderGizmo : MonoBehaviour {
void Start() {
}
// Update is called once per frame
void OnRenderObject() {
// 添加SphereCollider处理
var sphereColliders = gameObject.GetComponents<SphereCollider>();
if (sphereColliders != null) {
CreateLineMaterial();
lineMaterial.SetPass(0);
GL.PushMatrix();
GL.MultMatrix(transform.localToWorldMatrix);
foreach (var col in sphereColliders) {
float radius = col.radius;
Vector3 scale = transform.lossyScale;
// 计算最大缩放值
float maxScale = Mathf.Max(scale.x, scale.y, scale.z);
float scaledRadius = radius * maxScale;
Vector3 center = col.center;
// 绘制三个方向的圆环
DrawCircle(center, Vector3.up, Vector3.forward, scaledRadius);
DrawCircle(center, Vector3.forward, Vector3.up, scaledRadius);
DrawCircle(center, Vector3.right, Vector3.up, scaledRadius);
}
GL.PopMatrix();
}
// 原来的BoxCollider代码保持不变...
var colliders = gameObject.GetComponents<BoxCollider>();
// ... 后续BoxCollider的绘制代码保持不变 ...
}
// 新增辅助方法绘制圆形
void DrawCircle(Vector3 center, Vector3 normal, Vector3 axis, float radius) {
int segments = 32;
float angle = 0f;
GL.Begin(GL.LINE_STRIP);
GL.Color(Color.red);
for (int i = 0; i <= segments; i++) {
Vector3 point = center;
point += (axis * Mathf.Cos(angle) + Vector3.Cross(normal, axis) * Mathf.Sin(angle)) * radius;
GL.Vertex(point);
angle += Mathf.PI * 2f / segments;
}
GL.End();
}
static Material lineMaterial;
static void CreateLineMaterial() {
if (!lineMaterial) {
// Unity has a built-in shader that is useful for drawing
// simple colored things.
Shader shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// Turn on alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// Turn backface culling off
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", 0);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4b3d66befec6a284da8001399bdb0d8c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: