当前位置: 移动技术网 > 移动技术>移动开发>IOS > Unity代码生成经纬球

Unity代码生成经纬球

2020年10月10日  | 移动技术网移动技术  | 我要评论
Unity代码生成经纬球练习四叉树时写着玩的,有一些问题,仅供娱乐效果图:using System.Collections;using System.Collections.Generic;using UnityEngine;public class Planet_Generator : MonoBehaviour{ public float radius = 10; [Range(3, 1000)] public int xDivision = 10;

练习四叉树时写着玩的,有一些问题,仅供娱乐
效果图:

在这里插入图片描述
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Planet_Generator : MonoBehaviour
{
    public float radius = 10;
    [Range(3, 1000)]
    public int xDivision = 10;
    [Range(3,1000)]
    public int yDivision = 10;

    public Material mat;

    private MeshFilter filter;
    private MeshRenderer renderer;

    public bool UpdateSphere = false;

    public bool invert = false;

    void Start()
    {
        if (GetComponent<MeshFilter>() == null)
            filter = gameObject.AddComponent<MeshFilter>();
        if (GetComponent<MeshRenderer>() == null)
            renderer = gameObject.AddComponent<MeshRenderer>();

        filter.mesh = GenerateMesh();
        renderer.material = mat;
    }

    private void Update()
    {
        if(UpdateSphere)
        {
            filter.mesh = GenerateMesh();
            renderer.material = mat;
        }
    }

    Mesh GenerateMesh()
    {
        Vector3[] vertices = new Vector3[xDivision * yDivision ];
        Vector2[] texcoords = new Vector2[xDivision * yDivision];
        int[] indices = new int[xDivision * yDivision * 6];

        int num = 0;
        for(int y = 0; y < yDivision;y ++)
            for(int x = 0; x < xDivision; x ++)
            {
                float normalX = (float)x / (float)(xDivision - 1);
                float normalY = (float)y / (float)(yDivision - 1);


                Vector2 uv = new Vector2(normalX,GetSphereUVY(normalY));

                texcoords[x + y * xDivision] = uv;

                Vector3 vertex = new Vector3(uv.x,0,uv.y);
                
                vertices[x + y * xDivision] = GetSphereCoord1(vertex,normalX, normalY);
            }
        for (int y = 0; y < yDivision-1; y++)
            for (int x = 0; x < xDivision-1; x++)
            {
                indices[num++] = x + y * xDivision;
                indices[num++] = x + (y + 1) * xDivision;
                indices[num++] = x + 1 + y * xDivision;

                indices[num++] = x + (y + 1) * xDivision;
                indices[num++] = x + 1 + (y + 1) * xDivision;
                indices[num++] = x + 1 + y * xDivision;
            }
        Mesh mesh = new Mesh();

        mesh.vertices = vertices;
        mesh.uv = texcoords;
        mesh.triangles = indices;
        mesh.name = "LLS";
        mesh.hideFlags = HideFlags.HideAndDontSave;

        mesh.RecalculateNormals();
        mesh.Optimize();

        return mesh;
    }



    //角度均分网格
    Vector3 GetSphereCoord1(Vector3 planeCoord,float x, float y)
    {

        x = Mathf.Deg2Rad *x * 360;

        float sinH = Mathf.Sqrt(0.25f - Mathf.Pow((0.5f - y), 2)) / 0.5f;
        float angle = Mathf.Asin(sinH);
        float sign = Mathf.Sign(0.5f - y);
        if(sign < 0)
        {
            angle = Mathf.PI - angle;
        }
        //Debug.Log(angle);
       
        Vector2 cylinderCoord = new Vector2(Mathf.Cos(x) , Mathf.Sin(x));
        Vector2 tempCoord =  cylinderCoord * Mathf.Sin(angle);
        
        Vector3 coord = new Vector3(tempCoord.x, Mathf.Sin(angle - Mathf.PI/2) /* Mathf.PI */ , tempCoord.y) * radius;
        if(invert)
            coord = new Vector3(tempCoord.y, Mathf.Sin(angle - Mathf.PI / 2) /* Mathf.PI */ , tempCoord.x) * radius;
        return coord;
    }
    //线性均分
    Vector3 GetSphereCoord(Vector3 planeCoord, float x, float y)
    {

        x = Mathf.Deg2Rad * x * 360;

        float sinH = Mathf.Sqrt(0.25f - Mathf.Pow((0.5f - y), 2)) / 0.5f;
        float angle = Mathf.Asin(sinH);
        float sign = Mathf.Sign(0.5f - y);
        if (sign < 0)
        {
            angle = Mathf.PI - angle;
        }
        Debug.Log(angle);

        Vector2 cylinderCoord = new Vector2(Mathf.Cos(x), Mathf.Sin(x));
        Vector2 tempCoord = cylinderCoord * Mathf.Sin(angle);
        Vector3 coord = new Vector3(tempCoord.x, y /* Mathf.PI */ , tempCoord.y) * radius;

        return coord;
    }

    float GetSphereUVY(float y)
    {
        float sinH = Mathf.Sqrt(0.25f - Mathf.Pow((0.5f - y), 2)) / 0.5f;
        float angle = Mathf.Asin(sinH);
        float sign = Mathf.Sign(0.5f - y);
        if (sign < 0)
        {
            angle = Mathf.PI - angle;
        }
        
        return angle / (180 * Mathf.Deg2Rad);
    }
}

本文地址:https://blog.csdn.net/yuyangchen123/article/details/109002182

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网