zoukankan      html  css  js  c++  java
  • unity在Game窗口绘制网格Capsule

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ShowCapsuleColider : MonoBehaviour
    {
        //画线用的材质球
        Material lineMat;
        private float subdivide = 305.7f;
        void OnEnable()
        {
            if (lineMat == null)
            {
                lineMat = Resources.Load<Material>("Materials/LineMat");
            }
    
        }
        void OnRenderObject()
        {
            CapsuleCollider capsuleCollider = GetComponent<CapsuleCollider>();
            if (!capsuleCollider.enabled||capsuleCollider==null)
            {
                return;
            }
            Vector3 center = capsuleCollider.center;
            float radius = capsuleCollider.radius;
            float height = capsuleCollider.height;
            lineMat.SetPass(0);
            GL.Begin(GL.LINES);
            GL.Color(Color.green);
            float step =1.0f / subdivide;
            float halfHeight =( height - 2 * radius)*0.5f;
            for (float i = 0; i < 1; i += step)
            {
    
                Vector3 p1 = GraphLib.Circle(i, radius);
                Vector3 p = new Vector3(p1.x + center.x, p1.y + center.y+ halfHeight, p1.z + center.z);
                p = transform.TransformPoint(p);
                GL.Vertex(p);
    
            }
            for (float i = step; i < 1.0f+step; i += step)
            {
    
                Vector3 p1 = GraphLib.Circle(i, radius);
                Vector3 p = new Vector3(p1.x + center.x, p1.y + center.y + halfHeight, p1.z + center.z);
                p = transform.TransformPoint(p);
                GL.Vertex(p);
    
            }
            GL.End();
            GL.Begin(GL.LINES);
            GL.Color(Color.green);
            for (float i = 0; i < 1; i += step)
            {
    
                Vector3 p1 = GraphLib.Circle(i, radius);
                Vector3 p = new Vector3(p1.x + center.x, p1.y + center.y- halfHeight, p1.z + center.z);
                p = transform.TransformPoint(p);
                GL.Vertex(p);
    
            }
            for (float i = step; i < 1.0f+step; i += step)
            {
    
                Vector3 p1 = GraphLib.Circle(i, radius);
                Vector3 p = new Vector3(p1.x + center.x, p1.y + center.y - halfHeight, p1.z + center.z);
                p = transform.TransformPoint(p);
                GL.Vertex(p);
    
            }
            GL.End();
            GL.Begin(GL.LINES);
            GL.Color(Color.green);
            for (float i = 0; i < 1; i += step)
            {
                Vector3 p1 = GraphLib.Circle(i, radius);
                if (i<0.5f)
                {
                    Vector3 p = new Vector3(p1.x + center.x, p1.z + center.y+halfHeight, p1.y + center.z);
                    p = transform.TransformPoint(p);
                    GL.Vertex(p);
                }
                else
                {
                    Vector3 p = new Vector3(p1.x + center.x, p1.z + center.y - halfHeight, p1.y + center.z);
                    p = transform.TransformPoint(p);
                    GL.Vertex(p);
                }
                
                
    
            }
            for (float i = step; i < 1.0f+step; i += step)
            {
                Vector3 p1 = GraphLib.Circle(i, radius);
                if (i < 0.5f)
                {
                    Vector3 p = new Vector3(p1.x + center.x, p1.z + center.y + halfHeight, p1.y + center.z);
                    p = transform.TransformPoint(p);
                    GL.Vertex(p);
                }
                else
                {
                    Vector3 p = new Vector3(p1.x + center.x, p1.z + center.y - halfHeight, p1.y + center.z);
                    p = transform.TransformPoint(p);
                    GL.Vertex(p);
                }
    
    
    
            }
            GL.End();
            GL.Begin(GL.LINES);
            GL.Color(Color.green);
            for (float i = 0; i < 1; i += step)
            {
                Vector3 p1 = GraphLib.Circle(i, radius);
                if (i < 0.5f)
                {
                    Vector3 p = new Vector3(p1.y + center.x, p1.z + center.y + halfHeight, p1.x + center.z);
                    p = transform.TransformPoint(p);
                    GL.Vertex(p);
                }
                else
                {
                    Vector3 p = new Vector3(p1.y + center.x, p1.z + center.y - halfHeight, p1.x + center.z);
                    p = transform.TransformPoint(p);
                    GL.Vertex(p);
                }
    
    
    
            }
            for (float i = step; i < 1.0f+step; i += step)
            {
                Vector3 p1 = GraphLib.Circle(i, radius);
                if (i < 0.5f)
                {
                    Vector3 p = new Vector3(p1.y + center.x, p1.z + center.y + halfHeight, p1.x + center.z);
                    p = transform.TransformPoint(p);
                    GL.Vertex(p);
                }
                else
                {
                    Vector3 p = new Vector3(p1.y + center.x, p1.z + center.y - halfHeight, p1.x + center.z);
                    p = transform.TransformPoint(p);
                    GL.Vertex(p);
                }
            }
            GL.End();
            GL.Begin(GL.LINES);
            GL.Color(Color.green);
            Vector3 point = GraphLib.Circle(0, radius);
            point = new Vector3(point.y + center.x, point.z + center.y + halfHeight, point.x + center.z);
            point = transform.TransformPoint(point);
            GL.Vertex(point);
            GL.Vertex(point - Vector3.up* halfHeight*2);
    
            point = GraphLib.Circle(0.5f, radius);
            point = new Vector3(point.y + center.x, point.z + center.y + halfHeight, point.x + center.z);
            point = transform.TransformPoint(point);
            GL.Vertex(point);
            GL.Vertex(point - Vector3.up * halfHeight * 2);
    
            Vector3 point1 = GraphLib.Circle(0, radius);
            point1 = new Vector3(point1.x + center.x, point1.z + center.y + halfHeight, point1.y + center.z);
            point1 = transform.TransformPoint(point1);
            GL.Vertex(point1);
            GL.Vertex(point1 - Vector3.up * halfHeight * 2);
    
            point1 = GraphLib.Circle(0.5f, radius);
            point1 = new Vector3(point1.x + center.x, point1.z + center.y + halfHeight, point1.y + center.z);
            point1 = transform.TransformPoint(point1);
            GL.Vertex(point1);
            GL.Vertex(point1 - Vector3.up * halfHeight * 2);
            GL.End();
    
        }
    }
    

      

        //s角度  0-1之间  r:半径
        public static Vector3 Circle(float s,float r)
        {
            Vector3 p;
            p.x =r* Mathf.Cos(2*Mathf.PI * s);
            p.y = 0;
            p.z =r* Mathf.Sin(2*Mathf.PI * s);
            return p;
        }
  • 相关阅读:
    互斥锁Mutex与信号量Semaphore的区别
    c/c++强制类型转换
    c++中的隐藏、重载、覆盖(重写)
    运算符重载详解
    类的大小
    C++ static、const和static const 以及它们的初始化
    一种隐蔽性较高的Java ConcurrentModificationException异常场景
    Java编码常见的Log日志打印问题
    Java编程常见缺陷汇总(一)
    Java字符串连接的多种实现方法及效率对比
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14367235.html
Copyright © 2011-2022 走看看