75 lines
2.7 KiB
C#
75 lines
2.7 KiB
C#
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|