So, I am creating a spherical sector with the given theta and phi angles (Latitude and Longitude) and I know my vertices positioning is correct but no mesh is visible. Does any one have any idea about what is wrong with this code. Why isn’t it showing any mesh.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor.UIElements;
public class Please : MonoBehaviour
{
public int ThetaUpper = 30;
public int ThetaLower = 40;
public int PhiUpper = 30;
public int PhiLower = 10;
public bool edit = true;
private int ThetaU;
private int ThetaL;
private int PhiU;
private int PhiL;
public float size = 0.001f;
private readonly int res = 2;
private Mesh mesh;
private MeshFilter meshFilter;
public float radius = 5.0f;
private GameObject[] points;
private Vector3[] vertices;
private string[] names;
// Start is called before the first frame update
void Start()
{
this.gameObject.AddComponent<MeshFilter>();
this.gameObject.AddComponent<MeshRenderer>();
meshFilter = this.gameObject.GetComponent<MeshFilter>();
meshFilter.sharedMesh = new Mesh();
mesh = meshFilter.sharedMesh;
Create();
Assign();
}
// Update is called once per frame
void Update()
{
}
bool check()
{
if (ThetaLower == ThetaL && ThetaUpper == ThetaU && PhiLower == PhiL && PhiUpper == PhiU)
return true;
else return false;
}
void Assign()
{
ThetaU = ThetaUpper;
ThetaL = ThetaLower;
PhiL = PhiLower;
PhiU = PhiUpper;
}
void Create()
{
int index1 = (ThetaLower - ThetaUpper) * 2;
int index2 = (PhiUpper - PhiLower) * 2;
vertices = new Vector3[index1 * index2];
names = new string[vertices.Length];
int[,] temp = new int[index1, index2];
int no = 0;
for (int i = 0; i < index1; i++)
{
float theta = ((0.5f * i) + ThetaUpper) * Mathf.Deg2Rad;
for (int j = 0; j < index2; j++, no++)
{
float phi = ((0.5f * j) + PhiLower) * Mathf.Deg2Rad;
float x = radius * Mathf.Sin(theta) * Mathf.Cos(phi);
float z = radius * Mathf.Sin(theta) * Mathf.Sin(phi);
float y = radius * Mathf.Cos(theta);
vertices[no] = new Vector3(x, y, z);
names[no] = "Vertex No. " + no.ToString() + " Theta " + i.ToString() + " phi " + j.ToString();
temp[i, j] = no;
}
}
int[] triangles = new int[(index1 - 1) * (index2 - 1) * 6];
int triIndex = 0;
for (int i = 0; i < index1-1; i++)
{
for (int j = 0; j < index2-1; j++, triIndex += 6)
{
triangles[triIndex] = temp[i, j];
triangles[triIndex] = temp[i , j + 1];
triangles[triIndex] = temp[i + 1, j + 1];
triangles[triIndex] = temp[i, j];
triangles[triIndex] = temp[i + 1, j + 1];
triangles[triIndex] = temp[i + 1, j];
}
}
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
DrawSphere();
}
void DrawSphere()
{
points = new GameObject[mesh.vertexCount];
for (int i = 0; i < mesh.vertexCount; i++)
{
points *= GameObject.CreatePrimitive(PrimitiveType.Sphere);*
points_.name = names*;_
_points.transform.parent = transform;_
_points.transform.localScale = new Vector3(size, size, size);
points.transform.position = vertices;
}
}
}*_