using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Sn : MonoBehaviour {
void Start()
{
CreatPointOnSphere(50,30);
}
public void CreatPointOnSphere(int _wBornPointSum, float _dwDectRadius)
{
int tempIndex = 0;
//生成
float inc = Mathf.PI * (3.0f - Mathf.Sqrt(5.0f));
float off = 2.0f / _wBornPointSum; //注意保持数值精度 m_wBornPointSum:生成的点数
float y;
float r;
float phi;
for (int i = 0; i < _wBornPointSum; i++)
{
y = (float)i * off + (off / 2.0f) - 1.0f;
r = Mathf.Sqrt(1.0f - y * y);
phi = i * inc;
Vector3 pos = new Vector3(Mathf.Cos(phi) * r * _dwDectRadius, y * _dwDectRadius, Mathf.Sin(phi) * r * _dwDectRadius); //m_dwDectRadius 距离球心的距离
//法向量------------------------------------
Vector3 dir = pos - Vector3.zero;
GameObject null_obj = new GameObject();
null_obj.transform.position = Vector3.zero;
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
obj.transform.position += new Vector3(0, 0, 1f);
obj.transform.Rotate(Vector3.right * 90);
obj.transform.SetParent(null_obj.transform);
null_obj.transform.forward = dir;
null_obj.transform.localScale=new Vector3(0.1f,0.1f,15f);
//-----------------------------------------------以上追加
tempIndex++;
BornPoint(pos, tempIndex, null_obj);
}
}
RaycastHit m_hitInfo;
void BornPoint(Vector3 pos, int tempIndex,GameObject parent_sphere)
{
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
obj.transform.localScale = new Vector3(1f, 1f, 1f);
obj.transform.localPosition = pos;
obj.name = string.Concat("蒲公英种子", tempIndex);
parent_sphere.name = string.Concat("棒棒糖");
parent_sphere.transform.SetParent(obj.transform);
obj.transform.SetParent(transform);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sn : MonoBehaviour
{
[Header("总点数")]
public int point_sum;
[Header("半径")]
public float radius;
void Start()
{
//CreatPointOnSphere(50, 30,Vector3.one*50);
Spaw_list(new List<Vector3>() { Vector3.one, Vector3.one*50, Vector3.one*100 }, 50, 30);
}
/// <summary>
/// name="_wBornPointSum"小球数量
/// name="_dwDectRadius"半径
/// name="vector3"生成的位置
/// </summary>
/// <param name="_wBornPointSum">小球数量</param>
/// <param name="_dwDectRadius">半径</param>
/// <param name="vector3">生成的位置</param>
public void CreatPointOnSphere(int _wBornPointSum, float _dwDectRadius,Vector3 vector3)
{
int tempIndex = 0;
//生成
float inc = Mathf.PI * (3.0f - Mathf.Sqrt(5.0f));
float off = 2.0f / _wBornPointSum; //注意保持数值精度 m_wBornPointSum:生成的点数
float y;
float r;
float phi;
GameObject parent_null = new GameObject();
parent_null.transform.position = vector3;
for (int i = 0; i < _wBornPointSum; i++)
{
y = (float)i * off + (off / 2.0f) - 1.0f;
r = Mathf.Sqrt(1.0f - y * y);
phi = i * inc;
Vector3 pos = new Vector3(Mathf.Cos(phi) * r * _dwDectRadius, y * _dwDectRadius, Mathf.Sin(phi) * r * _dwDectRadius); //m_dwDectRadius 距离球心的距离
pos += vector3;
//法向量------------------------------------
Vector3 dir = pos - vector3;
GameObject null_obj = new GameObject();
null_obj.transform.position = vector3;
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
obj.transform.position += new Vector3(0, 0, 1f) + vector3;
obj.transform.Rotate(Vector3.right * 90);
obj.transform.SetParent(null_obj.transform);
null_obj.transform.forward = dir;
null_obj.transform.localScale = new Vector3(0.1f, 0.1f, _dwDectRadius * 0.5f);
//-----------------------------------------------以上追加
tempIndex++;
BornPoint(pos, tempIndex, null_obj, parent_null.transform);
}
}
/// <summary>
/// 生成point 球
/// </summary>
/// <param name="pos"></param>
/// <param name="tempIndex"></param>
/// <param name="parent_sphere"></param>
/// <param name="parent_obj"></param>
void BornPoint(Vector3 pos, int tempIndex, GameObject parent_sphere,Transform parent_obj)
{
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
obj.transform.localScale = new Vector3(1f, 1f, 1f);
obj.transform.localPosition = pos;
obj.name = string.Concat("蒲公英种子", tempIndex);
parent_sphere.name = string.Concat("棒棒糖");
parent_sphere.transform.SetParent(obj.transform);
obj.transform.SetParent(parent_obj);
parent_obj.name = "球";
parent_obj.SetParent(transform);
}
/// <summary>
/// 创建额外连接线
/// </summary>
/// <param name="start_point"></param>
/// <param name="end_point"></param>
void Creat_Cylinder(Vector3 start_point,Vector3 end_point)
{
Vector3 dir = end_point - start_point;
GameObject null_obj = new GameObject();
null_obj.transform.position = start_point;
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
obj.transform.position += new Vector3(0, 0, 1f) + start_point;
obj.transform.Rotate(Vector3.right * 90);
obj.transform.SetParent(null_obj.transform);
null_obj.transform.forward = dir;
null_obj.transform.localScale = new Vector3(0.1f, 0.1f,Vector3.Distance(start_point, end_point)*0.5f);
null_obj.name = "连接线";
null_obj.transform.SetParent(transform);
}
void Spaw_list(List<Vector3> list_one, int _wBornPointSum, float _dwDectRadius)
{
if (list_one.Count < 0) return;
if(list_one.Count>0&& list_one.Count < 2)
{
int offet = Random.Range(0, 20);
CreatPointOnSphere(_wBornPointSum + offet, _dwDectRadius + offet, list_one[0]);
}
else
{
for (int i = 0; i < list_one.Count; i++)
{
if(i< list_one.Count-1)
{
Creat_Cylinder(list_one[i], list_one[i + 1]);
}
int offet = Random.Range(0, 20);
CreatPointOnSphere(_wBornPointSum + offet, _dwDectRadius + offet, list_one[i]);
}
}
}
}