Fractal_Test
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Fractal_Test.html
参考:http://catlikecoding.com/unity/tutorials/constructing-a-fractal/
using UnityEngine;
using System.Collections; public class Fractal : MonoBehaviour {
public Mesh[] meshes;
public Material material;
public int maxDepth;
public float childScale;
private int depth;
public float spawnProbability;
public float maxRotationSpeed;
private float rotationSpeed; private static Vector3[] childDirections = {
Vector3.up,
Vector3.right,
Vector3.left,
Vector3.forward,
Vector3.back,
Vector3.down
}; private static Quaternion[] childOrientations = {
Quaternion.identity,
Quaternion.Euler(0f, 0f, -90f),
Quaternion.Euler(0f, 0f, 90f),
Quaternion.Euler(90f, 0f, 0f),
Quaternion.Euler(-90f, 0f, 0f),
Quaternion.Euler(0f, 0f, 180f)
}; private Material[,] materials; private void InitializeMaterials() {
materials = new Material[maxDepth + , ];
for (int i = ; i <= maxDepth; i++) {
float t = i / (maxDepth - 1f);
t *= t;
materials[i, ] = new Material(material);
materials[i, ].color = Color.Lerp(Color.white, Color.yellow, t);
materials[i, ] = new Material(material);
materials[i, ].color = Color.Lerp(Color.white, Color.cyan, t);
}
materials[maxDepth, ].color = Color.magenta;
materials[maxDepth, ].color = Color.red;
} // Use this for initialization
void Start() {
if(materials == null){
InitializeMaterials();
}
gameObject.AddComponent<MeshFilter>().mesh = meshes[Random.Range(, meshes.Length)];
gameObject.AddComponent<MeshRenderer>().material = materials[depth, Random.Range(, )];
rotationSpeed = Random.Range(-maxRotationSpeed, maxRotationSpeed);
if(depth < maxDepth) {
StartCoroutine(CreateChildren());
} } private IEnumerator CreateChildren() {
for (int i = ; i < childDirections.Length; ++i) {
if (Random.value < spawnProbability) {
yield return new WaitForSeconds(Random.Range(0.1f, 0.5f));
new GameObject("Fractal Child").AddComponent<Fractal>().Initialize(this, i);
} }
} private void Initialize(Fractal parent, int childIndex) {
meshes = parent.meshes;
materials = parent.materials;
maxDepth = parent.maxDepth;
depth = parent.depth + ;
transform.parent = parent.transform;
childScale = parent.childScale;
transform.localScale = Vector3.one * childScale;
transform.localPosition = childDirections[childIndex] * (0.5f + 0.5f * childScale);
transform.localRotation = childOrientations[childIndex];
spawnProbability = parent.spawnProbability;
maxRotationSpeed = parent.maxRotationSpeed;
} // Update is called once per frame
void Update() {
transform.Rotate(0f, rotationSpeed * Time.deltaTime, 0f);
}
}
PS:失败的地方是:没有DynamicBatch,为何原文说会合并呢?
unitypackage:http://files.cnblogs.com/files/YinaPan/Fractal_Test.rar
Fractal_Test的更多相关文章
随机推荐
- bzoj3744
这道题是目前我做bzoj最感动的一题没有之一……首先先警示一下,分块的题目能不套主席树尽量不套因为主席树不仅回答来一个log而且常数也比较大,对于分块这种根号的算法非常不适合这里是求区间逆序对,考虑查 ...
- sed的选项与命令简要
第一部分:sed命令选项 sed选项 说明 -n, --quiet, --silent 静默模式,取消将模式空间中的内容自动打印出来. -e script, --expression=script 以 ...
- Light OJ 1025 - The Specials Menu(区间DP)
题目大意: 给你一个字符串,问有多少种方法删除字符,使得剩下的字符是回文串. 有几个规定: 1.空串不是回文串 2.剩下的字符位置不同也被视为不同的回文串.如:AA有三种回文串 A, A, A ...
- HDU 5416 CRB and Tree
题目大意: T, T组测试数据 给你一个n有n个点,下标是从 1 开始的.这个是一棵树,然后下面是n-1条边, 每条边的信息是 s,e,w 代表 s-e的权值是w 然后是一个Q代表Q次询问. 每次询问 ...
- 线性代数(矩阵乘法):POJ 3233 Matrix Power Series
Matrix Power Series Description Given a n × n matrix A and a positive integer k, find the sum S = ...
- 连接Oracle的几种方式
如何引用Data.OracleClient.dll 由于从.net 4.0之后,微软将OracleClient.dll从框架里去除了,所以要使用,需要在VS2010里面去把项目的.net框架从.net ...
- bzoj 1093 [ZJOI2007]最大半连通子图(scc+DP)
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2286 Solved: 897[Submit][St ...
- python 解析xml 文件: SAX方式
环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...
- How many - HDU 2609 (trie+最小表示)
题目大意:有 N 个手链,每个手链的最大长度不超过100,求出来最多有多少个不同的手链. 分析:因为手链是可以转动的,所以只要两个手链通过转动达到相同,那么也被认为是一种手链,然而如果每次都循环比 ...
- 来自投资银行的20个Java面试题
问题一:在多线程环境中使用HashMap会有什么问题?在什么情况下使用get()方法会产生无限循环? HashMap本身没有什么问题,有没有问题取决于你是如何使用它的.比如,你在一个线程里初始化了一个 ...