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的更多相关文章
随机推荐
- 【Linux】鸟哥的Linux私房菜基础学习篇整理(三)
1. gzip [-cdtv#] filename:压缩.参数:-c:将压缩的数据输出到屏幕上,可通过数据重定向进行处理:-d:解压缩的参数:-t:可以用来检验一个压缩文件的一致性,查看文件有无错误: ...
- 【模拟】Codeforces 705B Spider Man
题目链接: http://codeforces.com/problemset/problem/705/B 题目大意: 两个人玩游戏,总共N个数,分别求前I(I=1 2 3...n)个数时游戏的获胜者是 ...
- 【有源汇上下界最大流】ZOJ 3229 Shoot the Bullet
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3229 题目大意: n天给m个女孩拍照(1<=n<= ...
- oracle 表的管理(数据类型,表创建删除,数据CRUD 操作)
表名和列的命名规则
- windows7旗舰版IIS6配置-保证能发布
http://wenku.baidu.com/view/20b4d26248d7c1c708a145d1.html
- java工作流bpm开发ERP实例
今天看了一个java工作流bpm开发ERP的例子,文章介绍:http://tech.it168.com/a2009/0507/275/000000275294_14.shtml 增加数据块 一路照做就 ...
- HDU1251 统计难题(Trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理
题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...
- Appium移动自动化测试(三)--安装Android模拟器(转)
Appium移动自动化测试(三)--安装Android模拟器 2015-06-08 10:33 by 虫师, 30828 阅读, 9 评论, 收藏, 编辑 当Android SDK安装完成之后,并不意 ...
- HashMap,TreeMap,LinkedHashMap学习
Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复.Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快 ...