unity 球体表面平均分割点
2015-12-02 21:28
621 查看
之前看了别人的一份源码,讲到了球体表面平均分割点,于是也好奇去查了一下算法,自己写不出来,借用算法在unity写了一个小demo
using UnityEngine; using System.Collections; public class DistrbutedPointsOnSphere : MonoBehaviour { public Camera camera; /// <summary> /// 父级 -- 球 /// </summary> public Transform parent; /// <summary> /// 预设 -- 球 /// </summary> public GameObject prefab; public Transform[] kids; // Use this for initialization void Start () { CalualteSphere(); } // Update is called once per frame void Update () { if (!Input.GetMouseButton(0)) return; float fMouseX = Input.GetAxis("Mouse X"); float fMouseY = Input.GetAxis("Mouse Y"); parent.Rotate(Vector3.up, -fMouseX * 2, Space.World); parent.Rotate(Vector3.right, fMouseY * 2, Space.World); for (int i = 0; i < kids.Length; i++) kids[i].LookAt(camera.transform); } /// <summary> /// 平均分成的等份 /// </summary> int N = 50; /// <summary> /// 小球的半径 /// </summary> float size = 0.5f; /// <summary> /// 球体表面平均分割点 /// </summary> void CalualteSphere() { float inc = Mathf.PI * (3.0f - Mathf.Sqrt(5.0f)); float off = 2.0f / N;//注意保持数值精度 kids = new Transform ; for(int i=0;i<N;i++) { float y = (float)i * off - 1.0f + (off / 2.0f); float r = Mathf.Sqrt(1.0f - y * y); float phi = i * inc; Vector3 pos=new Vector3(Mathf.Cos(phi)*r*size,y*size,Mathf.Sin(phi)*r*size); GameObject tempGo = Instantiate(prefab) as GameObject; tempGo.transform.parent = parent; tempGo.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); tempGo.transform.localPosition = pos; tempGo.SetActive(true); kids[i]=tempGo.transform; } } /// <summary> /// 点击小球,将他移动到中心点 /// </summary> /// <param name="pos">小球的自身位置</param> void ClickLittleSphere(Vector3 pos) { Vector3 vec301 = pos - Vector3.zero; Vector3 vec302 = camera.transform.position - parent.transform.position; parent.rotation = Quaternion.FromToRotation(vec301, vec302); } }
相关文章推荐
- unity基础概念
- unity3d脚本控制骨骼旋转
- Unity手游之路<十三>手游代码更新策略探讨
- Unity手游之路<十二>手游资源热更新策略探讨
- Unity手游之路<十一>资源打包Assetbundle
- Unity 行为树 Behavior Designer
- Unity下SpriteSheet 使用
- unity3d5.2.3中 调整视角
- Unity3d 手游之旅
- 【Unity3D ugui】UI特效的位置自适应及调整层次关系的一种解决方案
- UNITY 5.2 Camera类
- unity3d 笔记
- Unity脚本打包android工程
- 不用Unity库,利用.NET动态代理自己实现AOP
- Unity3D_Quad_实现播放序列帧
- neuron诺亦腾全身穿戴设备开发untiy项目
- UnityAPI翻译——VRNode
- UnityAPI翻译——VRDeviceType
- UnityAPI翻译——VRSettings
- UnityAPI翻译——VRDevice