Skip to content

Instantly share code, notes, and snippets.

@PrashantUnity
Created August 28, 2022 16:10
Show Gist options
  • Save PrashantUnity/dfdc6bd8664886fe7c343b8c5b59d9b1 to your computer and use it in GitHub Desktop.
Save PrashantUnity/dfdc6bd8664886fe7c343b8c5b59d9b1 to your computer and use it in GitHub Desktop.
Creating Shapes points in 3d Space
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
using static TMPro.SpriteAssetUtilities.TexturePacker_JsonArray;
public class ShapesData
{
public float circleRadius;
public Vector2 rectangle;
public float sphericalRadius;
public Vector2 cylindrical;
/// <summary>
/// This Method is return point in x-z plane only
/// </summary>
/// <param name="radius">Radius of Circle</param>
/// <param name="pos">Location of centre of cirle in space</param>
/// <param name="countOfObject"> number of object to be instatiated</param>
/// <returns>List of Vector3</Vector3></returns>
public static List<Vector3> Circle(float radius, Vector3 pos, int angle = 15)
{
var points = new List<Vector3>();
var count = 360 / angle;
for (int i = 0; i < count; i++)
{
var vect = new Vector3(pos.x + radius * Mathf.Cos(angle * i), pos.y, pos.z + radius * Mathf.Sin(angle * i));
points.Add(vect);
}
return points;
}
public static List<Vector3> CircularArea(int radius, Vector3 origin)
{
var vect = new List<Vector3>();
var pos = Square(radius, origin);
foreach (var item in pos)
{
if (Mathf.Abs(item.magnitude) <= radius)
{
vect.Add(item);
}
}
return vect;
}
public static List<Vector3> Rectangle(int height, int width, Vector3 pos)
{
var array = new List<Vector3>();
pos.x -= height / 2;
pos.y -= width / 2;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
array.Add(new Vector3(pos.x + i, pos.y + 0, pos.z + j));
}
}
return array;
}
public static List<Vector3> Square(int size, Vector3 pos)
{
var array = new List<Vector3>();
pos.x -= size / 2;
pos.y -= size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
array.Add(new Vector3(pos.x + i, pos.y + 0, pos.z + j));
}
}
return array;
}
public static List<Vector3> FibonacciSphere(int samples = 1000)
{
var points = new List<Vector3>();
float phi = Mathf.PI * (3 - Mathf.Sqrt(5)); // golden angle in radians
for (int i = 0; i < samples; i++)
{
float y = 1 - (i / (float)(samples - 1)) * 2; //# y goes from 1 to -1
float radius = Mathf.Sqrt(1 - y * y); //# radius at y
float theta = phi * i; //# golden angle increment
float x = Mathf.Cos(theta) * radius;
float z = Mathf.Sin(theta) * radius;
points.Add(new Vector3(x, y, z));
}
return points;
}
public static Vector3 MapXYZTOSphere(int x,int y, int z)
{
z = -1 + 2 * x;
float phi = 2 * Mathf.PI * y;
float theta = Mathf.Asin(z);
return new Vector3(Mathf.Cos(theta) * Mathf.Cos(phi),Mathf.Cos(theta) * Mathf.Sin(phi),z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment