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

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ShowSphereColider : MonoBehaviour
    {
        //画线用的材质球
        Material lineMat;
        private float subdivide =305.7f; //细分数
        void OnEnable()
        {
            if (lineMat == null)
            {
                lineMat = Resources.Load<Material>("Materials/LineMat");
            }
            
        }
        void OnRenderObject()
        {
            SphereCollider sphereCollider = GetComponent<SphereCollider>();
            if (!sphereCollider.enabled||sphereCollider==null)
            {
                return;
            }
            Vector3 center = sphereCollider.center;
            float radius = sphereCollider.radius;
            lineMat.SetPass(0);
            GL.Begin(GL.LINES);
            GL.Color(Color.green);
            float step = 1.0f / subdivide ;
            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, 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, 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.z + center.y, 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);
                Vector3 p = new Vector3(p1.x + center.x, p1.z + center.y, 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.0f; i += step)
            {
                Vector3 p1 = GraphLib.Circle(i, radius);
                Vector3 p = new Vector3(p1.y + center.x, p1.x + center.y, 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.y + center.x, p1.x + center.y, p1.z + center.z);
                p = transform.TransformPoint(p);
                GL.Vertex(p);
            }
            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;
        }
  • 相关阅读:
    MVC 控制器返回匿名对象集合(一般是用Linq 查出来的数据 或者其他) View 页面展示的解决方法 ........
    C#中base关键字的几种用法
    Aspose.cellls 的基本使用方法使用
    Web API 入门指南
    基于EF的外键的查询 使用【ForeignKey(“ SupplierId”】特性
    GET和POST两种基本请求方法的区别
    Replication的犄角旮旯(五)关于复制identity列
    Replication的犄角旮旯(二)寻找订阅端丢失的记录
    SQL优化(1)
    Replication的犄角旮旯(三)聊聊@bitmap
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14366739.html
Copyright © 2011-2022 走看看